C语言制作简单计算器
- 格式:doc
- 大小:45.50 KB
- 文档页数:4
一、实验题目:用visual studio .NET制作一个简单的计算器二、实验要求:该计算器能实现一般的加、减、乘、除的操作,能进行开方操作,有清除功能(另加求平方功能)。
三、代码实现:Form1.designer的代码:namespace计算器制作2005{partial class Form1{///<summary>///必需的设计器变量。
///</summary>private ponentModel.IContainer components = null;///<summary>///清理所有正在使用的资源。
///</summary>///<param name="disposing">如果应释放托管资源,为true;否则为false</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码?///<summary>///设计器支持所需的方法 - 不要///使用代码编辑-器修改此方法的内容。
///</summary>private void InitializeComponent(){this.show = new System.Windows.Forms.TextBox();this.groupBox1 = new System.Windows.Forms.GroupBox();this.dengyu = new System.Windows.Forms.Button();this.chu = new System.Windows.Forms.Button();this.point = new System.Windows.Forms.Button();this.plusMinus = new System.Windows.Forms.Button();this.num0 = new System.Windows.Forms.Button();this.sqr = new System.Windows.Forms.Button();this.cheng = new System.Windows.Forms.Button();this.num9 = new System.Windows.Forms.Button();this.num8 = new System.Windows.Forms.Button();this.num7 = new System.Windows.Forms.Button();this.pingfang = new System.Windows.Forms.Button();this.minus = new System.Windows.Forms.Button();this.num6 = new System.Windows.Forms.Button();this.num5 = new System.Windows.Forms.Button();this.num4 = new System.Windows.Forms.Button();this.clear = new System.Windows.Forms.Button();this.plus = new System.Windows.Forms.Button();this.num3 = new System.Windows.Forms.Button();this.num2 = new System.Windows.Forms.Button();this.num1 = new System.Windows.Forms.Button();this.menuStrip1 = new System.Windows.Forms.MenuStrip();this.editToolStripMenuItem = newSystem.Windows.Forms.ToolStripMenuItem();this.lookToolStripMenuItem = newSystem.Windows.Forms.ToolStripMenuItem();this.helpToolStripMenuItem = newSystem.Windows.Forms.ToolStripMenuItem();this.groupBox1.SuspendLayout();this.menuStrip1.SuspendLayout();this.SuspendLayout();//// show//this.show.Location = new System.Drawing.Point(19, 38); = "show";this.show.Size = new System.Drawing.Size(244, 21);this.show.TabIndex = 0;this.show.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;this.show.TextChanged += newSystem.EventHandler(this.show_TextChanged);//// groupBox1//this.groupBox1.Controls.Add(this.dengyu);this.groupBox1.Controls.Add(this.chu);this.groupBox1.Controls.Add(this.point);this.groupBox1.Controls.Add(this.plusMinus);this.groupBox1.Controls.Add(this.num0);this.groupBox1.Controls.Add(this.sqr);this.groupBox1.Controls.Add(this.cheng);this.groupBox1.Controls.Add(this.num9);this.groupBox1.Controls.Add(this.num8);this.groupBox1.Controls.Add(this.num7);this.groupBox1.Controls.Add(this.pingfang);this.groupBox1.Controls.Add(this.minus);this.groupBox1.Controls.Add(this.num6);this.groupBox1.Controls.Add(this.num5);this.groupBox1.Controls.Add(this.num4);this.groupBox1.Controls.Add(this.clear);this.groupBox1.Controls.Add(this.plus);this.groupBox1.Controls.Add(this.num3);this.groupBox1.Controls.Add(this.num2);this.groupBox1.Controls.Add(this.num1);this.groupBox1.Location = new System.Drawing.Point(19, 78); = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(244, 167);this.groupBox1.TabIndex = 1;this.groupBox1.TabStop = false;//// dengyu//this.dengyu.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.dengyu.ForeColor = System.Drawing.Color.Red;this.dengyu.Location = new System.Drawing.Point(188, 119); = "dengyu";this.dengyu.Size = new System.Drawing.Size(41, 28);this.dengyu.TabIndex = 23;this.dengyu.Text = "=";eVisualStyleBackColor = true;this.dengyu.Click += new System.EventHandler(this.dengyu_Click);//// chu//this.chu.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.chu.ForeColor = System.Drawing.Color.Red;this.chu.Location = new System.Drawing.Point(143, 119); = "chu";this.chu.Size = new System.Drawing.Size(41, 28);this.chu.TabIndex = 22;this.chu.Text = "/";eVisualStyleBackColor = true;this.chu.Click += new System.EventHandler(this.chu_Click);//// point//this.point.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.point.ForeColor = System.Drawing.Color.Blue;this.point.Location = new System.Drawing.Point(99, 119); = "point";this.point.Size = new System.Drawing.Size(41, 28);this.point.TabIndex = 21;this.point.Text = ".";eVisualStyleBackColor = true;this.point.Click += new System.EventHandler(this.point_Click);//// plusMinus//this.plusMinus.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.plusMinus.ForeColor = System.Drawing.Color.Red;this.plusMinus.Location = new System.Drawing.Point(55, 119); = "plusMinus";this.plusMinus.Size = new System.Drawing.Size(41, 28);this.plusMinus.TabIndex = 20;this.plusMinus.Text = "±¨¤";eVisualStyleBackColor = true;this.plusMinus.Click += new System.EventHandler(this.plusMinus_Click);//// num0//this.num0.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.num0.ForeColor = System.Drawing.Color.Blue;this.num0.Location = new System.Drawing.Point(11, 119); = "num0";this.num0.Size = new System.Drawing.Size(41, 28);this.num0.TabIndex = 19;this.num0.Text = "0";eVisualStyleBackColor = true;this.num0.Click += new System.EventHandler(this.num0_Click);//// sqr//this.sqr.Font = new System.Drawing.Font("宋体”, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.sqr.ForeColor = System.Drawing.Color.Red;this.sqr.Location = new System.Drawing.Point(188, 87); = "sqr";this.sqr.Size = new System.Drawing.Size(41, 28);this.sqr.TabIndex = 18;this.sqr.Text = "sqrt";eVisualStyleBackColor = true;this.sqr.Click += new System.EventHandler(this.sqr_Click);//// cheng//this.cheng.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.cheng.ForeColor = System.Drawing.Color.Red;this.cheng.Location = new System.Drawing.Point(143, 87); = "cheng";this.cheng.Size = new System.Drawing.Size(41, 28);this.cheng.TabIndex = 17;this.cheng.Text = "*";eVisualStyleBackColor = true;this.cheng.Click += new System.EventHandler(this.cheng_Click);//// num9//this.num9.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.num9.ForeColor = System.Drawing.Color.Blue;this.num9.Location = new System.Drawing.Point(99, 87); = "num9";this.num9.Size = new System.Drawing.Size(41, 28);this.num9.TabIndex = 16;this.num9.Text = "9";eVisualStyleBackColor = true;this.num9.Click += new System.EventHandler(this.num9_Click);//// num8//this.num8.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.num8.ForeColor = System.Drawing.Color.Blue;this.num8.Location = new System.Drawing.Point(55, 87); = "num8";this.num8.Size = new System.Drawing.Size(41, 28);this.num8.TabIndex = 15;this.num8.Text = "8";eVisualStyleBackColor = true;this.num8.Click += new System.EventHandler(this.num8_Click);//// num7//this.num7.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.num7.ForeColor = System.Drawing.Color.Blue;this.num7.Location = new System.Drawing.Point(11, 87); = "num7";this.num7.Size = new System.Drawing.Size(41, 28);this.num7.TabIndex = 14;this.num7.Text = "7";eVisualStyleBackColor = true;this.num7.Click += new System.EventHandler(this.num7_Click);//// pingfang//this.pingfang.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.pingfang.ForeColor = System.Drawing.Color.Red;this.pingfang.Location = new System.Drawing.Point(188, 55); = "pingfang";this.pingfang.Size = new System.Drawing.Size(41, 28);this.pingfang.TabIndex = 13;this.pingfang.Text = "X²";eVisualStyleBackColor = true;this.pingfang.Click += new System.EventHandler(this.pingfang_Click);//// minus//this.minus.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.minus.ForeColor = System.Drawing.Color.Red;this.minus.Location = new System.Drawing.Point(143, 55); = "minus";this.minus.Size = new System.Drawing.Size(41, 28);this.minus.TabIndex = 12;this.minus.Text = "-";eVisualStyleBackColor = true;this.minus.Click += new System.EventHandler(this.minus_Click);//// num6//this.num6.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.num6.ForeColor = System.Drawing.Color.Blue;this.num6.Location = new System.Drawing.Point(99, 55); = "num6";this.num6.Size = new System.Drawing.Size(41, 28);this.num6.TabIndex = 11;this.num6.Text = "6";eVisualStyleBackColor = true;this.num6.Click += new System.EventHandler(this.num6_Click);//// num5//this.num5.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.num5.ForeColor = System.Drawing.Color.Blue;this.num5.Location = new System.Drawing.Point(55, 55); = "num5";this.num5.Size = new System.Drawing.Size(41, 28);this.num5.TabIndex = 10;this.num5.Text = "5";eVisualStyleBackColor = true;this.num5.Click += new System.EventHandler(this.num5_Click);//// num4//this.num4.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134)));this.num4.ForeColor = System.Drawing.Color.Blue;this.num4.Location = new System.Drawing.Point(11, 55); = "num4";this.num4.Size = new System.Drawing.Size(41, 28);this.num4.TabIndex = 9;this.num4.Text = "4";eVisualStyleBackColor = true;this.num4.Click += new System.EventHandler(this.num4_Click);//// clear//this.clear.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.clear.ForeColor = System.Drawing.Color.Red;this.clear.Location = new System.Drawing.Point(188, 23); = "clear";this.clear.Size = new System.Drawing.Size(41, 28);this.clear.TabIndex = 8;this.clear.Text = "C";eVisualStyleBackColor = true;this.clear.Click += new System.EventHandler(this.clear_Click);//// plus//this.plus.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.plus.ForeColor = System.Drawing.Color.Red;this.plus.Location = new System.Drawing.Point(143, 23); = "plus";this.plus.Size = new System.Drawing.Size(41, 28);this.plus.TabIndex = 3;this.plus.Text = "+";eVisualStyleBackColor = true;this.plus.Click += new System.EventHandler(this.plus_Click);//// num3//this.num3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.num3.ForeColor = System.Drawing.Color.Blue;this.num3.Location = new System.Drawing.Point(99, 23); = "num3";this.num3.Size = new System.Drawing.Size(41, 28);this.num3.TabIndex = 2;this.num3.Text = "3";eVisualStyleBackColor = true;this.num3.Click += new System.EventHandler(this.num3_Click);//// num2//this.num2.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.num2.ForeColor = System.Drawing.Color.Blue;this.num2.Location = new System.Drawing.Point(55, 23); = "num2";this.num2.Size = new System.Drawing.Size(41, 28);this.num2.TabIndex = 1;this.num2.Text = "2";eVisualStyleBackColor = true;this.num2.Click += new System.EventHandler(this.num2_Click);//// num1//this.num1.Font = new System.Drawing.Font("宋体", 12F,System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.num1.ForeColor = System.Drawing.Color.Blue;this.num1.Location = new System.Drawing.Point(11, 23); = "num1";this.num1.Size = new System.Drawing.Size(41, 28);this.num1.TabIndex = 0;this.num1.Text = "1";eVisualStyleBackColor = true;this.num1.Click += new System.EventHandler(this.button1_Click);//// menuStrip1//this.menuStrip1.Items.AddRange(newSystem.Windows.Forms.ToolStripItem[] {this.editToolStripMenuItem,this.lookToolStripMenuItem,this.helpToolStripMenuItem});this.menuStrip1.Location = new System.Drawing.Point(0, 0); = "menuStrip1";this.menuStrip1.Size = new System.Drawing.Size(292, 24);this.menuStrip1.TabIndex = 2;this.menuStrip1.Text = "menuStrip1";//// editToolStripMenuItem// = "editToolStripMenuItem";this.editToolStripMenuItem.Size = new System.Drawing.Size(57, 20);this.editToolStripMenuItem.Text = "编辑(&E)";//// lookToolStripMenuItem// = "lookToolStripMenuItem";this.lookToolStripMenuItem.Size = new System.Drawing.Size(57, 20);this.lookToolStripMenuItem.Text = "查看(&F)";//// helpToolStripMenuItem// = "helpToolStripMenuItem";this.helpToolStripMenuItem.Size = new System.Drawing.Size(58, 20);this.helpToolStripMenuItem.Text = "帮助(&H)";//// Form1//this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(292, 273);this.Controls.Add(this.groupBox1);this.Controls.Add(this.show);this.Controls.Add(this.menuStrip1);this.MainMenuStrip = this.menuStrip1; = "Form1";this.Text = "软件09302刘志芳";this.groupBox1.ResumeLayout(false);this.menuStrip1.ResumeLayout(false);this.menuStrip1.PerformLayout();this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.TextBox show;private System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Button plus;private System.Windows.Forms.Button num3;private System.Windows.Forms.Button num2;private System.Windows.Forms.Button num1;private System.Windows.Forms.Button clear;private System.Windows.Forms.Button dengyu;private System.Windows.Forms.Button chu;private System.Windows.Forms.Button point;private System.Windows.Forms.Button plusMinus;private System.Windows.Forms.Button num0;private System.Windows.Forms.Button sqr;private System.Windows.Forms.Button cheng;private System.Windows.Forms.Button num9;private System.Windows.Forms.Button num8;private System.Windows.Forms.Button num7;private System.Windows.Forms.Button pingfang;private System.Windows.Forms.Button minus;private System.Windows.Forms.Button num6;private System.Windows.Forms.Button num5;private System.Windows.Forms.Button num4;private System.Windows.Forms.MenuStrip menuStrip1;private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem lookToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem; }}Form1.cs的代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace 计算器制作2005{public partial class Form1 : Form{double a = 0; //放第一个操作数double b=0; //放第二个操作数double sum=0; //最终结果int sign; //操作符public Form1(){InitializeComponent();}/*主要代码实现,对于点击按钮1、2、3 …… 0以及各种运算符的操作( +、-、*、/、* sqrt、X²),还有结果的得出,另外还有针对错误操作的一些处理,例如:当进行除* 法运算时,输入的除数为0,此时系统会有消息框弹出,提示:除数不能为0。
用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 = width /2; /*设置x的坐标值*/y = height/2; /*设置y的坐标值*/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); /*设置颜色为红色*/outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/x =2*width-width/2; /*设置x的坐标值*/y =2*height+height/2; /*设置y的坐标值*/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;strcpy(str2,""); /*设置str2为空串*/while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/ {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;} /*如果移到头,再左移,则移动到最右边字符位置*/ 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=str1[n*5+m]; /*将字符保存到变量c中*/if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/{if(flag==-1) /*如果标志为-1,表明为负数*/{strcpy(str2,"-"); /*将负号连接到字符串中*/flag=1;} /*将标志值恢复为1*/sprintf(temp,"%c",c); /*将字符保存到字符串变量temp中*/ strcat(str2,temp); /*将temp中的字符串连接到str2中*/ 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); /*将第一个操作数转换为浮点数*/strcpy(str2,""); /*将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=='-'){if(strcmp(str2,"")==0) /*如果str2为空,说明是负号,而不是减号*/flag=-1; /*设置负数标志*/else{num1=atof(str2); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将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); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将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); /*覆盖结果区*/ sprintf(temp,"%f",result); /*将结果保存到temp中*/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."); /*显示字符串*/}if(c=='Q')exit(0); /*如果选择了q回车,结束计算程序*/}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 );/*设置画线方式*/ getviewsettings( &vp );/*将当前视口信息装入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); /*键盘输入*/key=key&0xff? key&0xff:key>>8; /*只取特殊键的扫描值,其余为0*/ return(key); /*返回键值*/}。
物品折旧计算器C语言总结此类问题都要使用循环,要注意根据问题确定循环变量的初值、终值或结束条件,更要注意用来表示计数、和、阶乘的变量的初值。
一般情况下,计数初值为0、求和初值为0、阶乘初值为1。
例1:用随机函数产生100个[0,99]范围内的随机整数,统计个位上的数字分别为1,2,3,4,5,6,7,8,9,0的数的个数并打印出来。
本题使用数组来处理,用数组a[100]存放产生的确100个随机整数,数组x[10]来存放个位上的数字分别为1,2,3,4,5,6,7,8,9,0的数的个数。
即个位是1的个数存放在x[1]中,个位是2的个数存放在x[2]中,……个位是0的个数存放在x[10]。
void main(){int a[101],x[11],i,p;for(i=0;i<=11;i++)x[i]=0;for(i=1;i<=100;i++){a[i]=rand()%100;printf("%4d",a[i]);if(i%10==0)printf("\n");}for(i=1;i<=100;i++){p=a[i]%10;if(p==0)p=10;x[p]=x[p]+1;}for(i=1;i<=10;i++){p=i;if(i==10)p=0;printf("%d,%d\n",p,x[i]);}printf("\n");}采用举穷法对满足条件的数据进行处理。
例:求1到100范围内能被2或3整除的数之和。
Main(){int I,s=0;For(i=1;i<=100;i++){If(i%2==0||i%3==0)S=s+I;}Printf(“%d”,s);}例:求N的阶乘。
Main(){int I;s=1,n;;Scanf(“%d”,&n);For(i=1;i<=n;i++)S=s*I;Printf(“%d”,s);}判断素数(穷举法)。
简易计算器的制作在本文中,我们将介绍如何使用Visual FoxPro(VFP)制作一个简易计算器。
Visual FoxPro是一种开发工具,可以用于创建数据库应用程序和开发Windows桌面应用程序。
步骤1: 创建新的Visual FoxPro项目首先,打开Visual FoxPro并创建一个新的项目。
在“文件”菜单中选择“新建”,然后选择“项目”。
输入一个项目名称,选择一个合适的位置,然后点击“确定”。
步骤2:创建一个新的表单接下来,我们需要创建一个新的表单来作为计算器的界面。
在项目资源管理器中,右键点击“表单”文件夹,并选择“新建”。
输入一个表单名称,然后点击“确定”。
步骤3:添加控件步骤5:添加按钮事件首先,我们需要声明变量来存储用户输入的数字。
在按钮事件代码中,添加以下代码:```foxproLOCAL num1, num2, result```接下来,我们需要获取用户输入的数字。
在按钮事件代码中,添加以下代码:```foxpronum1 = VAL(Thisform.txtNum1.Value)num2 = VAL(Thisform.txtNum2.Value)```然后,我们可以执行所需的计算操作。
例如,如果我们想要添加两个数字,我们可以使用以下代码:```foxproresult = num1 + num2```最后,我们需要将结果显示在界面上。
在按钮事件代码中,添加以下代码:```foxproThisform.lblResult.Caption = STR(result)```步骤6:运行和测试完成上述步骤后,您可以保存并运行计算器。
在VFP的工具栏中,点击“运行”按钮来启动应用程序。
测试计算器,确保它可以正确执行计算操作并显示结果。
结论通过使用Visual FoxPro,我们可以轻松创建一个简易计算器。
这个计算器可以执行基本的加减乘除等操作,同时提供了一个直观的用户界面。
您可以根据需要进行自定义,添加更多功能和复杂的计算操作。
设计题目:计算器系统实现1.分别用API与MFC编程来实现计算器的以下功能。
2.实现计算器的基本功能:连续数据的无优先级混合运算(加减乘除)3.可以实现其他附加功能:优先级运算,加入括号,加入其他函数运算功能等。
(不在要求范围之内)4.要求界面良好,功能完整。
一.基于MFC的简单计算器1.设计思路打开MFC应用操作界面,布局计算器界面,利用组框将计算器界面分为三个部分,一个是编辑输入,一个是数字界面,一个是功能键部分。
利用布局参考线对齐按钮,使界面美观。
然后就是对各个按钮进行属性设置,关联类设置,接着对各个按钮进行源代码编程。
最后,调试找出问题,解决问题,运行MFC成品计算器。
2.简单操作以及功能说明由于本人技术有限,所以本程序只能按照正确的计算运算顺序进行,该简单计算器能进行四则混合运算,除了加减乘除外,添加了一个括号,对于有些未知的错误,由于时间有限,并未来得及全面测试使用。
该简单计算器能实现四则运算,退格运算,清零运算,并且支持输出的结果保留给直接的下一个运算。
3.系统实现的各个模块1)编辑框模块由于编辑框模块需要连续输入字符串,所以在给编辑框建立类向导时,给编辑框定义的成员变量应该是字符串(String)变量。
编辑框模块用于运算算式的输入,以及结果的输出。
2)数字键模块数字键模块比较简单,该简单计算器数字键模块设置有0~9数字,还设置有小数点,以及正负数转换实现按钮。
数字键模块按钮在建立类向导时无需定义成员变量,但是需要定义按钮响应链接,用于实现点击按钮,在编辑框上显示点击按钮信息。
3)功能键模块功能键模块包括加减乘除基本按钮,以及一个输入错误是后能用于退格功能的退格键按钮,还有一个用于区别优先级运算的括号功能,当然有最重要的计算结果输入按钮,等于号按钮。
在编辑框附近还有一个功能键,就是清除功能键,用于清除编辑框,以便实现下一轮输入。
4.设计过程1)设计总流程图2)界面的设计3)建立的变量,控件的命名,对应的消息处理函数对应表ID CAPTION MessageHandler IDD_JISUANQI_DIALOG 简易计算器N/AIDC_NUM0 0 OnNum0IDC_NUM1 1 OnNum1IDC_NUM2 2 OnNum2IDC_NUM3 3 OnNum3IDC_NUM4 4 OnNum4IDC_NUM5 5 OnNum5IDC_NUM6 6 OnNum6IDC_NUM7 7 OnNum7IDC_NUM8 8 OnNum8IDC_NUM9 9 OnNum9IDC_OPER_ADD + OnOperAdd IDC_OPER_SUB - OnOperSub IDC_OPER_MULTI * OnOperMulti IDC_OPER_DIV / OnOperDiv IDC_ADD_SUB +/- OnAddSubIDC_POINT . OnPointIDC_EQUAL = OnEqualIDC_LBRACKET ( OnLbracket1)数字键模块void CJiSuanQiDlg::OnNum0(){if(calculated == TRUE) //已经按了等号,不让其再接受字符return;UpdateData(TRUE);//刷新编辑框界面calcutateString += "0";//存储输入的数字m_data += "0";//显示输入的数字UpdateData(FALSE);}2)功能键模块①加法功能键源程序void CJiSuanQiDlg::OnOperAdd(){if(calculated == TRUE)///其前一步按了= 号{calculated = FALSE;m_data = oldResult;double temp = atof(oldResult);//定义临时变量存储上一步结果if(temp < 0){calcutateString = "0" + oldResult;}else{calcutateString = oldResult;}calcutateString += "+";//存储做完该功能后的结果m_data += "+";//显示该步骤完成的结果UpdateData(FALSE);}else ///前一步不是={UpdateData(TRUE);calcutateString += "+";//直接存储此步操作m_data += "+";//直接显示此步操作UpdateData(FALSE);}}②退格功能键源程序void CJiSuanQiDlg::OnBackspeace(){if(calculated == FALSE){UpdateData(TRUE);int count = m_data.GetLength();if(count >= 1)//判断是否能执行退格操作{m_data = m_data.Left(count -1);count = calcutateString.GetLength();calcutateString = calcutateString.Left(count -1);UpdateData(FALSE);}}}③清除功能键源程序void CJiSuanQiDlg::OnClear(){// TODO: Add your control notification handler code herem_data = "";oldResult = "";calcutateString = "";calculated = FALSE;UpdateData(FALSE);}④括号功能键源程序void CJiSuanQiDlg::OnLbracket(){// TODO: Add your control notification handler code hereif(calculated == TRUE) ///已经按了等号,不让其再接受字符return;UpdateData(TRUE);calcutateString += "(";m_data += "(";UpdateData(FALSE);}⑤正负转换功能键源程序void CJiSuanQiDlg::OnAddSub(){// TODO: Add your control notification handler code hereif(calculated == FALSE){UpdateData(TRUE);calcutateString = calcutateString + "0" + "-";m_data += "-";UpdateData(FALSE);}}6.运行结果界面截图7.制作过程中所遇问题以及解决方法过程1)源代码编写错误通过查阅资料,请教同学,逐步一一解决。
编写一个简单的计算器程序计算器程序是一种非常实用的工具,它可以帮助我们进行数学计算,并简化复杂的运算过程。
本文将介绍如何编写一个简单的计算器程序,实现基本的加减乘除运算。
首先,我们需要确定计算器程序的功能和界面设计。
在本文中,我们将使用Python编程语言来编写计算器程序,并使用命令行界面(CLI)进行交互。
这意味着我们将在终端窗口中输入表达式,并显示结果。
接下来,我们需要考虑计算器程序的基本运算功能。
一个简单的计算器需要实现四个基本的运算:加法、减法、乘法和除法。
我们将使用函数来实现每个运算功能。
以下是一个示例代码:```pythondef add(x, y):return x + ydef subtract(x, y):return x - ydef multiply(x, y):return x * ydef divide(x, y):return x / y```在这个示例代码中,我们定义了四个函数,每个函数接受两个参数,并返回计算结果。
接下来,我们需要处理输入表达式并调用相应的运算函数。
我们将使用一个循环来持续接收用户输入,并在用户输入“exit”时退出程序。
以下是一个示例代码:```pythonwhile True:expression = input("请输入一个表达式:")if expression == "exit":break#解析表达式,提取运算符和操作数operator = Nonefor op in ["+", "-", "*", "/"]:if op in expression:operator = opbreakif not operator:print("表达式错误,请重新输入!") continueoperands = expression.split(operator) x = float(operands[0])y = float(operands[1])if operator == "+":result = add(x, y)elif operator == "-":result = subtract(x, y)elif operator == "*":result = multiply(x, y)elif operator == "/":result = divide(x, y)print("运算结果:", result)print("谢谢使用,再见!")```在这个示例代码中,我们使用了一个无限循环来持续接收用户输入。
编程题#4:简单计算器描述⼀个最简单的计算器,⽀持+, -, *, / 四种运算。
仅需考虑输⼊输出为整数的情况,数据和运算结果不会超过int表⽰的范围。
输⼊输⼊只有⼀⾏,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。
输出输出只有⼀⾏,⼀个整数,为运算结果。
然⽽:1. 如果出现除数为0的情况,则输出:Divided by zero!2. 如果出现⽆效的操作符(即不为 +, -, *, / 之⼀),则输出:Invalid operator!#include <iostream>using namespace std;int main() {int a, b;char c;cin >> a >> b >> c;switch(c) {case'+': cout << a + b;break;case'-': cout << a - b;break;case'*': cout << a * b;break;case'/': if (b == 0) {cout << "Divided by zero!";break;}else{cout << a / b;break;}default: cout << "Invalid operator!";}cout << endl;return0;}。
先建立一个界面化Csharp程序,如图 各个按钮和属性需要对应才可以运行! 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace 简单计算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); groupBox1.Text = "计算:"; groupBox2.Text = "操作"; label1.Text = "计算表达式:"; label4.Text = ""; button1.Text = "4"; button2.Text = "6"; button3.Text = "5"; button4.Text = "8"; button5.Text = "9"; button6.Text = "1"; button7.Text = " + "; button8.Text = "清除"; button9.Text = "7"; button10.Text = "0"; button11.Text = "3"; button12.Text = "2"; button13.Text = " = "; } float temp1 = -1, temp2 = -1; string str = ""; public void addnum(int num) { label4.Text = label4.Text + num.ToString(); str += num.ToString(); }
C语言制作简单计算器
一、项目介绍
我们要用c语言做一个简单的计算器,进行加、减、乘、除操作。
本程序涉及的所有数学知识都很简单,但输入过程会增加复杂性。
我们需要检查输入,确保用户没有要求计算机完成不可能的任务。
还必须允许用户一次输入一个计算式,例如:32.4+32 或者9*3.2
项目效果图
编写这个程序的步骤如下:
∙获得用户要求计算机执行计算所需的输入。
∙检查输入,确保输入可以理解。
∙执行计算。
∙显示结果。
三、解决方案
1.步骤1
获得用户输入是很简单的,可以使用printf()和scanf()。
下面是读取用户输入的程序代码:
#include<stdio.h>int main(){
double number1=0.0; //定义第一个操作值
double number2=0.0; //定义第二个操作值
char operation=0; //operation必须是'+''-''*''/'或'%'
printf("\nEnter the calculation\n");
scanf("%lf%c%lf",&number1,&operation,&number2);
return0;
}
2.步骤2
接着,检查输入是否正确。
最明显的检查是要执行的操作是否有效。
有效的操作有+、-、*、/和%,所以需要检查输入的操作是否是其中的一个。
还需要检查第二个数字,如果操作是/或者%,第二个数字就不能是0。
如果右操作数是0,这些操作就是无效的。
这些操作都可以用if语句来完成,switch语句则为此提供了一种更好的方式,因此它比一系列if语句更容易理解。
switch(operation)
{
case'+':
printf("=%lf\n",number1+number2);
break;
case'-':
printf("=%lf\n",number1-number2);
break;
case'*':
printf("=%lf\n",number1*number2);
break;
case'/':
if(number2==0)
printf("\n\n\aDavision by zero error!\n");
else
printf("=%lf\n",number1/number2);
break;
if((long)number2==0)
printf("\n\n\aDavision by zero error!\n");
else
printf("=%ld\n",(long)number1%(long)number2);
break;
default:
printf("\n\n\aDavision by zero error!\n");
break;
取余运算符对float或double类型是没有意义的,因为他们可以表示精确的结果。
只有把%运算符应用于整数类型才有意义。
因此在应用这个运算符前,把操作数转换为整数。
当运算符是%时,将第二个操作数转换为一个整数,所以仅仅检查第二个操作数是否为0是不够的,还必须检查number2在转换为long时,值是否为0.例如0.5不是0,但是转换为整数时就是0。
四、程序的完整代码
#include<stdio.h>int main(){
double number1=0.0;
double number2=0.0;
char operation=0;
printf("\nEnter the calculation\n");
scanf("%lf%c%lf",&number1,&operation,&number2);
switch(operation)
{
case'+':
printf("=%lf\n",number1+number2);
break;
case'-':
printf("=%lf\n",number1-number2);
break;
case'*':
printf("=%lf\n",number1*number2);
break;
case'/':
if(number2==0)
printf("\n\n\aDavision by zero error!\n");
printf("=%lf\n",number1/number2);
break;
case'%':
if((long)number2==0)
printf("\n\n\aDavision by zero error!\n");
else
printf("=%ld\n",(long)number1%(long)number2);
break;
default:
printf("\n\n\aDavision by zero error!\n");
break;
}
return0;
}
如果有疑问或者想学习更多基础课和项目课的话,欢迎登陆实验楼官方网站:。