更新内容:
1.各种装饰(摆设)
2.转换进制(十转)
3.取消说明(清爽)
4.添加阶乘(摆烂)
5.修复错误(找错)
#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 SetSize(unsigned uCol,unsigned uLine)
{
char cmd[64];
sprintf(cmd,"mode con cols=%d lines=%d",uCol,uLine);
system(cmd);
}
void rooting(double a){
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");
}
void factorial(double a){
int num=1;
for(int i=2;i<=a;i++){
num*=i;
}
SetColorAndBackground(12,0);
cout<<"\t答案是"<<num<<"!\n";
SetColorAndBackground(15,0);
system("pause");
system("cls");
}
void change_scale(int s,int n){
int l=0,a[1011]={};
SetColorAndBackground(12,0);
while(s>0){
l++;
a[l]=s%n;
s/=n;
}
cout<<"答案是";
for(int i=l;i>=1;i--)
if(a[i]<10)
cout<<a[i];
else
cout<<char(a[i]+55);
cout<<"!\n";
SetColorAndBackground(15,0);
system("pause");
system("cls");
}
int main(){
SetSize(60,8);
SetConsoleTitle("简便计算器 作者:ZINCFFO");//i
double a,b;
char ch;
while(1){
meet:
double(a);
double(b);
SetColorAndBackground(11,0);
cout<<setw(25)<<"输入一个算式:\n";
SetColorAndBackground(15,0);
cout<<"\t1\t2\t3\t+\n";
cout<<"\t4\t5\t6\t-\n";
cout<<"\t7\t8\t9\t*\n";
cout<<"\t=\t0\t.\t/\n";
cout<<"软键盘研发中......\n";
SetColorAndBackground(14,0);
scanf("%lf",&a);
scanf("%c",&ch);
if(ch=='$'){
rooting(a);
goto meet;
}
if(ch=='!'){
factorial(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 '*':cout<<"\t答案是"<<a*b<<"!\n";break;
case '/':{
if(abs(b)<1e-9){
cout<<"除数不能为零!\n";
break;
}
SetColorAndBackground(11,0);
cout<<"输入小数位数:";
SetColorAndBackground(14,0);
int l;
cin>>l;
int ia=static_cast<int>(a);
int ib=static_cast<int>(b);
SetColorAndBackground(12,0);
cout<<"答案是";
cout<<ia/ib<<'.';
int r=ia%ib;
for(int i=1;i<=l;i++){
cout<<(r*10)/ib;
r=(r*10)%ib;
}
cout<<"!";
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答案是"<<pow(a,b)<<"!\n";break;
case '~':{
change_scale(a,b);
goto meet;
break;
}
default:
cout<<"未知的运算符!\n";
break;
}
SetColorAndBackground(15,0);
system("pause");
system("cls");
}
return 0;
}