四则运算《《《代码封装
2015-11-28 15:32
274 查看
[b]设计思路:[/b]
因为TabControl可以设置不同页的选项卡。所以我用它来分页,进行出题,答题设置。然后用savefiledialog保存所出题目。设置两个RichTextBox保存所出题目和出好题后做题时显示的题目。用Count计算做题总数,Right计算做正确的数目。点击结束时弹出Form对话框显示做题情况。
具体实现代码:
[b]Form1.cs[/b]
[b]Form2.cs[/b]
[b]代码封装:Class1.cs[/b]
[b]Form1.cs调用封装代码:[/b]
[b]运行过程:[/b]
保存所出题目
保存成功
运算
结束运算
[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]
这次做题虽然遇到很多困难,欣慰的是我还是坚持把它做完了。经过这次编程深感自己平时练习太少,知识面不广,所以我以后要多加练习,多多敲代码。
相关文章推荐
- spring核心组件
- python 切片
- 《Windows核心编程》の托盘程序的编写(CSystemTray类) (转)
- eclipse运行android时Console最后提示Done!不运行程序
- Python的渔网生成工具。属性表操作
- C#编程实现发送邮件的方法(可添加附件)
- c++中exit的使用
- 解决报错libc++abi.dylib: terminate_handler unexpectedly threw an exception
- C++primer plus第六版课后编程练习答案8.7
- 第一行代码总结:13.5.2进行单元测试
- 第一行代码总结:13.5.1创建测试工程
- 第一行代码总结:13.5编写测试用例
- 第一行代码总结:13.3定制自己的日志工具
- Java学习理解路线图
- java实现二叉树的中序遍历和后续遍历
- C++primer plus第六版课后编程练习答案8.6
- C++变量及初始化的总结
- spring properties 资源文件 方式一
- python线程和进程,生产者消费者模型
- 个人优秀代码片段2