-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (110 loc) · 4.31 KB
/
Copy pathProgram.cs
File metadata and controls
134 lines (110 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections;
using System.Collections.Generic;
namespace Yanyixiao
{
public class Program
{
public class Formula
{
public string Form { get; internal set; }
public Queue<string> FormQueue = new Queue<string>();
}
public static string[] op = { "+", "-", "*", "/" };// Operation set
static void Main(string[] args)
{
int i;
System.Console.WriteLine("请输入需要的题目数量:");
int n = Convert.ToInt32(System.Console.ReadLine());
for (i = 0; i < n;)
{
Formula question = MakeFormula();
double ret = Solve(question);
if ((int)ret == ret && ret >= 0)
{
System.Console.Write(question.Form);
System.Console.Write("=");
System.Console.WriteLine(ret);
i++;
}
}
}
public static Formula MakeFormula()
{
Random ran = new Random();
Formula Form = new Formula();
string build = null;
int count = ran.Next(1, 3); // generate random count
int start = 0;
int number1 = ran.Next(1, 99);
build = build + number1;
Form.FormQueue.Enqueue(Convert.ToString(number1));//uytd 8796r756eu675v 786f876v
while (start <= count)
{
int operation = ran.Next(0, 4); ; // generate operator
int number2 = ran.Next(1, 99);
build = build + op[operation] + number2;
Form.FormQueue.Enqueue(op[operation]);
Form.FormQueue.Enqueue(Convert.ToString(number2));
start++;
}
Form.Form = build;
return Form;
}//Make a Formula(like 1*5+62-5)
public static double Solve(Formula formula)
{
Stack<double> numberStack = new Stack<double>();//Store number
Stack operatorStack = new Stack();//Store operator
while (!(formula.FormQueue.Count == 0))
{
if (!(formula.FormQueue.Peek() == "+" || formula.FormQueue.Peek() == "-"
|| formula.FormQueue.Peek() == "*" || formula.FormQueue.Peek() == "/"))//判断是否数字或者operator
{
numberStack.Push(Convert.ToDouble(formula.FormQueue.Dequeue()));
}
else
{
double X, Y;
switch (formula.FormQueue.Dequeue())
{
case "*":
X = numberStack.Pop();
Y = Convert.ToDouble(formula.FormQueue.Dequeue());
numberStack.Push(X * Y);
goto First;
case "/":
X = numberStack.Pop();
Y = Convert.ToDouble(formula.FormQueue.Dequeue()); /////Designed by Yanyixiao
numberStack.Push(X / Y); /////09/18/2019
goto First;
case "+":
operatorStack.Push("+");
goto First;
case "-":
operatorStack.Push("-");
goto First;
}
First:;
}
}//乘除法计算完毕
while (numberStack.Count > 1)
{
switch (operatorStack.Peek())
{
case "+":
numberStack.Push((Convert.ToDouble(numberStack.Pop()) + Convert.ToDouble(numberStack.Pop())));
goto Second;
case "-":
double X, Y;
X = Convert.ToDouble(numberStack.Pop());
Y = Convert.ToDouble(numberStack.Pop());
numberStack.Push(Y - X);
goto Second;
}
Second:;
}
return numberStack.Pop();
}
}
}