四则运算生成器升级版2.0
一、题目要求
每个同学对已有的四则运算生成器进行优化,我选择的题目是:让程序能接受用户输入答案,并判断对错,最后给出总共对/错的数量。
二、设计思想
首先考虑用c#编写程序,找到一个能输出运算题目、能接收用户输入的还能反馈给用户做的对与错的控件,最后考虑选择的是datagridview控件,而且用了之后效果还是不错的,但是不进行数据库的链接,就是简单的实现这个控件的单元格的内容输入输出。
三、程序源代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace sizeyunsuanqi2._0 11 { 12 public partial class Form1 : Form 13 { 14 15 int shitishumu = 0; 16 int shuzhifanwei1 = 0; 17 int shuzhifanwei2 = 0; 18 int a = 0; 19 int b = 0; 20 int c = 0; 21 int addition, division = 0, subtraction1, subtraction2, multiplication, count = 0; 22 public Form1() 23 { 24 InitializeComponent(); 25 } 26 27 private void button1_Click(object sender, EventArgs e) 28 { 29 shitishumu = int.Parse(textBox4.Text);//用户控制输入试题数目 30 shuzhifanwei2 = int.Parse(textBox3.Text);//用户控制输入数值范围(大) 31 shuzhifanwei1 = int.Parse(textBox2.Text);//用户控制输入数值范围(小) 32 richTextBox1.Text += "尊敬的用户您好,您的请求已经得到确认" + "\r\n"; 33 richTextBox1.Text += "您将打印 " + shitishumu + " 道题目" + "\r\n"; 34 richTextBox1.Text += "您打印试题的数值范围是: " + shuzhifanwei1 + "-" + shuzhifanwei2 + "\r\n"; 35 if (checkBox2.Checked == true) 36 { 37 richTextBox1.Text += "运算试题的计算结果存在负数" + "\n"; 38 } 39 if (checkBox2.Checked == false) 40 { 41 richTextBox1.Text += "运算试题的计算结果不存在负数" + "\n"; 42 } 43 if (checkBox1.Checked == true) 44 { 45 richTextBox1.Text += "运算试题存在乘除法" + "\n"; 46 } 47 if (checkBox1.Checked == false) 48 { 49 richTextBox1.Text += "运算试题不存在乘除法" + "\n"; 50 } 51 System.Random number = new Random(System.DateTime.Now.Millisecond); 52 int num1=0, num2=0; 53 for (int i = 0; i < shitishumu; i++) 54 { 55 if (shuzhifanwei1 <=shuzhifanwei2) 56 { 57 num1 = number.Next(shuzhifanwei1, shuzhifanwei2); 58 num2 = number.Next(shuzhifanwei1, shuzhifanwei2); 59 } 60 else if (shuzhifanwei1 > shuzhifanwei2) 61 { 62 num1 = number.Next(shuzhifanwei2, shuzhifanwei1); 63 num2 = number.Next(shuzhifanwei2, shuzhifanwei1); 64 } 65 int yunsuan1 = number.Next(0, 4); 66 int yunsuan2 = number.Next(0, 2); 67 68 //判断条件并输出运算式 69 if (checkBox1.Checked == true)//有乘除法 70 { 71 if (checkBox2.Checked == true)//减法有负数 72 { 73 if (yunsuan1 == 0) { dataGridView1.Rows.Add(i + 1, num1, "+", num2, "="); } 74 else if (yunsuan1 == 1) { dataGridView1.Rows.Add(i + 1, num1, "*", num2, "="); } 75 else if (yunsuan1 == 2) { dataGridView1.Rows.Add(i + 1, num1, "-", num2, "="); }//减法有负数 76 else if (yunsuan1 == 3 && num2 != 0) { dataGridView1.Rows.Add(i + 1, num1, "/", num2, "="); }//除法有余数 77 } 78 else if (checkBox2.Checked == false)//减法没有负数 79 { 80 if (yunsuan1 == 0) { dataGridView1.Rows.Add(i + 1, num1, "+", num2, "="); } 81 else if (yunsuan1 == 1) { dataGridView1.Rows.Add(i + 1, num1, "*", num2, "="); } 82 else if (yunsuan1 == 2 && num1 > num2) { dataGridView1.Rows.Add(i + 1, num1, "-", num2, "="); }//减法没有负数 83 else if (yunsuan1 == 2 && num1 <= num2) { dataGridView1.Rows.Add(i + 1, num2, "-", num1, "="); }//减法没有负数 84 else if (yunsuan1 == 3 && num2 != 0) { dataGridView1.Rows.Add(i + 1, num1, "/", num2, "="); }//除法有余数 85 else if (yunsuan1 == 3 && num2 == 0) { dataGridView1.Rows.Add(i + 1, num2, "/", num1, "="); }//除法有余数 86 87 } 88 } 89 else if (checkBox1.Checked == false)//没有乘除法 90 { 91 if (checkBox2.Checked == true)//减法有负数 92 { 93 if (yunsuan2 == 0) { dataGridView1.Rows.Add(i + 1, num1, "+", num2, "="); } 94 else if (yunsuan2 == 1) { dataGridView1.Rows.Add(i + 1, num1, "-", num2, "="); }//减法有负数 95 } 96 else if (checkBox2.Checked == false)//减法没有负数 97 { 98 if (yunsuan2 == 0) { dataGridView1.Rows.Add(i + 1, num1, "+", num2, "="); } 99 else if (yunsuan2 == 1 && num1 > num2) { dataGridView1.Rows.Add(i + 1, num1, "-", num2, "="); }//减法没有负数 100 else if (yunsuan2 == 1 && num1 <= num2) { dataGridView1.Rows.Add(i + 1, num2, "-", num1, "="); }//减法没有负数 101 } 102 } 103 104 //dataGridView1.Rows.Add(i+1, num1, "+",num2,"="); 105 } 106 } 107 108 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 109 { 110 111 } 112 113 private void button5_Click(object sender, EventArgs e)//批改 114 { 115 //int a=0, b=0, c=0, addition, division=0, subtraction1,subtraction2, multiplication,count=0; 116 for (int i = 0; i < shitishumu; i++) 117 { 118 string x = "+"; 119 string w = "-"; 120 string y = "*"; 121 string z = "/"; 122 if (this.dataGridView1.Rows[i].Cells[1].Value.ToString() != null) 123 { 124 a = int.Parse(this.dataGridView1.Rows[i].Cells[1].Value.ToString()); 125 } 126 if (this.dataGridView1.Rows[i].Cells[3].Value.ToString() != null) 127 { 128 b = int.Parse(this.dataGridView1.Rows[i].Cells[3].Value.ToString()); 129 } 130 addition = a + b; 131 subtraction1 = a - b; 132 subtraction2 = b - a; 133 multiplication = a * b; 134 if (b != 0) 135 { 136 division = a / b; 137 } 138 139 if (this.dataGridView1.Rows[i].Cells[5].Value == null) 140 { 141 this.dataGridView1.Rows[i].Cells[6].Value = "错误"; 142 count = count + 1; 143 } 144 else if (this.dataGridView1.Rows[i].Cells[5].Value != null) 145 { 146 c = int.Parse(this.dataGridView1.Rows[i].Cells[5].Value.ToString()); 147 148 if (x == this.dataGridView1.Rows[i].Cells[2].Value.ToString())//判断加法结果 149 { 150 if (c == addition) 151 { 152 this.dataGridView1.Rows[i].Cells[6].Value = "正确"; 153 } 154 else if (c != addition) 155 { 156 this.dataGridView1.Rows[i].Cells[6].Value = "错误"; 157 count = count + 1; 158 } 159 160 } 161 else if (w == this.dataGridView1.Rows[i].Cells[2].Value.ToString())//判断减法结果 162 { 163 if (c == subtraction1 || c == subtraction2) 164 { 165 this.dataGridView1.Rows[i].Cells[6].Value = "正确"; 166 } 167 else if (c != subtraction1 && c != subtraction2) 168 { 169 this.dataGridView1.Rows[i].Cells[6].Value = "错误"; 170 count = count + 1; 171 } 172 173 } 174 if (y == this.dataGridView1.Rows[i].Cells[2].Value.ToString())//判断乘法结果 175 { 176 if (c == multiplication) 177 { 178 this.dataGridView1.Rows[i].Cells[6].Value = "正确"; 179 } 180 else if (c != multiplication) 181 { 182 this.dataGridView1.Rows[i].Cells[6].Value = "错误"; 183 count = count + 1; 184 } 185 186 } 187 if (z == this.dataGridView1.Rows[i].Cells[2].Value.ToString())//判断除法结果 188 { 189 if (c == division) 190 { 191 this.dataGridView1.Rows[i].Cells[6].Value = "正确"; 192 } 193 else if (c != division) 194 { 195 this.dataGridView1.Rows[i].Cells[6].Value = "错误"; 196 count = count + 1; 197 } 198 } 199 } 200 201 } 202 richTextBox1.Text += "正确数目:" + (shitishumu-count) + "\n"; 203 richTextBox1.Text += "错误数目:" + count + "\n"; 204 205 } 206 207 } 208 }
四、运行结果截图
五、设计题目遇到的问题与心得体会
遇到的问题:
datagridview控件以前没有用到过,现在是现学现卖,只是简单的学了控件的输入输出,学习了如何接收用户在单元格里的输入值。
心得体会:
不要觉得问题多么多么的难弄,程序多么多么的难编写,每一个大的程序大的项目都是一个个小的程序,小的功能搭建在一起实现的,我们在做程序的时候,不要想着一下子把所有的功能都实现了,一个一个功能的来,分成若干个小模块,小模块的问题解决了,那么也就完成,慢慢来,慢慢来,切勿浮躁。