求JAVA代码?

实现计算器,从控制台输入表达式,输出结果。支持四则运算。2,支持括号运算... 实现计算器,从控制台输入表达式,输出结果。支持四则运算。
2,支持括号运算
展开
 我来答
点点通软件公社
2019-11-30 · 超过80用户采纳过TA的回答
知道小有建树答主
回答量:148
采纳率:33%
帮助的人:9.7万
展开全部
/*
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 <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 = '\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!"<<endl;
keep_window_open();
}
}
}

void Token_stream::putback(Token t)
{
if(true == m_full){
cout<<"err: m_buffer is full."<<endl;
keep_window_open();
return;
}
m_full = true;
m_buffer = t;
}

void keep_window_open()
{
char ch;
fflush(stdin);
cin>>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"<<endl;
keep_window_open();
}
left /= d;
t = ts.get();
break;
}
case '%':
{
double d = primary();
int i1 = int(left);
int i2 = int(d);
if(i1 != left){
cout<<"left operand not int"<<endl;
keep_window_open();
}
if(i2 != d){
cout<<"right operand not int"<<endl;
keep_window_open();
}
if(i2 == 0){
cout<<"% divide into 0"<<endl;

}
left = i1 % i2;
t = ts.get();
break;
}
default:
ts.putback(t);
return left;
}
}
}

//处理()- + 括号、正、负运算
double primary()
{
Token t = ts.get();
switch(t.m_kind){
case number:
return t.m_value;
case '(':
double d = expression();
t = ts.get();
if(')' != t.m_kind){
cout<<"expected ')'"<<endl;
keep_window_open();
}
return d;
case '-':
return -primary();
case '+':
return +primary();
default:
cout<<"expected primary!"<<endl;
keep_window_open();
}
}

void calculate()
{
while(cin){
cout<<prompt;
Token t = ts.get();
while(prinwww.scxhdzs.com#kind)t = ts.get();
if(quiwww.scxhdzs.com#kind)return;
else
ts.putback(t);
cout<<myresult<<expression()<<endl;
}
}

void welcome()
{
cout<<"Welcome to our simple calculator."<<endl
<<"-----------------------------"<<endl
<<"floating-point numbers"<<endl
<<"operator: '+' '-' '*' '/' '%'"<<endl
<<"negative: '-'"<<endl
<<"positive: '+'"<<endl
<<"end of expression: ';'"<<endl
<<"quit: 'e'"<<endl
<<"-----------------------------"<<endl;
}

int main()
{
welcome();
calculate();
cout<<"See you."<<endl;
keep_window_open();
return 0;
}
杰西米特瓦利亚
2020-01-08 · TA获得超过1717个赞
知道小有建树答主
回答量:1239
采纳率:54%
帮助的人:346万
展开全部
你可以用java调用js 代码, 然后js中有eval('表达式'), 这样就可以帮你运行任何数据表达式了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式