用visual_studio制作的简单的计算器。
- 格式:doc
- 大小:378.50 KB
- 文档页数:14
《可视化编程技术课程设计》报告题目仿照Windows系统计算器设计计算器1.引言计算器的产生和发展是建立在电子计算机基础之上的,现代社会很需要一个健全、高效率的计算器。
为了提高自己的实践能力和将来开发一个更实用更全能更智能的计算器,以设计与实现计算器为课题。
此次设计的计算器应用软件从visual studio 2012编程环境中开发,是一个简单的窗体应用程序,实现简单的计算器功能。
以微软自带的计算器为模板,设计了简单易懂的计算器。
这款计算器不仅实现了简单的四则运算的功能,还可以进行复杂数学运算,和进制转换(十进制转二进制、八进制、十六进制)。
具有简洁的外观,即时准确的获得所需要要计算的结果,充分降低了数字计算器的难度和所需要的时间,对人们生活和学习具有有很大的帮助。
2. 系统设计(1)功能设计本计算器由四大模块,分别为:1.标准计算器(即为主窗口)2.科学型计算器3.程序员计算器(进制转换)4.开发者介绍(2)界面设计主窗口界面(from1)科学型计算器界面(from2)程序员界面(from3)开发者介绍(from4)1.文本框的设置:textBox1.Text = "0";窗口启动前:Form1.x = 0;//point:1 ;nopoint: 0Form1.a = 0;//1:+ 2:- 3:* 4:/Form1.b = 0;//判D断?是否执行过运算执行过运算Form1.shu1 = 0;Form1.shu2 = 0;Form1.result = 0;2.按键的设置:数字键及小数点:public static void number(object sender, TextBox textBox1){Button btn = (Button)sender;if ( == "one"){if (x == 1)//判断有小数点Ì{textBox1.Text = textBox1.Text + '1';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 1";elsetextBox1.Text = (textBox1.Text + '1').ToString(); }if ( == "two"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '2';return;}if (b != 0){textBox1.Text = "0";b = 0;}textBox1.Text = " 2";elsetextBox1.Text = (textBox1.Text + '2').ToString(); }if ( == "three"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '3';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 3";elsetextBox1.Text = (textBox1.Text + '3').ToString(); }if ( == "four"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '4';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 4";elsetextBox1.Text = (textBox1.Text + '4').ToString(); }if ( == "five"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '5';return;if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 5";elsetextBox1.Text = (textBox1.Text + '5').ToString(); }if ( == "six"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '6';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 6";elsetextBox1.Text = (textBox1.Text + '6').ToString(); }if ( == "seven"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '7';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 7";elsetextBox1.Text = (textBox1.Text + '7').ToString(); }{if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '8';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 8";elsetextBox1.Text = (textBox1.Text + '8').ToString(); }if ( == "nine"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '9';return;}if (b != 0){textBox1.Text = "0";b = 0;}if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 9";elsetextBox1.Text = (textBox1.Text + '9').ToString(); }if ( == "zero"){if (x == 1)//判断有无小数点{textBox1.Text = textBox1.Text + '0';return;}if (b != 0)//两个数的界限判断?{textBox1.Text = "0";b = 0;if (double.Parse(textBox1.Text) == 0)textBox1.Text = " 0";elsetextBox1.Text = (textBox1.Text + '0').ToString();}}public void one_Click(object sender, EventArgs e){number(sender, textBox1);}public void Form1_Load(object sender, EventArgs e){textBox1.Text = "0";}public static void point(object sender, TextBox textBox1){if (double.Parse(textBox1.Text) == 0)x = 1;textBox1.Text = (textBox1.Text + '.').ToString();}public void xiaoshudian_Click(object sender, EventArgs e){point(sender, textBox1);}运算符号1(加减乘除、退格。
一、实验题目:用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。
visualstudio设计计算器包括立方
设计一个带有立方功能的计算器是一个有趣且具有挑战性的任务。
使用Visual Studio,我们可以轻松地创建一个用户友好且功能强大的计算器应用程序。
首先,我们需要创建一个Windows窗体应用程序项目。
在MainForm 窗体上,我们可以布置各种控件,如按钮、标签和文本框,以实现计算器的功能。
为了实现基本的计算功能,我们可以为每个按钮添加Click事件处理程序。
例如,当用户按下数字按钮时,相应的数字将显示在文本框中。
当用户按下操作符按钮(如加减乘除)时,我们将记录当前的操作符,并清空文本框以便用户输入下一个数字。
然后,我们需要实现立方功能。
为此,我们可以添加一个按钮,标记为'立方',并为其添加Click事件处理程序。
在此处理程序中,我们将从文本框中获取当前的数字,并将其立方值计算出来。
最后,将结果显示在文本框中。
计算器的另一个重要功能是清除按钮,用户可以使用它来清空当前的计算。
我们可以添加一个'清除'按钮,并为其添加Click事件处理程序,以清空文本框中的内容。
此外,我们可以为计算器添加其他功能,如开方、取反、百分数计算等。
这些功能的实现与立方功能类似,只需添加相应的按钮和处理程序即可。
通过Visual Studio的设计和开发工具,我们可以轻松地创建一个直观且功能强大的计算器应用程序。
使用窗体控件和事件处理程序,我们可以实现各种计算功能,包括立方。
这将使用户能够进行复杂的计算,并以直观的方式获得结果。
c语言制作简单的计算器在计算机编程领域中,C语言是一种广泛使用的编程语言之一。
它具有简单易学的特点,并且能够进行复杂的计算和逻辑处理。
今天,我们就来学习一下如何使用C语言来制作一个简单的计算器。
首先,我们需要明确我们计算器的功能和界面设计。
本次制作的计算器将具有基本的四则运算功能,即加法、减法、乘法和除法。
我们将在控制台中实现计算器的操作,并通过简单的用户界面进行交互。
接下来,我们需要创建一个C语言程序的框架。
首先,我们需要包含头文件,以便在程序中使用各种函数和变量。
我们可以使用"#include" 来引入头文件,例如:```c#include <stdio.h>```然后,我们需要定义主函数 `main()`。
在这个函数中,我们将编写计算器的逻辑代码。
让我们开始编写函数吧!首先,我们需要声明一些变量来存储用户输入的数字和运算符。
我们可以使用 `int` 来定义整数变量,并用 `char` 定义字符变量。
例如:```cint num1, num2, result;char operator;```接下来,我们需要提示用户输入要进行的运算,并读取用户输入的数字和运算符。
我们可以使用 `printf` 和 `scanf` 函数来完成这个任务,例如:```cprintf("请输入第一个数字: ");scanf("%d", &num1);printf("请输入运算符(+, -, *, /): ");scanf(" %c", &operator);printf("请输入第二个数字: ");scanf("%d", &num2);```注意,我们在读取运算符之前添加了空格,这是为了消除输入缓冲区中可能残留的换行符等字符。
接下来,我们需要编写代码来进行实际的计算操作。
vb6.0开发实例-回复VB6.0开发实例-一个简单的计算器在VB6.0中,我们可以轻松地创建一个简单的计算器。
计算器是我们日常生活中常见的工具,让我们一步一步地在VB6.0中创建一个简单的计算器,来帮助我们理解和掌握VB编程的基础知识和技巧。
首先,我们需要创建一个新项目。
在VB6.0中,选择“新建”->“标准EXE”创建一个新的窗体应用程序。
接下来,我们需要在窗体上添加一些控件,如文本框、按钮等,以实现计算器的功能。
在VB6.0中,我们可以通过拖动控件到窗体上的方式来添加控件。
第一个控件是一个文本框,用于显示输入的数字和计算结果。
在工具箱中选择文本框控件,然后点击窗体上的位置来添加它。
第二个控件是数字按钮,用于输入数字。
在工具箱中选择按钮控件,然后点击窗体上的位置来添加一个按钮。
我们可以复制该按钮来创建其他数字按钮。
第三个控件是运算符按钮,用于进行加、减、乘、除等计算操作。
同样地,在工具箱中选择按钮控件,然后点击窗体上的位置来添加一个按钮,复制该按钮来创建其他运算符按钮。
接下来,我们需要为这些按钮添加事件处理程序,以实现相应的功能。
在VB6.0中,我们可以通过双击按钮来自动创建一个点击事件的处理程序。
在点击事件中,我们可以使用VB6.0提供的内置函数和运算符来编写计算逻辑。
例如,我们可以使用“+”运算符来进行两个数字的相加,并将结果显示在文本框中。
除了基本的运算功能外,我们还可以为计算器添加其他功能,如清除输入、退格、求平方根等。
这些功能可以使用VB6.0提供的其他内置函数和方法来实现。
最后,我们需要进行界面的美化和调整,以满足用户的需求和审美。
在VB6.0中,我们可以通过更改控件的属性、添加背景和图标等方式来实现界面的美化。
完成所有的功能和界面调整后,我们可以运行程序来测试计算器的功能。
在VB6.0中,我们可以点击“运行”按钮来启动程序,并进行测试和使用。
通过以上的步骤,我们就成功地在VB6.0中创建了一个简单的计算器。
以下是一个简单的 程序设计案例,用于实现一个计算器应用程序:打开 Visual Studio,创建一个新的 Windows Forms 应用程序项目。
在 Form1 上添加以下控件:两个 Label 控件,分别命名为 lblNum1 和 lblNum2,用于显示输入的两个数字。
两个 TextBox 控件,分别命名为 txtNum1 和 txtNum2,用于输入两个数字。
四个 Button 控件,分别命名为 btnAdd、btnSubtract、btnMultiply 和 btnDivide,用于执行加、减、乘、除运算。
一个 Label 控件,命名为 lblResult,用于显示运算结果。
为 btnAdd、btnSubtract、btnMultiply 和 btnDivide 按钮分别添加 Click 事件处理程序。
在每个 Click 事件处理程序中,获取 txtNum1 和 txtNum2 中输入的两个数字,并执行相应的运算操作。
将结果显示在 lblResult 中。
以下是一个示例代码:vbPublic Class Form1Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click Dim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 + num2lblResult.Caption = result.ToString()End SubPrivate Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 - num2lblResult.Caption = result.ToString()End SubPrivate Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 * num2lblResult.Caption = result.ToString()End SubPrivate Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)If num2 <> 0 ThenDim result As Double = num1 / num2lblResult.Caption = result.ToString()ElseMessageBox.Show("除数不能为0")End IfEnd SubEnd Class这个简单的计算器应用程序可以让用户输入两个数字,并选择执行加、减、乘、除运算操作。
用V B6.0编写简易计算器效果图:废话不多说,直接上步骤一、创建控件组1、创建控件组的方法??首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。
这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。
此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。
二、编写代码Dim s1 As Single, s2 As Single, ysf As String'定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符Private Sub Command1_Click(Index As Integer)Text1.Text = Text1.Text & Command1(Index).Caption'将command1的单击事件与文本框显示的内容连接End SubPrivate Sub Command2_Click()Text1.Text = Text1.Text + "."If (InStr(Text1.Text, ".") = 1) Then'第一位不能为小数Text1.Text = ""End IfIf InStr(Text1.Text, ".") < Len(Text1.Text) Then '防止出现两个小数点Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)End IfEnd SubPrivate Sub Command3_Click()s2 = Val(Text1.Text) '开始加减乘除运算Select Case ysfCase "+"Text1.Text = s1 + s2Case "-"Text1.Text = s1 - s2Case "*"Text1.Text = s1 * s2Case "/"If s2 = 0 ThenMsgBox "分母不能为零!"Text1.Text = ""ElseText1.Text = s1 / s2End IfEnd SelectText1 = IIf(Left(Text1.Text, 1) = ".", 0 & Text1.Text, Text1.Text) '这个很关键,如果没有这个的话,得出小于1的小数前面没有0End SubPrivate Sub Command4_Click()If Text1.Text = "" Then '文本为空就结束Exit SubEnd IfText1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '文本退一格End SubPrivate Sub Command5_Click()Text1.Text = "" '清除当前框内文本End SubPrivate Sub Command6_Click(Index As Integer)s1 = Val(Text1.Text) '将s1隐藏起来ysf = Command6(Index).CaptionText1.Text = ""End SubPrivate Sub Command7_Click()If Left(Text1.Text, 1) <> "-" Then '判断作为负数Text1.Text = "-" & Text1.TextElseText1.Text = Right(Text1.Text, Len(Text1.Text) - 1)End IfEnd SubPrivate Sub Command8_Click()Text1.Text = Text1.Text * Text1.Text '平方End Sub各位朋友,可以将红色代码复制到相应位置,不清楚的可以全选复制,但是一定要按照我的步骤和给的名称来哦!还可以再添加按钮Private Sub Command9_Click() '这是退出代码EndEnd Sub三、测试,成功的话给个好评哦!谢谢各位下载与支持!这个可以编写作为作业哦!。
C#2010 制作整数计算器本练习制作的是能够进行“加、减、乘、除”简单运算的计算器,主要是通过为按钮控件添加鼠标单击事件获取用户单击数字并获取用户选择的运算方式。
本练习需要用到的控件包括TextBox控件、Button控件等,具体操作步骤如下所示:(1)在Visual Studio中创建一个名为“Calcutor”的Windows应用程序。
然后,从左边的【公共控件】窗口中选中TextBox控件将其拖放到空白的Windows窗体上。
执行【视图】|【属性窗口】命令,并在该面板中设置TextBox的属性,如图8-9所示。
图8-9 设置控件属性设置上面创建的TextBox的Name属性值为“showNum”、TextAlign属性值为“Right”。
(2)以同样的方式,向Windows窗体中拖入16个Button控件,并调整好控件之间的距离以使窗体更加的美观,如图8-10所示。
图8-10 添加Button控件(3)在【属性窗口】面板中,设置各个Button控件的Name和Text属性。
然后,设置Form1的BackColor属性值为“DodgerBlue”、Text值为“计算器”,效果如图8-11所示。
图8-11 调整控件属性在上面的窗体中,各控件的名称如表8-1所示。
表8-1 【计算器】中的控件控件name 控件name控件nameCE button22 1 button1 2 button23 button3 + button4 4 button55 button176 button7 - button87 button9 8 button10 9 button11* button12 0 button18 . button19= button20 / button21(4)然后,双击Windows窗体,打开Form1.cs文件,在class Form1中声明几个全局变量供以后使用,代码如下所示。
1、单击File(文件)菜单,选中New(新建),创建新工程。
2、选择工程栏中MFC AppWizard[exe]应用程序,并命名工程名称为calculator.(如图)3、选择MFC应用程序为“基于对话框”类型,其他选项默认。
单击完成(如图)4、修改界面。
经过前3步骤,VC6.0生成的工作界面如图所示。
先将对话框中的“TODO:在这里设置对话控制”静态文字控件和“确定”按钮控件删除,再使用工具箱将界面拖动控件修改为如图所示的界面。
该图所示是使用工具箱的控件拖放制作的计算器初始界面。
由于这些控件都有默认的名字,所以我们要对控件进行改名,右击Button1按钮,选中Properties(属性)选项以修改控件的属性,如图所示。
在弹出的对话框中,将Button1按钮的Caption(标题)属性修改为“+”,此时初始界面同时变成相应的“+”如下图所示。
修改后的界面如图所示,控件的Caption属性就是控件显示出来的值。
按照前文所讲述的方式,将界面上的按钮控件和静态文本控件的Caption属性都做相应的修改,修改后的界面如图所示。
5、为Edit(编辑)控件添加类的成员变量。
首先,右击Edit控件,选择ClassWizard(建立类向导)选项,如下图所示。
在弹出的对话框中,选择上方MemberVariables选项卡,选中DIC_EDIT1,然后单击单击右侧的Add Variable按钮,如图所示。
单击Add Variable按钮后会跳出,如图所示的对话框,该对话框是设置成员变量的命名和数据类型,在此可以命名为m_data1.注意:控件的ID只是标记一个控件,程序中不能直接控制ID,只能通过修改变量来改变控件的状态。
00相应的,再将另外两个Edit控件命名,本项目就将这3个Edit控件命名为:●IDC_EDIT1:m_data1,类型为double.●IDC_EDIT2:m_data2,类型为double.●IDC_EDIT3:m_result,类型为double.6、添加按钮控件的消息处理函数。
首先利用visual studio里面的button和textbox模块建立上图模块。
然后就是编写程序:
该程序最重要的是要理解清楚计算器的运算逻辑。
public partial class Form1 : Form
{
string a, b, q, ac;
int c = 0, d = 0, n = 0, p = 0, m = 0;
double temp1 = -1;
int pos = 0;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void addNum(int num)
{
textBox1.Text = textBox1.Text +
num.ToString();
if (c > 0) // 管¨¹理¤¨ª加¨®减?乘?除y运?算?
{
textBox1.Text = "";
a = num.ToString();
b = b + a;
textBox1.Text = b;
}
}
private void button1_Click(object sender, EventArgs e)
{
addNum(7); //向¨°显?示º?屏¨¢加¨®字Á? n++;
}
private void button2_Click(object sender, EventArgs e)
{
addNum(8);
}
private void button3_Click(object sender, EventArgs e)
{
addNum(9);
}
EventArgs e)
{
addNum(4);
}
private void button6_Click(object sender, EventArgs e)
{
addNum(5);
}
private void button7_Click(object sender, EventArgs e)
{
addNum(6);
}
EventArgs e)
{
addNum(1);
}
private void button10_Click(object sender, EventArgs e)
{
addNum(2);
}
private void button11_Click(object sender, EventArgs e)
{
addNum(3);
}
private void button14_Click(object sender, EventArgs e)
{
addNum(0);
}
private void button15_Click(object sender, EventArgs e)
{
if (p==0)
textBox1.Text = textBox1.Text + ".";//加¨®上¦?小?数ºy点Ì?
p++;
m++;
if (c > 0) // 管¨¹理¤¨ª加¨®减?乘?除y运?算?
{
b = b + ".";
}
}
private void button4_Click(object sender, EventArgs e)
{
pos = 1;
temp1 = Convert .ToDouble (textBox1.Text); textBox1.Text =temp1+"+";
d++;
c++;
p = 0;
}
private void button8_Click(object sender, EventArgs e)
{
pos = 2;
temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = temp1 + "-";
c++; p = 0;
}
private void button12_Click(object sender, EventArgs e)
{
pos = 3;
temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = temp1 + "*";
c++; p = 0;
}
private void button16_Click(object sender, EventArgs e)
{
pos = 4;
temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = temp1 + "/";
c++; p = 0;
}
private void button13_Click(object sender, EventArgs e)
{
double temp2 = Convert.ToDouble(b);
switch (pos)
{
case 1:
textBox1.Text = (temp1 + temp2).ToString();
break;
case 2:
textBox1.Text = (temp1 - temp2).ToString();
break;
case 3:
textBox1.Text = (temp1 * temp2).ToString();
break;
case 4:
textBox1.Text = (temp1 / temp2).ToString();
break;
}
}
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";//清?空?显?示º?器¡Â temp1 = 0;
pos = 0;
b = "";
a = "";
n = 0;
c = 0;
p = 0;
}
}
}。