程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 四則運算生成器升級版2.0,四則運算生成器2.0

四則運算生成器升級版2.0,四則運算生成器2.0

編輯:C#入門知識

四則運算生成器升級版2.0,四則運算生成器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控件以前沒有用到過,現在是現學現賣,只是簡單的學了控件的輸入輸出,學習了如何接收用戶在單元格裡的輸入值。

心得體會:

不要覺得問題多麼多麼的難弄,程序多麼多麼的難編寫,每一個大的程序大的項目都是一個個小的程序,小的功能搭建在一起實現的,我們在做程序的時候,不要想著一下子把所有的功能都實現了,一個一個功能的來,分成若干個小模塊,小模塊的問題解決了,那麼也就完成,慢慢來,慢慢來,切勿浮躁。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved