四則運算-第三次實驗

來源:互聯網
上載者:User

標籤:stream   void   draw   tag   orm   ali   info   idv   bsp   

題目要求

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#的控制項,方便簡潔

 

四則運算-第三次實驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.