| 发表于:2007-09-06 19:41:26 楼主 |
#include <process.h> #include <iostream.h> #include <conio.h> #include <stdlib.h> #include <math.h> int number1,number2,answer,val; class oopcalc{ public: void calcadd(); void calcsub(); void calcdiv(); void calcmult(); void calcfartocel(); void calcceltofar(); void calcsroot(); void exitprog(); void menu(); void badinput(); private: int add(int x,int y); int sub(int x,int y); int div(int x,int y); int mult(int x,int y); int fartocel(int x); int celtofar(int x); int sqroot(int x); }; void oopcalc::calcadd() { cout < < "the add function\n "; cout < < "first number: "; cin> > number1; cout < < "second number: "; cin> > number2; answer=add(number1,number2); cout < <number1 < < "+ " < <number2 < < "= " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::calcsub() { cout < < "the subtract function\n "; cout < < "first number: "; cin> > number1; cout < < "second number: "; cin> > number2; answer=sub(number1,number2); cout < <number1 < < "- " < <number2 < < "= " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::calcdiv() { cout < < "the divide function\n "; cout < < "first number: "; cin> > number1; cout < < "second number: "; cin> > number2; answer=div(number1,number2); cout < <number1 < < "/ " < <number2 < < "= " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::calcmult() { cout < < "the multiply function\n "; cout < < "first number: "; cin> > number1; cout < < "second number: "; cin> > number2; answer=mult(number1,number2); cout < <number1 < < "x " < <number2 < < "= " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::calcfartocel() { cout < < "the farenheit to celsius function\n "; cout < < "enter a tempature in farenheit: "; cin> > number1; answer=fartocel(number1); cout < < "the tempature in celsius is " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::calcceltofar() { cout < < "the celsius to farenheit function\n "; cout < < "enter a tempature in celsius: "; cin> > number1; answer=celtofar(number1); cout < < "the tempature in farenheit is " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::calcsroot() { cout < < "the square root function\n "; cout < < "first number: "; cin> > number1; answer=sqroot(number1); cout < < "the square root of " < <number1 < < "is " < <answer < <endl; cout < < "press any key to continue\n "; getch(); menu(); } void oopcalc::exitprog() { exit(-1); } void oopcalc::menu() { char input; oopcalc a; system( "cls "); cout < < "==================menu===============\n "; cout < < "1:add two numbers\n "; cout < < "2:subtract two numbers\n "; cout < < "3:divide two numbers\n "; cout < < "4:multiply two numbers\n "; cout < < "5:convert farenheit to celsius\n "; cout < < "6:convert celsius to farenheit\n "; cout < < "7:find the square root of a number\n "; cout < < "8:exit this program\n "; cout < < "choice: "; cin> > input; cout < < "==========================================\n "; switch(input) { case '1 ': a.calcadd(); break; case '2 ': a.calcsub(); break; case '3 ': a.calcdiv(); break; case '4 ': a.calcmult(); break; case '5 ': a.calcfartocel(); break; case '6 ': a.calcceltofar(); break; case '7 ': a.calcsroot(); break; case '8 ': a.exitprog(); break; default: a.badinput(); } } void oopcalc::badinput() { cout < < "bad input!\n "; cout < < "press any key to continue\n "; getch(); menu(); } int oopcalc::add(int x,int y) { val=x+y; return val; } int oopcalc::sub(int x,int y) { val=x-y; return val; } int oopcalc::div(int x,int y) { val=x/y; return val; } int oopcalc::mult(int x,int y) { val=x*y; return val; } int oopcalc::fartocel(int x) { int cel =((x-32)*5)/9; return cel; } int oopcalc::celtofar(int x) { int f; f=x*9/5+32; return f; } int oopcalc::sqroot(int x) { int g=sqrt(x); return g ; } void main() { oopcalc s; s.menu(); } 这是模仿写的一个计算器吧 现在要对实数操作是不是就是把int 改为 double啊? 里面的计算都是只有两个数,如果要对多操作数进行计算,怎么改啊? 如果要在主菜单里加一个实现混合四则运算并有检测功能,增加函数就可以了,怎么添加阿?能不能给个代码看看啊? |
|
|
|
|