• 软件工程第四周作业


    单元测试内容:

    • 生成试题
    • 中缀表达式转后缀表达式并计算

    代码地址:https://git.coding.net/Vector121/f4.git

    结对成员:@刘耀泽


    生成试题

      测试用例(一)
      传入参数:
        参数1:string num1 = "10"; //第一个操作数
        参数2:string num2 = "20"; //第二个操作数
        参数3:string op = "*"; //运算符
        参数4:int flag = 1; //随机数,用来随机括号的位置
        参数5:ref int res = 60; //前一次生成的试题的计算结果
        参数6:ref int bracket = 1; //前一次试题是否生成括号
      期待结果:
        string str = "10 * 20";

      测试代码

       

      测试结果:通过。

      

      测试用例(二)
      传入参数:
        参数1:int num11 = 60; //第一个操作数的int值
        参数2:string num1 = "50"; //第一个操作数
        参数3:string num2 = "30"; //第二个操作数
        参数4:string op = "*"; //运算符
        参数5:int flag = 0; //随机数,用来随机括号的位置
        参数6:ref int res = 60; //前一次生成的试题的计算结果
        参数7:ref int bracket = 0; //前一次试题是否生成括号
      期待结果:
        string str = "( 50 + 30 )";

      测试代码

      

      测试结果:通过。

      

    实现代码如下:

    /// <summary>
    /// 生成试题
    /// </summary>
    /// <param name="num1">操作数1</param>
    /// <param name="num2">操作数2</param>
    /// <param name="op">操作符</param>
    /// <param name="flag">随机数</param>
    /// <param name="res">上次生成试题的结果</param>
    /// <param name="bracket">上次是否生成括号</param>
    /// <returns>试题字符串</returns>
    public static string CreateString(string num1, string num2, string op, int flag, ref int res, ref int bracket)
    {
    	Random rand = new Random();
    	string str;
    	if (op == "+" || op == "-")
    	{
    		if (flag == 0)
    		{
    			str = "( " + num1 + " " + op + " " + num2 + " )";
    			res = calc(Convert.ToInt32(num2), Convert.ToInt32(num1), op);
    		}
    		else if (flag == 1)
    		{
    			str = "( " + num2 + " " + op + " " + num1 + " )";
    			res = calc(Convert.ToInt32(num2), Convert.ToInt32(num1), op);
    		}
    		else
    		{
    			str = num1 + " " + op + " " + num2;
    			res = calc(Convert.ToInt32(num2), Convert.ToInt32(num1), op);
    			bracket = 0;
    		}
    	}
    	else
    	{
    		if (op == "/")
    		{
    			while (Convert.ToInt32(num1) % Convert.ToInt32(num2) != 0)
    			{
    				num2 = rand.Next(1, 100).ToString();
    			}
    		}
    		str = num1 + " " + op + " " + num2;
    		res = calc(Convert.ToInt32(num2), Convert.ToInt32(num1), op);
    	}
    	return str;
    }
    /// <summary>
    /// 生成试题
    /// </summary>
    /// <param name="num11">操作数1的int值</param>
    /// <param name="num1">操作数1</param>
    /// <param name="num2">操作数2</param>
    /// <param name="op">操作符</param>
    /// <param name="flag">随机数</param>
    /// <param name="res">上次生成试题的结果</param>
    /// <param name="bracket">上次是否生成括号</param>
    /// <returns>试题字符串</returns>
    public static string CreateString(int num11, string num1, string num2, string op, int flag, ref int res, int bracket)
    {
    	Random rand = new Random();
    	string str;
    	if (op == "+" || op == "-")
    	{
    		if (flag == 0)
    		{
    			str = "( " + num1 + " " + op + " " + num2 + " )";
    			res = calc(Convert.ToInt32(num2), res, op);
    		}
    		else if (flag == 1)
    		{
    			str = "( " + num2 + " " + op + " " + num1 + " )";
    			res = calc(Convert.ToInt32(num2), res, op);
    		}
    		else
    		{
    			str = num1 + " " + op + " " + num2;
    			res = calc(Convert.ToInt32(num2), res, op);
    			bracket = 0;
    		}
    	}
    	else
    	{
    		if (op == "/")
    		{
    			if (bracket == 1)
    			{
    				while (res % Convert.ToInt32(num2) != 0)
    				{
    					num2 = rand.Next(1, 100).ToString();
    				}
    			}
    			else
    			{
    				while (num11 % Convert.ToInt32(num2) != 0)
    				{
    					num2 = rand.Next(1, 100).ToString();
    				}
    			}
    		}
    		str = num1 + " " + op + " " + num2;
    		res = calc(Convert.ToInt32(num2), res, op);
    	}
    	return str;
    }

    中缀表达式转后缀表达式并计算

      测试用例(一)
        传入参数:string zStr = "( 4 + 28 ) * 66 + 17"; //试题的中缀表达式
      期待结果:
        int res = 2095;

      测试代码

      

      测试结果:通过。

       

      测试用例(二)
        传入参数:string zStr = "32 + 16 * 52 - 17"; //试题的中缀表达式
      期待结果:
        int res = 847;

      测试代码

      

      测试结果:失败。应为: <847>,实际为: <-847>。

      经过查找,原因是因为在对后缀表达式进行计算的时候需弹出两个操作数进行计算,先弹出的操作数实际上是第二个数字,后弹出来的实际上是第一个数字,而我们想当然的将先弹出的数字当做第一操作数,后弹出的数字当做第二个操作数。测试用例(一)通过了是因为,测试用例一都是加法和乘法,即使是交换了位置也没有关系,而测试用例二里面有减法,如果交换的操作数的位置就会计算错误。

      测试用例(三)

        传入参数:string zStr = "35 * ( 42 + 75 ) / 5"; //试题的中缀表达式
      期待结果:
        int res = 819;

      测试代码

      

      测试结果:通过。

      

    实现代码如下:

    /// <summary>
    /// 中缀表达式转后缀表达式并计算
    /// </summary>
    /// <param name="str">传入中缀表达式</param>
    /// <returns>计算结果</returns>
    public static int ChangeExpression(string zStr)
    {
    	char a = ' ';
    	String[] sp = zStr.Split(a);//字符串数组sp
    
    	Stack st = new Stack();  //栈st
    
    	Stack st1 = new Stack();
    	//int j = 0;
    	int res = 0;
    	for (int i = 0; i < sp.Length; i++)
    	{
    		if (sp[i] != "+" && sp[i] != "-" && sp[i] != "*" && sp[i] != "/" && sp[i] != "(" && sp[i] != ")")
    		{
    			st1.Push(sp[i]);
    			//j++;
    		}
    		else
    		{
    			if (st.Count == 0 || IsPriority(sp[i], st.Peek().ToString()) == 1 || sp[i] == "(")
    			{
    				st.Push(sp[i]);
    			}
    			else
    			{
    				while (st.Count != 0)
    				{
    					if (st.Peek().ToString() != "(")
    					{
    
    						res = calc(Convert.ToInt32(st1.Pop().ToString()), Convert.ToInt32(st1.Pop().ToString()), st.Pop().ToString());
    						st1.Push(res);
    						//j++;
    					}
    					else if (sp[i] != ")")
    					{
    						break;
    					}
    					else if (sp[i] == ")")
    					{
    						if (st.Peek().ToString() == "(")
    						{
    							st.Pop();
    							break;
    						}
    						else
    						{
    							res = calc(Convert.ToInt32(st1.Pop().ToString()), Convert.ToInt32(st1.Pop().ToString()), st.Pop().ToString());
    							st1.Push(res);
    						}
    					}
    				}
    				if (sp[i] != ")")
    				{
    					st.Push(sp[i]);
    				}
    			}
    		}
    	}
    	while (st.Count != 0)
    	{
    		res = calc(Convert.ToInt32(st1.Pop().ToString()), Convert.ToInt32(st1.Pop().ToString()), st.Pop().ToString());
    		st1.Push(res);
    		//j++;
    	}
    
    	return res;
    }
    
  • 相关阅读:
    头部尾部始终处于两端(适用于pc端和移动端)
    运用active和hover实现导航栏的页面切换
    POJ1423-阶乘的位数-Big Number
    大数阶乘
    n皇后
    4103:踩方格
    2815:城堡问题
    特殊回文数
    十六进制转十进制
    十六进制转八进制
  • 原文地址:https://www.cnblogs.com/vector121/p/7648240.html
Copyright © 2020-2023  润新知