表达式变量计算器 java,计算器类(C++&JAVA——表达式转换、运算、模板公式)...

运行:

(a+b)*c

后缀表达式:ab+c*

赋值:

Enter the a : 10

Enter the b : 3

Enter the c : 5

结果为:65

代码是我从的逻辑判断系统改过来的,可进行扩展或者修改

注意:1、适用变量为单字符。

2、表达式不含空格

PS:如果想让变量为多字符(字符串),那么变量与变量、变量与运算符之间应该用空格分开

#include

#include

#include

#include

#include

using namespace std;

class Logic {

public:

Logic() {} //构造函数

void Load(string); //input

int priority(char); //获取运算符优先级

string trans(string); //中缀式->后缀式

double calculate(); //逻辑判断

void V_assign(); //变量赋值

string M_exp; //中缀式

string P_exp; //后缀式

map variate; //赋值序列

};

void Logic::Load(string str) {

M_exp = str;;

P_exp = trans(M_exp); //处理数据(表达式转换)

}

int Logic::priority(char ch) {

if (ch == '*'||ch=='/')

return 2;

if (ch == '+'||ch=='-')

return 1;

if (ch == '(')

return -1;

return 0;

}

double Logic::calculate() {

string operators("+-*/");

stack res; //此栈用作运算

double a, b;

for (int i = 0; i

if (operators.find(P_exp[i]) == string::npos) { //遇到操作数,根据“字典”翻译后入栈

res.push(variate[P_exp.substr(i, 1)]);

}

else {

switch (P_exp[i]) {

case '+':

a = res.top();

res.pop();

b = res.top();

res.pop();

res.push(a + b);

break;

case '*':

a = res.top();

res.pop();

b = res.top();

res.pop();

res.push(a * b);

break;

case '-':

a = res.top();

res.pop();

b = res.top();

res.pop();

res.push(b-a);

break;

case '/':

a = res.top();

res.pop();

b = res.top();

res.pop();

res.push(b/a);

break;

}

}

}

return res.top();

}

string Logic::trans(string m_exp) {

string p_exp;

stack stk;

string operators("+-*/(");

for (int i = 0; i < m_exp.length(); i++) {

string one;

if (operators.find(m_exp[i]) != string::npos) { //出现操作符

if (m_exp[i] == '(') //栈中添加左括号

stk.push(m_exp[i]);

else { //操作符的优先级判断

while ((!stk.empty()) && (priority(m_exp[i]) <= priority(stk.top()))) { //当栈不为空时,进行优先级判断

p_exp.push_back(stk.top()); //若当前操作符优先级低于栈顶,弹出栈顶,放到后缀式中

stk.pop();

}

stk.push(m_exp[i]); //将当前操作符入栈

}

}

else if (m_exp[i] == ')') { //出现右括号时,将栈中元素一直弹出,直至弹出左括号

while (stk.top() != '(') {

p_exp.push_back(stk.top());

stk.pop();

}

stk.pop(); //弹出左括号

}

else { //把操作数加入到后缀式中

variate[m_exp.substr(i, 1)] = 0;

p_exp.push_back(m_exp[i]);

}

}

while (!stk.empty()) { //将栈中剩余操作符放到后缀式中

p_exp.push_back(stk.top());

stk.pop();

}

return p_exp;

}

void Logic::V_assign() { //公式赋值

int i = 0;

for (auto it = variate.begin(); it != variate.end(); it++) {

cout << "Enter the " << it->first << " : ";

cin >> it->second;

}

}

int main() {

Logic my;

string str;

cin >> str;

my.Load(str);

cout << "后缀表达式:" << my.P_exp << endl;

cout << "赋值:" << endl;

my.V_assign();

cout<

return 0;

}

JAVA:

import java.util.Scanner;

public class Calculate {

String m_exp=new String() ,p_exp=new String();

int result=0;

public Calculate(String exp) {

m_exp=exp;

}

int counter(){

int stk[]=new int[100],a,b;

int top=-1;

for(int i=0;i

if(p_exp.charAt(i)>='0'&&p_exp.charAt(i)<='9'){

stk[++top]=Character.getNumericValue(p_exp.charAt(i));

}

else{

switch (p_exp.charAt(i)) {

case '+':

a = stk[top--];

b = stk[top--];

stk[++top]=a+b;

break;

case '*':

a = stk[top--];

b = stk[top--];

stk[++top]=a*b;

break;

case '-':

a = stk[top--];

b = stk[top--];

stk[++top]=b-a;

break;

case '/':

a = stk[top--];

b = stk[top--];

if(a==0)

return 1;

stk[++top]=b/a;

break;

}

}

}

if(top!=0)

return 1;

result=stk[0];

return 0;

}

String trans(){

char stk[]=new char[100];

int top=-1;

String operators=new String("+-*/(");

for (int i = 0; i < m_exp.length(); i++) {

if (operators.indexOf(m_exp.charAt(i)) != -1) { //出现操作符

if (m_exp.charAt(i) == '(') //栈中添加左括号

stk[++top]=m_exp.charAt(i);

else { //操作符的优先级判断

while ((top>=0) && (priority(m_exp.charAt(i)) <= priority(stk[top]))) { //当栈不为空时,进行优先级判断

p_exp+=stk[top--]; //若当前操作符优先级低于栈顶,弹出栈顶,放到后缀式中

}

stk[++top]=(m_exp.charAt(i)); //将当前操作符入栈

}

}

else if (m_exp.charAt(i) == ')') { //出现右括号时,将栈中元素一直弹出,直至弹出左括号

while (stk[top] != '(') {

p_exp+=stk[top--];

}

top--; //弹出左括号

}

else { //把操作数加入到后缀式中

p_exp+=m_exp.charAt(i);

}

}

while (top>=0) { //将栈中剩余操作符放到后缀式中

p_exp+=stk[top--];

}

return p_exp;

}

int priority(char ch){

if (ch == '*'||ch=='/')

return 2;

if (ch == '+'||ch=='-')

return 1;

if (ch == '(')

return -1;

return 0;

}

public static void main(String []args){ //TEST

Scanner input=new Scanner(System.in);

Calculate my=new Calculate(input.next());

my.trans(); //后缀试转换

if(my.counter()==1)

System.out.println("ERROR");

else

System.out.println(my.result);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值