1.更新开根,次方
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void rooting(int a){
cout<<setw(50);
int ia=static_cast<int>(a);
SetColorAndBackground(12,0);
if(a<0)
cout<<"不能对负数"<<a<<"开平方根!\n";
else
cout<<"\t"<<"答案是"<<sqrt(a)<<"!\n";
SetColorAndBackground(15,0);
system("pause");
system("cls");
}
int main(){
SetConsoleTitle("简便计算器 作者:ZINCFFO");//i
double a,b;
char ch;
while(1){
double(a);
double(b);
meet:
SetColorAndBackground(11,0),
cout<<setw(50)<<"输入一个算式:\n";
cout<<setw(50)<<"n$是sqrt(n)\n";
cout<<setw(50)<<"a.b也是a*b\n";
cout<<setw(50)<<"a^b是pow(a,b)\n";
SetColorAndBackground(14,0),
scanf("%lf",&a);
scanf("%c",&ch);
if(ch=='$'){
rooting(a);
goto meet;
}
scanf("%lf",&b);
SetColorAndBackground(12,0);
switch(ch){
case '+':cout<<"\t答案是"<<a+b<<"!\n";break;
case '-':cout<<"\t答案是"<<a-b<<"!\n";break;
case '*':
case '.':cout<<"\t答案是"<<a*b<<"!\n";break;
case '/':{
if(abs(b)<1e-9){
cout<<"除数不能为零!\n";
break;
}
cout<<"输入小数位数:";
int l;
cin>>l;
int ia=static_cast<int>(a);
int ib=static_cast<int>(b);
cout<<ia/ib<<'.';
int r=ia%ib;
for(int i=1;i<=l;i++){
cout<<(r*10)/ib;
r=(r*10)%ib;
}
cout<<endl;
break;
}
case '%': {
if(b==0){
cout<<"除数不能为零!\n";
break;
}
int ia=static_cast<int>(a);
int ib=static_cast<int>(b);
cout<<"\t答案是"<<ia%ib<<"!\n";
break;
}
case '^':cout<<"\t答案是"<<a+b<<"!\n";break;
default:
cout<<"未知的运算符!\n";
break;
}
SetColorAndBackground(15,0);
system("pause");
system("cls");
}
return 0;
}