码迷,mamicode.com
首页 > 其他好文 > 详细

改动后的封装

时间:2015-12-03 18:42:43      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace 简易运算
{
class Pack //封装
{
private int x;
private int y;
private string oper;
private int anster = 0;

public int OP
{
set
{
x = value;
}
get
{
return x;
}
}
public int Pt
{
set
{
y = value;
}
get
{
return y;
}
}
public string Oper
{
set
{
oper = value;
}
get
{
return oper;
}
}
public int Aster
{
get
{
return anster;
}
}
public int yusuan()
{
switch (oper)
{
case "+":
anster = x + y;
break;
case "-":
if (x > y)
anster = x - y;
//else
// throw new Exception("被减数不能小于减数,既不能出现负数");
break;
case "*":
anster = x * y;
break;
case "/":
//if (y == 0)
//{
// throw new Exception("被除数不能等于零!");

//}
//else
anster = x / y;
break;
}
return anster;

}
public int fanhui()
{
return anster;
}

}
}
<span style="font-size: 18px;">Form1代码:</span>
<br>using System;
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 计算机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = "F:/";
public static int count = 0;
public static int t = 0;
public static int right = 0;
public static int sum;
int x = 0;
int y = 0;
private void Form1_Load(object sender, EventArgs e)
{
Class1 s = new Class1();
s.text1();
}

private void button1_Click(object sender, EventArgs e)
{
StreamWriter l1 = File.AppendText("1.txt");
WriteLine(text1.Text);
Close();
StreamWriter l2 = File.AppendText("2.txt");
WriteLine(comboBox1.SelectedItem.ToString());
Close();
StreamWriter l3 = File.AppendText("3.txt");
WriteLine(text2.Text);
Close();
richTextBox1.Text += text1.Text + comboBox1.SelectedItem.ToString() + text2.Text + "\n";
x++;
text1.Text = "";
text2.Text = "";
savet.Enabled = true;
opent.Enabled = true;
text1.Focus();
}

private void savet_Click(object sender, EventArgs e)
{
SaveFileDialog TxtSaveDialog = new SaveFileDialog();
TxtSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
if (File.Exists(path))
{
this.richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
MessageBox.Show("储存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.richTextBox1.Clear();
savet.Enabled = false;
}
else
{
if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
{

this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.RichText);
MessageBox.Show("储存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.richTextBox1.Clear();
savet.Enabled = false;
}
}
}

private void opent_Click(object sender, EventArgs e)
{
OpenFileDialog TxTOpenDialog = new OpenFileDialog();
TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
{
path = TxTOpenDialog.FileName;
this.richTextBox1.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
savet.Enabled = false;
opent.Enabled = false;
MessageBox.Show("打开成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}

private void button2_Click(object sender, EventArgs e)
{
jishi.Text = t.ToString();
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
string[] l1 = new string[100];
l1 = File.ReadAllLines("1.txt");
string[] l2 = new string[100];
l2 = File.ReadAllLines("2.txt");
string[] l3 = new string[100];
l3 = File.ReadAllLines("3.txt");
Box1.Text = l1[0];
Box4.Text = l2[0];
Box2.Text = l3[0];
}

private void Box3_KeyDown(object sender, KeyEventArgs e)
{

//string s = Box4.Text;
Pack operast = new Pack();
operast.Pt = int.Pars(Box1.Text);
operast.Oper = Box4.Text;
//switch (s)
//{
//case "+":
//sum = int.Parse(Box1.Text) + int.Parse(Box2.Text);

//break;
//case "-":
//sum = int.Parse(Box1.Text) - int.Parse(Box2.Text);

//break;
//case "*":
//sum = int.Parse(Box1.Text) * int.Parse(Box2.Text);

//break;
//case "/":
//sum = int.Parse(Box1.Text) / int.Parse(Box2.Text);

//break;
//default:
//break;
//}


operast.yusuan();
if (e.KeyCode == Keys.Enter)
{
if (Box4.Text.ToString() == Box3.Text)
{
MessageBox.Show("回答正确");
count++;
right++;
}
else
{
MessageBox.Show("回答错误");
count++;
}
y++;
if (y < x)
{
Box3.Clear();
string[] ll = new string[100];
ll = File.ReadAllLines("1.txt");
Box1.Text = ll[y];
string[] l2 = new string[100];
l2 = File.ReadAllLines("2.txt");
Box4.Text = l2[y];
string[] l3 = new string[100];
l3 = File.ReadAllLines("3.txt");
Box2.Text = l3[y];
}
else
{
jishi.Enabled = false;
Form2 frm = new Form2();
frm.ShowDialog();
}
}
}

private void timer1_Tick(object sender, EventArgs e)
{
t = t + 1;
jishi.Text = t.ToString();
}

private void Box1_TextChanged(object sender, EventArgs e)
{

}
}


<span style="font-size: 18px;">Form2代码:</span><br>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 计算机
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

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

private void label1_Click(object sender, EventArgs e)
{

}
}

改动后的封装

标签:

原文地址:http://www.cnblogs.com/hanghang0829/p/5017033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!