您的位置:首页 > 编程语言

四则运算《《《代码封装

2015-11-28 15:32 274 查看
[b]设计思路:[/b]

因为TabControl可以设置不同页的选项卡。所以我用它来分页,进行出题,答题设置。然后用savefiledialog保存所出题目。设置两个RichTextBox保存所出题目和出好题后做题时显示的题目。用Count计算做题总数,Right计算做正确的数目。点击结束时弹出Form对话框显示做题情况。

具体实现代码:

[b]Form1.cs[/b]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace sum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//   bool bFileName = false;                    //标记文本框内容是否被命名
string fileName;                           //存储当前文件的名字
public static int Count = 0;               //计算所做的题数
public static int Right = 0;               //回答正确的题数
public static double answer;
int i;
public void SaveTofile()                   //保存所出题目的方法
{
sfd.InitialDirectory = "C\\";               //设置保存默认目录
sfd.Filter = "txt files(*.txt)|*.txt|all files(*.*)|*.*";
sfd.FilterIndex = 1;                       //默认保存类型为txt
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
fileName = sfd.FileName;
//  bFileName = true;
}
}

private void button1_Click(object sender, EventArgs e)  //把所出题目保存在txt文档中
{
StreamWriter stw1 = File.AppendText("number1.txt");
stw1.WriteLine(textBox1.Text);
stw1.Close();
StreamWriter stw2 = File.AppendText("number2.txt");
stw2.WriteLine(textBox2.Text);
stw2.Close();
StreamWriter stw3 = File.AppendText("number3.txt");
stw3.WriteLine(textBox3.Text);
stw3.Close();
richTextBox1.Text += textBox1.Text + textBox2.Text + textBox3.Text + textBox4.Text + "" + "\n";
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}

private void save_Click(object sender, EventArgs e) //保存
{
SaveTofile();
MessageBox.Show("保存成功!");

}

private void button2_Click(object sender, EventArgs e)  //打开
{

richTextBox2.Text = richTextBox1.Text;
}

private void write_Click(object sender, EventArgs e)
{
string[] one = new string[100];
one = File.ReadAllLines("number1.txt");
textBox5.Text = one[0];
string[] two = new string[100];
two = File.ReadAllLines("number2.txt");
textBox6.Text =two[0];
string[] three = new string[100];
three = File.ReadAllLines("number3.txt");
textBox7.Text = three[0];

}

private void textBox8_KeyDown(object sender, KeyEventArgs e)
{
Class1 cs = new Class1();
cs.X = double.Parse(textBox5.Text);
cs.Y = double.Parse(textBox7.Text);
cs.result = answer;
string opera = textBox6.Text;
cs.opera = opera;
cs.Add();                         //调用Class1的Add方法;
cs.Min();                        //调用Class1的Min方法;
cs.Mul();                        //调用Class1的Mul方法;
cs.Div();                        //调用Class1的Div方法;
if (e.KeyCode == Keys.Enter)
{
if (textBox8.Text == cs.result.ToString())
{
MessageBox.Show("回答正确");
Count++;
Right++;
}
else
{
MessageBox.Show("回答错误");
Count++;
}
i++;
textBox8.Clear();
string[] one = new string[100];
one = File.ReadAllLines("number1.txt");
textBox5.Text = one[i];
string[] two = new string[100];
two = File.ReadAllLines("number2.txt");
textBox6.Text = two[i];
string[] three = new string[100];
three = File.ReadAllLines("number3.txt");
textBox7.Text = three[i];
if (one.Count() == i)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
}

private void button4_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}

private void button3_Click(object sender, EventArgs e)   //退出程序
{
Application.Exit();
}

}
}


[b]Form2.cs[/b]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sum
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.Count.ToString();
textBox2.Text = Form1.Right.ToString();
textBox3.Text = ((Form1.Right / (double)(Form1.Count)) * 100).ToString() + "%";
}

}
}


[b]代码封装:Class1.cs[/b]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sum
{
class Class1
{
private double x;   //定义x变量;

public double X  //封装字段
{
get { return x; }
set { x = value; }

}
private double y;  //定义y变量

public double Y        //封装字段
{
get { return y; }
set { y = value; }

}
public string opera = "";  //运算符号
public double result;      //计算结果
public void Add()          //加法
{
if (opera == "+")
{
result = X + Y;
}
}
public void Min()              //减法
{
if (opera == "-")
{
result = X - Y;
}
}
public void Mul()             //乘法
{
if (opera == "*")
{
result = X * Y;
}
}
public void Div()          //除法
{
if (opera == "/")
{
result = X / Y;
}
}

}
}


[b]Form1.cs调用封装代码:[/b]

private void textBox8_KeyDown(object sender, KeyEventArgs e)
{
Class1 cs = new Class1();
cs.X = double.Parse(textBox5.Text);
cs.Y = double.Parse(textBox7.Text);
cs.result = answer;
string opera = textBox6.Text;
cs.opera = opera;
cs.Add();                         //调用Class1的Add方法;
cs.Min();                        //调用Class1的Min方法;
cs.Mul();                        //调用Class1的Mul方法;
cs.Div();                        //调用Class1的Div方法;
if (e.KeyCode == Keys.Enter)
{
if (textBox8.Text == cs.result.ToString())
{
MessageBox.Show("回答正确");
Count++;
Right++;
}
else
{
MessageBox.Show("回答错误");
Count++;
}
i++;
textBox8.Clear();
string[] one = new string[100];
one = File.ReadAllLines("number1.txt");
textBox5.Text = one[i];
string[] two = new string[100];
two = File.ReadAllLines("number2.txt");
textBox6.Text = two[i];
string[] three = new string[100];
three = File.ReadAllLines("number3.txt");
textBox7.Text = three[i];
if (one.Count() == i)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
}


[b]运行过程:[/b]

保存所出题目



保存成功



运算





结束运算



[b]总结:[/b]

这次做题虽然遇到很多困难,欣慰的是我还是坚持把它做完了。经过这次编程深感自己平时练习太少,知识面不广,所以我以后要多加练习,多多敲代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: