**Basic idea** : Use the Getline function to read the infix expression from TXT file sequentially, convert it to the suffix expression and calculate the result, and compare with the user result.

The processing of integers, fractions, and decimals: treats decimals and integers as a fraction of the default denominator of 1. Establishes a fractional class that converts integers and decimals to fractions when infix is converted to suffixes.

classfenshu{Private: intFZ; intFM; Public: intGetfz () {returnFZ; } intGetfm () {returnFM; } voidSETFZ (intFZ) {FZ=FZ; } voidSETFM (intFM) { if(fm!=0) FM=FM; Elsecout<<"the denominator cannot be 0\n"; } Fenshu () {FZ=-1; FM=-1; } Fenshu (intAintb) {FZ=A; if(b!=0) FM=b; Elsecout<<"the denominator cannot be 0\n"; } intgcdintAintb//Beg Greatest common divisor { if(b = =0) returnA; returnGCD (b, a%b); } voidYuefen () {//Numerator intGcd=1; GCD=gcd (FZ,FM); //cout<< "GCD:" <<Gcd<<endl;fz=fz/GCD; FM=fm/GCD; //cout<< "Results:"; show ();//Test} Fenshuoperator+(Fenshu F) {returnFenshu (fz*f.fm+f.fz*fm,fm*f.fm); } Fenshuoperator-(Fenshu F) {returnFenshu (fz*f.fm-f.fz*fm,fm*f.fm); } Fenshuoperator*(Fenshu F) {returnFenshu (fz*f.fz,fm*f.fm); } Fenshuoperator/(Fenshu F) {returnFenshu (fz*f.fm,fm*F.FZ); } voidShow () {cout<<fz<<"/"<<fm<<Endl; }};

The method of dividing the greatest common divisor is already written in the previous blog post. For later code writing convenience, the use of overloading.

infix conversion to suffix processing: use stacks as data structures.

Template <classElemtype>classmystack{ Public: Const Static intMAXSIZE = -; Elemtype Data[maxsize]; intTop//stack top foot mark Public: voidInit ();//Initialize Stack BOOLEmpty ();//determine if the stack is emptyElemtype GetTop ();//read stack top element (not out) voidPush (Elemtype x);//into the stackElemtype pop ();//out of the stack};

Problems encountered:

1. The fractional class definition is the numerator and the denominator as the int type variable, the decimal number which appears in the question cannot calculate;

Analysis: To convert a decimal number into an integer that can be deposited into fractions, each one after the decimal point, the numerator denominator is multiplied by 10,

//converts a numeric string into a corresponding numberFenshu Read_number (CharStr[],int*i) {Fenshu x (0,1); intK =0; while(Str[*i] >='0'&& str[*i]<='9')//working with integer parts{X.SETFZ (X.GETFZ ()*Ten+ (str[*i]-'0') ); (*i) + +; } if(str[*i]=='.')//working with decimal parts { (*i) + +; while(Str[*i] >='0'&&str[*i] <='9') {X.SETFZ (X.GETFZ ()*Ten+ (str[*i]-'0')); X.SETFM (X.GETFM ()*Ten); (*i) + +; K++; } } returnx;}

2. For the existence of the negative sign '-' The topic tip stack is empty;

Analysis: The minus sign and the minus sign descriptor as '-', causing the minus sign and minus sign to perform the same operation (the number of two top-of-stack operations) causes the operand error operation in the infix suffix;

Workaround: Find the '-' that is used as the minus sign and replace it with another symbol to write the suffix expression. When calculating a suffix expression, use the number after the symbol as the 0 indentation stack

//correction of the negative sign of the occasion if(pre[i]=='-')//Negative sign use occasions { if(pre[i-1]<'0'|| pre[i-1]>'9'|| pre[i-1]=='('|| i==0) {post[j++]='_'; I++; Continue; } }

TXT arithmetic calculator