Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Goal Develop and implement a Calculator that performs basic arithmetic functions

ID: 3582268 • Letter: G

Question

Goal Develop and implement a Calculator that performs basic arithmetic functions. Details Your program will read a list of arithmetic expressions, evaluate them and display their results. All numbers in the expression will be integers, however, the results of calculations will be fractions. Your program must be able to process any valid arithmetic expression that includes the following: Nonnegative integer numbers The four basic arithmetic operations Parentheses One-letter variable names, case insensitive andXare the same) Assignment in the form var expression For each expression, evaluate it, display the result and store the result in the appropriate variable, if necessary. There are 26 variables available. These should all be fractions initialized to 0/1

Explanation / Answer

The four arithmetic operations I am assuming to be: 1. addition (+) 2. subtraction (-) 3. multiplication (*) 4. division (/) #include 002 #include 003 #include 004 #include 005 006 //#include   // string streams 007 #include   // C-style string streams 008 009 using namespace std; 010 011 istream* input; // pointer to input stream 012 013 int no_of_errors;   // note: default initialized to 0 014 015 double error(const char* s) 016 { 017     no_of_errors++; 018     cerr number_value; 057         return curr_tok=NUMBER; 058     default:            // NAME, NAME=, or error 059         if (isalpha(ch)) { 060             string_value = ch; 061             while (input->get(ch) && isalnum(ch)) 062                 string_value += ch; // string_value.push_back(ch); 063                             // to work around library bug 064             input->putback(ch); 065             return curr_tok=NAME; 066         } 067         error("bad token"); 068         return curr_tok=PRINT; 069     } 070 } 071 072 map table; 073 074 double expr(bool);  // cannot do without 075 076 077 double prim(bool get)       // handle primaries 078 { 079     if (get) get_token(); 080 081     switch (curr_tok) { 082     case NUMBER:        // floating-point constant 083     {   double v = number_value; 084         get_token(); 085         return v; 086     } 087     case NAME: 088     {   double& v = table[string_value]; 089         if (get_token() == ASSIGN) v = expr(true); 090         return v; 091     } 092     case MINUS:     // unary minus 093         return -prim(true); 094     case LP: 095     {   double e = expr(true); 096         if (curr_tok != RP) return error(") expected"); 097         get_token();        // eat ')' 098         return e; 099     } 100     default: 101         return error("primary expected"); 102     } 103 } 104 105 double term(bool get)       // multiply and divide 106 { 107     double left = prim(get); 108 109     for (;;)/> 110         switch (curr_tok) { 111         case MUL: 112             left *= prim(true); 113             break; 114         case DIV: 115             if (double d = prim(true)) { 116                 left /= d; 117                 break; 118             } 119             return error("divide by 0"); 120         default: 121             return left; 122         } 123 } 124 125 double expr(bool get)       // add and subtract 126 { 127     double left = term(get); 128 129     for (;;)/>               // ``forever'' 130         switch (curr_tok) { 131         case PLUS: 132             left += term(true); 133             break; 134         case MINUS: 135             left -= term(true); 136             break; 137         default: 138             return left; 139         } 140 } 141 142 143 144 int main(int argc, char* argv[]) 145 { 146 147     switch (argc) { 148     case 1:                 // read from standard input 149         input = &cin; 150         break; 151     case 2:                 // read argument string 152 //      input = new istringstream(argv[1]); 153         input = new istrstream(argv[1]); 154         break; 155     default: 156         error("too many arguments"); 157         return 1; 158     } 159 160     table["pi"] = 3.1415926535897932385;    // insert predefined names 161     table["e"] = 2.7182818284590452354; 162 163     while (*input) { 164         get_token(); 165         if (curr_tok == END) break; 166         if (curr_tok == PRINT) continue; 167         cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote