C#桌面应用制作计算器进阶版01

embedded/2024/11/22 10:22:09/

基于C#桌面应用制作计算器做出了少量改动,其主要改动为新增加了一个label控件,使其每一步运算结果由label2展示出来,而当点击“=”时,最终运算结果将由label1展示出来,此时label清空。

修改后运行效果

修改后全篇代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp计算器
{public partial class Form1 : Form{string symbol, num1, num2,Num1,Num2;double rt;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){label1.Text = null;label2.Text = null;num1=null;num2=null;symbol = null;}private void button2_Click(object sender, EventArgs e){if (label1.Text.Length == 0&&symbol==null){label1.Text = null;}else if (label1.Text.Length == 0 && symbol != null){label1.Text = symbol;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = num1;symbol = null;}else{string del = label1.Text;label1.Text = del.Substring(0, del.Length - 1);label2.Text = label1.Text;if (label2.Text.Length>0&& label1.Text.Length != 0){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}else{if (label1.Text.Length <= 1 && symbol == null){label2.Text=null;}else{label2.Text = num1;}}if (label1.Text.Length < 30){if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}}}}private void button3_Click(object sender, EventArgs e){if (label1.Text.Length == 0){MessageBox.Show("error");}else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{double reserve = double.Parse(label1.Text);reserve = -reserve;label1.Text = reserve.ToString();if (num1 != null && symbol != null){num2 = reserve.ToString();if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}}private void button4_Click(object sender, EventArgs e){string divide = "÷";if (symbol == null && label1.Text.Length<1&&num1==null){MessageBox.Show("无运算数字");}else{if (symbol == null){if(num1 == null){num1 = label1.Text;}symbol = divide;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷") {MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = divide;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void button8_Click(object sender, EventArgs e){string seven = "7";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;//如果前面有运算符,则先消除运算符再把数字投射到屏幕上}label1.Text = label1.Text + seven;//将数字投射到屏幕上if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if(num1!=null&&symbol!=null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button7_Click(object sender, EventArgs e){string eight = "8";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + eight;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button6_Click(object sender, EventArgs e){string nine = "9";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + nine;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button5_Click(object sender, EventArgs e){string multiply = "×";if (symbol == null && label1.Text.Length < 1 && num1 == null){MessageBox.Show("无运算数字");}else{if (symbol == null){if (num1 == null){num1 = label1.Text;}symbol = multiply;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = multiply;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void button12_Click(object sender, EventArgs e){string four = "4";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + four;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button11_Click(object sender, EventArgs e){string five = "5";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + five;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button10_Click(object sender, EventArgs e){string six = "6";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + six;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button9_Click(object sender, EventArgs e){string subtract = "-";if (symbol == null && label1.Text.Length < 1 && num1 == null){MessageBox.Show("无运算数字");}else{if (symbol == null){if (num1 == null){num1 = label1.Text;}symbol = subtract;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = subtract;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void button16_Click(object sender, EventArgs e){string one = "1";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + one;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button15_Click(object sender, EventArgs e){string two = "2";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + two;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button14_Click(object sender, EventArgs e){string three = "3";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + three;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button13_Click(object sender, EventArgs e){string add = "+";if (symbol == null && label1.Text.Length < 1 && num1 == null){MessageBox.Show("无运算数字");}else{if (symbol == null){if (num1 == null){num1 = label1.Text;}symbol = add;label1.Text = symbol;label2.Text = num1;}else{if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("您已输入运算符");}else{num2 = label1.Text;num1 = rt.ToString();symbol = add;label1.Text = symbol;label2.Text = rt.ToString();num2 = null;}}}}private void label2_Click(object sender, EventArgs e){}private void label1_Click(object sender, EventArgs e){}private void button20_Click(object sender, EventArgs e){if (label1.Text.Length == 0){MessageBox.Show("error");}else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{double reserve = double.Parse(label1.Text);reserve = reserve / 100;label1.Text = reserve.ToString();if (num1 != null && symbol != null){num2 = reserve.ToString();if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}}private void button19_Click(object sender, EventArgs e){string zero = "0";if (label1.Text.Length < 30){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){label1.Text = null;}label1.Text = label1.Text + zero;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}if (num1 != null && symbol != null){num2 = label1.Text;if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label2.Text = rt.ToString();}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label2.Text = rt.ToString();}}}private void button18_Click(object sender, EventArgs e){string dot = ".";int f=label1.Text.IndexOf(".");if(f == -1){if(label1.Text.Length == 0){MessageBox.Show("error");}else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{if (label1.Text.Length < 30){label1.Text = label1.Text + dot;if (label1.Text.Length > 16){label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);}else{label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);}}else{MessageBox.Show("字符长度超出范围");}}}else{MessageBox.Show("error");}}private void button17_Click(object sender, EventArgs e){if (symbol == null && label1.Text.Length < 1){MessageBox.Show("无运算数字");}else{if (symbol == null){num1 = label1.Text;}else{if (label1.Text != null){if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷"){MessageBox.Show("error");}else{num2 = label1.Text;}}}}if (num2 != null){if (symbol == "+"){rt = double.Parse(num1) + double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}else if (symbol == "-"){rt = double.Parse(num1) - double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}else if (symbol == "×"){rt = double.Parse(num1) * double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}else if (symbol == "÷"){rt = double.Parse(num1) / double.Parse(num2);label1.Text = rt.ToString();label2.Text=null;num1 = rt.ToString();symbol = null;num2 = null;}}}}
}


http://www.ppmy.cn/embedded/139595.html

相关文章

qt调试手段

Qt调试手段包括多种方法&#xff0c;可以帮助开发者在开发和测试过程中找出问题。以下是一些常用的Qt调试手段&#xff1a; Qt Creator调试器&#xff1a; Qt Creator提供了强大的调试工具&#xff0c;可以设置断点、观察变量的值、单步调试等&#xff0c;帮助开发者逐行检查…

鸿蒙系统下使用AVPlay播放视频,封装播放器

鸿蒙系统下使用AVPlay开发一款视频播放器流程 一. 申请权限 申请相关权限&#xff0c;主要是读取存储卡权限&#xff0c;方便后面扫描视频用&#xff1a; getPermission(): void {let array: Array<Permissions> [ohos.permission.WRITE_DOCUMENT,ohos.permission.REA…

django从入门到实战(一)——路由的编写规则与使用

Django 路由的编写规则与使用 在 Django 中&#xff0c;路由&#xff08;URLconf&#xff09;是将 URL 映射到视图函数的机制。它允许我们定义网站的 URL 结构&#xff0c;并将请求分发到相应的处理函数。以下是关于 Django 路由的定义规则及使用的详细介绍。 1. Django 的路…

如何更改手机GPS定位

你是否曾想过更改手机GPS位置以保护隐私、玩游戏或访问受地理限制的内容&#xff1f;接下来我将向你展示如何使用 MagFone Location Changer 更改手机GPS 位置&#xff01;无论是在玩Pokmon GO游戏、发布社媒贴子&#xff0c;这种方法都快速、简单且有效。 第一步&#xff1a;下…

word设置交叉引用快捷键和居中快捷键

1Word 设置页码从指定页开始的详细步骤&#xff01; - 知乎 2居中快捷键是CTRLE。 3word页码从正文是1&#xff1f; 首先把光标移到正文开始处&#xff0c;然后【布局】-“分割符”-“下一页”&#xff0c;点一下。 然后就可以设置页码从指定页开始计数1234。。。但是需要注…

SpringBoot,IOC,DI,分层解耦,统一响应

目录 详细参考day05 web请求 1、BS架构流程 2、RequestParam注解 完成参数名和形参的映射 3、controller接收json对象&#xff0c;使用RequestBody注解 4、PathVariable注解传递路径参数 5、ResponseBody&#xff08;return 响应数据&#xff09; RestController源码 6、统一响…

鸿蒙学习高效开发与测试-ArkUI 框架(2)

文章目录 1、声明式开发范式2、类 Web 开发范式3、可视可说 ArkUI 是鸿蒙生态原生的 UI 开发框架。主体结构如下图所示&#xff1a; ArkUI 框架提供给开发者两种开发方式&#xff1a;基于 ArkTS 的声明式开发范式和基于 JS 扩展的类 Web 开发范式。声明式开发范式更加简洁高效…

贴代码框架PasteForm特性介绍之select,selects,lselect和reload

简介 PasteForm是贴代码推出的 “新一代CRUD” &#xff0c;基于ABPvNext&#xff0c;目的是通过对Dto的特性的标注&#xff0c;从而实现管理端的统一UI&#xff0c;借助于配套的PasteBuilder代码生成器&#xff0c;你可以快速的为自己的项目构建后台管理端&#xff01;目前管…