程序中过度地使用if else会导致代码臃肿 纠正:代码舒畅。
或者说,过度地使用switch会导致代码臃肿。
我为什么这么说呢?因为,我上期的代码是100行,这期的代码是
/*我在此再次致谢我的第一任C++老师,the book!
没有它,这个程序无从完成。
悲!系统原装计算器!*/
#include "myinclude.h"
jmp_buf start2;
double string_switch_mycalc_P1(const string input_value, double a, double b){
map<string, unsigned>map_ = {
{"+", 1},
{"-", 2},
{"*", 3},
{"/", 4},
{"^", 5},
};
unsigned casevalue = map_[input_value];
double c;
switch(casevalue){
case 1:c = a + b;break;
case 2:c = a - b;break;
case 3:c = a * b;break;
case 4:{
unsigned d = fmod(a, b);
c = a / b;
d = constrain(d, -2147483648, 2147483647);
c = constraind(c, -999999999999999, 999999999999999);
cout << "\n结果:" << setprecision(20) << fixed << setprecision(0) << c << "余" << setprecision(10) << fixed << setprecision(0) << d << " " << setprecision(20) << fixed << setprecision(3) << c << "\n谢谢惠顾,欢迎下次光临!\n\n";
system("pause");
longjmp(start2, 1);
}
case 5:{
if((a == 1) || (b == 1)){
printf("\n乘方运算中第一或第二个数字不能为1!\n\n");
system("pause");
longjmp(start2, 1);
}
c = pow(a, b);
break;
}
default:
cout << "出现错误,已从头运行。" << endl << endl;
longjmp(start2, 1);
}
return c;
}
int main(){
double a, b, c;unsigned Start, End;string i;bool yes_no_map = 0;
Start = time(NULL);
printf("计算器欢迎您的光临!");
_setjmp(start2);//无用[doge]
start://无用[doge]
printf("\n请选择您要计算的运算方式(如加减乘除)或命令(命令帮助请输入/help),请输入加减乘除的符号(如 + - * /),否则将运算失败。\n");
printf("目前支持加减乘除和乘方;开平方\t对应符号+ - * / ^ ^/\n");
cin >> i; //困扰我多年的scanf string问题!
if((i != "+") and (i != "-") and (i != "*") and (i != "/") and (i != "^") and (i != "^/") and (i != "/help") and (i != "/sr") and (i != "/cls") and (i != "/exit") and (i != "/shut")){
srand(unsigned(time(NULL)));
unsigned errq = rand() % 4;
printf("\n");
if(errq == 0) printf("Error(一定有bug)");
else if(errq == 1) printf("你说啥?");
else if(errq == 2) printf("傻");
else if(errq == 3) printf("what?");
printf("\n\n");
system("pause");
goto start;
}
if(i == "^/"){
yes_no_map = 0;
if(a == 1){
printf("\n开平方运算中数字不能为1!\n\n");
system("pause");
goto start;
}
printf("请输入要开平方的数字:");
scanf("%lf", &a);
c = sqrt(a);
goto stop;
}
else yes_no_map = 1;
if(i == "/help"){
printf("\n1、/cls:清空屏幕,重新从头运行;\n2、/sr:显示程序源代码;\n3、/shut:关机;\n4、/exit:退出程序;\n\n#以上是所有命令帮助#\n\n");
system("pause");
goto start;
}
if(i == "/exit"){
End = time(NULL);
unsigned Time = End - Start;
printf("\nTotal running time : %u seconds\n\n", Time);
system("pause");
return 0;
}
if(i == "/cls"){
system("cls");
printf("计算器欢迎您的光临!");
goto start;
}
if(i == "/sr"){
fstream fin;
fin.open("计算器.cpp", ios::in);
if(!fin.is_open()){
printf("\nError\n\n");
system("pause");
goto start;
}
system("cls");
printf("源代码:\n");
char buf[100000];
while(fin.getline(buf, sizeof(buf))) printf("%s\n", buf);
printf("\n");
system("pause");
fin.close();
system("cls");
printf("计算器欢迎您的光临!");
goto start;
}
if(i == "/shut") system("shutdown /s /t 0");
printf("请输入您要计算的两个数字,两个数字间请加空格(此计算器不支持两个以上数字的运算,但支持重复运算):");
scanf("%lf %lf", &a, &b);
stop: /*在这里我只能用传统cout了..*/
yes_no_map == 1 ? cout << "\n结果:" << setprecision(20) << fixed << setprecision(3) << string_switch_mycalc_P1(i, a, b) << "\n谢谢惠顾,欢迎下次光临!\n\n" : cout << "\n结果:" << setprecision(20) << fixed << setprecision(3) << c << "\n谢谢惠顾,欢迎下次光临!\n\n";
system("pause");
goto start;
}
122行。
所以,过度地使用switch会导致代码臃肿。
开玩笑!是仅string情况下的。。。