热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

计算器的封装

Form1代码如下:namespaceszys{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}

Form1代码如下:

namespace szys
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public static int right = 0;
public static int Count = 0;
private int t;
string path = ".\text1.txt";
int a = 0;
private void button5_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text.Trim());
int b = int.Parse(textBox2.Text.Trim());
Char c
= Convert.ToChar(textBox4.Text.Trim());
Lei con
= new Lei();
con.Max(a,b,c);
Count
++;
StreamWriter baocun1
= File.AppendText("baocun1.txt");
baocun1.WriteLine(textBox1.Text);
baocun1.Close();
StreamWriter baocun2
= File.AppendText("baocun2.txt");
baocun2.WriteLine(textBox4.Text);
baocun2.Close();
StreamWriter baocun3
= File.AppendText("baocun3.txt");
baocun3.WriteLine(textBox2.Text);
baocun3.Close();
richTextBox1.Text
+= textBox1.Text + textBox4.Text + textBox2.Text + label2.Text + textBox3.Text + "\n";
textBox1.Clear();
textBox4.Clear();
textBox2.Clear();
}
private void btnsave_Click(object sender, EventArgs e)//保存已出试题;
{
SaveFileDialog TxtSaveDialog
= new SaveFileDialog();
TxtSaveDialog.Filter
= "文本文档(*.txt)|*.txt";
if (File.Exists(path))
{
this.richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
MessageBox.Show(
"保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.richTextBox1.Clear();
btnsave.Enabled
= false;
}
else
{
if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.PlainText);
MessageBox.Show(
"保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.richTextBox1.Clear();
btnsave.Enabled
= false;
button1.Visible
= true;
}
}
}
private void button1_Click(object sender, EventArgs e)//打开试题;
{

OpenFileDialog TxTOpenDialog
= new OpenFileDialog();
TxTOpenDialog.Filter
= "文本文档(*.txt)|*.txt";
if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
{
path
= TxTOpenDialog.FileName;
this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.PlainText);
btnsave.Enabled
= false;
button1.Enabled
= false;
MessageBox.Show(
"打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void button2_Click(object sender, EventArgs e)//添加算式题;
{

textBox1.Enabled
= false;
textBox2.Enabled
= false;
textBox4.Enabled
= false;
textBox1.BackColor
= Color.Yellow;
textBox4.BackColor
= Color.Yellow;
textBox2.BackColor
= Color.Yellow;
textBox3.BackColor
= Color.Yellow;
string[] m = new string[100];
m
= File.ReadAllLines("baocun1.txt");
textBox1.Text
= m[a];
string[] n = new string[100];
n
= File.ReadAllLines("baocun2.txt");
textBox4.Text
= n[a];
string[] v = new string[100];
v
= File.ReadAllLines("baocun3.txt");
textBox2.Text
= v[a];
a
++;
}
private void textBox3_MouseClick(object sender, MouseEventArgs e)//计时;
{
label3.Text
= t.ToString();
timer1.Enabled
= true;
timer1.Interval
= 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
t
= t + 1;
label3.Text
= t.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(
"一共用时" + label3.Text + "");
Form2 frm2
= new Form2();
frm2.ShowDialog();
}
private void button4_Click(object sender, EventArgs e)
{
//调用;
int a = int.Parse(textBox1.Text.Trim());
int b = int.Parse(textBox2.Text.Trim());
Char c
= Convert.ToChar(textBox4.Text.Trim());
Lei con
= new Lei();
con.Max(a,b,c);
if (con.Answer == int.Parse(textBox3.Text.Trim()))
{
MessageBox.Show(
"恭喜你,小朋友!回答正确!");
right
++;
Count
++;
}
else
{
MessageBox.Show(
"回答错误,继续加油!小朋友");
Count
++;
}
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}

}
}

Form2代码如下:

namespace szys
{
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();
textBox4.Text
= ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";
}
}
}

封装代码:

namespace szys
{
class Lei
{
public int Sum;

public int Answer
{
get { return Sum; }
}
public int Max(int n1,int n2,char yunsuanfu)
{
if (yunsuanfu == '+')
{
return Sum = n1 + n2;
}
else if (yunsuanfu == '-')
{
if (n1 > n2)
{
return Sum = n1 - n2;
}
else
{
MessageBox.Show(
"第一个数要大于第二个数!");
}
}
else if (yunsuanfu == '/')
{
if (n2 == 0 || n2 <0)
{
MessageBox.Show(
"分母不能为0且大于0!");
}
else
{
return Sum = n1 / n2;
}
}
else if (yunsuanfu == '*')
{
return Sum = n1 * n2;
}
return Sum;
}
}
}

 


二、测试

 





推荐阅读
  • 抽空写了一个ICON图标的转换程序
    抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换。具体的程序就在下面,如果看的人多,过两天再把思路写一下。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Android源码中的Builder模式及其作用
    本文主要解释了什么是Builder模式以及其作用,并结合Android源码来分析Builder模式的实现。Builder模式是将产品的设计、表示和构建进行分离,通过引入建造者角色,简化了构建复杂产品的流程,并且使得产品的构建可以灵活适应变化。使用Builder模式可以解决开发者需要关注产品表示和构建步骤的问题,并且当构建流程发生变化时,无需修改代码即可适配新的构建流程。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • Python使用Pillow包生成验证码图片的方法
    本文介绍了使用Python中的Pillow包生成验证码图片的方法。通过随机生成数字和符号,并添加干扰象素,生成一幅验证码图片。需要配置好Python环境,并安装Pillow库。代码实现包括导入Pillow包和随机模块,定义随机生成字母、数字和字体颜色的函数。 ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • VS2010MFC(对话框:为对话框添加控件)
    转自:http:www.jizhuomi.comsoftware151.html上一讲创建了一个名为“Addition”的工程,目的是生成一个实现加法运 ... [详细]
  • ExcelApp#启动excel程序ExcelAppCreateOleObject(“Excel.Application”);#加载文件但不显示文件内容(true表 ... [详细]
author-avatar
吴小彬x
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有