C 简单计算器代码
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 System.Windows.Forms;using System.Data;namespace Calc{///<summary>/// QQ:479340056 温柔一刀C#简易计算器的实现///</summary>public class CalcForm : System.Windows.Forms.Form{private System.Windows.Forms.Button btn_0;private System.Windows.Forms.Button btn_1;private System.Windows.Forms.Button btn_2;private System.Windows.Forms.Button btn_3;private System.Windows.Forms.Button btn_4;private System.Windows.Forms.Button btn_5;private System.Windows.Forms.Button btn_6;private System.Windows.Forms.Button btn_7;private System.Windows.Forms.Button btn_8;private System.Windows.Forms.Button btn_9;private System.Windows.Forms.Button btn_add;private System.Windows.Forms.Button btn_sub;private System.Windows.Forms.Button btn_mul;private System.Windows.Forms.Button btn_div;private System.Windows.Forms.Button btn_sqrt;private System.Windows.Forms.Button btn_sign;private System.Windows.Forms.Button btn_equ;private System.Windows.Forms.Button btn_dot;private System.Windows.Forms.Button btn_rev;private System.Windows.Forms.TextBox txtShow;private System.Windows.Forms.Button btn_sqr;private PictureBox pictureBox1;private LinkLabel linkLabel1;///<summary>///必需的设计器变量。
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语言代码

#include<stdio.h>#include<math.h>int main(void){int choice,t=1;printf("**********************计算器*****************************************\n"); printf(" 1.加法运算2.减法运算\n"); printf(" 3.乘法运算4.除法运算\n"); printf(" 5.平方运算6.开方运算\n"); printf(" 7.解一元二次方程By:_一念成殇、\n"); printf("**********************************************************************\n");printf("请选择您即将进行的运算方式(输入对应数字):");while(t==1){scanf("%d",&choice);if(choice>7) printf("请您输入正确的选项编号!\n");//加法if(choice==1){float addend_1,addend_2,sum;printf("请输入两个加数:\n");scanf("%f",&addend_1);scanf("%f",&addend_2);sum=addend_1+addend_2;printf("结果为:%.2f",sum);}//减法if(choice==2){float reduction,minuend,difference;printf("请输入被减数和减数:\n");scanf("%f",&reduction);scanf("%f",&minuend );difference=reduction-minuend ;printf("结果为:%.2f",difference);}//乘法if(choice==3){float multiplier_1,multiplier_2,product;printf("请输入两个乘数:\n");scanf("%f",&multiplier_1);scanf("%f",&multiplier_2);product= multiplier_1 * multiplier_2;printf("结果为:%.2f",product);}//除法if(choice==4){float dividend,divisor,conslut;printf("请输入被除数和除数:\n");scanf("%f",÷nd);scanf("%f",&divisor);conslut=dividend/divisor;printf("结果为:%.2f",conslut);}//平方if(choice==5){float base,power;printf("请输入底数:\n");scanf("%f",&base);power=base*base;printf("结果为:%.2f",power);}//开方if(choice==6){float power,base;printf("请输入需要开方的数:\n");scanf("%f",&power);base=sqrt(power);printf("结果为:%.2f",base);}//解一元二次方程if(choice==7){float a,b,c,delta,root_1,root_2;printf("方程一般形式:ax^2+bx+c=0,请输入其中常量a,b,c的值:\n"); scanf("%f",&a);if(a==0){printf("此方程不是一元二次方程!请重新输入!\n");}else{scanf("%f",&b);scanf("%f",&c);delta=(b*b)-(4*a*c);printf("△=%f\n",delta);if(delta<0){printf("此方程无实数根.\n");}else{root_1=(sqrt(delta)-b)/(2.0*a);root_2=((-b)-sqrt(delta))/(2.0*a);printf("解得:x1=%f x2=%f\n",root_1,root_2);}}}printf("\n\n\n\n请选择您即将进行的运算方式(输入对应数字):"); }return 0;}。
简易计算器代码

button[10] = new QPushButton(buttontext[10],this,buttontext[10]); // “—”
button[10]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
edit->setText(tr("%1").arg(0)); //这个函数可获取文本编辑框中光标的位置,并显示在状态栏中
//setText(tr(“%1行%2列”).arg(rowNum).arg(colNum));
initialize(); //调用初始化函数
}
void CWidget::initialize()
cwidget.cpp
#include "cwidget.h"
#define KEY_CLR "CLR"
#define KEY_ADD "+"
#define KEY_SUB "-"
#define KEY_MUL "*"
#define KEY_DIV "/"
#define KEY_EQ "="
#define KEY_0 "0"
{
delete edit;
delete *button;
delete mainLayout;
delete topLayout;
delete bottomLayout;
}
void CWidget::calculate() //响应按键“=”,调用计算功能
计算器编程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语⾔实现简易计算器(加减乘除)计算器作为课设项⽬,已完成答辩,先将代码和思路(注释中)上传⼀篇博客已增添、修改、整理⾄⽆错且可正常运⾏虽使⽤了栈,但初学者可在初步了解栈和结构语法后理解代码#include <stdlib.h>#include <stdio.h>#include <string.h>#define IsDouble 0#define IsChar 1//_______________________________________________________________________________________________________________________________________________________ //1.⽀持浮点数和字符的栈typedef struct {char * buffer;int typesize;int top;int max;} stack;stack * CreateStack(int max, int typesize);//创建⼀个栈void DestroyStack(stack *);//释放栈的空间void ClearStack(stack *);//清空栈int Push(stack *, void *);//⼊栈int Pop(stack *, void *);//出栈int GetSize(stack *s);//得到栈的⼤⼩int GetTop(stack *, void *);//找到栈顶int IsEmpty(stack *);//判断是否为空栈,空则下溢int IsFull(stack *);//判断栈是否已满,满则溢出stack * CreateStack(int max, int typesize){stack * s = (stack*)malloc(sizeof(stack));//为栈s malloc内存if (!s) return 0;//为结构中buffer元素malloc内存s->buffer = (char *)malloc(sizeof(char) * max * typesize);if (!s->buffer) return 0;//初始化结构中的栈顶,最⼤值,类型⼤⼩s->top = -1;s->max = max;s->typesize = typesize;return s;}void DestroyStack(stack* s){free(s->buffer);//先释放buffer的空间free(s);//在释放s的空间}void ClearStack(stack * s){s->top = -1;//清空栈(栈头位置归零)}int Push(stack * s, void * data){if (IsFull(s)) return 0;//如果栈已满则return 0,防⽌溢出//栈未满则将栈头移动打动下⼀位置,并将data中的元素拷⼊栈中buffer的第top位置s->top++;memcpy(s->buffer + s->top*s->typesize, data, s->typesize);//⼊栈成功return 1return 1;}int Pop(stack * s, void * data){if (IsEmpty(s)) return 0;//出栈判断栈是否为空,若为空则return 0//栈未空则将buffer中top位置的字符拷⼊data记录,并让栈头向前移动⼀个位置memcpy(data, s->buffer + s->top*s->typesize, s->typesize);s->top--;//成功则return 1return 1;}int GetSize(stack *s){return s -> top+1;//栈头位置+1得到⼤⼩}int GetTop(stack *s, void * data){if (IsEmpty(s)) return 0;//如果栈空return 0//栈不为空则将top位置的字符拷回data记录,得到栈头memcpy(data, s->buffer + s->top*s->typesize, s->typesize);//成功则return 1;return 1;}int IsEmpty(stack * s){return s->top == -1;//如果top为-1则栈空}int IsFull(stack * s){return s->top == s->max-1;//如果top为max-1则栈满}//___________________________________________________________________________________________________________________________________________________ //2.定义⼀个cal类型,其中data存数时sign为IsDouble,存字符时,sign为Ischartypedef struct {double data;char sign;} cal;//3.查找对应符号(找到则返回该符号下标)(找不到则说明该部分为数字返回-1)int SearchCode(char ch){char * code = "+-*/()@";//@为终⽌符,算式输⼊结束int index = 0;//while (code[index]){if (code[index] == ch) return index;index++;}return -1;}//4.得到两个符号间的优先级//与SearchCode相对应,char GetPriority(char ch, char next){//创建⼀个perferen表,第i⾏(列)对应SearchCode函数中code中的第i个字符char perferen[7][7] = {">><<<>>",">><<<>>",">>>><>>",">>>><>>","<<<<<=E",">>>>E>>","<<<<<E="};//找到两个形参对应的字符int c = SearchCode(ch);int n = SearchCode(next);//如果找不到对应运算符(字符不是运算符⽽是为数字)return Eif (c==-1 || n==-1) return 'E';//如果找到两个对应运算符则按照优先级表返回两个运算符的优先级return perferen[c][n];}//5.四则运算double add(double a, double b) { return a+b; }double sub(double a, double b) { return a-b; }double mul(double a, double b) { return a*b; }double ddiv(double a, double b) { return a/b; }//整合四种运算double calcu(double a, char ch, double b){double (*calculation[4])(double,double) = {add,sub,mul,ddiv};return calculation[SearchCode(ch)](a,b);}//6.检测字符串int CheckStr(char * buffer){int n;//遍历字符串确保算式中⽆⾮法字符若检测到⾮法字符return 0,若都合法则return 1for (n = 0;buffer[n];n++){if ((SearchCode(buffer[n]) != -1 || buffer[n] == '.' || (buffer[n] >= '0' && buffer[n] <= '9')) && buffer[n] != '@') continue;else return 0;}buffer[n] = '@';//加上终⽌符,表⽰算式结束buffer[n+1] = '\0';return 1;}//7.得到数据转化为double类型存⼊rsint GetDigit(char * buffer, int * n, double * rs){char str[30];int i,j = 0;for (i = 0;SearchCode(buffer[*n]) == -1;i++){str[i] = buffer[*n];//从*n位置开始,将这⼀串数字字符存⼊str(*n)++;}str[i] = '\0';for (i = 0;str[i];i++){if (str[i] == '.') j++;}//如果⼀段⼩数有多个⼩数点或⼩数点在数字⾸尾,return 0if (j>1 || str[i-1] == '.' || str[0] == '.') return 0;//rs接收转化为double的数据*rs = atof(str);//操作成功return 1return 1;}//8.将⽤户输⼊的buffer字符串转化为可供程序运算的calstr数组int resolu(char * buffer, cal * calstr){int i = 0, j = 0;cal c;while (buffer[i]){if (SearchCode(buffer[i]) == -1){//如果得到数据不成功则return 0if (GetDigit(buffer,&i, &c.data) == 0) return 0;//如果成功得到数据则在c.sign标记为浮点数c.sign = IsDouble;//将c存⼊数组calstr中calstr[j++] = c;}else{//若符号为运算符//判断正负号if (buffer[i] == '-' && (buffer[i-1] == '('||buffer[i-1] == '+'||buffer[i-1] == '-'||buffer[i-1] == '*'||buffer[i-1] == '/') || (i==0 && buffer[0] == '-')){ i++;if (GetDigit(buffer,&i, &c.data) == 0) return 0;//在符号的下⼀位开始查找,若找不到数字return 0//否则,给数字取相反数,c.sign标记为浮点数,存⼊calstr中c.data = 0 - c.data;c.sign = IsDouble;calstr[j++] = c;} else//如果是正号,与符号处理⽅式同理if (buffer[i] == '+' && (buffer[i-1] == '('||buffer[i-1] == '+'||buffer[i-1] == '-'||buffer[i-1] == '*'||buffer[i-1] == '/') || (i==0 && buffer[0] == '+')){ i++;if (GetDigit(buffer, &i, &c.data) == 0) return 0;c.sign = IsDouble;calstr[j++] = c;}else{//如果不是正负号,则为运算符,先强制转换为double类型存在c.data⾥,然后c.sign标记为char类型,存⼊calstrc.data = (double)buffer[i++];c.sign = IsChar;calstr[j++] = c;}}}//操作蔡成功则return 1return 1;}//9.计算出结果int result(cal * calstr, double * rs){stack * pst = CreateStack(100,sizeof(char));//运算符栈stack * pnd = CreateStack(100,sizeof(double));//数据栈double num1,num2;int n = 0;char ch = '@';Push(pst, &ch);//在转换得到的calstr中遍历直到终⽌符'@"while(ch != '@' || !(calstr[n].sign == IsChar && (char)calstr[n].data == '@')){//如果calstr的n位上是浮点数,则将这个data压栈进⼊数据栈pnd中if (calstr[n].sign == IsDouble){Push(pnd, &(calstr[n].data));n++;}//反之,如果是运算符,则要检测优先级else{switch( GetPriority(ch, (char)calstr[n].data)){//如果运算符优先级较⼩,则让ch等于优先级⼤的符号并压⼊符号栈pst中case '<':ch = (char)calstr[n].data;Push(pst, &ch);n++;break;//如果结果为等号,让符号出栈暂存到ch中case '=':if (!Pop(pst, &ch)) return 0;n++;break;//如果ch优先级较⾼,则将前两个数字及运算符出栈,分别储存⾄num2,ch,num1中,进⾏运算,得到的结果再次压栈进⼊pnd中 case '>':if (!(Pop(pnd,&num2) && Pop(pst,&ch) && Pop(pnd,&num1))) return 0;num1 = calcu(num1,ch,num2);Push(pnd, &num1);break;//如果符号顺序出错,return 0case 'E':return 0;}}//检测是否可以得到栈顶符号,栈空则return 0if (!GetTop(pst, &ch)) return 0;}//如果栈中得到了最终结果,并且取出pnd中的最终结果到rs,return 1if (GetSize(pnd) == 1 && GetTop(pnd,rs)){DestroyStack(pst);DestroyStack(pnd);return 1;}//否则 return 0else{return 0;}}//10.⽤户交互函数void treatment(){char buffer[100];//⽤户输⼊的字符串(算式)cal calstr[50];//计算⽤的数组double rs = 0;//计算结果printf("Enter your equation:");gets(buffer);//让⽤户输⼊算式buffer//⽤户不输⼊"exit"就不退出while (!(buffer[0]=='e' && buffer[1]=='x' && buffer[2]=='i' && buffer[3]=='t')){//检查buffer中字符君合法,成功将buffer转化为⽤于计算的calstr数组,成功计算出结果存⼊rsif (CheckStr(buffer) && resolu(buffer,calstr) && result(calstr,&rs)){printf("\n%lf\n",rs);}else{printf("\nError!\n");}printf("Enter \"exit\"to quit");printf("\nEnter your equation:");gets(buffer);//再次让⽤户输⼊算式}printf("\nbye\n");}//11.主函数int main(){treatment();}参考⽂献链接如下[参考⽂献]()。
用c语言写的简单计算器

⽤c语⾔写的简单计算器这是⾃⼰在学习C语⾔,凭借⾃⼰的兴趣,将课本的知识运⽤后整理的关于C语⾔计算器的代码。
计算器实现的功能有:加、减、乘、除、求余。
功能简单,但对于初学者的我来说能把它写出来,排除每个错误依旧是个难题。
前后两种代码是由⾃⼰所学东西的质量决定的,如有错误,请前辈指出。
最初版本的计算器:1 #include<stdio.h>23int checkPassword();45int add(int x,int y);67int sub(int x,int y);89int multi(int x,int y);1011float quo(float x1,float y1);1213int com(int x,int y);1415int x,y;1617int result;1819int select;2021float result1;2223float x1,y1;2425int success,psw;2627int a;2829int i;3031int t;323334353637void main()3839 {4041 printf("请输⼊密码:\n");4243 i=0;4445do{46474849 scanf("%d",&a);50515253if( a!=1234 && i<2)5455 printf("请重新输⼊密码:\n");5657else5859 i=3;6061 i++;6263 }while( i<=2);6465if(a==1234)6667 {6869for(t=1;t!=6;t=select)7071 {72737576777879 printf("+=============================+\n"); 8081 printf("+======计=算=器=主=菜=单======+\n"); 8283 printf("+=============================+\n"); 8485 printf("+==========1、加法==========+\n"); 8687 printf("+==========2、减法==========+\n"); 8889 printf("+==========3、乘法==========+\n"); 9091 printf("+==========4、除法==========+\n"); 9293 printf("+==========5、求余==========+\n"); 9495 printf("+==========6、退出==========+\n"); 9697 printf("+=============================+\n"); 9899100101102103 printf("请输⼊你的选择:\n");104105 scanf("%d",&select);106107108109switch(select)110111 {112113114115case1:116117 printf("请输⼊两个数:\n");118119 scanf("%d%d",&x,&y);120121 result=add(x,y);122123 printf("%d+%d=%d\n",x,y,result);124125break;126127case2:128129 printf("请输⼊两个数:\n");130131 scanf("%d%d",&x,&y);132133 result=sub(x,y);134135 printf("%d-%d=%d\n",x,y,result);136137break;138139case3:140141 printf("请输⼊两个数:\n");142143 scanf("%d%d",&x,&y);144145 result=multi(x,y);146147 printf("%d*%d=%d\n",x,y,result);148149break;150151case4:152153 printf("请输⼊两个数:\n");154155 scanf("%f%f",&x1,&y1);156157 result1=quo(x1,y1);159 printf("%f/%f=%f\n",x1,y1,result1); 160161break;162163case5:164165 printf("请输⼊两个数:\n");166167 scanf("%d%d",&x,&y);168169 result=com(x,y);170171 printf("%d/%d=%d\n",x,y,result); 172173break;174175case6:176177break;178179default:180181 printf("请输⼊1~5之间的数\n"); 182183break;184185186187188189 }190191 }192193 }194195 }196197int add(int x,int y)198199 {200201int sum;202203 sum=x+y;204205return sum;206207 }208209int sub(int x,int y)210211 {212213int sub;214215 sub=x-y;216217return sub;218219 }220221int multi(int x,int y)222223 {224225int multi;226227 multi=x*y;228229return multi;230231 }232233float quo(float x,float y)234235 {236237float quo;238239 quo=x/y;240241return quo;243 }244245int com(int x,int y)246247 {248249int com;250251 com=x%y;252253return com;254255 }256257int checkPassword( int psw)258259 {260261if(psw==1234)262263 success=1;264265else266267 success=-1;268269return success;270271 }改版后的计算器:1 #include<stdio.h>23int yanzheng(int );45void caidan();67void jiafa(int,int);89void jianfa(int,int);1011void chengfa(float,float);1213void chufa(float,float);1415void mo(int,int);16171819void main(){2021int x,m;2223int y;2425float a,b;2627 printf("请输⼊密码:\n");2829 scanf("%d",&x);3031 m=yanzheng(x);3233if(m==1){3435 caidan();3637//int y;3839//float a,b;4041for(;y!=6;){4243 printf("请输⼊选项:\n"); 4445 scanf("%d",&y);4647if(y>=1 && y<6){4849 printf("请输⼊要运算的两个数:\n");5051 scanf("%f%f",&a,&b);5253 }54555657switch(y){5859case1 :jiafa(a,b);break;6061case2 :jianfa(a,b);break;6263case3 :chengfa(a,b);break;6465case4 :chufa(a,b);break;6667case5 :mo(a,b);break;6869case6 :break;7071default : printf("选项输⼊错误!请重试\n");break; 7273 }7475 }7677 }7879 }808182838485int yanzheng(int x){8687int i,m;8889for(i=1,m=0;i<3;i++){9091if(x==1234){9293 m=1;9495break;9697 }9899 printf("请重新出⼊密码:");100101 scanf("%d",&x);102103104105 }106107return m;108109 }110111112113void caidan(){114115 printf("+=============================+\n"); 116117 printf("+======计=算=器=主=菜=单======+\n"); 118119 printf("+=============================+\n"); 120121 printf("+==========1、加法==========+\n"); 122123 printf("+==========2、减法==========+\n"); 124125 printf("+==========3、乘法==========+\n"); 126127 printf("+==========4、除法==========+\n"); 128129 printf("+==========5、求余==========+\n"); 130131 printf("+==========6、退出==========+\n"); 132133 printf("+=============================+\n"); 134135 }136137138139void jiafa(int x,int y){140141int sum;142143 sum=x+y;144145 printf("%d\n", sum);146147 }148149150151void jianfa(int x,int y){152153int cha;154155 cha=x-y;156157 printf("%d\n", cha);158159 }160161162163void chengfa(float x,float y){164165float ji;166167 ji=x*y;168169 printf("%f\n", ji);170171 }172173174175void chufa(float x,float y){176177float shang;178179 shang=x/y;180181 printf("%f\n", shang);182183 }184185186187void mo(int x,int y){188189int mo;190191 mo=x%y;192193 printf("%d\n", mo);194195 }。
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;
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
a=f/100; b=(f/10)%10; c=f%10; prime1(a);prime1(b);prime1(c);cout<<endl; prime2(a);prime2(b);prime2(c);cout<<endl; prime3(a);prime3(b);prime3(c);cout<<endl; prime4(a);prime4(b);prime4(c);cout<<endl; prime5(a);prime5(b);prime5(c);cout<<endl; } if(f>=1000&&f<10000) { a=f/1000; b=(f/100)%10; c=(f/10)%10; d=f%10; prime1(a);prime1(b);prime1(c);prime1(d);cout<<endl; prime2(a);prime2(b);prime2(c);prime2(d);cout<<endl; prime3(a);prime3(b);prime3(c);prime3(d);cout<<endl; prime4(a);prime4(b);prime4(c);prime4(d);cout<<endl; prime5(a);prime5(b);prime5(c);prime5(d);cout<<endl; } }
cout<<"继续计算按“y”,退出计算请按“n”"<<endl; cin>>ch; {
if(ch=='y') flag=true;
else flag=false;
} } } int prime1(int m)
{ if(m==0) cout<<" -- "; if(m==1) cout<<" "; if(m==2) cout<<" -- "; if(m==3) cout<<" -- "; if(m==4) cout<<" "; if(m==5) cout<<" -- "; if(m==6) cout<<" -- "; if(m==7) cout<<" -- "; if(m==8) cout<<" -- "; if(m==9) cout<<" -- "; return 0;
} int prime2(int m) {
if(m==0) cout<<"| | ";
if(m==1) cout<<" | ";
if(m==2) cout<<" | ";
if(m==3) cout<<" | ";
if(m==4) cout<<"| | ";
if(m==5) cout<<"| ";
if(m==6) cout<<"| ";
cout<<" 请输入表达式,操作数与操作符之间用空格分隔"<<endl; cout<<"加减乘除的运算符分别用“+ - * /”表示"<<endl; cin>>x>>n>>y; {
if(n=='+') f=x+y;
if(n=='-') f=x-y;
if(n=='*') f=x*y;
if(n=='/') f=x/y;
cout<<"| | "; if(m==9)
cout<<" | "; return 0; } int prime5(int m) { if(m==0)
cout<<" -- "; if(m==1)
cout<<" "; if(m==2)
cout<<" -- "; if(m==3)
cout<<" -- "; if(m==4)
prime1(a);prime1(b);cout<<endl; prime2(a);prime2(b);cout<<endl; prime3(a);prime3(b);cout<<endl; prime4(a);prime4(b);cout<<endl; prime5(a);prime5(b);cout<<endl;
if(m==7) cout<<" | ";
if(m==8) cout<<"| | ";
if(m==9)
cout<<"| | "; return 0; } int prime3(int m) { if(m==0)
cout<<" "; if(m==1)
cout<<" "; if(m==2)
cout<<" -- "; if(m==3)
} cout<<"计算结果是:"<<endl; if(f<0) {
s=f*(-1); if(s>=0&&s<10) {
cout<<" ";prime1(s);cout<<endl; cout<<" ";prime1(s);cout<<endl; cout<<" -- ";prime1(s);cout<<endl; cout<<" ";prime1(s);cout<<endl; cout<<" ";prime1(s);cout<<endl; } if(s>=10&&s<100) { a=s/10; b=s%10;
cout<<" -- "; if(m==4)
cout<<" -- "; if(m==5)
cout<<" -- "; if(m==6)
cout<<" -- "; if(m==7)
cout<<" | "; if(m==8)
cout<<" -- "; if(m==9)
cout<<" -- "; return 0; } int prime4(int m) { if(m==0)
cout<<" ";prime1(a);prime1(b);cout<<endl; cout<<" ";prime2(a);prime2(b);cout<<endl; cout<<" -- ";prime3(a);prime3(b);cout<<endl; cout<<" ";prime4(a);prime4(b);cout<<endl; cout<<" ";prime5(a);prime5(b);cout<<endl; } if(s>=100&&s<1000) { a=s/100; b=(s/10)%10; c=s%10; cout<<" ";prime1(a);prime1(b);prime1(c);cout<<endl; cout<<" ";prime2(a);prime2(b);prime2(c);cout<<endl; cout<<" -- ";prime3(a);prime3(b);prime3(c);cout<<endl; cout<<" ";prime4(a);prime4(b);prime4(c);cout<<endl; cout<<" ";prime5(a);prime5(b);prime5(c);cout<<endl; } if(s>=1000&&s<10000) { a=s/1000; b=(s/100)%10; c=(s/10)%10; d=s%10; cout<<" ";prime1(a);prime1(b);prime1(c);prime1(d);cout<<endl; cout<<" ";prime2(a);prime2(b);prime2(c);prime2(d);cout<<endl; cout<<" ";prime3(a);prime3(b);prime3(c);prime3(d);cout<<endl; cout<<" ";prime4(a);prime4(b);prime4(c);prime4(d);cout<<endl; cout<<" ";prime5(a);prime5(b);prime5(c);prime5(d);cout<<endl; } } if(f>=0) { if(f>=0&&f<10) { prime1(f);cout<<endl; prime2(f);cout<<endl; prime3(f);cout<<endl; prime4(f);cout<<endl; prime5(f);cout<<endl; } if(f>=10&&f<100) { a=f/10; b=f%10;