c语言计算器源代码

合集下载

c语言计算器代码

c语言计算器代码

}
}
}
void jianfa()
{
int a,b,c,l;
printf("您现在即将使用减法器^-^\n");
printf("请输入两个数吧^-^\n");
scanf("%d%d",&a,&b);
c=a-b;
printf("您输入的两个数是%d %d\n",a,b);
p=m+x;
q=n+y;
printf("%d+%di+%d+%di=%d+%di\n",m,n,x,y,p,q);
break;
c=p;
t=q;
case 4:l=0;break;
printf("%d/%d=%d\n",a,b,c);
}
void caozuo()
{ int a,p;
p=1;
printf("您使用的是两位数计算器\n");
printf("1表示加法\n2表示减法\n3表示乘法\n4表示除法\n5表示退出\n");
while(p)
{
scanf("%d",&r);
switch(r)
{
case 1:printf("请继续输入一个数吧\n");
scanf("%d",&m);
n=c-m;
printf("%d-%d=%d\n",c,m,n);
}
}
}

简单计算器c语言源码

简单计算器c语言源码

#include <stdio.h>#include <string.h>#include <ctype.h>#include <math.h>//expression evaluate#define iMUL 0#define iDIV 1#define iADD 2#define iSUB 3#define iCap 4//#define LtKH 5//#define RtKH 6#define MaxSize 100void iPush(float);float iPop();float StaOperand[MaxSize];int iTop=-1;//char Srcexp[MaxSize];char Capaexp[MaxSize];char RevPolishexp[MaxSize];float NumCapaTab[26];char validexp[]="*/+-()";char NumSets[]="0123456789";char StackSymb[MaxSize];int operands;//void NumsToCapas(char [], int , char [], float []);int CheckExpress(char);int PriorChar(char,char);int GetOperator(char [], char);void counterPolishexp(char INexp[], int slen, char Outexp[]); float CalcRevPolishexp(char [], float [], char [], int);void main(){char ch;char s;int bl=1;while(bl==1){int ilen;float iResult=0.0;printf("输入计算表达式(最后以=号结束):\n");memset(StackSymb,0,MaxSize);memset(NumCapaTab,0,26); //A--NO.1, B--NO.2, etc.gets(Srcexp);ilen=strlen(Srcexp);NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);ilen=strlen(Capaexp);counterPolishexp(Capaexp,ilen,RevPolishexp);ilen=strlen(RevPolishexp);iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);printf("\n计算结果:\n%.6f\n",iResult);printf("是否继续运算?(Y/N)\n");ch=getchar();s=getchar();//接受回车if(ch=='y'||ch=='Y'){bl=1;}else{bl=0;}}}void iPush(float value){if(iTop<MaxSize) StaOperand[++iTop]=value;}float iPop(){if(iTop>-1)return StaOperand[iTop--];return -1.0;}void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[]) {char ch;int i, j, k, flg=0;int sign;float val=0.0,power=10.0;i=0; j=0; k=0;while (i<slen){ch=Srcexp[i];if (i==0){sign=(ch=='-')?-1:1;if(ch=='+'||ch=='-'){ch=Srcexp[++i];flg=1;}}if (isdigit(ch)){val=ch-'0';while (isdigit(ch=Srcexp[++i])) {val=val*10.0+ch-'0';}if (ch=='.'){while(isdigit(ch=Srcexp[++i])) {val=val+(ch-'0')/power; power*=10;}} //end ifif(flg){val*=sign;flg=0;}} //end if//write Capaexp array// write NO.j to arrayif(val){Capaexp[k++]='A'+j; Capaexp[k++]=ch;NumCapaTab[j++]=val; //A--0, B--1,and C, etc.}else{Capaexp[k++]=ch;}val=0.0;power=10.0;//i++;}Capaexp[k]='\0';operands=j;}float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen) {float sval=0.0, op1,op2;int i, rt;char ch;//recursive stacki=0;while((ch=RevPolishexp[i]) && i<slen){switch(rt=GetOperator(validexp, ch)){case iMUL: op2=iPop(); op1=iPop();sval=op1*op2;iPush(sval);break;case iDIV: op2=iPop(); op1=iPop();if(!fabs(op2)){printf("overflow\n");iPush(0);break;}sval=op1/op2;iPush(sval);break;case iADD: op2=iPop(); op1=iPop();sval=op1+op2;iPush(sval);break;case iSUB: op2=iPop(); op1=iPop();sval=op1-op2;iPush(sval);break;case iCap: iPush(NumCapaTab[ch-'A']); break;default: ;}++i;}while(iTop>-1){sval=iPop();}return sval;}int GetOperator(char validexp[],char oper) {int oplen,i=0;oplen=strlen(validexp);if (!oplen) return -1;if(isalpha(oper)) return 4;while(i<oplen && validexp[i]!=oper) ++i;if(i==oplen || i>=4) return -1;return i;}int CheckExpress(char ch){int i=0;char cc;while((cc=validexp[i]) && ch!=cc) ++i;if (!cc)return 0;return 1;}int PriorChar(char curch, char stach){//栈外优先级高于(>)栈顶优先级时,才入栈//否则(<=),一律出栈if (curch==stach) return 0; //等于时应该出栈else if (curch=='*' || curch=='/'){if(stach!='*' && stach!='/')return 1;}else if (curch=='+' || curch=='-'){if (stach=='(' || stach==')')return 1;}else if (curch=='('){if (stach==')')return 1;}return 0;}void counterPolishexp(char INexp[], int slen, char Outexp[]) {int i, j, k,pr;char t;i=0;j=k=0;while (INexp[i]!='=' && i<slen){if (INexp[i]=='(')StackSymb[k++]=INexp[i];//iPush(*(INexp+i));else if(INexp[i]==')'){//if((t=iPop())!=-1)while((t=StackSymb[k-1])!='('){Outexp[j++]=t;k--;}k--;}else if (CheckExpress(INexp[i])) // is oparator{// printf("operator %c k=%d\n",INexp[i],k);while (k){// iPush(*(INexp+i));if(pr=PriorChar(INexp[i],StackSymb[k-1])) break;else{//if ((t=iPop())!=-1)t=StackSymb[k-1]; k--;Outexp[j++]=t;}} //end whileStackSymb[k++]=INexp[i]; //common process }else //if() 变量名{// printf("operand %c k=%d\n",INexp[i],k); Outexp[j++]=INexp[i];}i++; //}while (k){t=StackSymb[k-1]; k--;Outexp[j++]=t;}Outexp[j]='\0';}。

C语言计算器源代码

C语言计算器源代码

C语言计算器源代码下面是一个简单的C语言计算器源代码:```c#include<stdio.h>int mainfloat num1, num2, result;char operator;printf("请输入两个数字:");scanf("%f %f", &num1, &num2);printf("请选择运算符(+,-,*,/):"); scanf(" %c", &operator);switch(operator)case '+':result = num1 + num2;printf("结果: %.2f\n", result);break;case '-':result = num1 - num2;printf("结果: %.2f\n", result);break;case '*':result = num1 * num2;printf("结果: %.2f\n", result); break;case '/':if(num2 != 0)result = num1 / num2;printf("结果: %.2f\n", result); }elseprintf("除数不能为0!\n");}break;default:printf("无效的运算符!\n"); break;}return 0;```这个计算器程序可以接受两个数字和一个运算符作为输入,并根据运算符的类型进行相应的计算。

程序使用了`switch`语句来根据不同的运算符执行不同的操作。

输入数字之后,输出计算结果或者错误提示信息。

c语言计算的代码

c语言计算的代码

c语言计算的代码#include <stdio.h>// 定义一个函数,用于计算两个数的和int sum(int a, int b) {return a + b;}// 定义一个函数,用于计算两个数的差int subtract(int a, int b) {return a - b;}// 定义一个函数,用于计算两个数的积int multiply(int a, int b) {return a * b;}// 定义一个函数,用于计算两个数的商float divide(int a, int b) {if (b == 0) {printf("除数不能为0。

\n");return 0;} else {return (float)a / b;}}int main() {int num1, num2;printf("请输入两个整数:\n");scanf("%d %d", &num1, &num2);printf("两个数的和:%d\n", sum(num1, num2));printf("两个数的差:%d\n", subtract(num1, num2));printf("两个数的积:%d\n", multiply(num1, num2));printf("两个数的商:%f\n", divide(num1, num2));return 0;}这是一个C语言程序,用于计算两个数的和、差、积和商。

程序先通过输入函数`scanf`获取用户输入的两个整数,并分别保存到`num1`和`num2`变量中。

然后调用自定义的计算函数`sum`、`subtract`、`multiply`和`divide`分别进行加法、减法、乘法和除法运算,并通过`printf`语句将计算结果输出给用户。

计算器(c 语言)实现源代码

计算器(c  语言)实现源代码

计算器(c++语言)实现源代码.txt如果真诚是一种伤害,请选择谎言;如果谎言是一种伤害,请选择沉默;如果沉默是一种伤害,请选择离开。

#include <iostream>using namespace std;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10//-----------------------------------------------------------------------------------------------class Stack1{//定义字符栈public:char * base;char * top;int stacksize;Stack1();int GetTop(char &e);//取栈顶元素int Push(char e);//压栈int Pop(char &e);//跳栈};Stack1::Stack1(){base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));top=base;stacksize=STACK_INIT_SIZE;}int Stack1::GetTop(char &e){if (top==base) return -1;else {e=*(top-1);return 0;}}int Stack1::Push(char e){char *new_base=0;if(top-base>=stacksize){new_base=(char *)realloc(base,(stacksize+STACKINCREMENT)*sizeof(char));if(!new_base) return -2;else{base=new_base;top=base+stacksize;stacksize+=STACKINCREMENT;}}*top++=e;return 0;}int Stack1::Pop(char &e){if(top==base) return -1;e=*--top;return 0;}//----------------------------------------------------------------------------------------------class Stack2{//定义数字栈public:double * base;double * top;int stacksize;Stack2();int GetTop(double &e);//取栈顶元素int Push(double e);//压栈int Pop(double &e);//跳栈};Stack2::Stack2(){base=(double *)malloc(STACK_INIT_SIZE*sizeof(double));top=base;stacksize=STACK_INIT_SIZE;}int Stack2::GetTop(double &e){if (top==base) return -1;e=*(top-1);return 0;}int Stack2::Push(double e){double *new_base=0;if(top-base>=stacksize){new_base=(double*)realloc(base,(stacksize+STACKINCREMENT)*sizeof(double));if(!new_base) return -2;else{new_base=base;top=base+stacksize;stacksize+=STACKINCREMENT;}}*top++=e;return 0;}int Stack2::Pop(double &e){if(top==base) return -1;e=*--top;return 0;}//------------------------------------------------------------------------------------------------class Calculator{private:bool IsOperand(char ch);//判断ch是否是操作数bool IsOperator(char ch);//判断ch是否是操作符char Precede(char op1,char op2);//判断两个操作符优先级的高低double Operate(double left,char op,double right);//执行运算left op right int Translate(char op);//将操作符映射到行、列游标public:void Run();//执行表达式求值运算};bool Calculator::IsOperand(char ch){if(ch>47&&ch<58) return true;else return false;}bool Calculator::IsOperator(char ch){if(ch>39&&ch<44||ch==45||ch==47||ch==61) return true;else return false;}char Calculator::Precede(char op1,char op2){int op10,op20;op10=op20=0;op10=Translate(op1);op20=Translate(op2);charOP[7][7]={'>','>','<','<','<','>','>','>','>','<','<','<','>','>','>','>','>','> ','<','>','>','>','>','>','>','<','>','>','<','<','<','<','<','=','e','>','>','> ','>','e','>','>','<','<','<','<','<','e','='};return OP[op10][op20];//将操作符优先级表映射到二维数组}double Calculator::Operate(double left,char op,double right){ switch(op){case '+':return (left+right);break;case '-':return (left-right);break;case '*':return (left*right);break;case '/':return (left/right);break;}}int Calculator::Translate(char op){switch(op){case '+':return 0;break;case '-':return 1;break;case '*':return 2;break;case '/':return 3;break;case '(':return 4;break;case ')':return 5;break;case '=':return 6;break;}}void Calculator::Run(){Stack1 OPTR;Stack2 OPND;char c;//用于临时存放输入的操作数或操作符char c0;//用于存放操作符栈顶的操作符char x;//用于存放弹出相同操作符后返回的操作符double a=0,b=0;double r=0;//用于存放运算结果int flag=0;//标记异常OPTR.Push('=');OPTR.GetTop(c0);cout<<"输入格式例如“3+4/(3-1)=”"<<endl;c=getchar();while(c!='='||c0!='='){if(IsOperand(c)) {OPND.Push(c-48);c=getchar();}else if(IsOperator(c)){switch(Precede(c0,c)){case '<':OPTR.Push(c);OPTR.GetTop(c0);c=getchar();break;case '=':OPTR.Pop(x);OPTR.GetTop(c0);c=getchar();break;case '>':OPTR.Pop(c0);OPND.Pop(b);OPND.Pop(a);OPND.Push(Operate(a,c0,b));OPTR.GetTop(c0);break;default:cout<<"输入有误!"<<endl;flag=-1;break;}}else {cout<<"未输入等于号“=”!"<<endl;flag=-1;break;}if (flag==-1) {break;}}if (flag!=-1){OPND.GetTop(r);cout<<"结果为:"<<r<<endl;}}void main (){Calculator C;C.Run();}。

计算器编程c语言

计算器编程c语言

计算器编程 c语言用C语言设计计算器程序源代码#include <dos.h> /*DOS接口函数*/#include <math.h> /*数学函数的定义*/#include <conio.h> /*屏幕操作函数*/函数*/#include <stdio.h> /*I/O#include <stdlib.h> /*库函数*/变量长度参数表*/#include <stdarg.h> /*图形函数*/#include <graphics.h> /*字符串函数*/#include <string.h> /*字符操作函数*/#include <ctype.h> /*#define UP 0x48 /*光标上移键*/#define DOWN 0x50 /*光标下移键*/#define LEFT 0x4b /*光标左移键*/#define RIGHT 0x4d /*光标右移键*/#define ENTER 0x0d /*回车键*/void *rar; /*全局变量,保存光标图象*/使用调色板信息*/struct palettetype palette; /*int GraphDriver; /* 图形设备驱动*/int GraphMode; /* 图形模式值*/int ErrorCode; /* 错误代码*/int MaxColors; /* 可用颜色的最大数值*/int MaxX, MaxY; /* 屏幕的最大分辨率*/double AspectRatio; /* 屏幕的像素比*/void drawboder(void); /*画边框函数*/初始化函数*/void initialize(void); /*计算器计算函数*/void computer(void); /*改变文本样式函数*/ void changetextstyle(int font, int direction, int charsize); /*窗口函数*/void mwindow(char *header); /*/*获取特殊键函数*/int specialkey(void) ;设置箭头光标函数*//*int arrow();/*主函数*/int main(){设置系统进入图形模式 */initialize();/*运行计算器 */computer(); /*系统关闭图形模式返回文本模式*/closegraph();/*/*结束程序*/return(0);}/* 设置系统进入图形模式 */void initialize(void){int xasp, yasp; /* 用于读x和y方向纵横比*/GraphDriver = DETECT; /* 自动检测显示器*/initgraph( &GraphDriver, &GraphMode, "" );/*初始化图形系统*/ErrorCode = graphresult(); /*读初始化结果*/如果初始化时出现错误*/if( ErrorCode != grOk ) /*{printf("Graphics System Error: %s\n",显示错误代码*/grapherrormsg( ErrorCode ) ); /*退出*/exit( 1 ); /*}getpalette( &palette ); /* 读面板信息*/MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/MaxX = getmaxx(); /* 读屏幕尺寸 */MaxY = getmaxy(); /* 读屏幕尺寸 */getaspectratio( &xasp, &yasp ); /* 拷贝纵横比到变量中*/计算纵横比值*/ AspectRatio = (double)xasp/(double)yasp;/*}/*计算器函数*/void computer(void){定义视口类型变量*/struct viewporttype vp; /*int color, height, width;int x, y,x0,y0, i, j,v,m,n,act,flag=1;操作数和计算结果变量*/float num1=0,num2=0,result; /*char cnum[5],str2[20]={""},c,temp[20]={""};定义字符串在按钮图形上显示的符号 char str1[]="1230.456+-789*/Qc=^%";/**/mwindow( "Calculator" ); /*显示主窗口 */设置灰颜色值*//*color = 7;getviewsettings( &vp ); /* 读取当前窗口的大小*/width=(vp.right+1)/10; /* 设置按钮宽度 */设置按钮高度 */height=(vp.bottom-10)/10 ; /*/*设置x的坐标值*/x = width /2;设置y的坐标值*/y = height/2; /*setfillstyle(SOLID_FILL, color+3);bar( x+width*2, y, x+7*width, y+height );/*画一个二维矩形条显示运算数和结果*/setcolor( color+3 ); /*设置淡绿颜色边框线*/rectangle( x+width*2, y, x+7*width, y+height );/*画一个矩形边框线*/设置颜色为红色*/setcolor(RED); /*输出字符串"0."*/outtextxy(x+3*width,y+height/2,"0."); /*/*设置x的坐标值*/x =2*width-width/2;设置y的坐标值*/y =2*height+height/2; /*画按钮*/for( j=0 ; j<4 ; ++j ) /*{for( i=0 ; i<5 ; ++i ){setfillstyle(SOLID_FILL, color);setcolor(RED);bar( x, y, x+width, y+height ); /*画一个矩形条*/rectangle( x, y, x+width, y+height );sprintf(str2,"%c",str1[j*5+i]);/*将字符保存到str2中*/outtextxy( x+(width/2), y+height/2, str2);移动列坐标*/x =x+width+ (width / 2) ;/*}y +=(height/2)*3; /* 移动行坐标*/x =2*width-width/2; /*复位列坐标*/}x0=2*width;y0=3*height;x=x0;y=y0;gotoxy(x,y); /*移动光标到x,y位置*/显示光标*/arrow(); /*putimage(x,y,rar,XOR_PUT);m=0;n=0;设置str2为空串*/strcpy(str2,""); /*当压下Alt+x键结束程序,否则执行下面的循环while((v=specialkey())!=45) /**/{当压下键不是回车时*/while((v=specialkey())!=ENTER) /*{putimage(x,y,rar,XOR_PUT); /*显示光标图象*/if(v==RIGHT) /*右移箭头时新位置计算*/if(x>=x0+6*width)如果右移,移到尾,则移动到最左边字符位置*//*{x=x0;m=0;}else{x=x+width+width/2;m++;否则,右移到下一个字符位置*/} /*if(v==LEFT) /*左移箭头时新位置计算*/if(x<=x0){x=x0+6*width;m=4;} /*如果移到头,再左移,则移动到最右边字符位置*/else{x=x-width-width/2;m--;} /*否则,左移到前一个字符位置*/if(v==UP) /*上移箭头时新位置计算*/if(y<=y0){y=y0+4*height+height/2;n=3;} /*如果移到头,再上移,则移动到最下边字符位置*/else{y=y-height-height/2;n--;} /*否则,移到上边一个字符位置*/if(v==DOWN) /*下移箭头时新位置计算*/if(y>=7*height){ y=y0;n=0;} /*如果移到尾,再下移,则移动到最上边字符位置*/else{y=y+height+height/2;n++;} /*否则,移到下边一个字符位置*/putimage(x,y,rar,XOR_PUT); /*在新的位置显示光标箭头*/ }将字符保存到变量c中*/c=str1[n*5+m]; /*判断是否是数字或小数点*/if(isdigit(c)||c=='.') /*{如果标志为-1,表明为负数*/if(flag==-1) /*{将负号连接到字符串中*/strcpy(str2,"-"); /*flag=1;} /*将标志值恢复为1*/将字符保存到字符串变量temp中*/ sprintf(temp,"%c",c); /*将temp中的字符串连接到str2中*/strcat(str2,temp); /*setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);显示字符串*/outtextxy(5*width,height,str2); /*}if(c=='+'){将第一个操作数转换为浮点数*/num1=atof(str2); /*将str2清空*/strcpy(str2,""); /*做计算加法标志值*/act=1; /*setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='-'){如果str2为空,说明是负号,而不是减号*/ if(strcmp(str2,"")==0) /*设置负数标志*/flag=-1; /*else{将第二个操作数转换为浮点数*/num1=atof(str2); /*将str2清空*/strcpy(str2,""); /*act=2; /*做计算减法标志值*/setfillstyle(SOLID_FILL,color+3);画矩形*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}}if(c=='*'){将第二个操作数转换为浮点数*/num1=atof(str2); /*strcpy(str2,""); /*将str2清空*/做计算乘法标志值*/act=3; /*setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width /2,3*height/2);显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='/'){将第二个操作数转换为浮点数*/num1=atof(str2); /*strcpy(str2,""); /*将str2清空*/做计算除法标志值*/act=4; /*setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='^'){将第二个操作数转换为浮点数*/num1=atof(str2); /*将str2清空*/strcpy(str2,""); /*做计算乘方标志值*/act=5; /*设置用淡绿色实体填充*/ setfillstyle(SOLID_FILL,color+3); /*画矩形*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='%'){将第二个操作数转换为浮点数*/num1=atof(str2); /*strcpy(str2,""); /*将str2清空*/做计算模运算乘方标志值*/act=6; /*setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/画矩形*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='='){将第二个操作数转换为浮点数*/num2=atof(str2); /*根据运算符号计算*/switch(act) /*{case 1:result=num1+num2;break; /*做加法*/case 2:result=num1-num2;break; /*做减法*/case 3:result=num1*num2;break; /*做乘法*/case 4:result=num1/num2;break; /*做除法*/case 5:result=pow(num1,num2);break; /*做x的y次方*/case 6:result=fmod(num1,num2);break; /*做模运算*/ }设置用淡绿色实体填充*/ setfillstyle(SOLID_FILL,color+3); /*覆盖结果区*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*将结果保存到temp中*/sprintf(temp,"%f",result); /*outtextxy(5*width,height,temp); /*显示结果*/}if(c=='c'){num1=0; /*将两个操作数复位0,符号标志为1*/num2=0;flag=1;strcpy(str2,""); /*将str2清空*/设置用淡绿色实体填充*/ setfillstyle(SOLID_FILL,color+3); /*覆盖结果区*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}如果选择了q回车,结束计算程序*/if(c=='Q')exit(0); /*}putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/返回*/return; /*}/*窗口函数*/void mwindow( char *header ){int height;cleardevice(); /* 清除图形屏幕 */setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*//* 设置视口大小 */ setviewport( 20, 20, MaxX/2, MaxY/2, 1 );height = textheight( "H" ); /* 读取基本文本大小 */settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/输出标题*/outtextxy( MaxX/4, 2, header ); /*setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/ 画边框*/drawboder(); /*}画边框*/void drawboder(void) /*{定义视口类型变量*/struct viewporttype vp; /*setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/将当前视口信息装入vp所指的结构中*/getviewsettings( &vp );/*画矩形边框*/rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*}/*设计鼠标图形函数*/int arrow(){int size;定义多边形坐标*/int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*设置填充模式*/setfillstyle(SOLID_FILL,2); /*/*画出一光标箭头*/fillpoly(8,raw);测试图象大小*/size=imagesize(4,4,16,16); /*分配内存区域*/rar=malloc(size); /*存放光标箭头图象*/getimage(4,4,16,16,rar); /*putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/return 0;}/*按键函数*/int specialkey(void){int key;等待键盘输入*/while(bioskey(1)==0); /*key=bioskey(0); /*键盘输入*/只取特殊键的扫描值,其余为0*/ key=key&0xff? key&0xff:key>>8; /*return(key); /*返回键值*/}。

c语言科学计算器程序编写

c语言科学计算器程序编写

以下是一个简单的C语言科学计算器程序,可以执行基本的数学运算和科学计算:```c#include <stdio.h>#include <math.h>int main() {char op;double num1, num2, result;printf("请输入一个数学表达式(例如:2 + 3):\n"); scanf("%lf %c %lf", &num1, &op, &num2);switch(op) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '*':result = num1 * num2;break;case '/':if(num2 == 0) {printf("错误:除数不能为0!\n");return 0;} else {result = num1 / num2;}break;case '^':result = pow(num1, num2);break;default:printf("错误:不支持的操作符!\n");return 0;}printf("结果为:%lf\n", result);return 0;}```该程序首先要求用户输入一个数学表达式,然后使用scanf函数读取表达式中的操作数和运算符。

接下来,使用switch语句根据运算符执行相应的计算,并将结果存储在result变量中。

最后,程序输出计算结果。

用c语言编写的计算器源代码

用c语言编写的计算器源代码

作品:科学计算器作者:欧宗龙编写环境:vc++6.0语言:c#include"stdafx.h"#include<stdio.h>#include<windows.h>#include<windowsx.h>#include"resource.h"#include"MainDlg.h"#include<math.h>#include<string.h>#definePI3.141593BOOLA_Op=FALSE;BOOLWINAPIMain_Proc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam) {switch(uMsg){HANDLE_MSG(hWnd,WM_INITDIALOG,Main_OnInitDialog);HANDLE_MSG(hWnd,WM_COMMAND,Main_OnCommand);HANDLE_MSG(hWnd,WM_CLOSE,Main_OnClose);}returnFALSE;}BOOLMain_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam){returnTRUE;}voidTrimNumber(chara[])//判断并删除小数点后无用的零{for(unsignedi=0;i<strlen(a);i++){if(a[i]=='.'){for(unsignedj=strlen(a)-1;j>=i;j--){if(a[j]=='0'){a[j]='\0';}elseif(a[j]=='.'){a[j]='\0';}elsebreak;}}}}doubleOperate(charOperator,doublen1,doublen2)//判断符号,进行相应的运算{if(Operator=='0'){}if(Operator=='+'){n2+=n1;}if(Operator=='-'){n2=n1-n2;}if(Operator=='*'){n2*=n1;}if(Operator=='/'){n2=n1/n2;}if(Operator=='^'){n2=pow(n1,n2);}return n2;}////////////////////////////////////////////////voidIntBinary(chara[],intn){if(n>1)IntBinary(a,n/2);sprintf(a,"%s%i",a,n%2);}voiddecimal(chara[],doublem){if(m>0.000001){m=m*2;sprintf(a,"%s%d",a,(long)m);decimal(a,m-(long)m);}}voidBinary(chara[],doubleNum){charDecP[256]="";doublex,y;double*iptr=&y;x=modf(Num,iptr);decimal(DecP,x);IntBinary(a,(int)y);strcat(a,".");strcat(a,DecP);}////////////////////////////////////voidMain_OnCommand(HWNDhwnd,intid,HWNDhwndCtl,UINTcodeNotify) {staticDELTIMES=0;staticcharstr[256];staticcharOperator='0';staticdoubleRNum[3];switch(id){caseIDC_BUTTONN1://数字1{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"1");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN2://数字2{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"2");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN3://数字3{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"3");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN4://数字4{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"4");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN5://数字5{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"5");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN6://数字6{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"6");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN7://数字7{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"7");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN8://数字8{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"8");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN9://数字9{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"9");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN0://数字0{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"0");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));if(DELTIMES==0){strcat(str,".");}DELTIMES++;SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=FALSE;}break;caseIDC_BUTTONADD://加法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='+';DELTIMES=0;A_Op=TRUE;}break;caseIDC_BUTTONSUB://减法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);DELTIMES=0;A_Op=TRUE;Operator='-';}break;caseIDC_BUTTONMUL://乘法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='*';DELTIMES=0;A_Op=TRUE;}break;caseIDC_BUTTONDIV://除法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='/';DELTIMES=0;A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='^';DELTIMES=0;}break;caseIDC_BUTTONPI://圆周率PI,弧度{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));if(atof(str)!=0){RNum[2]=atof(str)*PI;sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);}else{sprintf(str,"%f",PI);SetDlgItemText(hwnd,IDC_EDIT,str);}A_Op=TRUE;}break;caseIDC_BUTTONSQRT://开根号{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=sqrt(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONSIN://三角函数sin函数{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=sin(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=cos(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=tan(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONSQ://平方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str)*atof(str);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str)*atof(str)*atof(str);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=exp(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=pow(10,atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONLN://lnx{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=log(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONLOG10://log10{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=log10(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONBINARY://十进制转换为二进制{chara[256]="";GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str);Binary(a,RNum[2]);strcpy(str,a);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{DELTIMES=0;Operator='0';RNum[0]=RNum[1]=RNum[2]=0;memset(str,0,sizeof(str));SetDlgItemText(hwnd,IDC_EDIT,NULL);A_Op=FALSE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));inti=strlen(str);str[i-1]='\0';SetDlgItemText(hwnd,IDC_EDIT,str);}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='0';DELTIMES=0;}break;default:break;}}voidMain_OnClose(HWNDhwnd){EndDialog(hwnd,0);}本人拙作,如有不足之处请谅解。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

c语言计算器源代码 IMB standardization office【IMB 5AB- IMBK 08- IMB 2C】#i n c l u d e<> #include<>#include<>#definemaxsize100typedefdoubledatatype1;typedefchardatatype2;typedefstructstack1{datatype1data1[maxsize];inttop1; /*栈顶元素*/}seqstack1,*pseqstack1;/*顺序栈*/typedefstructstack2{datatype2data2[maxsize];inttop2; /*栈顶元素*/}seqstack2,*pseqstack2;/*顺序栈*//*栈的初始化*/pseqstack1init_seqstack1(void){pseqstack1S;S=(pseqstack1)malloc(sizeof(pseqstack1));if(S)S->top1=-1;returnS;}pseqstack2init_seqstack2(void){pseqstack2S;S=(pseqstack2)malloc(sizeof(pseqstack2));if(S)S->top2=-1;returnS;}/*判断栈空*/intempty_seqstack1(pseqstack1S)if(S->top1==-1)return1;elsereturn0;}intempty_seqstack2(pseqstack2S){if(S->top2==-1)return1;elsereturn0;}/*X入栈*/intpush_seqstack1(pseqstack1S,datatype1X) {if(S->top1==maxsize-1){printf("栈满,无法入栈!\n");return0;}else{S->top1++;S->data1[S->top1]=X;return1;}}intpush_seqstack2(pseqstack2S,datatype2X) {if(S->top2==maxsize-1){printf("栈满,无法入栈!\n");return0;}else{S->top2++;S->data2[S->top2]=X;return1;}/*X出栈*/intpop_seqstack1(pseqstack1S,datatype1*X) {if(empty_seqstack1(S))return0;else{*X=S->data1[S->top1];S->top1--;return1;}}intpop_seqstack2(pseqstack2S,datatype2*X) {if(empty_seqstack2(S))return0;else{*X=S->data2[S->top2];S->top2--;return1;}}/*求栈顶元素*/intgettop_seqstack1(pseqstack1S,datatype1*X) {if(empty_seqstack1(S))return0;else*X=S->data1[S->top1];return1;}intgettop_seqstack2(pseqstack2S,datatype2*X) {if(empty_seqstack2(S))return0;else*X=S->data2[S->top2];return1;}/*判断字符是否为操作数。

若是返回1,否则返回0*/intisnum(charc){if(c>='0'&&c<='9')return1;elsereturn0;}/*求后缀表达式的值*/doublepostfix_exp(char*A){pseqstack1S; /*定义栈S*/doubleoperand=0;doubleresult; /*存放栈顶元素*/doublea; /*运算符ch前的操作数出栈存入a*/doubleb; /*运算符ch后的操作数出栈存入b*/doublec; /*c==achb*/charch; /*存放读取到的表达式(A)的字符*/ch=*A++; /*读表达式字符=>A*/S=init_seqstack1(); /*初始化栈*/while(ch!='#')/*遇到元素!='#'时*/{if(isnum(ch))/*判断ch是否为数字字符,计算出操作数*/operand=operand*10+(ch-'0');else /*否则*/{if(operand){push_seqstack1(S,operand);/*当前字符不是数字,操作数结束,要入栈*/operand=0;}if(ch!='@'&&ch!=''){pop_seqstack1(S,&b); /*运算符ch后的操作数出栈存入b*/pop_seqstack1(S,&a); /*运算符ch前的操作数出栈存入a*/switch(ch) /*求achb==,将结果赋给c*/{case'+':c=a+b;break;case'-':c=a-b;break;case'*':c=a*b;break;case'/':if(b!=0)c=a/b;elseprintf("分母为零!");}push_seqstack1(S,c); /*将c压入栈中*/}}ch=*A++; /*指针向下移动一位*/}/*遇到'#'循环结束*/gettop_seqstack1(S,&result);/*此时栈顶元素即为计算结果result*/returnresult;}/*优先级判断函数*/intpriority(charop){switch(op){case'#':return1;case')':return2;case'+':case'-':return3;case'*':case'/':return4;case'(':return5;default:return0;}}/*将指针infixexp指向的中缀表达式转换为指针postfixexp指向的后缀表达式*/ intinfix_exp_value(char*infixexp,char*postfixexp){pseqstack2S; /*定义栈S*/intcount=0;charw; /*存放读取到的表达式(infixexp)的字符*/charc; /*存放栈顶元素*/chartopelement;/*存出栈元素*/S=init_seqstack2(); /*初始化栈*/if(!S) /*栈的初始化判断*/{printf("栈初始化失败!");return0;}push_seqstack2(S,'#'); /*将结束符'#'加入运算符栈S中*/w=*infixexp; /*读表达式字符=>w*/while((gettop_seqstack2(S,&c),c)!='#'||w!='#')/*<3>栈顶元素不等于'#'或w不等于'#'时循环*/{if(isnum(w))/*判断w是否为操作数,若是直接输出,读下一个字符=>w,转<3>*/{if(count){*postfixexp='@';postfixexp++;count=0;}*postfixexp=w;postfixexp++;w=*(++infixexp);}else /*w若是运算符分类如下*/{count=1;if((gettop_seqstack2(S,&c),c)=='('&&w==')')/*如果栈顶为'('并且w 为')'则'('出栈不输出,读下一个字符=>w,转<3>*/{pop_seqstack2(S,&topelement);/*将'('出栈存入topelement*/w=*(++infixexp);}elseif((gettop_seqstack2(S,&c),c)=='('||priority((gettop_seqstack2(S,&c),c))<priority(w ))/*如果栈顶为'('或者栈顶优先级小于w优先级,则w入栈,读下一个字符=>w,转<3>*/{push_seqstack2(S,w);w=*(++infixexp);}else/*否则*//*从运算符栈中出栈并输出,转<3>*/ {pop_seqstack2(S,&topelement);*postfixexp=topelement;postfixexp++;}}}*postfixexp='#';/*在指针postfixexp指向的后缀表达式结尾追加字符'#'*/*(++postfixexp)='\0';/*在指针postfixexp指向的后缀表达式最后追加结束符'\0'*/return1;}/*主函数*/intmain(){inti=0;charA[maxsize];charB[maxsize];printf("请输入表达式,如:20+13#,必须以#号结尾!\n");/*1+2*(9+7)-4/2#23+((12*3-2)/4+34*5/7)+108/9#*/A[i]=getchar();while(A[i++]!='#'){A[i]=getchar();}A[i]='\0';infix_exp_value(A,B);printf("A==%s\n",A);printf("B==%s\n",B);printf("上式的结果为:");printf("%g\n",postfix_exp(B));return0;getch();}。

相关文档
最新文档