简单计算器 C++语言实现 支持四则运算、取余、正负数、括号

/* 
Simple calculator 
input from cin,output to cout 
简单计算器:支持加减乘除取余、正负数、括号  
 
主要实现: 
 
1.两个类Token和Token_stream:将输入表达式分为单词如1.82,+,(,存储在Token类对象中,Token_stream类实现能缓存1个单词的流 
 
2.三个实现语法的函数expression(),term(),primary():实现表达式计算顺序 
 
3.计算流程函数calculate():用循环实现任意次数表达式计算,直到出错或正常退出 
 
The grammar is: 
 
Statement: 
    Expression 
    Print 
    Quit 
Print: 
    ; 
Quit: 
    q 
Expression 
    Term 
    Expression + Term 
    Expression - Term 
Term: 
    primary 
    Term * primary 
    Term / primary 
    Term % primary 
Primary: 
    Num 
    ( Expression ) 
    + primary 
    - primary 
Num: 
    floating-point-literal 
*/  
  
#include   
using namespace std;  
  
class Token;  
class Token_stream;  
  
void keep_window_open();  
double expression();  
double term();   
double primary();  
void welcome();  
void calculate();  
  
const char number = '8';  
const char print = ';';  
const char quit = 'e';  
const string prompt = ">";  
const string myresult = "=";   
  
class Token{  
public:  
    char m_kind;  
    double m_value;  
    Token(char kind = '\0', double value = 0)  
        :m_kind(kind)  
        ,m_value(value)  
    {}  
};   
  
class Token_stream{  
public:  
    Token get();  
    void putback(Token t);  
    Token_stream()  
        :m_full(false)  
        ,m_buffer('\0', 0)  
    {}   
private:  
    bool m_full;  
    Token m_buffer;  
};  
//定义能够缓存1个单词的流ts   
Token_stream ts;  
  
Token Token_stream::get()  
{  
    //缓冲区有单词,从缓冲区中取单词   
    if(true == m_full){  
        m_full = false;  
        return m_buffer;  
    }  
    //缓冲区没有单词,从输入流中取单词   
    char ch;  
    cin>>ch;  
    switch(ch){  
        case '+':  
        case '-':  
        case '*':  
        case '/':  
        case '%':  
        case '(':  
        case ')':  
        case print:  
        case quit:  
            return Token(ch);  
        case '.':  
        case '0':case '1':case '2':case '3':case '4':  
        case '5':case '6':case '7':case '8':case '9':  
            {  
                cin.putback(ch);  
                double val;  
                cin>>val;  
                return Token(number, val);  
            }  
        default:  
            {  
                cout<<"err input!"<>ch;  
    exit(EXIT_FAILURE);  
}  


//处理+ - 加、减运算  
double expression()  
{  
    double left = term();  
    Token t = ts.get();  
    while(true){  
        switch(t.m_kind){  
            case '+':  
                left += term();  
                t = ts.get();  
                break;   
            case '-':  
                left -= term();  
                t = ts.get();  
                break;  
            default:  
                ts.putback(t);  
                return left;  
        }  
    }  
}   

//处理* / % 乘、除、取余运算  
double term()  
{  
    double left = primary();  
    Token t = ts.get();  
    while(true){  
        switch(t.m_kind){  
            case '*':  
                left *= primary();  
                t = ts.get();  
                break;   
            case '/':  
                {  
                    double d = primary();  
                    if(0 == d){  
                        cout<<"divide into zero"<

你可能感兴趣的:(小程序)