-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathProgram.cs
More file actions
99 lines (94 loc) · 3.46 KB
/
Program.cs
File metadata and controls
99 lines (94 loc) · 3.46 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace Operation
{
public class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("请输入数量:");
n = Convert.ToInt32(Console.ReadLine());
GetSubject(n);
}
static public void GetSubject(int n)
{
Random rdm = new Random();
int[] num = new int[4];
char[] ch = new char[3];
for (int i = 0; i < n; i++)
{
int cnum = rdm.Next(2, 4); //运算符个数
num[cnum] = rdm.Next(101);
double resultTest = num[0]; //用于检查结果是否为整数
long result = num[0];
string subject = num[0].ToString(); //代表算式的字符串
for (int m = 0; m < cnum; m++)
{
num[m] = rdm.Next(101);
int r = rdm.Next(4);
switch (r)
{
case 0:
ch[m] = '+'; break;
case 1:
ch[m] = '-'; break;
case 2:
ch[m] = '*'; break;
case 3:
ch[m] = '/'; break;
default:
break;
}
if (ch[m] == '/' && num[m] == 0)
{
m--;
continue;
}
switch (ch[m])
{
case '+':
resultTest = resultTest + (double)num[m]; break;
case '-':
resultTest = resultTest - (double)num[m]; break;
case '*':
resultTest = resultTest * (double)num[m]; break;
case '/':
resultTest = resultTest / (double)num[m]; break;
default:
break;
}
//检验resulttest是否为整数,若不是,重新循环。
if (resultTest != Convert.ToDouble(Convert.ToInt64(resultTest)))
{
m--;
continue;
}
switch (ch[m])
{
case '+':
result = result + num[m]; break;
case '-':
result = result - num[m]; break;
case '*':
result = result * num[m]; break;
case '/':
result = result / num[m]; break;
default:
break;
}
subject += ch[m] + num[m].ToString();
}
subject += "=" + result.ToString();
Console .WriteLine(subject);
}
Console.WriteLine("已輸出" );
Console.ReadLine();
}
}
}