c++计算器(升级版)

该程序是一个用C++编写的简易命令行计算器,支持加、减、乘、除以及随机数生成和条件判断等操作。当输入特定运算符时,程序会执行相应的计算或逻辑检查。例如,它可以处理除零错误,并在用户请求时生成指定范围内的随机数。
摘要由CSDN通过智能技术生成
#include<bits/stdc++.h>
#include<cstdlib>
using namespace std;

下面就是计算器的一些功能了(可以根据需要进行修改)。 


	while(n!='|'){
		cin>>a>>n>>b;
		//+ - * /
		if(n=='+'){
			cout<<a+b<<endl;
		}
		if(n=='-'){
			cout<<a-b<<endl;
		}
		if(n=='*'){
			cout<<a*b<<endl;
		}
		if(n=='/'){
			if(b!=0){
				cout<<a/b<<endl;
			}else{
				cout<<"错误"<<endl;
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		//~
		if(n=='~'){
			if(a<b){
			int mu,ch;
			ch=b;
			srand(time(NULL)); 
			mu=rand()%ch;
			system("cls");
			cout<<"0~"<<ch<<endl;
			cout<<mu<<endl;
			}else{
				cout<<"错误"<<endl;
			}
			
		}
		
		
		
		if(n=='!'){
			system("cls");
			char mn,nn;
			int f,g;
			cin>>f>>mn>>nn>>g;
			if(mn=='!'){
				if(nn=='='){
					if(!(f==g)){
						cout<<"是";
					}else if(f==g){
						cout<<"否";
					}
				}
				
				if(nn=='<'){
					if(!(f<g)){
						cout<<"是";
					}else if(f<g){
						cout<<"否";
					}
				}
				
				if(nn=='>'){
					if(!(f>g)){
						cout<<"是";
					}else if(f>g){
						cout<<"否";
					}
				}
			}
		}
	}

 

(求余) 

//%
		if(n=='%'){
			c=a;
			d=b;
			cout<<c/d<<endl;			
		}

 


 (判断)

//> = <
		if(n=='>'){
			if(a>b){
				cout<<"是"<<endl;
			}else{
				cout<<"否"<<endl;
			}
		}
		if(n=='='){
			if(a==b){
				cout<<"是"<<endl;
			}else{
				cout<<"否"<<endl;
			}
		}
		if(n=='<'){
			if(a<b){
				cout<<"是"<<endl;
			}else{
				cout<<"否"<<endl;
			}
		}
		

(求随机数) 

//~
		if(n=='~'){
			if(a<b){
			int mu,ch;
			ch=b;
			srand(time(NULL)); 
			mu=rand()%ch;
			system("cls");
			cout<<"0~"<<ch<<endl;
			cout<<mu<<endl;
			}else{
				cout<<"错误"<<endl;
			}
			
		}

1+1

2

1-1

0

1*1

1

1/1

1

1/0

错误

0~100

(略)

1>1

2>1

1<1

1<2

1=1

2=1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值