c++ 题目;实现算数表达式的计算

来源:百度知道 编辑:UC知道 时间:2024/05/26 15:03:59
要求:用户输入表达式,其中包括字符有:+、-、*、/、)、(和数字,编程完成计算结果并打印出。实现算数表达式的计算

// 加减乘除.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class cal //加减乘出计算类
{
public:
int add(int x,int y) //加法定义函数
{
return x+y;
}
int sub(int x,int y) //减法定义函数
{
return x-y;
}
int mul(int x,int y) //乘法定义函数
{
return x*y;
}
int div(int x,int y) //除法定义函数
{
if(y==0)
cout<<"0不能作除数"<<endl;
else
return x/y;
return 0;
}
};

int main()
{
cal c; //定义对象
int x, y;
cout<<"请输入要计算的两个数,中间空格隔开比如2 3:";
cin>>x>>y;
cout<<"x+y="<<c.add(x,y)<<endl;
cout<<"x-y="<<c.sub(x,y)<<endl;
cout<<"y-x="<<c.sub(y,x)<<endl;
cout<<"x*y="&l