📄 subject_53482.htm
字号:
<p>
序号:53482 发表者:yirushenhai 发表日期:2003-09-19 21:47:17
<br>主题:一个计算器程序的指正,谢谢帮助:)
<br>内容://*第1题 计算器--源代码及关键源代码注解如下:*/<BR>// This is a simple OOP Calculator that i made because i wanted to learn how to use classes<BR>// so i made an OOP calc, anyways i was gonna use inline asm for the add, sub, div, mult functions<BR>#include <process.h><BR>#include <iostream.h><BR>#include <conio.h><BR>#include <stdlib.h><BR>#include <math.h><BR>float number1,number2,answer,val;<BR>class oopcalc<BR> {<BR>public:<BR>void supercalc();<BR>void calcadd();<BR>void calcsub();<BR>void calcdiv();<BR>void calcmult();<BR>void calcfartocel();<BR>void calcceltofar();<BR>void calcsroot();<BR>void exitprog();<BR>void menu();<BR>void badinput();<BR>private:<BR>static int k,t,s;<BR>float add(float x, float y);<BR>float sub(float x, float y);<BR>float div(float x, float y);<BR>float mult(float x, float y);<BR>float fartocel(float x);<BR>float celtofar(float x);<BR>float sqroot(float x);<BR>};<BR>int oopcalc::k=0;<BR>int oopcalc::t=0;<BR>int oopcalc::s=0;<BR>void oopcalc::calcadd()<BR>{ <BR> cout << "The Add Function\n";<BR> cout << "First number: ";<BR> cin >> number1;<BR> cout << "Second number: ";<BR> cin >> number2;<BR> answer = add(number1,number2);<BR> while(number2)<BR> {<BR> cout<<"next number or 0 end"<<'\n';<BR> cin>>number2;<BR> if(number2==0)break;<BR> answer=add(number2,answer);<BR> }<BR><BR>cout <<"the sum" << " = " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::calcsub()<BR>{<BR>cout << "The Subtract Function\n";<BR>cout << "First number: ";<BR>cin >> number1;<BR>cout << "Second number: ";<BR>cin >> number2;<BR>answer = sub(number1,number2);<BR>while(number2)<BR>{<BR> cout<<"next number or 0 end"<<'\n';<BR> cin>>number2;<BR> if(number2==0)break;<BR> answer=sub(answer,number2);<BR>}<BR><BR>cout << "the end"<< " = " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::calcdiv()<BR>{<BR>cout << "The Divide Function\n";<BR>cout << "First number: ";<BR>cin >> number1;<BR>cout << "Second number: ";<BR>cin >> number2;<BR>answer = div(number1,number2);<BR><BR>while(number2)<BR>{<BR> cout<<"next number or 0 end"<<'\n';<BR> cin>>number2;<BR> if(number2==0)break;<BR> answer=div(answer,number2);<BR>}<BR>cout << "the end"<< " = " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::calcmult()<BR>{<BR>cout << "The Multiply Function\n";<BR>cout << "First number: ";<BR>cin >> number1;<BR>cout << "Second number: ";<BR>cin >> number2;<BR>answer = mult(number1,number2);<BR>while(number2)<BR>{<BR> cout<<"next number or 0 end"<<'\n';<BR> cin>>number2;<BR> if(number2==0)break;<BR> answer=mult(answer,number2);<BR>}<BR>cout << "the end" << " = " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::calcfartocel()<BR>{<BR>cout << "The Farenheit to Celsius Function\n";<BR>cout << "Enter a tempature in Farenheit: ";<BR>cin >> number1;<BR>answer = fartocel(number1);<BR>cout << "The tempature in Celsius is " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::calcceltofar()<BR>{<BR>cout << "The Celsius to Farenheit Function\n";<BR>cout << "Enter a tempature in Celsius: ";<BR>cin >> number1;<BR>answer = celtofar(number1);<BR>cout << "The tempature in Farenheit is " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::calcsroot()<BR>{<BR>cout << "The Square Root Function\n";<BR>cout << "First number: ";<BR>cin >> number1;<BR>answer = sqroot(number1);<BR>cout << "The square root of " << number1 << " is " << answer << endl;<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>void oopcalc::exitprog()<BR>{<BR>exit(-1);<BR>}<BR>void oopcalc::supercalc()<BR>{ <BR> float even[100];<BR> char odd[100];<BR> int p[100];<BR> char q[100];<BR> answer=0;<BR> cout<<"欢迎使用麻烦四则运算法"<<'\n';<BR> cout<<"输入要运行的数,和运算符号"<<'\n';<BR> for( int i=0;i<100;i++)<BR> { <BR> cin>>even[i];<BR> cin>>odd[i];<BR> s++;<BR> if(odd[i]=='=')break;<BR> }; <BR> for(i=0;i<s;i++)<BR> {<BR> if(odd[i]=='/'||odd[i]=='*')<BR> { <BR> p[k]=i;<BR> k++;<BR> switch(odd[i])<BR> {<BR> case '/' : even[p[k]]=div(even[p[k]],even[i+1]);break;<BR> case '*' : even[p[k]]=mult(even[p[k]],even[i+1]);break;<BR> }<BR> while(1)<BR> {<BR> if (odd[++i]=='/'||odd[++i]=='*')<BR> switch(odd[i])<BR> {<BR> case '/' : even[p[k]]=div(even[p[k]],even[i+1]);break;<BR> case '*' : even[p[k]]=mult(even[p[k]],even[i+1]);break;<BR> default: ;<BR> }<BR> else <BR> {<BR> q[t]=odd[i];<BR> t++;<BR> break;<BR> }<BR> }<BR> <BR> }<BR> else {q[t]=odd[i];t++;};<BR> };<BR> answer=even[1];<BR> for(i=0;q[i]!='=';i++)<BR> { <BR> switch(odd[i])<BR> {<BR> case '+' : answer=add(answer,even[p[i+1]]);break;<BR> case '-' : answer=sub(answer,even[p[i+1]]);break; <BR><BR> }<BR> }<BR> cout<<"answer="<<answer;<BR>}<BR><BR> <BR>void oopcalc::menu()<BR>{<BR>char input;<BR>oopcalc a;<BR>system("cls"); //执行系统命令:cls-清屏<BR>cout << "==================MENU===============\n";<BR>cout << "1: Add two numbers\n";<BR>cout << "2: Subtract two numbers\n";<BR>cout << "3: Divide two numbers\n";<BR>cout << "4: Multiply two numbers\n";<BR>cout << "5: Convert Farenheit to Celsius\n";<BR>cout << "6: Convert Celsius to Farenheit\n";<BR>cout << "7: Find the square root of a number\n";<BR>cout << "8: Exit this program\n";<BR>cout << "9: 四则麻烦运算";<BR>cout << "Choice: ";<BR>cin >> input;<BR>cout << "=====================================\n";<BR>switch (input)<BR>{<BR>case '1': a.calcadd();<BR>break;<BR>case '2': a.calcsub();<BR>break;<BR>case '3': a.calcdiv();<BR>break;<BR>case '4': a.calcmult();<BR>break;<BR>case '5': a.calcfartocel();<BR>break;<BR>case '6': a.calcceltofar();<BR>break;<BR>case '7': a.calcsroot();<BR>break;<BR>case '8': a.exitprog();<BR>break;<BR>case '9': a.supercalc();<BR> break;<BR>default: a.badinput();<BR>}<BR>}<BR>void oopcalc::badinput()<BR>{<BR>cout << "BAD INPUT!\n";<BR>cout << "Press any key to continue\n";<BR>getch();<BR>menu();<BR>}<BR>float oopcalc::add(float x, float y)<BR>{ <BR>val = x + y;<BR>return val;<BR>}<BR>float oopcalc::sub(float x,float y)<BR>{<BR>val = x - y;<BR>return val;<BR>}<BR>float oopcalc::div(float x, float y)<BR>{<BR>val = x / y;<BR>return val;<BR>}<BR>float oopcalc::mult(float x, float y)<BR>{<BR>val = x * y;<BR>return val;<BR>}<BR>float oopcalc::fartocel(float x)<BR>{<BR>float cel = ((x - 32) * 5) / 9;<BR>return cel;<BR>}<BR>float oopcalc::celtofar(float x)<BR>{<BR>float f;<BR>f = x * 9 / 5 + 32;<BR>return f;<BR>}<BR>float oopcalc::sqroot(float x)<BR>{<BR>float g = sqrt(x);<BR>return g;<BR>}<BR>void main()<BR>{<BR>oopcalc s;<BR>s.menu();<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:yirushenhai 回复日期:2003-09-19 21:50:46
<br>内容:其中的四则运算功能,我从新编了一下,但是执行时中是出现问题,<BR>希望可以帮助解答。其中的四则运算功能函数为:void oopcalc::supercalc()<BR>{ <BR> float even[100];<BR> char odd[100];<BR> int p[100];<BR> char q[100];<BR> answer=0;<BR> cout<<"欢迎使用麻烦四则运算法"<<'\n';<BR> cout<<"输入要运行的数,和运算符号"<<'\n';<BR> for( int i=0;i<100;i++)<BR> { <BR> cin>>even[i];<BR> cin>>odd[i];<BR> s++;<BR> if(odd[i]=='=')break;<BR> }; <BR> for(i=0;i<s;i++)<BR> {<BR> if(odd[i]=='/'||odd[i]=='*')<BR> { <BR> p[k]=i;<BR> k++;<BR> switch(odd[i])<BR> {<BR> case '/' : even[p[k]]=div(even[p[k]],even[i+1]);break;<BR> case '*' : even[p[k]]=mult(even[p[k]],even[i+1]);break;<BR> }<BR> while(1)<BR> {<BR> if (odd[++i]=='/'||odd[++i]=='*')<BR> switch(odd[i])<BR> {<BR> case '/' : even[p[k]]=div(even[p[k]],even[i+1]);break;<BR> case '*' : even[p[k]]=mult(even[p[k]],even[i+1]);break;<BR> default: ;<BR> }<BR> else <BR> {<BR> q[t]=odd[i];<BR> t++;<BR> break;<BR> }<BR> }<BR> <BR> }<BR> else {q[t]=odd[i];t++;};<BR> };<BR> answer=even[1];<BR> for(i=0;q[i]!='=';i++)<BR> { <BR> switch(odd[i])<BR> {<BR> case '+' : answer=add(answer,even[p[i+1]]);break;<BR> case '-' : answer=sub(answer,even[p[i+1]]);break; <BR><BR> }<BR> }<BR> cout<<"answer="<<answer;<BR>}<BR><BR><BR>具体思路是先算乘除,再算加减,不知为何无法运行,如果您编出成功的程序,就是这个四则运算,请发给我<BR>xz1984zx@163.com谢谢!:)<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:yirushenhai 回复日期:2003-09-21 09:41:27
<br>内容:一定要帮忙啊:)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -