大学计算机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++语言)实现源代码.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语言设计计算器程序源代码#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语言编写的计算器源代码

作品:科学计算器作者:欧宗龙编写环境: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>#define PI 3.141593BOOL A_Op=FALSE;BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {switch(uMsg){HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);HANDLE_MSG(hWnd, WM_MAND, Main_Onmand);HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);}return FALSE;}BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam){return TRUE;}void TrimNumber(char a[])//判断并删除小数点后无用的零for(unsigned i=0;i<strlen(a);i++){if(a[i]=='.'){for(unsigned j=strlen(a)-1;j>=i;j--){if(a[j]=='0'){a[j]='\0';}else if(a[j]=='.'){a[j]='\0';}else break;}}}}double Operate(char Operator,double n1,double n2) //判断符号,进行相应的运算{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;}////////////////////////////////////////////////void IntBinary(char a[],int n){if(n>1)IntBinary(a,n/2);sprintf(a,"%s%i",a,n%2);}void decimal(char a[],double m){if(m>0.000001){m=m*2;sprintf(a,"%s%d",a,(long)m);decimal(a,m-(long)m);}}void Binary(char a[],double Num){char DecP[256]="";double x,y;double *iptr=&y;x=modf(Num,iptr);decimal(DecP,x);IntBinary(a,(int)y);strcat(a,".");strcat(a,DecP);}////////////////////////////////////void Main_Onmand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {static DELTIMES=0;static char str[256];static char Operator='0';static double RNum[3];switch(id){case IDC_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;case IDC_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;case IDC_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;case IDC_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;case IDC_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;case IDC_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;case IDC_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;case IDC_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;case IDC_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;case IDC_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 IDC_BUTTONDEL://小数点.del{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;case IDC_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;case IDC_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;case IDC_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;case IDC_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 IDC_BUTTONXY://x的y次方{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;case IDC_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;case IDC_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;case IDC_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 IDC_BUTTONCOS://三角函数cos函数{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 IDC_BUTTONTAN://三角函数tan函数{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;case IDC_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 IDC_BUTTONCUBE://三次方{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 IDC_BUTTONEX://e的x次方{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 IDC_BUTTON10X://10的x次方{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;case IDC_BUTTONLN: //ln x{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;case IDC_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;case IDC_BUTTONBINARY: //十进制转换为二进制{char a[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 IDC_BUTTONCLEAR://清除数据{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 IDC_BUTTONBACKSPACE://退格键{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));int i=strlen(str);str[i-1]='\0';SetDlgItemText(hwnd,IDC_EDIT,str);}break;case IDC_ENTER://Enter键{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;}}void Main_OnClose(HWND hwnd){EndDialog(hwnd, 0);}本人拙作,如有不足之处请谅解。
C语言计算器源程序

#include<stdio.h>//标准输入输出#include<conio.h>//system设置颜色使用到#include<stdlib.h>//清屏时用到#include<math.h>//数学函数用到int main(){ void mnue();//菜单函数声明void add();//加法函数声明void minus();//减法函数void multiply();//乘法函数void divided();//除法函数void sinx();//sin函数void cosx();//cos函数void tanx();//tan函数void arcsin();//void arccos();void arctan();void sqrtx();void logx();void log10x();void expx();void powx();void tuichu();system("color 5B");mnue();}void mnue()//菜单{ char M;printf("\n\n\n\t\t\t ********* ********\n");printf("\t\t\t ** ** ** **\n");printf("\t\t\t ** ** ** **\n");printf("\t\t\t** 欢迎*** 使用**\n");printf("\t\t\t ** * **\n");printf("\t\t\t ** 简易计算器**\n");printf("\t\t\t ** **\n");printf("\t\t\t.........按任意键进入——》》》\n");printf("\t\t\t ** **\n");printf("\t\t\t ** **\n");printf("\t\t\t ** **\n");printf("\t\t\t *\n");getch();system("cls");while(M!='h'){printf("\n\n\n\t\t\t|------------------------------|\n");printf("\t\t\t| 加法—>A\t| 减法—>a |\n");printf("\t\t\t| 乘法—>B\t| 除法—>b |\n");printf("\t\t\t| 正弦—>C\t| 反正弦—>c |\n");printf("\t\t\t| 余弦—>D\t| 反余弦—>d |\n");printf("\t\t\t| 正切—>E\t| 反正切—>e |\n");printf("\t\t\t| ln —>F\t| log10 —>f |\n");printf("\t\t\t| 幂指数—>G\t| E 幂数—>g |\n");printf("\t\t\t| 开根—>H\t| 退出—>h |\n");printf("\t\t\t|______________________________|\n");printf("\n\t\t请输入要进行运算的法则!\n");M=getch();while(1){if('A'<=M&&M<='H' ||'a'<=M&&M<='h'){ //system("cls");break;}else{printf("\n\n\t提示:输入错误\n\t—>请正确输入!\n\a\a\a\a\a\a\a");M=getch();}}switch(M){case 'A': add();break;case 'a': minus();break;case 'B': multiply();break;case 'b': divided();break;case 'C': sinx();break;case 'c': arcsin();break;case 'D': cosx();break;case 'd': arccos();break;case 'E': tanx();break;case 'e': arctan();break;case 'F': logx();break;case 'f': log10x();break;case 'G': powx();break;case 'g': expx();break;case 'H': sqrtx();break;}}if("M==h") tuichu();}void add()//加法函数{int z, y,x;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%d%d",&x,&y);z=y+x;printf("%d+%d=%d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void minus()//减法函数{int x,y,z;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%d%d",&x,&y);z=x-y;printf("%4d-%4d=%4d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void multiply()//乘法函数{int x,y,z;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%d%d",&x,&y);z=x*y;printf("%4d*%4d=%4d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void divided()//除法函数{float x,y,z;char n='Y';while(n=='Y'){printf("请输入:\n");scanf("%f%f",&x,&y);z=x/y;printf("%4d/%4d=%4d\n",x,y,z);printf("是否继续(Y/N)\n");n=getch();if(n=='N')system("cls");}}void sinx()//sin函数{double x,y;scanf("%lf",&x);y=sin(x);printf("sin%lf=%lf",x,y);}void cosx()//cos函数{double a,b;scanf("%lf",&a);b=cos(a);printf("cos%lf=%lf",b);}void tanx()//tan函数{double a,b;scanf("%lf",&a);b=tan(a);printf("tan%lf=%lf",b);}void arcsin(){double a,b;scanf("%lf",&a);b=asin(a);printf("arcsin%lf=%lf",b);void arccos(){double a,b;scanf("%lf",&a);b=acos(a);printf("arccos%lf=%lf",b); }void arctan(){double a,b;scanf("%lf",&a);b=atan(a);printf("arctan%lf=%lf",b); }void sqrtx(){double a,b;scanf("%lf",&a);b=sqrt(a);printf("sqrt%lf=%lf",b); }void logx(){double a,b;scanf("%lf",&a);b=log(a);printf("log%lf=%lf",a,b); }void log10x(){double a,b;scanf("%lf",&a);b=log10(a);printf("ln%lf=%lf",b);}void expx(){double a,b;scanf("%lf",&a);b=exp(a);printf("exp%lf=%lf\n",b); }void powx()double a,b,c;scanf("%lf%lf",&a,&b);c=pow(a,b);printf("pow%lf=%lf\n",c);}void tuichu(){ system("cls");printf("\n\n\n\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");printf("\n\n\t\t\t谢谢使用再见!\n\n\n");printf("\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n\n\t"); }。
C语言计算器代码

printf("小组组长:");Sleep(1000);
puts("尹化荣\n");Sleep(300);
printf("小组成员:");Sleep(300);
printf("尹化荣,");Sleep(300);
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//------------------------------------------------------------------||
//迷宫游戏,所有声明:
#define Height 23 //迷宫的高度,必须为奇数
#define Width 39 //迷宫的宽度,必须为奇数
#define Wall 1
double jiafa(double a,double b);
double jianfa(double a,double b);
double chengfa(double a,double b);
double chufa(double a,double b);
intqiuyu(int a,int b);
CONSOLE_CURSOR_INFO cci;
计算器c语言代码

计算器c语言代码计算器C语言代码计算器是一种常见的工具,用于进行数值计算和数学运算。
计算器通常具有基本的四则运算功能,可以执行加法、减法、乘法和除法运算,同时还可以进行其他高级计算,比如开方、求幂、取余等。
在本文中,我们将介绍如何使用C语言编写一个简单的计算器。
我们需要定义计算器的基本功能。
我们可以使用函数来实现不同的计算操作。
下面是一个示例代码,实现了加法、减法、乘法和除法四种功能:```c#include <stdio.h>// 加法函数double add(double a, double b) {return a + b;}// 减法函数double subtract(double a, double b) {return a - b;}// 乘法函数double multiply(double a, double b) { return a * b;}// 除法函数double divide(double a, double b) { if (b == 0) {printf("除数不能为零!\n"); return 0;}return a / b;}int main() {double num1, num2;char operator;printf("请输入两个操作数:");scanf("%lf %lf", &num1, &num2); printf("请输入操作符:");scanf(" %c", &operator);switch (operator) {case '+':printf("结果:%lf\n", add(num1, num2));break;case '-':printf("结果:%lf\n", subtract(num1, num2));break;case '*':printf("结果:%lf\n", multiply(num1, num2));break;case '/':printf("结果:%lf\n", divide(num1, num2));break;default:printf("无效的操作符!\n");break;}return 0;}```在上面的代码中,我们使用了四个函数来实现不同的计算操作。
C编写简易计算器附源代码超详细

超详细一、因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:二、向窗体中拖入需要的控件,如下图所示:(完成效果图)结果显示区(作者博客左边的文本框)是TextBox控件,并修改其name为txtShow ,按键0~9为Button控件,并将其name分别修改为btn_0、btn_1、btn_2、btn_3、btn_4、btn_5、btn_6、btn_7、btn_8、btn_9;按键【负数】的name值修改为btn_sign,按键【.】的name 修改为btn_dot,按键【+ - * /】的name值分别修改为btn_add、btn_sub、btn_mul、btn_div,按键【=】的name值修改为btn_equ,按键【倒数】的name值修改为btn_rev,按键【平方】的name值修改为btn_sqr,按键【开方】的name值修改为btn_sqrt。
右边的计算器图片空间是PictureBox,作者博客控件是LinkLabel,可以不添加,以上所有控件均可按照需求添加,只保留自己需要的按钮控件和textbox控件即可。
三、代码部分(含解释),采用switch多分支语句编写using System;using System.Drawing;using System.Collections;using ponentModel;using ;using System.Data;namespace Calc{///<summary>/// QQ:6 温柔一刀C#简易计算器的实现///</summary>public class CalcForm :{private btn_0;private btn_1;private btn_2;private btn_3;private btn_4;private btn_5;private btn_6;private btn_7;private btn_8;private btn_9;private btn_add;private btn_sub;private btn_mul;private btn_div;private btn_sqrt;private btn_sign;private btn_equ;private btn_dot;private btn_rev;private txtShow;private btn_sqr;private PictureBox pictureBox1;private LinkLabel linkLabel1;///<summary>///必需的设计器变量。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
r = sqrt(one);
else if(cc == 'C')
r = cos(one);
else if(cc == 'S')
r = sin(one);
else if(cc == 'T')
r = tan(one);
else if(cc == 'c')
r = acos(one);
{
* ++S_char.top = c;
}
double pop_num()//数字出栈
{
double m = *S_num.top;
S_num.top--;
return m;
}
char pop_char()//运算符号出栈
{
char cc = *S_char.top;
S_char.top--;
return cc;
i++;
break;
}
if(flag1)
{
h = h * 10 + (line[i] - '0');
flag = 1;
i++;
if(biao_dian)
ge++;
}
else
break;
}
if(flag)
{
if(biao_dian)
{
int r = 1;
for(int k = 1; k <= ge; k++)
double two_L = pop_num();
char dian = pop_char();
push_num(operate_L(two_L, one_L, dian));
}
else
{
double x = pop_num();
double y = pop_num();
char dian = pop_char();
shu[++p] = -8;
else if(line[i] == 'C')
shu[++p] = -9;
else if(line[i] == 'S')
shu[++p] = -10;
else if(line[i] == 'T')
shu[++p] = -11;
else if(line[i] == 'c')
else if(line[i] == '/')
shu[++p] = -4;
else if(line[i] == '%')
shu[++p] = -5;
else if(line[i] == '^')
shu[++p] = -6;
else if(line[i] == 'Q')
shu[++p] = -7;
else if(line[i] == 'L')
int compare[1000];//表现出各运算符号的优先级
double shu[1000];//存储"数"的数组
double dai_result;//运算的结果,是为了处理M运算(简介函数里有M的定义)
int biao = 0;//和dia_result一样,为了处理M运算
char line[SIZE];//输入的所要计算的表达式
kuo[++f] = 1;
if(biao)
push_num(dai_result);//把运算的结果先进栈,在这个结果的基础上继续进行运算biao = 0;
for(i = 0; line[i] != '\0';)//把表达式中的数字字符串转化成可计算的数字
{
int flag = 0;
int flag1 = 1;//标记是否是运算符号
break;
}
}
if(line[i] == '_')
{
break;
}
if(line[i] == '.')
{
i++;
ge = 0;
biao_dian = 1;
}
if(line[i] == 'P')
{
shu[++p] = pi;
i++;
break;
}
if(line[i] == 'E')
{
shu[++p] = e;
shu[++p] = -16;
else if(line[i] == '\n')
shu[++p] = -17;
i++;
}
i = 1;
while(shu[i] != -17 || get_top_char() != '\n')
{
double m = shu[i];
if(m >= 0)
{
push_num(m);
return result;
int check_kuohao()//检查表达式括号是否匹配
{
int i, f = 0;
int kuo[SIZE], key = 1;
memset(kuo, 0, sizeof(kuo));
for(i = 0; line[i] != '\0'; i++)
{
if(line[i] == '(')
c = 'Q';
else if(m == -8)
c = 'L';
else if(m == -9)
c = 'C';
else if(m == -10)
c = 'S';
else if(m == -11)
c = 'T';
else if(m == -12)
c = 'c';
else if(m == -13)
c = 's';
r *= 10;
h /= r;
}
shu[++p] = h;//把转化而来的数字存于数组
}
if(line[i] == '+')
shu[++p] = -1;
else if(line[i] == '-')
shu[++p] = -2;
else if(line[i] == '*')
shu[++p] = -3;
{
char *top;
char *base;
}stack_char;
stack_num S_num;//定义
stack_char S_char;//定义
char fu[18] = {'\n', ')', '+', '-', '*', '/', '%', '^',
'Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('};
|| ch == 'c' || ch == 's' || ch == 't')
{
double one = pop_num();
char dian = pop_char();
push_num(operate_one(one, dian));
}
else if(ch == 'L')
{
double one_L = pop_num();
compare[fu[6]] = 4;
compare[fu[7]] = 5;
for(int i = 8; i <= 15; i++)
compare[fu[i]] = 6;
compare[fu[16]] = 7;
S_num.base = (double*)malloc(sizeof(double)*SIZE);//为栈开辟空间
typedef struct node//为了处理符号而建立的链表(如: 1+(-2))
{
char data;
node *next;
}node;
typedef struct stack_num//存储数的栈
{
double *top;
double *base;
}stack_num;
typedef struct stack_char//存储运算符号的栈
if(dian == '/' && x == 0)//判断是否除了"零"
{
cout<<"由于您除了零,结果将是错误的"<<endl;
}
push_num(operate(x, dian, y));//把进行一次计算的结果入栈}
}
else
{
push_char(c);
i++;