# 第二次作业

GIT地址：https://github.com/

GIT用户名：littlecat

PART2   克隆

GIT安装，下载的github for windows，

新建项目文件夹

PART3  单元测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp5
{
class Program
{
Random random = new Random();
Stack<string> stack_number = new Stack<string>();
Stack<string> stack_operator = new Stack<string>();
List<string> formulas = new List<string>();
string str = "";
string substr = "";
string nstr = "";
int m = 0;

static void Main(string[] args)
{
Program program = new Program();
program.FormulaGeneration(n);
foreach(var str in program.formulas)
{
Console.WriteLine(str);
}
}

private bool judgenumber(string text)//判断是否为数字
{
try
{
int var1 = Convert.ToInt32(text);
return true;
}
catch
{
return false;
}
}
private bool judgeoperator(string text)//判断是否为运算符
{
if (text == "(" || text == ")" || text == "+" || text == "-" || text == "*" || text == "/")
{
return true;
}
else
return false;
}
public object addition(object a, object b)//加法
{
Decimal d1 = Decimal.Parse(a.ToString());
Decimal d2 = Decimal.Parse(b.ToString());
return d2 + d1;
}
public object subduction(object a, object b)//减法
{
Decimal d1 = Decimal.Parse(a.ToString());
Decimal d2 = Decimal.Parse(b.ToString());
return d2 - d1;
}
public object multiplication(object a, object b)//乘法
{
Decimal d1 = Decimal.Parse(a.ToString());
Decimal d2 = Decimal.Parse(b.ToString());
return d2 * d1;
}
public object division(object a, object b)//除法
{
Decimal d1 = Decimal.Parse(a.ToString());
Decimal d2 = Decimal.Parse(b.ToString());
if (d2 % d1 != 0 || d2 < d1)
{
m++;
return -10000;
}
else
{
return d2 / d1;
}

}
int judgelevel(string text)//判断优先级
{
if (text.Equals("("))
{
return 1;
}
else if (text.Equals(")") || text.Equals("（") || text.Equals("）"))
{
return 1;
}
else if (text.Equals("+") || text.Equals("-"))
{
return 2;
}
else if (text.Equals("*") || text.Equals("/"))
{
return 3;
}
else
return 10;

}
int operator_dected(string types, string a, string b)//根据运算符的类型返回对应的值
{
if (types == "+")
{
}
else if (types == "-")
{
return Convert.ToInt32(subduction(a, b));
}
else if (types == "*")
{
return Convert.ToInt32(multiplication(a, b));
}
else if (types == "/")
{
return Convert.ToInt32(division(a, b));
}
else
return 999;
}
double operate(string Str)
{
stack_number.Clear();//清空栈
stack_operator.Clear();
str = Str + "!";//！为结束运算符
int temp_count = 0;
try
{
for (int i = 0; i < str.Length; i++)
{
substr = str.Substring(i, 1);
if (judgenumber(substr))//如果是数字
{
if (temp_count == 0)
{
stack_number.Push(substr);
}
else
temp_count--;
if (judgenumber(str.Substring(i + 1, 1)))
{
link1 += str.Substring(i + 1, 1);
temp_count++;
}
}
else if (judgeoperator(substr))
{
if (stack_operator.Count >= 1)
{
int new1 = judgelevel(substr);
int old1 = judgelevel(stack_operator.Peek());
if (old1 < new1 || substr == "(")//判断优先级
{
stack_operator.Push(substr); //将运算符插入栈中
}
else
{
if (substr == ")")
{
for (; stack_operator.Count > 0; stack_operator.Pop())
{
if (stack_operator.Contains("(") && stack_operator.Peek() == "(")
{
stack_operator.Pop();
break;
}
else
{
int temp1 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
int temp2 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
stack_number.Push(operator_dected(stack_operator.Peek(), temp1.ToString(), temp2.ToString()).ToString());
}
}
}
else
{
for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop())
{
string temp_a = substr;
int new2 = judgelevel(temp_a);
int old2 = judgelevel(stack_operator.Peek());
if (old2 < new2 || substr == "(")//判断优先级
{
break;
}
else
{
int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString());
}
}
stack_operator.Push(substr);
}
}
}
else
{
stack_operator.Push(substr);

}
}
else if (substr == "!")
{
for (; stack_operator.Count > 0 && stack_number.Count >= 2 && stack_operator.Peek() != "("; stack_operator.Pop())
{
int temp3 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
int temp4 = Convert.ToInt32(stack_number.Peek()); stack_number.Pop();
stack_number.Push(operator_dected(stack_operator.Peek(), temp3.ToString(), temp4.ToString()).ToString());
}
return Convert.ToDouble(stack_number.Peek());
}
else
{

break;
}
}
}
catch
{

}
return 0;
}

void FormulaGeneration(int n)
{
int i = 0;
int num = 0;
int a, b, c, d;
int x, y, z;

string formula = "";

for (; i < n; i++)
{
do
{
m = 0;
a = random.Next(0, 101);
b = random.Next(0, 101);
c = random.Next(0, 101);
x = random.Next(0, 4);
y = random.Next(0, 4);
num = random.Next(2, 4);
if (num == 2)
{
nstr = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "!";
formula = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "=" + operate(nstr);
}
else if (num == 3)
{
d = random.Next(0, 101);
z = random.Next(0, 4);
nstr = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "" + Operator(z) + "" + d + "!";
formula = a + "" + Operator(x) + "" + b + "" + Operator(y) + "" + c + "" + Operator(z) + "" + d + "=" + operate(nstr);
}
else
{
Console.WriteLine("运算符数量错误!");
}
} while (!(operate(nstr) % 1 == 0 && operate(nstr) > 0 && m == 0));
}
}

string Operator(int n)
{
switch (n)
{
case 0:
return "+";
case 1:
return "-";
case 2:
return "*";
case 3:
return "/";
}
return "";
}

double op(int num1, int num2)
{
if (num1 < num2 || num1 % num2 != 0)
{
m++;
return 0;
}
else
{
return num1 / num2;
}
}
}
}

posted @ 2019-03-29 15:12  挥起菜刀！！  阅读(129)  评论(1编辑  收藏  举报