TXT arithmetic calculator, C language arithmetic Calculator

Source: Internet
Author: User

TXT arithmetic calculator, C language arithmetic Calculator

Basic Ideas: Use the getline function to read the infix expression from the TXT file in sequence, convert it into a suffix expression, and then calculate the result and compare it with the user result.

Processing of integers, scores, and decimal places: treats decimal places and integers as the default denominator of 1. Creates a score class, and converts integers and decimal places to scores when being converted to Suffixes by infix.

Class Fenshu {private: int fz; int fm; public: int getfz () {return fz;} int getfm () {return fm;} void setfz (int FZ) {fz = FZ;} void setfm (int FM) {if (FM! = 0) fm = FM; else cout <"denominator cannot be 0 \ n";} Fenshu () {fz =-1; fm =-1 ;} fenshu (int a, int B) {fz = a; if (B! = 0) fm = B; else cout <"denominator cannot be 0 \ n";} int gcd (int a, int B) // returns a; return gcd (B, a % B);} void yuefen () {// int Gcd = 1; gcd = gcd (fz, fm); // cout <"GCD:" <Gcd <endl; fz = fz/Gcd; fm = fm/Gcd; // cout <"results:"; show (); // test} Fenshu operator + (Fenshu F) {return Fenshu (fz * F. fm + F. fz * fm, fm * F. fm);} Fenshu operator-(Fenshu F) {return Fenshu (fz * F. fm-F.fz * fm, fm * F. fm);} Fenshu operator * (Fenshu F) {return Fenshu (fz * F. fz, fm * F. fm);} Fenshu operator/(Fenshu F) {return Fenshu (fz * F. fm, fm * F. fz);} void show () {cout <fz <"/" <fm <endl ;}};

The division of the moving phase used to calculate the maximum common appointment has been written in the previous blog. To facilitate subsequent code writing, heavy load is used.

Processing of converting infix to Suffix: Using stack as the data structure.

Template <class ElemType> class MyStack {public: const static int MAXSIZE = 100; ElemType data [MAXSIZE]; int top; // stack pin public: void init (); // initialize the stack bool empty (); // judge whether the stack is empty ElemType gettop (); // read the top element of the stack (not included) void push (ElemType x ); // element pop (); // output stack };

 

 

Problems:

1. The score class is defined as the numerator and denominator as int type variables. Decimals in the question cannot be computed;

Analysis: converts decimals into integers that can be saved into scores. Each digit after the decimal point has a denominator of 10,

// Convert the numeric string into the corresponding number Fenshu read_number (char str [], int * I) {Fenshu x (); int k = 0; while (str [* I]> = '0' & str [* I] <= '9') // process the integer part {x. setfz (x. getfz () * 10 + (str [* I]-'0'); (* I) ++;} if (str [* I] = '. ') // process the fractional part {(* I) ++; while (str [* I]> = '0' & str [* I] <= '9 ') {x. setfz (x. getfz () * 10 + (str [* I]-'0'); x. setfm (x. getfm () * 10); (* I) ++; k ++ ;}} return x ;}

 

2. If a question with a negative sign '-' is displayed, the stack is empty;

Analysis: the negative and minus signs are both '-'. As a result, when the suffix is changed, the negative and minus signs are operated in the same way (the top operands of the two stacks are displayed), resulting in incorrect operations;

Solution: Find the '-' used as the negative sign and replace it with another symbol to write the suffix expression. When calculating the suffix expression, press the number after the symbol as 0 to the stack.

// If (pre [I] = '-') // negative sign usage {if (pre [I-1] <'0' | pre [I-1]> '9' | pre [I-1] = '(' | I = 0) {post [j ++] = '_'; I ++; continue ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.