1. 程式人生 > >線上答題

線上答題

 

設計思想:先通過之前的程式輸出四則運算的式子

                  再計算出正確答案

                  然後再輸入答案

                  最後比較輸入的答案與正確答案進行判斷對錯

日誌:

程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace 線上答題 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } public void button1_Click(object sender, EventArgs e) { dataGridView1.DataSource = null; double n = Convert.ToDouble(textBox3.Text); double
m = Convert.ToDouble(textBox2.Text); double num = Convert.ToDouble(textBox1.Text); double[] a = new double[2* (int)num]; double[] tureans = new double[(int)num]; dataGridView1.RowCount = (int)num; Random r = new Random(); for (int i = 0; i < 2 * num; i++) { double temp = r.Next((int)m, (int)n); a[i] = temp; } string sign = "+"; for(int i=0;i<num ;i++) { double sum=a[i]+a[i+(int)num]; if (a[i] % a[i + (int)num] == 0&&a[i + (int)num]!=0) { sign = "/"; tureans[i] = a[i] / a[i + (int)num]; } else if (sum <= (int)n / 2) { sign = "*"; tureans[i] = a[i] * a[i + (int)num]; } else if (a[i] > a[i + (int)num]) { sign = "-"; tureans[i] = a[i] - a[i + (int)num]; } else { sign = "+"; tureans[i] = a[i] + a[i + (int)num]; } dataGridView1.Rows[i].Cells[0].Value = Convert.ToString(a[i]) + sign + Convert.ToString(a[i+(int)num])+"="; dataGridView1.Rows[i].Cells[2].Value = Convert.ToString(tureans[i]); } } public void button2_Click(object sender, EventArgs e) { string path = @"C:\Users\11134\Desktop\錯題本.txt"; if (File.Exists(path)) { File.Delete(path); } double num = Convert.ToDouble(textBox1.Text); dataGridView1.Columns[2].Visible = true; dataGridView1.Columns[3].Visible = true; string wrrong = ""; for (int i = 0; i < num; i++) { if (Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value) == Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value)) { dataGridView1.Rows[i].Cells[3].Value = ""; } else if (Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value) != Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value)) { dataGridView1.Rows[i].Cells[3].Style.BackColor = Color.Red; dataGridView1.Rows[i].Cells[3].Value = ""; wrrong += Convert.ToString(dataGridView1.Rows[i].Cells[0].Value + "\r\n"); } } StreamWriter sw = new StreamWriter(path); sw.WriteLine(wrrong); sw.Close(); } private void button3_Click(object sender, EventArgs e) { Application.Exit(); } } }

視窗設計截圖:

 

 運算結果截圖:

錯題本截圖: