四则运算2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算2相关的知识,希望对你有一定的参考价值。
设计思路:
将各个所要设计的条件拆开,各自实现。
截图:
程序代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace sz { class Program { static Random rand = new Random(); static void Main(string[] args) { String filepath; String path; int min, max;//数据范围限定 int dao; // 道数 int number; //所含数的个数 int middle; int outpath; String ch;//判断乘除 String yu = "N";//判断余数 String kuo = "N";//判断括号 String fu = "N";//判断有无负数 Console.WriteLine("请输入题目的道数:"); dao = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入所含数的个数:"); number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("数的下限:"); min = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("数的上限:"); max = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("是否有乘除(Y/N):"); ch = Console.ReadLine(); if (number == 2) { if (ch == "Y") { Console.WriteLine("是否有余数(Y/N):"); yu = Console.ReadLine(); } if (min >= 0) { Console.WriteLine("是否有负数(Y/N):"); fu = Console.ReadLine(); } } else { Console.WriteLine("是否有括号(Y/N):"); kuo = Console.ReadLine(); } int[,] Data = new int[dao, number];//保存数据 int[,] Operator = new int[dao, number];//保存运算符 int[] Data_first = new int[dao];//保存每个表达式的首个字符 int[,] kuohao = new int[dao, number]; if (ch == "Y") { for (int ii = 0; ii < dao; ii++) { for (int jj = 0; jj < number - 1; jj++) { Operator[ii, jj] = rand.Next(1, 5); } Operator[ii, number - 1] = 5; } } else { for (int i = 0; i < dao; i++) { for (int j = 0; j < number - 1; j++) { Operator[i, j] = rand.Next(1, 3); } Operator[i, number - 1] = 5; } } Data_first[0] = rand.Next(min, max); for (int s = 1; s < dao; s++) { Data_first[s] = rand.Next(min, max); for (int h = 0; h < s - 1; h++) { if (Data_first[s] == Data_first[h]) { s--; } } } for (int x = 0; x < dao; x++) { Data[x, 0] = Data_first[x]; for (int y = 1; y < number; y++) { if (Operator[x, y - 1] == 4) { for (; ; ) { Data[x, y] = rand.Next(min, max); if (Data[x, y] != 0) { break; } } } else { Data[x, y] = rand.Next(min, max); } } } if (fu == "N") { for (int i2 = 0; i2 < dao; i2++) { if (Operator[i2, 0] == 2 && Data[i2, 0] < Data[i2, 1]) { middle = Data[i2, 1]; Data[i2, 1] = Data[i2, 0]; Data[i2, 0] = middle; } } } if (ch == "Y") { if (yu == "N") { for (int i3 = 0; i3 < dao; i3++) { if (Operator[i3, 0] == 4) { middle = Data[i3, 0] % Data[i3, 1]; while (middle != 0) { Data[i3, 1] = rand.Next(1, Data[i3, 0]); middle = Data[i3, 0] % Data[i3, 1]; } } } } } if (kuo == "Y") { for (int j1 = 0; j1 < dao; j1++) { for (int j2 = 0; j2 < number - 2; j2++) { kuohao[j1, j2] = rand.Next(0, 2); } kuohao[j1, number - 2] = 0; kuohao[j1, number - 1] = 0; } } Console.WriteLine("选择最终的输出方式:\n1、控制台显示 2、文本输出"); outpath = Convert.ToInt32(Console.ReadLine()); if (outpath == 1) { for (int x1 = 0; x1 < dao; x1++) { for (int x2 = 0; x2 < number; x2++) { if (kuohao[x1, x2] == 1) { Console.Write("("); Console.Write(Data[x1, x2]); if (Operator[x1, x2] != 5) { if (Operator[x1, x2] == 1) { Console.Write("+"); } if (Operator[x1, x2] == 2) { Console.Write("-"); } if (Operator[x1, x2] == 3) { Console.Write("*"); } if (Operator[x1, x2] == 4) { Console.Write("/"); } } } else { Console.Write(Data[x1, x2]); if (Operator[x1, x2] != 5) { if (Operator[x1, x2] == 1) { Console.Write("+"); } if (Operator[x1, x2] == 2) { Console.Write("-"); } if (Operator[x1, x2] == 3) { Console.Write("*"); } if (Operator[x1, x2] == 4) { Console.Write("/"); } } else { for (int x3 = 0; x3 < number; x3++) { if (kuohao[x1, x3] == 1) { Console.Write(")"); } } Console.Write("="); } } } Console.WriteLine(); } } else { Console.WriteLine("请给出文件路径:"); filepath = Console.ReadLine(); path = @filepath; StreamWriter strm1 = File.CreateText(path); for (int x1 = 0; x1 < dao; x1++) { for (int x2 = 0; x2 < number; x2++) { if (kuohao[x1, x2] == 1) { strm1.Write("("); strm1.Write(Data[x1, x2]); if (Operator[x1, x2] != 5) { if (Operator[x1, x2] == 1) { strm1.Write("+"); } if (Operator[x1, x2] == 2) { strm1.Write("-"); } if (Operator[x1, x2] == 3) { strm1.Write("*"); } if (Operator[x1, x2] == 4) { strm1.Write("/"); } } } else { strm1.Write(Data[x1, x2]); if (Operator[x1, x2] != 5) { if (Operator[x1, x2] == 1) { strm1.Write("+"); } if (Operator[x1, x2] == 2) { strm1.Write("-"); } if (Operator[x1, x2] == 3) { strm1.Write("*"); } if (Operator[x1, x2] == 4) { strm1.Write("/"); } } else { for (int x3 = 0; x3 < number; x3++) { if (kuohao[x1, x3] == 1) { strm1.Write(")"); } } strm1.Write("="); } } } strm1.WriteLine(); } strm1.Close(); } } } }
代码行数:
336行
以上是关于四则运算2的主要内容,如果未能解决你的问题,请参考以下文章
获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算