c制作计算器_第1页
c制作计算器_第2页
c制作计算器_第3页
c制作计算器_第4页
c制作计算器_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

虽然电脑坏了,之前设计的计算器的代码也没了,但是重新又做了一个也没用了多久。这说明放在脑子里的东西还是最安全的!其实做一个计算器也不难,第一先把界面设计好,这里要用到 XAML 语音,然后用 C#将背后的处理函数写好就好了。我做的这个计算器功能其实还是不是很完善的,但还是欢迎大家免费下载/share/link?shareid=470362using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace 计算器/ / MainWindow.xaml 的交互逻辑/ public partial class MainWindow : Windowpublic MainWindow()InitializeComponent();MessageBox.Show(“本计算器暂不支持负数计算!“);private string txt=“0+“;/定义一个字符串变量来存储数据static double temp = 0;/定义一个临时变量用来存储计算的数据string txt1;private void text_TextChanged(object sender, TextChangedEventArgs e)txt1 =txt.Substring(2);text.Text = txt1;private void button1_Click(object sender, RoutedEventArgs e)txt += “1“;text.Text = txt;private void button2_Click(object sender, RoutedEventArgs e)txt += “2“;text.Text = txt;private void button3_Click(object sender, RoutedEventArgs e)txt += “3“;text.Text = txt;private void button4_Click(object sender, RoutedEventArgs e)txt += “4“;text.Text = txt;private void button5_Click(object sender, RoutedEventArgs e)txt += “5“;text.Text = txt;private void button6_Click(object sender, RoutedEventArgs e)txt += “6“;text.Text = txt;private void button7_Click(object sender, RoutedEventArgs e)txt += “7“;text.Text = txt;private void button8_Click(object sender, RoutedEventArgs e)txt += “8“;text.Text = txt;private void button9_Click(object sender, RoutedEventArgs e)txt += “9“;text.Text = txt;private void button_plus_Click(object sender, RoutedEventArgs e)txt += “+“;text.Text = txt;private void button_subtraction_Click(object sender, RoutedEventArgs e)txt += “-“;text.Text = txt;private void button_multiple_Click(object sender, RoutedEventArgs e)txt += “x“;text.Text = txt;private void button_division_Click(object sender, RoutedEventArgs e)txt += “/“;text.Text = txt;private void button_point_Click(object sender, RoutedEventArgs e)txt += “.“;text.Text = txt;private void button0_Click(object sender, RoutedEventArgs e)txt += “0“;text.Text = txt;private void button_equal_Click(object sender, RoutedEventArgs e)txt += “=“;text.Text = txt;string str1=“;/用来作为中间变量char sign = new chartxt.Length;/用来存储运算符int j = 0;double number = new doubletxt.Length;/用来存储输入的数据char str3 = txt.ToCharArray();/将字符串 txt 存入字符型数组 str3中/从 txt 字符串中分离数据和运算符for (int i = 0; i = 0|str3i=.)str1 += str3i;/str3数组中的数字与运算符分离开来,str1作为中间变量elsesignj = str3i;/存入运算符numberj=Convert.ToDouble(str1); /将 str1存入的数据转化为 double 型数据str1 = “;/将 str 清空便于下次存入j+;if (str3i = =)int k = 0;int index = 0;double temp2 = 1;switch(signk)case +:temp = number0 + number1; index = k; break;case -:temp = number0 - number1; index = k; break;case x:temp=number0*number1;break;case /:temp=number0/number1;break;for (k = 1; signk != =; k+)switch (signk)case +:temp = temp + numberk + 1; index = k; break;case -:temp = temp - numberk + 1; index = k; break;case x:/考虑连乘情况if (signk - 1 = +)temp = temp - numberk;temp2 = numberk * numberk + 1;temp += temp2; break;if (signk - 1 = -)temp = temp + numberk;temp2 = numberk * numberk + 1;temp -= temp2; break;if (signk - 1 = x)if (signindex = +)temp = temp - temp2;temp2 = temp2 * numberk + 1;temp = temp + temp2;if (signindex = -)temp = temp + temp2;temp2 = temp2 * numberk + 1;temp = temp - temp2;break;if (signk - 1 = /)if (signindex = +)temp = temp - temp2;temp2 = temp2 * numberk +1;temp = temp + temp2;if (signindex = -)temp = temp + temp2;temp2 = temp2 * numberk + 1;temp = temp - temp2;break;case /:if (signk - 1 = +)temp = temp - numberk;temp2 = numberk / numberk + 1;temp += temp2; break;if (signk - 1 = -)temp = temp + numberk;temp2 = numberk / numberk + 1;temp -= temp2; break;if (signk - 1 = x)if (signindex = +)temp = temp - temp2;temp2 = temp2 / numberk + 1;temp = temp + temp2;if (signindex = -)temp = temp + temp2;temp2 = temp2 / numberk + 1;temp = temp - temp2;break;if (signk - 1 = /)if (signindex = +)temp = temp - temp2;temp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论