求C预言计算器代码

yingzijuntuan 2012-03-26 12:34:43
计算器一定要能加减乘除,括号,等运算!要给说明一下思路!在学数据结构的时候没有 学好呀!求高人指点指点!
...全文
259 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
TopsLuo 2012-04-06
  • 打赏
  • 举报
回复
//显示菜单,以输入选项和操作
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);
}
TopsLuo 2012-04-06
  • 打赏
  • 举报
回复
// Calc.cpp : Defines the entry point for the console application.

///////////////////////////////////////////////////////////
//author:luoshibin //
//dat:12/8/2011 //
//E-mail:luoshibin@visiontops.com //
//////////////////////////////////////////////////////////
#include "stdafx.h"
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>

using namespace std;
#pragma warning(disable:4996) //全部关掉

#define new_cal 1
#define old_cal 0

//stand_calc类定义了标准的计算器的函数

class stand_calc
{
public:
static double addition( double ,double );//两数相加函数
static double substract( double ,double );//两数相减函数
static double multiplication( double ,double );//两数相乘函数
static double division( double ,double* );//两数相除函数
static double modulus( double* ,double* );//求余函数
};

//scien_calc类定义了科学型计算器的函数
class scien_calc
{
public:
static double square(double);//求平方函数
static double cube(double);//求立方函数
static double power(double, double);//幂运算函数
static double sq_root(double);//求开方函数
static long int fact(double);//求阶乘函数
static double sin_func(double);//求正弦函数
static double cos_func(double);//求余弦函数
static double tan_func(double);//求正切函数
};

//两数相加
double stand_calc::addition(double a, double b)
{
return (a+b);
}
//两数相减
double stand_calc::substract(double a, double b)
{
return (a-b);
}
//两数相乘
double stand_calc::multiplication(double a, double b)
{
return (a*b);
}

/*除法函数将执行被除数和除数的相除,该函数有两个参数,其中一个是变量副本,另一个是指针,这是因为,如果除数是0,则该函数将显示再次输入除数的信息,使用指针后,再次输入的除数值也会在主函数中被更新*/
double stand_calc::division(double a, double* b)
{
while( *b==0 )
{
cout<<"\nCannot divide by zero";
cout<<"\nEnter second number again:";
cin>>*b;
}
return (a/(*b));
}
/*求余函数将执行被除数和除数的相除,并返回除法的余数,和除尘操作类似,它的除数也不能为0,由于不能对双精度求余,所以得先转换成整数*/
double stand_calc::modulus(double *a, double *b)
{
while( *b==0 )
{
cout<<"\nCannot divide by zero.";
cout<<"\nEnter second nymber again:";
cin>>*b;
}

int x = (int)*a;
int y = (int)*b;
if( *a-x>0 || *b-y>0 )
{
cout<<"\nConverting decimal number into an integer to perform modulus";
}
*a=x;
*b=y;
return(x%y);
}

//scien_calc类的从此开始
//返回数值的平方
double scien_calc::square(double x)
{
return (pow(x,2));
}

double scien_calc::cube(double x)
{
return (pow(x,3));
}

//返回以x为底,y为指数的幂
double scien_calc::power(double x, double y)
{
return (pow(x,y));
}
//返回数值的开方
double scien_calc::sq_root(double x)
{
return (sqrt(x));
}

//返回数值的阶乘,计算前应先把双精度的数转换为整数
long int scien_calc::fact(double x)
{
int n = (int)x;
long int f = 1;
while(n>1)
{
f*=n;
n--;
}
return f;
}

//返回函数的正弦值
double scien_calc::sin_func(double x)
{
return (sin(x));
}

//返回函数的余弦值
double scien_calc::cos_func(double x)
{
return (cos(x));
}

//返回数值的正切值
double scien_calc::tan_func(double x)
{
return (tan(x));
}
yingzijuntuan 2012-04-02
  • 打赏
  • 举报
回复
没有思路呀。。。。。把思路写明白一点呀。。。。
  • 打赏
  • 举报
回复
预言 or 语言
用个栈存放操作数和符号,制定下运算优先级,网上一搜一大堆
pengfoo 2012-03-26
  • 打赏
  • 举报
回复
laoyang103 2012-03-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhao4zhong1 的回复:]

关于自己是否适合编程的很简单的测试:
在报纸或杂志上随便找一段约1000字的文章,在Word中输入一遍。输完后再参考下面答案:

A里面有10处以上文字或标点错误
B里面没有文字或标点错误并敢为此跟人打赌
C里面没有文字或标点错误并且字体和排版完全与原稿一致
D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感

A不适合编程(理由:打字准确度偏低、粗心大意)
……
[/Quote]

哇塞 小弟初来贵地 拜模赵大侠
赵4老师 2012-03-26
  • 打赏
  • 举报
回复
关于自己是否适合编程的很简单的测试:
在报纸或杂志上随便找一段约1000字的文章,在Word中输入一遍。输完后再参考下面答案:

A里面有10处以上文字或标点错误
B里面没有文字或标点错误并敢为此跟人打赌
C里面没有文字或标点错误并且字体和排版完全与原稿一致
D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感

A不适合编程(理由:打字准确度偏低、粗心大意)
B初级程序员(理由:打字准确度很高、认真细致、自信、理解全角半角概念)
C高级程序员(理由:在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证)
D软件项目经理(理由:能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业!)

如果想从A变成B的话,到我的资源http://download.csdn.net/detail/zhao4zhong1/4084259里面下载“适合程序员的键盘练习”
赵4老师 2012-03-26
  • 打赏
  • 举报
回复
/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成
功能:
目前提供了10多个常用数学函数:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷开平方sqrt
⑸反正弦arcsin
⑹反余弦arccos
⑺反正切arctan
⑻常用对数lg
⑼自然对数ln
⑽e指数exp
⑾乘幂函数∧
用法:
如果要求2的32次幂,可以打入2^32<回车>
如果要求30度角的正切可键入tan(Pi/6)<回车>
注意不能打入:tan(30)<Enter>
如果要求1.23弧度的正弦,有几种方法都有效:
sin(1.23)<Enter>
sin 1.23 <Enter>
sin1.23 <Enter>
如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>
此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)
当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。
本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2 实际上相当于:2+(3*(4*4))
另外函数名前面如果是数字,那么自动认为二者相乘.
同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。
如:3sin1.2^2+5cos2.1^2 相当于3*sin2(1.2)+5*cos2(2.1)
又如:4(3-2(sqrt5-1)+ln2)+lg5 相当于4*(3-2*(√5 -1)+loge(2))+log10(5)
此外,本计算器提供了圆周率 Pi键入字母时不区分大小写,以方便使用。
----------------------------------------*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
const char Tab=0x9;
const int DIGIT=1;
const int MAXLEN=16384;
char s[MAXLEN],*endss;
int pcs=15;
double fun(double x,char op[],int *iop) {
while (op[*iop-1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>
switch (op[*iop-1]) {
case 7: x=sin(x); (*iop)--;break;
case 8: x=cos(x); (*iop)--;break;
case 9: x=tan(x); (*iop)--;break;
case 10: x=sqrt(x); (*iop)--;break;
case 11: x=asin(x); (*iop)--;break;
case 12: x=acos(x); (*iop)--;break;
case 13: x=atan(x); (*iop)--;break;
case 14: x=log10(x);(*iop)--;break;
case 15: x=log(x); (*iop)--;break;
case 16: x=exp(x); (*iop)--;break;
}
return x;
}
double calc(char *expr,char **addr) {
static deep; //递归深度
static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};
double ST[10]={0.0}; //数字栈
char op[10]={'+'}; //运算符栈
char c,*rexp,*pp,*pf;
int ist=1,iop=1,last;
if (!deep) {
pp=pf=expr;
do {
c = *pp++;
if (c!=' '&& c!=Tab)
*pf++ = c;
} while (c!='\0');
}
pp=expr;
if ((c=*pp)=='-'||c=='+') {
op[0] = c;
pp++;
}
last = !DIGIT;
while ((c=*pp)!='\0') {
if (c=='(') {//左圆括弧
deep++;
ST[ist++]=calc(++pp,addr);
deep--;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp = *addr;
last = DIGIT;
if (*pp == '('||isalpha(*pp) && strnicmp(pp,"Pi",2)) {//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
else if (c==')') {//右圆括弧
pp++;
break;
} else if (isalpha(c)) {
if (!strnicmp(pp,"Pi",2)) {
if (last==DIGIT) {
cout<< "π左侧遇)" <<endl;exit(1);
}
ST[ist++]=3.14159265358979323846264338328;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp += 2;
last = DIGIT;
if (!strnicmp(pp,"Pi",2)) {
cout<< "两个π相连" <<endl;exit(2);
}
if (*pp=='(') {
cout<< "π右侧遇(" <<endl;exit(3);
}
} else {
for(int i=0; (pf=fname[i])!=NULL; i++)
if (!strnicmp(pp,pf,strlen(pf)))break;
if (pf!=NULL) {
op[iop++] = 07+i;
pp += strlen(pf);
} else {
cout<< "陌生函数名" <<endl;exit(4);
}
}
} else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='^') {
char cc;
if (last != DIGIT) {
cout<< "运算符粘连" <<endl;exit(5);
}
pp++;
if (c=='+'||c=='-') {
do {
cc = op[--iop];
--ist;
switch (cc) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
} while (iop);
op[iop++] = c;
} else if (c=='*'||c=='/') {
operate: cc = op[iop-1];
if (cc=='+'||cc=='-') {
op[iop++] = c;
} else {
--ist;
op[iop-1] = c;
switch (cc) {
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
} else {
cc = op[iop-1];
if (cc=='^') {
cout<< "乘幂符连用" <<endl;exit(6);
}
op[iop++] = c;
}
last = !DIGIT;
} else {
if (last == DIGIT) {
cout<< "两数字粘连" <<endl;exit(7);
}
ST[ist++]=strtod(pp,&rexp);
ST[ist-1]=fun(ST[ist-1],op,&iop);
if (pp == rexp) {
cout<< "非法字符" <<endl;exit(8);
}
pp = rexp;
last = DIGIT;
if (*pp == '('||isalpha(*pp)) {
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
}
*addr=pp;
if (iop>=ist) {
cout<< "表达式有误" <<endl;exit(9);
}
while (iop) {
--ist;
switch (op[--iop]) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
return ST[0];
}
int main(int argc,char **argv) {
if (argc<=1) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页
cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;
while (1) {
cout << "请输入表达式:";
gets(s);
if (s[0]==0) break;//
cout << s <<"=";
cout << setprecision(15) << calc(s,&endss) << endl;
}
} else {
strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;
if (argc>=3) {
pcs=atoi(argv[2]);
if (pcs<0||15<pcs) pcs=15;
printf("%.*lf\n",pcs,calc(s,&endss));
} else {
printf("%.15lg\n",calc(s,&endss));
}
}
return 0;
}

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧