Tag: C

13 Posts

Numerical Analysis – Lab
Index Lab2 - Direct Methods for Solving Linear EquationsLab3 - Inverse Power Method to Solve EigenvalueLab4 - Jacobi Method to Solve EigenvalueLab 5 - Cubic Spline InterpolationLab 6 - FFT & IFFTLab 7 - Romberg Integral Lab2 - Direct Methods for Solving Linear Equations Description Use C/C++, implement the following algorithms: Gaussian EliminationDoolittle Decomposition Implement Predefinition and Useful functions #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> #include <math.h> using namespace std; #define SUCCESS 0 #define FAILURE 1 #define INPUTPATH "./InputData.txt" typedef int Status; typedef double* Vector; typedef double** Matrix; Status PrintMatrix(int n, Matrix A){ /* * === FUNCTION ====================================================================== * Name: PrintMatrix * Description: Print the Matrix A to the terminal. n is the Dimension of A. * ===================================================================================== */ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ printf("%.2lf\t", A[i][j]); } printf("\n"); } return SUCCESS; } Status PrintVector(int n, Vector v){ /* * === FUNCTION ====================================================================== * Name: PrintVector * Description: Print the Vector v to the terminal(in row form). n is the Dimension…
What do these Mojibake mean? – UTF-8 Encoding Scheme Exploration
This article may contain horrific and gory videos. Be careful when clicking on the URLs. If you have any discomfort when browsing, please stop and leave. / 本文可能含有恐怖、血腥性质的图片、视频。请谨慎进入文中的链接。如果您在浏览过程中产生任何不适,请停止浏览并离开。 Introduction We often meet mojibake, in games, web pages, or somewhere else. UTF-8 is the most widespread encoding scheme. These days my friends @叉叉 and @北嘲 find some weird videos on YouTube, with obscure titles and terrifying cover. We are curious about the origin of these creepy videos. This article is to record our exploration process.  Some Example Videos (Click with Caution, Collapsed by default).... ø·ø ̈ùšù„ø© ... ø£øoù†ùšø© ù„ù„ø£ø·ù ø§ù„ ... ù„ùšø ̈ùšø§.. آمراة تدعى أنها السيدة مريم العذراء بأنها متزوجة من المسيح Exploration Association with encoding When @叉叉 first send me the video title (.... ø·ø ̈ùšù„ø© ... ø£øoù†ùšø© ù„ù„ø£ø·ù ø§ù„ ... ù„ùšø ̈ùšø§), it reminds me of what my misconfigured C-language IDE returns me as console error info: What the IDE returns: "²»ÊÇÄڲ¿»òÍⲿÃüÁҲ²»ÊǿÉÔËÐеijÌÐò|| »òÅú´¦ÀíÎļþ¡£" This is apparently a decoding error. Characters are all Latin Characters. Remind me of some schemes like ISO 8859-1. @叉叉…
Expression Tree – BiTree’s application
Introduction An expression can be converted to a tree, when every operator in the expression is binocular. One who is familiar with Mathematica may also know Mathematica expressions are all pre-order expressions. PreOrder expressions in mma This cpp program is used to read a expression and convert to a tree in memory, and calculate its value by means of the tree. Code #ifndef LINK_QUEUE_H #define LINK_QUEUE_H /* * ===================================================================================== * * Filename: Link-Queue.h * * Description: Implement a link queue in C. * * Version: 1.0 * Created: 2021/10/20 23:22:12 * Compiler: gcc * Author: CuSO4_Deposit (Depoze), CuSO4D@protonmail.com */ #include <stdio.h> #include <iostream> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define SUCCESS 1 #define FAILURE 0 typedef int Status; typedef union{ float Number; char Operator; }OpElem; typedef struct{ bool tag; OpElem Elem; }QElemtype; typedef struct QNode{ QElemtype data; struct QNode* next; }QNode; typedef struct{ QNode* front; QNode* rear; }LinkQueue; // Function Declaration Status InitQueue(LinkQueue& Q); Status DestroyQueue(LinkQueue& Q); Status ClearQueue(LinkQueue& Q); bool QueueEmpty(LinkQueue Q); int QueueLength(LinkQueue Q); Status GetHead(LinkQueue Q, QElemtype& e); Status EnQueue(LinkQueue&…
PostOrderThreadTree – Implement in C
/* * ===================================================================================== * * Filename: ThrTree.cpp * * Description: A BiThrTree * * Version: 1.0 * Created: 2021/11/3 22:50:46 * Revision: none * Compiler: gcc * * Author: CuSO4_Deposit (Depoze), CuSO4D@protonmail.com */ #include <stdio.h> #include <iostream> #include <stdlib.h> #include ".\BiTreeForThr.h" #define TRUE 1 #define FALSE 0 #define SUCCESS 1 #define FAILURE 0 typedef int Status; typedef char TElemtype; typedef enum PointerTag{Child = 0, Thread = 1} PointerTag; typedef struct BiThrNode{ struct BiThrNode* lptr; PointerTag ltag; TElemtype data; struct BiThrNode* parent; PointerTag rtag; struct BiThrNode* rptr; }BiThrNode; BiThrNode* PostCopyTree(BiTNode* T){ /* * === FUNCTION ====================================================================== * Name: PostCopyTree * Description: Copy a Bitree T to a ThrTree in post order return the BiThrtree's root. * ===================================================================================== */ if(!T) return NULL; BiThrNode* newlp = PostCopyTree(T->lchild); BiThrNode* newrp = PostCopyTree(T->rchild); BiThrNode* newnode = (BiThrNode*)malloc(sizeof(BiThrNode)); newnode->data = T->data; newnode->lptr = newlp; newnode->rptr = newrp; newnode->ltag = Child; newnode->rtag = Child; newnode->parent = NULL; newlp->parent = newnode; newrp->parent = newnode; return newnode; } Status PostThreading(BiThrNode* ThrT, BiThrNode*& Pre){ /* * === FUNCTION ====================================================================== * Name: PostThreading * Description: Initialize…
BiTree – Implement in C
Basis LevelOrderTraverse is implemented based on Queue. typedef BiTNode* QElemtype; typedef struct QNode{ QElemtype data; struct QNode* next; }QNode; typedef struct{ QNode* front; QNode* rear; }LinkQueue; // Queue Function Declaration Status InitQueue(LinkQueue& Q); Status DestroyQueue(LinkQueue& Q); bool QueueEmpty(LinkQueue Q); Status ClearQueue(LinkQueue& Q); Status EnQueue(LinkQueue& Q, QElemtype e); Status DeQueue(LinkQueue& Q, QElemtype& e); Status InitQueue(LinkQueue& Q){ /* * === FUNCTION ====================================================================== * Name: InitQueue * Description: Initialize a Queue. Will allocate memory for pointers. * ===================================================================================== */ Q.front = (QNode*)malloc(sizeof(QNode)); if(!Q.front) return FAILURE; Q.front->next = NULL; Q.rear = Q.front; return SUCCESS; } Status DestroyQueue(LinkQueue& Q){ /* * === FUNCTION ====================================================================== * Name: DestroyQueue * Description: Destroy a Queue. Free all the pointers. * ===================================================================================== */ if(!ClearQueue(Q)) return FAILURE; free(Q.rear); Q.rear = NULL; //Q.rear and Q.front must point to the Q.front = NULL; // same address, free once. return SUCCESS; } Status ClearQueue(LinkQueue& Q){ /* * === FUNCTION ====================================================================== * Name: ClearQueue * Description: Clear Q to an emply queue. * ===================================================================================== */ QElemtype temp; while(!QueueEmpty(Q)) if(!DeQueue(Q, temp)) return FAILURE; return SUCCESS; } Status EnQueue(LinkQueue& Q, QElemtype…
Link Queue – Implement in C
Code /* * ===================================================================================== * * Filename: Link-Queue.cpp * * Description: Implement a link queue in C. * * Version: 1.1 * Created: 2021/10/20 23:22:12 * Compiler: gcc * Author: CuSO4_Deposit (Depoze), CuSO4D@protonmail.com */ #include <stdio.h> #include <iostream> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define SUCCESS 1 #define FAILURE 0 typedef int Status; typedef char QElemtype; typedef struct QNode{ QElemtype data; struct QNode* next; }QNode; typedef struct{ QNode* front; QNode* rear; }LinkQueue; // Function Declaration Status InitQueue(LinkQueue& Q); Status DestroyQueue(LinkQueue& Q); Status ClearQueue(LinkQueue& Q); bool QueueEmpty(LinkQueue Q); int QueueLength(LinkQueue Q); Status GetHead(LinkQueue Q, QElemtype& e); Status EnQueue(LinkQueue& Q, QElemtype e); Status DeQueue(LinkQueue& Q, QElemtype& e); Status QueueTraverse(LinkQueue Q, Status (*Visit)(QNode*)); Status TestTraverse(QNode* n); // Function Implement Status InitQueue(LinkQueue& Q){ /* * === FUNCTION ====================================================================== * Name: InitQueue * Description: Initialize a Queue. Will allocate memory for pointers. * ===================================================================================== */ Q.front = (QNode*)malloc(sizeof(QNode)); if(!Q.front) return FAILURE; Q.rear = Q.front; return SUCCESS; } Status DestroyQueue(LinkQueue& Q) { /* * === FUNCTION ====================================================================== * Name: DestroyQueue * Description: Destroy a Queue. Free all the pointers.…
Probability of Christmas on Wednesday
Original Problem Proof: In the system of the current Common Era, the probability of Christmas on Wed. is not 1/7. What exactly the probability is In 400 years there are 303 non-leap years and 97 leap years. Total number of days: $$T = 303\cdot 365 + 97\cdot 366\equiv 303 + 97\cdot 2\equiv 0\pmod 7.$$ This means: If the Christmas is on Mon. in year x, then it would be on Mon. in year (x + 400). That's to say, there's a difference between a day of the week and another day of the week. Solve in C /* * ===================================================================================== * * Filename: LeapYear_Xmas.cpp * * Description: A program to count which day of the week does the X-mas falls. * * Version: 1.0 * Created: 2021/10/9 20:31:35 * Revision: none * Compiler: gcc * * Author: CuSO4_Deposit, CuSO4D@protonmail.com * * ===================================================================================== */ #include <stdio.h> #include <iostream> #define TRUE 1 #define FALSE 0 #define Status int #define SUCCESS 1 #define FAILURE 0 #define FEB29 59 #define DEC25_nonleap 358 //Function Declaration bool ifLeap(int year); Status RunCalendar(int…
C/C++ File Operations
C File Operation VS do not allow "fopen" function to pass compilation. FILE* fp //using MS Visual Studio(2019) if ((fopen_s(&fp, "<path>", "<mode>"))){ printf("Cannot open the file.\n"); exit(<error code>); } //using other compiler if((fp=fopen("<path>", "mode")) == NULL ){ printf("Fail to open the file.\n"); exit(<error code>); } C++ File Operation https://www.cplusplus.com/doc/tutorial/files/
C Errors & Warnings Record
Search [ivory-search id="284" title="Default Search Form"] Index [label color = "orange" shape = "round"]C4819[/label] [label color="orange" shape="round"]C6386[/label] Warnings Warning C4819 The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss [To be continued...] [admonition title = "Warning C6386" color="orange"] Buffer overrun while writing to <Pointer to char>: the writable size is '<number>' bytes, but '2' bytes might be written. [/admonition] Analysis:This warning indicates that the writable extent of the specified buffer might be smaller than the index used to write to it. This can cause buffer overrun.[ref]Code Analysis for C/C++ Warning C6386 - real or false positive?[/ref]When compiler tries to analyze the code, it can do some simple calculates but can't do it deeply. It happens a lot when you allocate memory for a string and then access the string. Solution:This warning can be a false positive.When this happens, try to exam: will the <writible size> be smaller than <actually written size> in any condition? If the answer is no,…
Sequence Stack – Implement in C
/* * ===================================================================================== * * Filename: Sequence Stack.cpp * * Description: Implement sequence stack in C. * * Version: 1.0 * Created: 2021/9/29 23:31:18 * Revision: none * Compiler: gcc * Author: CuSO4_Deposit (Depoze), CuSO4D@protonmail.com */ #include <stdio.h> #include <stdlib.h> #include <iostream> #define TRUE 1 #define FALSE 0 #define SUCCESS 1 #define FAILURE 0 #define InitialSize 50 #define IncrementSize 10 typedef int Status; typedef char SElemtype; typedef struct { SElemtype* base; SElemtype* top; int Stacksize; }SqStack; //Function Declaration Status InitStack(SqStack& S); Status DestroyStack(SqStack& S); Status ClearStack(SqStack& S); bool StackEmpty(SqStack S); int StackLength(SqStack S); Status GetTop(SqStack S, SElemtype& e); Status StackIncrease(SqStack& S); Status Push(SqStack& S, SElemtype e); Status Pop(SqStack& S, SElemtype& e); Status StackTraverse(SqStack S, Status(*Visit)(SElemtype*)); Status Visit(SElemtype* VisitPtr); //Function Implement Status InitStack(SqStack& S) { /* * Precondition: S exists. * return value: 0:failure; 1:success. */ S.base = (SElemtype*)malloc(InitialSize * sizeof(SElemtype)); S.top = S.base; S.Stacksize = InitialSize; return SUCCESS; } Status DestroyStack(SqStack& S) { /* * Precondition: S exists. * return value: 0:failure; 1:success. */ free(S.base); S.top = NULL; S.base = NULL; S.Stacksize = 0; return…