1. 程式人生 > >四則運算-第三次實驗

四則運算-第三次實驗

題目要求

1)能進行線上答題

2)能判蹲四則運算的對錯

3)能儲存四則運算

 

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 _20160674_李秉謙
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void button1_Click(object sender, EventArgs e)
{
dataGridView1.RowCount = 30; //定義表格的行數
Random r = new Random(); //取隨機數的函式
for (int i = 0; i < 30; i++)
{
double num1 = r.Next(0, 99); //取隨機數
double num2 = r.Next(0, 99);
int a = r.Next(0, 3);
double c = 1;
char signal = '+';
if (a % 3 == 0) //隨機取四則運算的符號
{
signal = '+';
c = num1 + num2;
}
else if (a % 3 == 1)
{
signal = '-';
if (num1 < num2)
{
double temp;
temp = num1;
num1 = num2;
num2 = temp;
}
c = num1 - num2;
}
else if (a % 3 == 2)
{
signal = '*';
c = num1 * num2;
}
else if (num1 / num2 == 0 && num1 != 0 && num2 != 0)
{
signal = '/';
c = num1 / num2;
}
dataGridView1.Rows[i].Cells[2].Value = c; //四則運算的正確結果
string s = Convert.ToString(num1) + signal + Convert.ToString(num2) + '=';
dataGridView1.Rows[i].Cells[0].Value = s; //顯示四則運算
}
}

private void button2_Click(object sender, EventArgs e)
{
double q = 30;
dataGridView1.RowCount = (int)q;
dataGridView1.Columns[2].Visible = true; //將列“正確結果”和“結果正確性”變為可見
dataGridView1.Columns[3].Visible = true;
int a = 0;
for (int i = 0; i < q; i++) //判斷結果正確性
{
if (Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value) == Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value))
{
dataGridView1.Rows[i].Cells[3].Value = "正確";
a += 1;
}
else
{
dataGridView1.Rows[i].Cells[3].Value = "錯誤";
}
}
}

private void button3_Click(object sender, EventArgs e)
{
string path = @"D:\C語言\錯題本";
if (File.Exists(path))
{
File.Delete(path);
}
string wrrong = "";
double q = 30;
dataGridView1.RowCount = 30;
for (int i = 0; i < 30; i++)
{
if (Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value) != Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value))
{
wrrong += Convert.ToString(dataGridView1.Rows[i].Cells[0].Value + "\r\n");
}
}
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(wrrong);
sw.Close();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}

設計思想:想到線上答題就想到用到C#的控制元件,方便簡潔