求C预言计算器代码
计算器一定要能加减乘除,括号,等运算!要给说明一下思路!在学数据结构的时候没有 学好呀!求高人指点指点!
[解决办法]
http://blog.csdn.net/kuzuozhou/article/details/7179188
参见我的博客
[解决办法]
//显示菜单,以输入选项和操作
void main()
{
double num1,num2,num3,temp;
int choice1 = 0, choice2, flag;
//主菜单循环,程序从此开始,它将显示选择计算器类型的菜单
do{
system("CLS");
cout<<"==========Type of Calculators============";
cout<<"\n1\tStandard Calculator\n2\tScientific Calculator\n3\tQuit";
cin>>choice1;
flag=new_cal;
switch( choice1 )
{
case 1://显示标准计算器菜单,自毁长城相应操作
do{
system("CLS");
cout<<"===============Standard Calculator==============";
cout<<"\n1\tAddtion\n2\tSubtraction\n3\tMultiplication\n4\tDivision\n5\tModulus\n6\tReturn toPrevious Menu\n7\tQuit";
if( flag==old_cal )
{
cout<<"\n8\tClear Memory";
}
cout<<"\nChoose the type of calculation:";
cin>>choice2;
switch(choice2)
{
case 1:
//如果是新计算模式,则接受第 1个操作数,否则第一个推翻九由上一次计算结果提供
if(flag==new_cal)
{
cout<<"Enter first number:";
cin>>num1;
}else{
num1=temp;
cout<<"\nFirst number is "<<num1<<endl;
}
cout<<"Enter second number:";
cin>>num2;
num3=stand_calc::addition(num1,num2);
cout<<"\nAddtion of "<<num1<<" and "<<num2<<" is "<<num3;
cout<<"\nPress any key to continue.........................";
getch();
temp=num3;
flag=old_cal;
break;
case 2:
if(flag==new_cal)
{
cout<<"Enter first number:";
cin>>num1;
}else{
num1=temp;
cout<<"\nFirst number is "<<num1<<endl;
}
cout<<"Enter second number:";
cin>>num2;
num3=stand_calc::substract(num1,num2);
cout<<"\nSubstaraction of "<<num2<<" from "<<num1<<" is "<<num3;
cout<<"\nPress any key to continue..................";
getch();
temp=num3;
flag=old_cal;
break;
case 3:
if(flag==new_cal)
{
cout<<"Enter first number:";
cin>>num1;
}else{
num1=temp;
cout<<"\nFirst number is "<<num1<<endl;
}
cout<<"Enter second number:";
cin>>num2;
num3=stand_calc::multiplication(num1,num2);
cout<<"\nMultiplication of "<<num1<<" and "<<num2<<" is "<<num3;
cout<<"\nPress any key to continue..........................";
getch();
temp=num3;
flag=old_cal;
break;
case 4:
if(flag==new_cal)
{
cout<<"Enter first number:";
cin>>num1;
}else{
num1=temp;
cout<<"\nFirst number is "<<num1<<endl;
}
cout<<"Enter second number:";
cin>>num2;
num3=stand_calc::division(num1,&num2);
cout<<"\nDivision of "<<num1<<" by "<<num2<<" is "<<num3;
cout<<"\nPress any key to continue...............";
getch();
flag=old_cal;
break;
case 5:
if(flag==new_cal)
{
cout<<"Enter first number:";
cin>>num1;
}else{
num1=temp;
cout<<"\nFirst number is "<<num1<<endl;
}
cout<<"Enter second number:";
cin>>num2;
num3=stand_calc::modulus(&num1,&num2);
cout<<"\nModulus of "<<num1<<" by "<<num2<<" is "<<num3;
cout<<"\nPress any key to continue.....................";
getch();
temp=num3;
flag=old_cal;
break;
case 6:
cout<<"\nReturning to previous menu.";
cout<<"\nPress any key to continue.......................";
getch();
break;
case 7:
cout<<"\nQuitting..........................";
cout<<"\nPress any key to coninue..........................";
getch();
exit(0);
break;
case 8:
//如果是新计算器模式,则8是非法选项,否则8选项用以重新启动一个新的计算
if(flag==new_cal)
{
cout<<"\nInvalid choice.";
cout<<"\nPerss any key to continue................";
getch();
}else{
temp=0;
flag=new_cal;
}
break;
default:
cout<<"\nInvalid choice.";
cout<<"\nPress any key to continue.......................";
getch();
break;
}
}while(choice2!=6);
break;
case 2:
//显示科学型计算器菜单的循环
do{
system("CLS");
cout<<"============Scientific Calculator===============";
cout<<"\n1\tSquare\n2\tCube\n3\tPower\n4\tFactorial\n5\tSin\n6\tCos\n7\tTan\n8\tSreturn to previous menu\n9\tQuit";
if( flag==old_cal )
{
cout<<"\n10\tClear Memory";
}
cout<<"\nChoose the type of calculation:";
cin>>choice2;
switch(choice2)
{
case 1:
if(flag==new_cal)
{
cout<<"Enter number to find square";
cin >>num1;
}else{
num1=temp;
cout<<"\nNumer is "<<num1<<endl;
}
num3=scien_calc::square(num1);
cout<<"\nSquare of "<<num1<<" is "<<num3;
cout<<"\nPress any key to continue................";
getch();
flag=old_cal;
break;
case 2:
if(flag==new_cal)
{
cout<<"Enter number to find cube";
cin >>num1;
}else{
num1=temp;
cout<<"\nNumer is "<<num1<<endl;
}
num3=scien_calc::cube(num1);
cout<<"\nCube of "<<num1<<" is "<<num3;
cout<<"\nPress any key to continue................";
getch();
flag=old_cal;
break;
case 3:
if(flag==new_cal)
{
cout<<"Enter first number for base to find power:";
cin>>num1;
}else{
num1=temp;
cout<<"\nFirst number is "<<num1<<endl;;
}
cout<<"\nEnter secodn number for power to find power:";
cin>>num2;
num3=scien_calc::power(num1,num2);
cout<<"\n"<<num1<<" to the power "<<num2<<" is "<<num3;
cout<<"\nPress any key to continue......................";
getch();
temp=num3;
flag=old_cal;
break;
case 4:
long int num4;
if(flag==new_cal)
{
cout<<"\nEnter first numberto find factorial:";
cin>>num1;
}else{
num1=temp;
cout<<"\nNumber to find factorial is "<<num1<<endl;
}
num4=scien_calc::fact(num1);
cout<<"\nFactorial of "<<num1<<" is "<<num4;
cout<<"\nPress any key to continue...................";
getch();
temp=num4;
flag=old_cal;
break;
case 5:
if(flag==new_cal)
{
cout<<"\nEnter number to find sin value:";
cin>>num1;
}else{
num1=temp;
cout<<"\nNumber for sin value is "<<num1<<endl;
}
num3=scien_calc::sin_func(num1);
cout<<"\nSin value of "<<num1<<" is "<<num3;
cout<<"\nPress any key to continue..................";
getch();
temp=num3;
flag=old_cal;
break;
case 6:
if(flag==new_cal)
{
cout<<"\nEnter number to find cos value:";
cin>>num1;
}else{
num1=temp;
cout<<"\nNumber for cos value is "<<num1<<endl;
}
num3=scien_calc::cos_func(num1);
cout<<"\nCos value of "<<num1<<" is "<<num3;
cout<<"\nPress any key to continue.......................";
getch();
temp=num3;
flag=old_cal;
break;
case 7:
if(flag==new_cal)
{
cout<<"\nEnter number to find tan value:";
cin>>num1;
}else{
num1=temp;
cout<<"\nNumber for tan value is "<<num1<<endl;
}
num3=scien_calc::tan_func(num1);
cout<<"\nTan value of "<<num1<<" is "<<num3;
cout<<"\nPress any key to continue.........................";
getch();
temp=num3;
flag=old_cal;
break;
case 8:
cout<<"\nReturning to previous menu.";
cout<<"\nPress any key to continue......................";
getch();
break;
case 9:
cout<<"\nQuitting..............................";
cout<<"\nPress any key to continue.......................";
getch();
exit(0);
break;
case 10:
if(flag==new_cal)
{
cout<<"nInvalid choice.";
cout<<"\nPress any key to continue...............";
getch();
}else{
temp=0;
flag=new_cal;
}
break;
default:
cout<<"\nInvalid choice.";
cout<<"\nPress any key to continue.................";
getch();
break;
}
}while(choice2!=8);
break;
case 3:
cout<<"\nQuitting........................";
cout<<"\nPress any key to continue......................";
getch();
break;
default:
cout<<"\nInvalid choice.";
cout<<"\nPress any key to continue....................";
getch();
break;
}
}while(choice1!=3);
}