功能描述:只實現加減法
C#不放設計圖就是耍流氓啦
【myform.cs】
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 實驗一
{
public partial class Myform : Form
{
//不加的話會報錯
public static void Main(string[] args)
{
// Starts the application.
Application.Run(new Myform());//FormXXX是你定義的窗口類
}
public static List<char> inputStr = new List<char>(1000); //用戶的輸入
public Myform()
{
InitializeComponent();
}
private void button12_Click(object sender, EventArgs e)
{
//等號代碼
textBox1.AppendText("=");
//textBox1.Text = textBox1.Text;
textBox1.Text = DataOp.DataMain();
string temp = DataOp.DataMain();
inputStr.Clear();
for (int i = 0; i < temp.Length; i++)
{
inputStr.Add(temp[i]);
}
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text = "";
inputStr.Clear(); //清空鏈表的所有元素
}
private void button1_Click_1(object sender, EventArgs e)
{
inputStr.Add('1');
textBox1.AppendText("1");
}
private void button2_Click(object sender, EventArgs e)
{
inputStr.Add('2');
textBox1.AppendText("2");
}
private void button10_Click(object sender, EventArgs e)
{
inputStr.Add('+');
textBox1.AppendText("+");
}
private void button11_Click(object sender, EventArgs e)
{
inputStr.Add('-');
textBox1.AppendText("-");
}
private void button3_Click(object sender, EventArgs e)
{
inputStr.Add('3');
textBox1.AppendText("3");
}
private void button4_Click(object sender, EventArgs e)
{
inputStr.Add('4');
textBox1.AppendText("4");
}
private void button5_Click(object sender, EventArgs e)
{
inputStr.Add('5');
textBox1.AppendText("5");
}
private void button6_Click(object sender, EventArgs e)
{
inputStr.Add('6');
textBox1.AppendText("6");
}
private void button7_Click(object sender, EventArgs e)
{
inputStr.Add('7');
textBox1.AppendText("7");
}
private void button8_Click(object sender, EventArgs e)
{
inputStr.Add('8');
textBox1.AppendText("8");
}
private void button9_Click(object sender, EventArgs e)
{
inputStr.Add('9');
textBox1.AppendText("9");
}
private void button13_Click_1(object sender, EventArgs e)
{
inputStr.Add('0');
textBox1.AppendText("0");
}
}
}
【program.cs】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 實驗一
{
class DataOp : Myform
{
static Stack<int> m = new Stack<int>();//數字棧,數字類型爲int
static Stack<char> s = new Stack<char>();//符號棧
public static void Read() //Read()從inputStr輸入流中讀值
{
for (int i = 0; i < inputStr.Count; i++)
{
if (!IsOperator(inputStr[i])) //數字
{
string s = null;
while (i < inputStr.Count && !IsOperator(inputStr[i]))
{
s += inputStr[i];
i++;
}
i--;
int mm = Convert.ToInt16(s);
m.Push(mm);
}
else if (IsOper(inputStr[i])) //+ - * /
{
if (s.Count.Equals(0))
{
s.Push(inputStr[i]);
}
else
{
int n1, n2;
char s1;
n2 = m.Pop();
n1 = m.Pop();
s1 = s.Pop();
int sum = Operat(n1, n2, s1);
m.Push(sum);
s.Push(inputStr[i]);
}
}
}
}
public static int PopStack()
{
int sum = 0;
while (s.Count != 0)
{
int n1, n2;
char s1;
n2 = m.Pop();
n1 = m.Pop();
s1= s.Pop();
sum = Operat(n1, n2, s1);
m.Push(sum);
}
return sum;
}
public static bool IsOperator(char c) //是否是操作符
{
if (c.Equals('+') || c.Equals('-'))
return true;
return false;
}
public static bool IsOper(char c) //是否是運算符符
{
if (c.Equals('+') || c.Equals('-'))
return true;
return false;
}
public static int Operat(int n1, int n2, char s1)
{
int sum = 0;
switch (s1)
{
case '+': sum = n1 + n2; break;
case '-': sum = n1 - n2; break;
}
return sum;
}
public static string DataMain()
{
Read();
return PopStack().ToString();
}
}
}
PS:應付實驗課,之前沒學過照着其他大佬的寫的 ……入門的可以參考