模拟简单计算器

程序模拟简单运算器的工作:输入一个算式(没有空格),遇到‘=’号说明输入结束,输出结果。假设计算器只能进行加、减、乘、除运算,运算数和结果都是整数,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

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:19
点赞 评论 收藏
分享
Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
11-28 17:58
门头沟学院 Java
美团 JAVA开发 n×15.5
牛客786276759号:百度现在晋升很难的 而且云这块的业务没美团好 你看百度股价都跌成啥样了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务