参考链接: C 程序,使简单的计算器使用switch ... case进行加,减,乘或除运算
/*
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
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 <iostream>
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 = '