#include<bits/stdc++.h>
using namespace std;
int main(){
double sum=0,s3,s7;
int sum1,f=0,s4,s6,f1=0;
char s,s1;
srand(time(0));
cout<<"您要使用全自动还是半自动(q/b)(全自动:询问,半自动:输入算式):";
cin>>s;
cout<<"可用符号有,%:百分比、^:幂、/:除号、*:乘号、+:加号、-:减号、r:随机数、x:指定数二次方、q:取余、g:根号、z:z分之1、!:阶乘、a:绝对值、p:π"<<endl;
if(s=='q'){
while(s!='返回'){
cout<<"请输入符号:";
cin>>s1;
if(s1!='r'){
if(s1=='x'){
cout<<"请输入第一个数字:";
cin>>s3;
cout<<s3*s3<<endl;
f=1;
}else if(s1=='p'){
cout<<"3.1415926535897932384626433832795";
}else if(s1=='g'){
cout<<"请输入第一个数字:";
cin>>s3;
cout<<sqrt(s3)<<endl;
f=1;
}else if(s1=='%'){
cout<<"请输入总数:";
cin>>s7;
cout<<"请输入小数:";
cin>>s3;
cout<<s3/s7*100<<"%"<<endl;
f=1;
}else if(s1=='+'||s1=='-'||s1=='*'||s1=='/'||s1=='^'||s1=='%'||s1=='r'){
cout<<"请输入第一个数字:";
cin>>s3;
cout<<"请输入第二个数字:";
cin>>s7;
}else if(s1=='!'||s1=='z'||s1=='a'){
cout<<"请输入第一个数字:";
cin>>s3;
}else{
cout<<"请输入有效符号"<<endl;
f1=1;
f=1;
}
if(f1==0){
if(s1=='+'){
sum=sum+s7+s3;
}else if(s1=='-'){
sum=sum+s7-s3;
}else if(s1=='*'){
sum=sum+s7*s3;
}else if(s1=='/'&&s7!=0){
sum=sum+s7/s3;
}else if(s1=='q'){
sum=sum+(int)s7%(int)s3;
}else if(s1=='/'&&s7==0){
cout<<"除数不能等于零"<<endl;
f=1;
}else if(s1=='pi'){
cout<<"3.141592653589793";
}else if(s1=='^'){
sum1=sum;
for(int i=1;i<s7;i++){
sum*=sum1;
}
}else if(s1=='z'){
sum=1/s3;
}else if(s1=='!'){
sum=1;
for(int i=s3;i>0;i--){
sum*=i;
}
}else if(s1=='a'){
sum=abs(s3);
}
}else if(s1=='r'){
cout<<"请输入随机数最小值:";
cin>>s4;
cout<<"请输入随机数最大值:";
cin>>s6;
sum=(rand()%(s6-s4+1))+s4;
}
}
if(f==0){
cout<<sum<<endl;
f=0;
f1=0;
}
cout<<"是否归零(y/n)"<<endl;
cin>>s;
if(s=='y'){
sum=0;
}
cout<<"是否退出(y/n)"<<endl;
cin>>s;
if(s=='y'){
return 0;
}
}
}else if(s=='b'){
while(s!='返回'){
cout<<"请输入算式(不用输入等号)"<<endl;
cin>>s3;
cin>>s1;
if(s1!='g'&&s1!='!'&&s1!='a'){
cin>>s7;
}
if(s1!='r'){
if(s1!='+'&&s1!='-'&&s1!='*'&&s1!='/'&&s1!='^'&&s1!='%'&&s1!='g'&&s1!='!'&&s1!='a'&&s1!='p'){
cout<<"请输入有效符号"<<endl;
f1=1;
f=1;
}else if(s1=='%'){
cout<<s3/s7*100<<"%"<<endl;
f=1;
}
if(f1==0){
if(s1=='+'){
sum=sum+s7+s3;
}else if(s1=='-'){
sum=sum+s7-s3;
}else if(s1=='*'){
sum=sum+s7*s3;
}else if(s1=='pi'){
cout<<"3.141592653589793";
}else if(s1=='/'&&s7!=0){
sum=sum+s7/s3;
}else if(s1=='q'){
sum=sum+(int)s7%(int)s3;
}else if(s1=='/'&&s7==0){
cout<<"除数不能等于零"<<endl;
f=1;
}else if(s1=='^'){
sum1=sum;
for(int i=1;i<s7;i++){
sum*=sum1;
}
}else if(s1=='g'){
sum=sqrt(s3);
}else if(s1=='z'){
sum=1/s3;
}else if(s1=='!'){
sum=1;
for(int i=s3;i>0;i--){
sum*=i;
}
}else if(s1=='a'){
sum=abs(s3);
}
}else if(s1=='r'){
sum=(rand()%((int)s7-(int)s3+1))+(int)s3;
}
}
if(f==0){
cout<<sum<<endl;
f=0;
f1=0;
}
}
}
return 0;
}
计算器c++
于 2024-08-18 12:12:44 首次发布