c语言编程 设计一个计算器
发布网友
发布时间:2022-04-21 21:35
我来回答
共2个回答
热心网友
时间:2023-05-20 12:29
只能给你一个思路,首先你要确定你做的交互界面是什么样的,比如画出来的对话框,还是使用命令行的输入;主要的工作量在解析用户的输入上,比如 输入 (1+3)*5, 网上有一大堆这样解析的方式;其他实现log 开方等,有math lib 可以调用,不够用的可以自己补充,也可以上网上找现成的code。
希望对你有帮助。
热心网友
时间:2023-05-20 12:29
#include "std_lib_facilities.h"
#include "math.h"
class Token {
public:
char kind; // what kind of token
double value; // for numbers: a value
Token(char ch) // make a Token from a char
:kind(ch), value(0) { }
Token(char ch, double val) // make a Token from a char and a double
:kind(ch), value(val) { }
};
//------------------------------------------------------------------------------
class Token_stream {
public:
Token_stream(); // make a Token_stream that reads from cin
Token get(); // get a Token (get() is defined elsewhere)
void putback(Token t); // put a Token back
private:
bool full; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};
//------------------------------------------------------------------------------
Token_stream::Token_stream()
:full(false), buffer(0) // no Token in buffer
{
}
//------------------------------------------------------------------------------
void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}
//------------------------------------------------------------------------------
Token Token_stream::get()
{
if (full) { // do we already have a Token ready?
// remove token from buffer
full=false;
return buffer;
}
char ch;
cin >> ch;
switch (ch) {
case ';':
case 'q':
case '(': case ')': case '+': case '-': case '*': case '/': case '^':case '!':case '%':
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('8',val);
}
default:
error("Bad token");
}
}
//------------------------------------------------------------------------------
Token_stream ts;
//------------------------------------------------------------------------------
double expression();
//------------------------------------------------------------------------------
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '+':return primary();
case '-':return -primary();
case '(':
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case '8':
return t.value;
default:
error("primary expected");
}
}
//------------------------------------------------------------------------------
double term()
{
double left = primary();
Token t = ts.get();
while(true) {
switch (t.kind) {
case '^':
{
double lefttemp;
lefttemp=left;
left=pow(lefttemp,primary());
t = ts.get();
break;
}
case '!':
{
double v = 1;
for(double i=left;i>0;i--)
v=v*i;
left = v;
t = ts.get();
break;
}
case '*':
left *= primary();
t = ts.get();
break;
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
}
}
//------------------------------------------------------------------------------
// deal with + and -
double expression()
{
double left = term();
Token t = ts.get();
while(true) {
switch(t.kind) {
case '+':
left += term();
t = ts.get();
break;
case '-':
left -= term();
t = ts.get();
break;
default:
ts.putback(t);
return left;
}
}
}
//------------------------------------------------------------------------------
int main(){
try
{
double val = 0;
while (cin) {
Token t = ts.get();
if (t.kind == 'q') break;
if (t.kind == ';')
cout << "=" << val << '\n';
else
ts.putback(t);
val = expression();
}
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
}
}
//------------------------------------------------------------------------------
仅供参考~