程序设计实践课程作业一
好久没用的blog了, 以后还是要把自己的东西整理一下,看到周围的人变得越来越强,我越来越想当只咸鱼,划划水的了
using System; using System.Collections; 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 计算器 { public partial class Form1 : Form { Stack st = new Stack(); double num1,num2,temp; string Operator; //列表初始化 public Form1() { InitializeComponent(); textBox1.Text = ""; } //添加字符. private void Button_dian_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text)) textBox1.Text = "0."; else if (!textBox1.Text.Contains(".")) textBox1.Text += "."; } //添加字符0 private void Button7_Click(object sender, EventArgs e) { textBox1.Text += "0"; } //添加字符1 private void Button1_Click(object sender, EventArgs e) { textBox1.Text += "1"; } //添加字符2 private void Button2_Click(object sender, EventArgs e) { textBox1.Text += "2"; } //添加字符3 private void Button3_Click(object sender, EventArgs e) { textBox1.Text += "3"; } //添加字符4 private void Button4_Click(object sender, EventArgs e) { textBox1.Text += "4"; } //添加字符5 private void Button5_Click(object sender, EventArgs e) { textBox1.Text += "5"; } //添加字符6 private void Button6_Click(object sender, EventArgs e) { textBox1.Text += "6"; } //添加字符7 private void Button9_Click(object sender, EventArgs e) { textBox1.Text += "7"; } //添加字符8 private void Button10_Click(object sender, EventArgs e) { textBox1.Text += "8"; } //添加字符9 private void Button11_Click(object sender, EventArgs e) { textBox1.Text += "9"; } //加法运算 private void button_add_Click(object sender, EventArgs e) { try { st.Push(textBox1.Text); if(st.Count >= 3) { num1 = Convert.ToDouble(st.Pop()); Operator = Convert.ToString(st.Pop()); num2 = Convert.ToDouble(st.Pop()); //temp = num1 + num2; //st.Push(temp); if(Operator == "+") { temp = num1 + num2; st.Push(temp); } else if(Operator == "-") { temp = num2 - num1; st.Push(temp); } else if(Operator == "*") { temp = num2 * num1; st.Push(temp); } else if(Operator == "/") { temp = num2 / num1; st.Push(temp); } else if(Operator == "%") { temp = num2 % num1; st.Push(temp); } textBox1.Text = temp.ToString(); } st.Push("+"); textBox1.Text = ""; } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } } //减法运算 private void button_jian_Click(object sender, EventArgs e) { try { st.Push(textBox1.Text); if (st.Count >= 3) { num1 = Convert.ToDouble(st.Pop()); Operator = Convert.ToString(st.Pop()); num2 = Convert.ToDouble(st.Pop()); if (Operator == "+") { temp = num1 + num2; st.Push(temp); } else if (Operator == "-") { temp = num2 - num1; st.Push(temp); } else if (Operator == "*") { temp = num2 * num1; st.Push(temp); } else if (Operator == "/") { temp = num2 / num1; st.Push(temp); } else if (Operator == "%") { temp = num2 % num1; st.Push(temp); } textBox1.Text = temp.ToString(); } st.Push("-"); textBox1.Text = ""; } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } } //乘法运算 private void Button_cheng_Click(object sender, EventArgs e) { try { st.Push(textBox1.Text); if (st.Count >= 3) { num1 = Convert.ToDouble(st.Pop()); Operator = Convert.ToString(st.Pop()); num2 = Convert.ToDouble(st.Pop()); if (Operator == "+") { temp = num1 + num2; st.Push(temp); } else if (Operator == "-") { temp = num2 - num1; st.Push(temp); } else if (Operator == "*") { temp = num2 * num1; st.Push(temp); } else if (Operator == "/") { temp = num2 / num1; st.Push(temp); } else if (Operator == "%") { temp = num2 % num1; st.Push(temp); } textBox1.Text = temp.ToString(); } st.Push("*"); textBox1.Text = ""; } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } } //除法运算 private void Button_chu_Click(object sender, EventArgs e) { try { st.Push(textBox1.Text); if (st.Count >= 3) { num1 = Convert.ToDouble(st.Pop()); Operator = Convert.ToString(st.Pop()); num2 = Convert.ToDouble(st.Pop()); if (Operator == "+") { temp = num1 + num2; st.Push(temp); } else if (Operator == "-") { temp = num2 - num1; st.Push(temp); } else if (Operator == "*") { temp = num2 * num1; st.Push(temp); } else if (Operator == "/") { temp = num2 / num1; st.Push(temp); } else if (Operator == "%") { temp = num2 % num1; st.Push(temp); } textBox1.Text = temp.ToString(); } st.Push("/"); textBox1.Text = ""; } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } } //取余运算 private void button7_Click_1(object sender, EventArgs e) { try { st.Push(textBox1.Text); if (st.Count >= 3) { num1 = Convert.ToDouble(st.Pop()); Operator = Convert.ToString(st.Pop()); num2 = Convert.ToDouble(st.Pop()); if (Operator == "+") { temp = num1 + num2; st.Push(temp); } else if (Operator == "-") { temp = num2 - num1; st.Push(temp); } else if (Operator == "*") { temp = num2 * num1; st.Push(temp); } else if (Operator == "/") { temp = num2 / num1; st.Push(temp); } else if (Operator == "%") { temp = num2 % num1; st.Push(temp); } textBox1.Text = temp.ToString(); } st.Push("%"); textBox1.Text = ""; } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } } //等于 计算总表达式 private void Button16_Click(object sender, EventArgs e) { try { st.Push(textBox1.Text); if (st.Count >= 3) { num1 = Convert.ToDouble(st.Pop()); Operator = Convert.ToString(st.Pop()); num2 = Convert.ToDouble(st.Pop()); temp = num1 / num2; st.Push(temp); if (Operator == "+") { temp = num1 + num2; st.Push(temp); } else if (Operator == "-") { temp = num2 - num1; st.Push(temp); } else if (Operator == "*") { temp = num2 * num1; st.Push(temp); } else if (Operator == "/") { temp = num2 / num1; st.Push(temp); } else if (Operator == "%") { temp = num2 % num1; st.Push(temp); } textBox1.Text = temp.ToString(); } textBox1.Text = temp.ToString(); st.Clear(); } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } } //退格 private void button13_Click(object sender, EventArgs e) { try { textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); } catch { MessageBox.Show("已经为空,无法再退格了", "错误", MessageBoxButtons.OK); } } //清空C private void button_c_Click(object sender, EventArgs e) { textBox1.Text = ""; st.Clear(); } //清除CE private void button_ce_Click(object sender, EventArgs e) { textBox1.Text = ""; } //求倒数 private void button12_Click(object sender, EventArgs e) { double t=0; try { t = Convert.ToDouble(textBox1.Text); } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } if(t != 0) { t = 1 / t; textBox1.Text = t.ToString(); } } //平方运算 private void button_pingfang_Click(object sender, EventArgs e) { double t = 0; try { t = Convert.ToDouble(textBox1.Text); } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } t = t * t; textBox1.Text = t.ToString(); } //开根运算 private void button8_Click(object sender, EventArgs e) { double t = 0; try { t = Convert.ToDouble(textBox1.Text); } catch (Exception) { MessageBox.Show("先输入数字才能进行此运算!", "错误", MessageBoxButtons.OK); } t = Math.Sqrt(t); textBox1.Text = t.ToString(); } private void Form1_Load(object sender, EventArgs e) { } } }
另外附上一个 中缀式 转 后缀式 的用栈解决的 代码(数据结构实验写的
#include <iostream> #include <stack> #include <string.h> #include <string> #include <cstdio> using namespace std; int sum = 0; char p[10000]; int isp(char c) { if(c == '#') return 0; if(c == '(') return 1; if(c == ')') return 6; if(c == '*'||c == '/'|| c == '%') return 5; if(c == '+' || c == '-') return 3; }//栈内 int icp(char c) { if(c == '#') return 0; if(c == '(') return 6; if(c == ')') return 1; if(c == '*'||c == '/'|| c == '%') return 4; if(c == '+' || c == '-') return 2; }//栈外 void zhuan(char *c ) { stack<char> s; char ch ='#',ch1,op; int i =0; s.push('#'); //ch = c[i]; while (!s.empty() && c[i] != '#') { if('0'<=c[i] && c[i] <='9') { //cout << c[i]<<endl; p[sum++]=c[i]; i++; while('0'<=c[i] && c[i] <='9'){ p[sum++]=c[i]; i++; } p[sum++]=' '; } else{ op=s.top(); if(isp(op) < icp(c[i])) { s.push(c[i]);i++; } else if(isp(op) > icp(c[i])){ s.pop();//cout << op; p[sum++]=op; p[sum++]=' '; } else{ s.pop(); if(op=='(') i++; } } } while (s.top() != '#') { if(s.top()=='('||s.top()==')') s.pop(); p[sum++]=s.top(); p[sum++]=' '; s.pop(); } p[sum++] = '