四则运算-第三次实验

Posted lbq-0412

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算-第三次实验相关的知识,希望对你有一定的参考价值。

题目要求

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 + " ");
}
}
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(wrrong);
sw.Close();
}

private void Form1_Load(object sender, EventArgs e)
{

}
}

设计思想:想到在线答题就想到用到C#的控件,方便简洁
技术分享图片

 









































































































以上是关于四则运算-第三次实验的主要内容,如果未能解决你的问题,请参考以下文章

四则运算第三次

第三次实验总结

第三次实验总结

第三次实验作业总结

第三次作业

四则运算网页版