C语言计算器源码
简易计算器程序源代码

简易计算器程序源代码下面是一个简易计算器程序的源代码,它可以执行基本的四则运算:```python#定义加法函数def add(x, y):return x + y#定义减法函数def subtract(x, y):return x - y#定义乘法函数def multiply(x, y):return x * y#定义除法函数def divide(x, y):if y == 0:return "除数不能为0"else:return x / y#显示菜单print("选择操作:")print("1. 相加")print("2. 相减")print("3. 相乘")print("4. 相除")#获取用户输入choice = input("输入你的选择(1/2/3/4): ")#获取用户输入的两个数字num1 = float(input("输入第一个数字: "))num2 = float(input("输入第二个数字: "))#根据用户选择执行相应操作if choice == '1':print(num1, "+", num2, "=", add(num1, num2))elif choice == '2':print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3':print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4':print(num1, "/", num2, "=", divide(num1, num2))else:print("请输入有效的选择")```运行这个程序,你将看到一个简易的计算器菜单。
y用C#制作计算器(实训的题目)

一、实验题目:用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语言程序:求常用圆形体的体积

double r,h;
printf("请输入圆锥的底圆半径和高:");
scanf("%lf%lf",&r,&h);
return (PI*r*r*h/3.0);
}
我们一起的痕迹
break;
case 2:
printf("圆柱体积为:%.2f\n",vol_cylind());
break;
case 3:
printf("圆锥体积为:%.2f\n",vol_cone());
break;
}
}
double vol_ball()
{
double r;
printf("请输入球的半径:");
{
int sel;
while(1){
printf("1-计算球体体积\n");
printf("2-计算圆柱体积\n");
printf("3-计算圆锥体积\n");
printf("其他-退出程序运行\n");
printf("请输入计算命令:");
scanf("%d",&sel);
if(sel<1||sel>3)
break;
else
cal(sel);
}
return 0;
}
void cal(int sel)
{
double vol_ball(void);
double vol_cylind(void);
double vol_cone(void);
switch(sel){
MFC计算器实现步骤和代码

MFC计算机程序步骤:1. 在进入VC++的第一个界面下,选择MFC AppWizard(exe),并设置工程名机器所在位置。
2. 在MFC AppWizard—step1 中选中Dialog based 选项,建立一个基于对话框的程序框架。
3. 使用资源编辑器建立对话框4. 编辑其中的各个控件的属性5. 插入菜单资源并编辑各项属性各属性为:Caption + * / ClearID ID_ADD_MENU ID_DIFFERENCE _MENU ID_MULTIPL Y_MENU ID_DEVIDE_MENU ID_CLEAR_MENUAbout ExitID_ABOUT_MENU ID_EXIT_MENU6. 添加代码使用Class Wizard 给编辑框连接变量(1)实现基本的加、减、乘、除的代码的添加。
(2)添加与菜单相关联的代码(3)填添加与滚动条相关联的代码在MFC AppWized(exe)项目下做。
界面自己做1.在对话框的头文件CalculatorDlg.h中添加#include<math.h>2.为CCalculatorDlg类添加成员数据和成员函数double number1,number2;int NumberState,OperationState;void cal();并在CCalculatorDlg类的构造函数中增加NumberState=1;3.添加消息按钮afx_msg void OnNumberKey(UINT nID);afx_msg void OnOperationKey(UINT nID);4.在CalculatorDlg.cpp文件中BEGIN_MESSAGE_MAP(CMy1Dlg, CDialog)和END_MESSAGE_MAP()间添加代码ON_COMMAND_RANGE(IDC_NUMBER1,IDC_NUMBER10,OnNumberKey)ON_COMMAND_RANGE(IDC_NUMBER11,IDC_NUMBER20,OnOperationKey)5.为成员函数OnNumberKey和OnOperationKey添加代码void CCalculatorDlg::OnNumberKey(UINT nID){int n=0;switch(nID){case IDC_NUMBER1:n=1;break;case IDC_NUMBER2:n=2;break;case IDC_NUMBER3:n=3;break;case IDC_NUMBER4:n=4;break;case IDC_NUMBER5:n=5;break;case IDC_NUMBER6:n=6;break;case IDC_NUMBER7:n=7;break;case IDC_NUMBER8:n=8;break;case IDC_NUMBER9:n=9;break;case IDC_NUMBER10:n=0;break;}if(NumberState==1){m_result=m_result*10+n;number1=m_result;UpdateData(FALSE); // 更新编辑框中的值}else{m_result=m_result*10+n;number2=m_result;UpdateData(FALSE);}}void CCalculatorDlg::OnOperationKey(UINT nID){switch(nID){case IDC_NUMBER13: // "/"按钮OperationState=1;UpdateData(FALSE);m_result=0;NumberState=2;break;case IDC_NUMBER14: // "*"按钮OperationState=2;UpdateData(FALSE);m_result=0;NumberState=2;break;case IDC_NUMBER15: // "+"按钮OperationState=3;UpdateData(FALSE);m_result=0;NumberState=2;break;case IDC_NUMBER16: // "-"按钮OperationState=4;UpdateData(FALSE);m_result=0;NumberState=2;break;case IDC_NUMBER17: // "C"按钮,撤消用,不需要可以删除number1=number2=m_result=0;UpdateData(FALSE);NumberState=1;break;case IDC_NUMBER20: // "="按钮cal(); // 调用cal成员函数break;}}6.为成员函数cal()添加代码void CCalculatorDlg::cal(){switch(OperationState){case 1:m_result=(double)number1/number2;UpdateData(FALSE); // 更新编辑框中的结果number1=m_result; // 把此次的运算结果作为下一次运算的第一个操作数NumberState=2; // 下次输入的数作为第二个操作数break;case 2:m_result=number1*number2;UpdateData(FALSE); // 更新编辑框中的结果number1=m_result;NumberState=2;break;case 3:m_result=number1+number2;UpdateData(FALSE); // 更新编辑框中的结果number1=m_result;NumberState=2;break;case 4:m_result=number1-number2;UpdateData(FALSE); // 更新编辑框中的结果number1=m_result;NumberState=2;break;}OperationState=0;} 注意按钮的ID号要和程序中的ID号相同!!!!。
可视化编程课程设计(含源码)计算器系统实现

设计题目:计算器系统实现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)源代码编写错误通过查阅资料,请教同学,逐步一一解决。
CRC16校验C语言程序源码(附完整的可执行的C语言代码)

CRC16校验C 语言程序源码 (附完整的可执行的 C 语言代码)//CRC16校验在通讯中应用广泛,这里不对其理论进行讨论,只对常见的 2种//实现方法进行测试。
方法一:查表法(256长度的校验表)速度快,准确,但是对于单片机设备存储占用大,且校验表长度大,输入时容易岀现错误 // .................. --POPULAR POLYNOMIALS ....................// CCITT: x A 16 + x A 12 + x A 5 + x A 0(0x1021) // CRC-16: x A 16 + xA15 + xA2 + xA0(0x8005)#defi neCRC_16_POLYNOMIALS0x8005const BYTE chCRCHTalbe[]=// CRC 高位字节值表{0x00, 0xC1, 0x81, 0x40, 0x01, OxCO, 0x80, 0x41, 0x01, OxCO, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1,0x81,0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1,0x81,0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41,0x00, 0xC1,0x81,0x40,0x01, 0xC0, 0x80, 0x41,0x01,0xC0, 0x80, 0x41, 0x00, 0xC1,0x81,0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1,0x81,0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,0x01,0xC0, 0x80, 0x41,0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01,0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1,0x81,0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01,0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40 };con st BYTE chCRCLTalbe[] = // CRC 低位字节值表 {0x00, 0xC0, 0xC1,0x01,0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1代 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, OxD1, OxDO, 0x10, OxFO, 0x30, 0x31, OxF1,0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61,0xA1,0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,0x41, 0x81, 0x80, 0x40};WORD CRC16_1(BYTE* pchMsg, WORD wDataLe n){BYTE chCRCHi = 0xFF; // 高CRC字节初始化BYTE chCRCLo = 0xFF; // 低CRC字节初始化WORD win dex; // CRC 循环中的索引while (wDataLe n--){// 计算CRCwin dex = chCRCLo A *pchMsg++ ;chCRCLo = chCRCHi A chCRCHTalbe[wl ndex]; chCRCHi =chCRCLTalbe[wi ndex];}return ((chCRCHi << 8) | chCRCLo);}方法一:列表法(简单表)con st WORD wCRCTalbeAbs[]={0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800,0xB401,0x5000, 0x9C01,0x8801,0x4400,};WORD CRC16_2(BYTE* pchMsg, WORD wDataLe n){WORD wCRC = OxFFFF;WORD i;BYTE chChar;for (i = 0; i < wDataLe n; i++){chChar = *pchMsg++;wCRC = wCRCTalbeAbs[(chChar A wCRC)& 15] A (wCRC>> 4);wCRC = wCRCTalbeAbs[((chChar >> 4) A wCRC) & 15] A (wCRC >> 4);}return wCRC;}方法二:定义法根据CRC16/MODBUS理直接计算,算法简单但对单片机计算压力大。
c语言五则运算代码
c语言五则运算代码C语言是一种广泛应用于计算机编程的编程语言,它具有灵活、高效的特点,常被用于进行各种数学计算和表达式求值。
本文将围绕C语言的五则运算(加法、减法、乘法、除法、取余)展开,探讨其在实际编程中的应用。
一、加法运算加法运算是最基本的数学运算之一,在C语言中使用加号(+)来表示。
在实际编程中,加法运算常用于计算两个数的和。
例如,我们可以编写一个程序,实现输入两个数并计算它们的和:```c#include <stdio.h>int main() {int a, b, sum;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);sum = a + b;printf("它们的和为:%d\n", sum);return 0;}```二、减法运算减法运算是计算两个数之差的操作,在C语言中使用减号(-)来表示。
我们可以编写一个程序,实现输入两个数并计算它们的差:```c#include <stdio.h>int main() {int a, b, difference;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);difference = a - b;printf("它们的差为:%d\n", difference);return 0;}```三、乘法运算乘法运算是计算两个数的积的操作,在C语言中使用星号(*)来表示。
我们可以编写一个程序,实现输入两个数并计算它们的积:```c#include <stdio.h>int main() {int a, b, product;printf("请输入两个整数:\n");scanf("%d %d", &a, &b);product = a * b;printf("它们的积为:%d\n", product);return 0;}```四、除法运算除法运算是计算两个数的商的操作,在C语言中使用斜杠(/)来表示。
编写一个简单的计算器程序
编写一个简单的计算器程序计算器程序是一种非常实用的工具,它可以帮助我们进行数学计算,并简化复杂的运算过程。
本文将介绍如何编写一个简单的计算器程序,实现基本的加减乘除运算。
首先,我们需要确定计算器程序的功能和界面设计。
在本文中,我们将使用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("谢谢使用,再见!")```在这个示例代码中,我们使用了一个无限循环来持续接收用户输入。
C语言,简单计算机代码
{
case 1:
ret = jia(x, y);
printf("%d+%d = %d\n",x,y,ret);
break;
case 2:
ret = jian(x, y);
printf("%d-%d = %d\n",x,y,ret);
break;
case 3:
ret = cheng(x, y);
}
int cheng(int a, int b) //乘
{
return a * b;
}
int chu(int a, int b) //除
{
return a / b;
}
void menu()
{
printf(" @@@ 菜单 @@@\n");
printf("***** 1.加减 2.十进制转换 3.三角函数 *****\n");
printf("%0.5f",a);
break;
case 6:
a=atan(n*p/180); //角度转化为弧度
printf("%0.5f",a);
break;
}
}
system("pause");//暂停函数
system("cls");//清屏函数
}
}
{
printf("输入度数");
scanf("%d", &n);
}Hale Waihona Puke elseprintf("输入错误");
switch (input)
编程题#4:简单计算器
编程题#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;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
main()
{
int a,x,y;
int func_1(int x,int y);
int func_2(int x,int y);
int func_3(int x,int y);
int func_4(int x,int y);
int func_5(int x, int y);
long func_6(int x);
long func_7(int x);
long func_8(int x);
void func_9();
func_9();
printf("\n请选择你要进入的计算类型:");
scanf("%d",&a);
switch(a)
{
case 1:func_1(x,y);break;
case 2:func_2(x,y);break;
case 3:func_3(x,y);break;
case 4:func_4(x,y);break;
case 5:func_5(x,y);break;
case 6:func_6(x);break;
case 7:func_7(x);break;
case 8:func_8(x);break;
case 9:printf("欢迎下次使用!");break;
default:printf("请输入正确的数字!");
}
}
/*加法*/
int func_1(int x,int y)
{
printf("请输入求x的值:\n");
scanf("%d",&x);
printf("请输入求y的值:\n");
scanf("%d",&y);
printf("x+y=%d\n",x+y);
}
/*减法*/
int func_2(int x,int y)
{
printf("请输入求x的值:");
scanf("%d",&x);
printf("请输入求y的值:");
scanf("%d",&y);
printf("x-y=%d\n",x-y);
}
/*乘法*/
int func_3(int x,int y)
{
printf("请输入求x的值:");
scanf("%d",&x);
printf("请输入求y的值:");
scanf("%d",&y);
printf("x4*y=%d\n",x*y);
}
/*除法*/
int func_4(int x,int y)
{
printf("请输入求x的值:");
scanf("%d",&x);
printf("请输入求y的值:");
scanf("%d",&y);
printf("x/y=%d\n",x/y);
}
/*求余*/
int func_5(int x, int y)
{
printf("请输入求x的值:");
scanf("%d",&x);
printf("请输入求y的值:");
scanf("%d",&y);
printf("x%y=%d\n",x%y);
#include
main()
{
int a, x, y;
int func_1(int x, int y);
int func_2(int x, int y);
int func_3(int x, int y);
int func_4(int x, int y);
int func_5(int x, int y);
long func_6(int x);
long func_7(int x);
long func_8(int x);
void func_9();
func_9();
printf("\n请选择你要进入的计算类型:");
scanf("%d", &a);
switch (a)
{
case 1:func_1(x, y); break;
case 2:func_2(x, y); break;
case 3:func_3(x, y); break;
case 4:func_4(x, y); break;
case 5:func_5(x, y); break;
case 6:func_6(x); break;
case 7:func_7(x); break;
case 8:func_8(x); break;
case 9:printf("欢迎下次使用!"); break;
default:printf("请输入正确的数字!");
}
}
/*加法*/
int func_1(int x, int y)
{
printf("请输入求x的值:\n");
scanf("%d", &x);
printf("请输入求y的值:\n");
scanf("%d", &y);
printf("x+y=%d\n", x + y);
}
/*减法*/
int func_2(int x, int y)
{
printf("请输入求x的值:");
scanf("%d", &x);
printf("请输入求y的值:");
scanf("%d", &y);
printf("x-y=%d\n", x - y);
}
/*乘法*/
int func_3(int x, int y)
{
printf("请输入求x的值:");
scanf("%d", &x);
printf("请输入求y的值:");
scanf("%d", &y);
printf("x4*y=%d\n", x*y);
}
/*除法*/
int func_4(int x, int y)
{
printf("请输入求x的值:");
scanf("%d", &x);
printf("请输入求y的值:");
scanf("%d", &y);
printf("x/y=%d\n", x / y);
}
/*求余*/
int func_5(int x, int y)
{
printf("请输入求x的值:");
scanf("%d", &x);
printf("请输入求y的值:");
scanf("%d", &y);
printf("x%y=%d\n", x%y);
}
/*求x!*/
long func_6(int x)
{
int f = 1;
printf("请输入x!中的x值 :");
scanf("%d", &x);
for (; x>0; x--)
f = f*x;
printf("x!=%d\n", f);
}
/*十进制转换二进制*/
long func_7(int x)
{
int a[30], i = 0, rem;
printf("请输入一个十进制的正整数:");
scanf("%d\n", &x);
do{
rem = x % 2; x = x / 2;
a[i] = rem;
i++;
} while (x != 0);
while (i>0)
printf("%d", a[--i]);
printf("二进制为:%d\n", rem);
}
/*二进制转换十进制*/
long func_8(int x)
{
int b[10], i = 0, c = 0;
printf("请输入一个只有0或1 组成的二进制:");
scanf("%d", &x);
while (x != 0)
{
b[i] = x % 10;
x /= 10;
i++;
}
i--;
for (; i >= 0; i--)
c = c * 2 + b[i];
printf("十进制为:%d\n", c);
}
/*界面*/
void func_9()
{
printf("1.加法\t\t2.减法\n");
printf("3.乘法\t\t4.除法\n");
printf("5.求余\t\t6.求X!\n");
printf("7.将十进制转换二进制\n");
printf("8.将二进制转换十进制\n");
}