程序模拟简单运算器的工作:输入一个算式(没有空格),遇到‘=’号说明输入结束,输出结果。假设计算器只能进行加、减、乘、除运算,运算数和结果都是整数,4种运算符的优先级相同,按从左到右的顺序计算。例如,输入“1+2*10-10/2"后结果为10。
C++版本一
#include <stdio.h>
#include <stdlib.h>
int chartonum(char c);
int main(int argc, char *argv[]) {
char str[1000];//存储算试
while(gets(str)!=NULL){
int i;
int result=0;//存储结果
int temp=0;//临时数据存储
int flag=0;//标记是否完成第一个算术运算
char c;//记录运算符
for(i=0;str[i]!='\0';i++)
{
if('0'<=str[i]&&str[i]<='9'){
//获取两个运算符之间数
temp=temp*10+chartonum(str[i]);
}else{
if(flag==1){
//运算
switch (c){
case '+':
result+=temp;
temp=0;
c=str[i];
break;
case '-':
result-=temp;
temp=0;
c=str[i];
break;
case '*':
result*=temp;
temp=0;
c=str[i];
break;
case '/':
result/=temp;
temp=0;
c=str[i];
break;
}
}
else{
//第一个运算符处理
result=temp;
temp=0;
c=str[i];
flag=1;
}
}
}
printf("%d\n",result);
}
return 0;
}
//字符转换数字的函数
int chartonum(char c){
switch (c) {
case '0':return 0;
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
}
}
C++版本二
PS:与题目无关
main.cpp
/* *@Author: STZG *@Language: C++ */
#include<iostream>
#include "mymath.h"
using namespace std;
int main()
{
long long a, b, result;
char c;
while(1){
cout<<"请输入第一个数:"<<endl;
cin>>a;
cout<<"请输入第二个数:"<<endl;
cin>>b;
cout<<"请输入想要进行运算的符号:"<<endl;
cin>>c;
if(c=='+'){
result=add(a,b);
}else if(c=='-'){
result=minu(a,b);
}else if(c=='*'){
result=times(a,b);
}else if(c=='/'){
result=divide(a,b);
}else if(c=='%'){
result=mod(a,b);
}else{
cout<<"无效运算符"<<endl;
continue;
}
cout<<a<<" "<<c<<" "<<b<<" = "<<result<<endl;
}
return 0;
}
mymath.cpp
#include "mymath.h"
extern int add(int a,int b){
return a+b;
}
extern long add(long a,long b){
return a+b;
}
extern long long add(long long a,long long b){
return a+b;
}
extern double add(double a,double b){
return a+b;
}
extern float add(float a,float b){
return a+b;
}
extern short add(short a,short b){
return a+b;
}
extern int times(int a,int b){
return a*b;
}
extern long times(long a,long b){
return a*b;
}
extern long long times(long long a,long long b){
return a*b;
}
extern double times(double a,double b){
return a*b;
}
extern float times(float a,float b){
return a*b;
}
extern short times(short a,short b){
return a*b;
}
extern int minu(int a,int b){
return a-b;
}
extern long minu(long a,long b){
return a-b;
}
extern long long minu(long long a,long long b){
return a-b;
}
extern double minu(double a,double b){
return a-b;
}
extern float minu(float a,float b){
return a-b;
}
extern short minu(short a,short b){
return a-b;
}
extern int divide(int a,int b){
return a/b;
}
extern long divide(long a,long b){
return a/b;
}
extern long long divide(long long a,long long b){
return a/b;
}
extern double divide(double a,double b){
return a/b;
}
extern float divide(float a,float b){
return a/b;
}
extern short divide(short a,short b){
return a/b;
}
extern int mod(int a,int b){
return a%b;
}
extern long mod(long a,long b){
return a%b;
}
extern long long mod(long long a,long long b){
return a%b;
}
extern short mod(short a,short b){
return a%b;
}
mymath.h
#ifndef MYMATH_H_INCLUDED
#define MYMATH_H_INCLUDED
int add(int a,int b);
long add(long a,long b);
long long add(long long a,long long b);
double add(double a,double b);
float add(float a,float b);
short add(short a,short b);
int times(int a,int b);
long times(long a,long b);
long long times(long long a,long long b);
double times(double a,double b);
float times(float a,float b);
short times(short a,short b);
int minu(int a,int b);
long minu(long a,long b);
long long minu(long long a,long long b);
double minu(double a,double b);
float minu(float a,float b);
short minu(short a,short b);
int divide(int a,int b);
long divide(long a,long b);
long long divide(long long a,long long b);
double divide(double a,double b);
float divide(float a,float b);
short divide(short a,short b);
int mod(int a,int b);
long mod(long a,long b);
long long mod(long long a,long long b);
short mod(short a,short b);
#endif // MYMATH_H_INCLUDED
C++版本三
类与对象版本
main.cpp
/* *@Author: STZG *@Language: C++ */
#include<iostream>
#include"Calculator.h"
using namespace std;
int main()
{
Calculator mycalculator;
mycalculator.run();
return 0;
}
mymath.cpp
#include "mymath.h"
extern int add(int a,int b){
return a+b;
}
extern long add(long a,long b){
return a+b;
}
extern long long add(long long a,long long b){
return a+b;
}
extern double add(double a,double b){
return a+b;
}
extern float add(float a,float b){
return a+b;
}
extern short add(short a,short b){
return a+b;
}
extern int times(int a,int b){
return a*b;
}
extern long times(long a,long b){
return a*b;
}
extern long long times(long long a,long long b){
return a*b;
}
extern double times(double a,double b){
return a*b;
}
extern float times(float a,float b){
return a*b;
}
extern short times(short a,short b){
return a*b;
}
extern int minu(int a,int b){
return a-b;
}
extern long minu(long a,long b){
return a-b;
}
extern long long minu(long long a,long long b){
return a-b;
}
extern double minu(double a,double b){
return a-b;
}
extern float minu(float a,float b){
return a-b;
}
extern short minu(short a,short b){
return a-b;
}
extern int divide(int a,int b){
if(b==0){
cout<<"除数不能为0!"<<endl;
return -1;
}
return a/b;
}
extern long divide(long a,long b){
if(b==0){
cout<<"除数不能为0!"<<endl;
return -1;
}
return a/b;
}
extern long long divide(long long a,long long b){
if(b==0){
cout<<"除数不能为0!"<<endl;
return -1;
}
return a/b;
}
extern double divide(double a,double b){
if(b==0){
cout<<"除数不能为0!"<<endl;
return -1;
}
return a/b;
}
extern float divide(float a,float b){
if(b==0){
cout<<"除数不能为0!"<<endl;
return -1;
}
return a/b;
}
extern short divide(short a,short b){
if(b==0){
cout<<"除数不能为0!"<<endl;
return -1;
}
return a/b;
}
extern int mod(int a,int b){
if(b==0){
cout<<"模数不能为0!"<<endl;
return -1;
}
return a%b;
}
extern long mod(long a,long b){
if(b==0){
cout<<"模数不能为0!"<<endl;
return -1;
}
return a%b;
}
extern long long mod(long long a,long long b){
if(b==0){
cout<<"模数不能为0!"<<endl;
return -1;
}
return a%b;
}
extern short mod(short a,short b){
if(b==0){
cout<<"模数不能为0!"<<endl;
return -1;
}
return a%b;
}
mymath.h
#ifndef MYMATH_H_INCLUDED
#define MYMATH_H_INCLUDED
#include<iostream>
using namespace std;
int add(int a,int b);
long add(long a,long b);
long long add(long long a,long long b);
double add(double a,double b);
float add(float a,float b);
short add(short a,short b);
int times(int a,int b);
long times(long a,long b);
long long times(long long a,long long b);
double times(double a,double b);
float times(float a,float b);
short times(short a,short b);
int minu(int a,int b);
long minu(long a,long b);
long long minu(long long a,long long b);
double minu(double a,double b);
float minu(float a,float b);
short minu(short a,short b);
int divide(int a,int b);
long divide(long a,long b);
long long divide(long long a,long long b);
double divide(double a,double b);
float divide(float a,float b);
short divide(short a,short b);
int mod(int a,int b);
long mod(long a,long b);
long long mod(long long a,long long b);
short mod(short a,short b);
#endif // MYMATH_H_INCLUDED
Calculator.cpp
#include "Calculator.h"
Calculator::Calculator()
{
a=0;
b=0;
result=0;
//ctor
}
Calculator::~Calculator()
{
//dtor
}
void Calculator::run(){
while(1){
cout<<"请输入第一个数:"<<endl;
cin>>a;
cout<<"请输入第二个数:"<<endl;
cin>>b;
cout<<"请输入想要进行运算的符号:"<<endl;
cin>>c;
if(c=='+'){
result=add(a,b);
}else if(c=='-'){
result=minu(a,b);
}else if(c=='*'){
result=times(a,b);
}else if(c=='/'){
result=divide(a,b);
}else if(c=='%'){
result=mod(a,b);
}else{
cout<<"无效运算符"<<endl;
continue;
}
cout<<a<<" "<<c<<" "<<b<<" = "<<result<<endl;
}
}
Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<iostream>
#include "mymath.h"
using namespace std;
class Calculator
{
public:
Calculator();
virtual ~Calculator();
void run();
protected:
private:
long long a, b, result;
char c;
};
#endif // CALCULATOR_H