Hello, I have a good thing here.

Source: Internet
Author: User
Tags mul

This is the C + + programming language Textbook of the desktop Calculator source code.
It took me a day to fix it. Can be calculated using, such as input:
a=2
B=4
2*b+34* (A+7)
Program can run the results
The meta program is as follows:


Desktop Calculator Application Example
#include <iostream>
#include <string>
#include <map>
#include <cctype>
using namespace Std;

Enum token_value{
NAME, number, end,
Plus= ' + ', minus= '-', mul= ' *, div= '/',
Print= '; ', assign= ' = ', lp= ' (', rp= ') '
};
Token_value Curr_tok=print;

Double expr (bool get);
Double term (bool get);
Double Prim (bool get);
Token_value Get_token ();
Double error (const string&s);
map<string,double> table;

Double expr (bool get)
{
Double Left=term (get); Add and Substrack
for (;;)
Switch (Curr_tok) {
Case PLUS:
Left+=term (TRUE);
Break
Case minus:
Left-=term (TRUE);
Break
Default
return to left;
}
}


Double term (bool get)
{
Double Left=prim (get);
for (;;)
Switch (Curr_tok) {
Case MUL:
Left*=prim (TRUE);
Break
Case DIV:
if (double D=prim (true)) {
Left/=d;
Break
}
return error ("divide by 0");
Default
return to left;
}
}
Double Number_value;
String string_value;
Double Prim (bool get)//handle primaries
{
if (get) get_token ();
Switch (Curr_tok) {
Case Number://floating-point constant
{Double v=number_value;
Get_token ();
return v;
}
Case NAME:
{double&v=table[string_value];
if (Get_token () ==assign) v=expr (true);
return v;
}
Case minus://unary minus
Return-prim (TRUE);
Case LP:
{Double e=expr (true);
if (CURR_TOK!=RP) return error (") epected");
Get_token (); Eat ') '
return e;
}
Default
Return error ("Primry expected");
}
}


Token_value Get_token ()
{
Char ch=0;
cin>>ch;
Switch (CH) {
Case 0:
return curr_tok=end;
Case '; ':
Case ' * ':
Case '/':
Case ' + ':
Case '-':
Case ' (':
Case ') ':
Case ' = ':
return Curr_tok=token_value (CH);
Case ' 0 ': Case ' 1 ': Case ' 2 ': Case ' 3 ': Case ' 4 ':
Case ' 5 ': Case ' 6 ': Case ' 7 ': Case ' 8 ': Case ' 9 ':
Case '. ':
Cin.putback (CH);
cin>>number_value;
return curr_tok=number;
Default://name,name=,or Error
if (Isalpha (CH)) {
Cin.putback (CH);
cin>>string_value;
return curr_tok=name;
}
Error ("Bad token");
return curr_tok=print;
}
}


int no_of_errors;
Double error (const string &s)
{
no_of_errors++;
cerr<< "error:" <<s<< ' n ';
return 1;
}

int main ()
{

table["PI"]=3.1415926535897932385;
table["E"]=2.7182818284590452354;
while (CIN)
{
Get_token ();
if (curr_tok==end) break;
if (curr_tok==print) continue;
Cout<<expr (false) << '/n ';
}

return no_of_errors;
}


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.