标题:论坛进行C# 最基础的考试,请大家都来试试
取消只看楼主
一线两电脑
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2007-7-22
 问题点数:0 回复次数:1 
论坛进行C# 最基础的考试,请大家都来试试



下面是计算器的代码,里边有许多错误,每找到一处,加3分;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double num1 = 0, num2 = 0, result = 0; //用于描述第一个操作数,第二个操作数和结果
int type = 0;  //用于描述运算符的类型
int dot_num = 1;
bool is_num1 = false; //描述是否为第一个操作符
bool is_dot = false; //描述是否为小数
bool is_fushu = false; //描述是否为负数
bool doted = false; //为小数的标识

void func(int numb)
{
if (is_num1 ==false )
{
if (is_dot == true)
{
double tempNum = 1;
for (int i = 0; i < dot_num; i++)
{
tempNum = 10 * tempNum;
}
if (!is_fushu)
num1 = num1 + (double)numb / (double)(tempNum);
else
num1 = num1 - (double)numb / (double)(tempNum);
dot_num++;
}
else
{
if (!is_fushu)
num1 = num1 * 10 + numb;
else
num1 = num1 * 10 - numb;
}
textBox1.Text = num1.ToString();
}
else
{
if(is_dot==true)
{
double tempNum = 1;
for (int i=0;i<dot_num;i++)
{
tempNum=10*tempNum;
}
if(!is_fushu)
num2=num2+(double)numb/(double)(tempNum);
else
num2=num2-(double)numb/(double)(tempNum);
dot_num++;
}
else
{
if(!is_fushu)
num2=num2*10+numb;
else
num2=num2*10-numb;
}
textBox1.Text=num2.ToString();
}
}
void calType()
{
is_num1 = true; //第一个操作数标识为真
dot_num = 1;
is_dot = false;
is_fushu = false;
doted = false;
}

private void button1_Click(object sender, EventArgs e)
{
func(1);
}

private void button2_Click(object sender, EventArgs e)
{
func(2);
}

private void button3_Click(object sender, EventArgs e)
{
func(3);
}

private void button4_Click(object sender, EventArgs e)
{
func(4);
}

private void button5_Click(object sender, EventArgs e)
{
func(5);
}
private void button6_Click(object sender, EventArgs e)
{
func(6);
}
private void button7_Click(object sender, EventArgs e)
{
func(7);
}
private void button8_Click(object sender, EventArgs e)
{
func(8);
}
private void button9_Click(object sender, EventArgs e)
{
func(9);
}
private void button10_Click(object sender, EventArgs e)
{
func(0);
}

[此贴子已经被作者于2007-8-22 8:35:59编辑过]

搜索更多相关主题的帖子: 计算器 考试 基础 bool 
2007-08-22 00:07
一线两电脑
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2007-7-22
得分:0 
回复:(一线两电脑)论坛进行C# 最基础的考题,请大家...

private void button11_Click(object sender, EventArgs e)
{
is_dot = true;
if (doted == false)
{
doted = true;
dot_num = 1;
}
}

private void button13_Click(object sender, EventArgs e)
{
type = 1;
calType();
textBox1.Text = "+";
}

private void button14_Click(object sender, EventArgs e)
{
type = 2;
textBox1.Text = "-";
calType();
}

private void button15_Click(object sender, EventArgs e)
{
type = 3;
textBox1.Text = "*";
calType();
}

private void button16_Click(object sender, EventArgs e)
{
type = 4;
textBox1.Text = "/";
calType();
}

private void button17_Click(object sender, EventArgs e)
{
textBox1.Text = "";
num1 = 0; num2 = 0; result = 0;
type = 0;
dot_num = 1;
is_num1 = false;
is_dot = false;
is_fushu = false;
}

private void button12_Click(object sender, EventArgs e)
{
switch (type)
{
case 1: result = num1 + num2;
break;
case 2: result = num1 - num2;
break;
case 3: result = num1 * num2;
break;
case 4: result = num1 / num2;
break;
}
textBox1.Text = textBox1.Text + "=" + result.ToString();
num1 = 0;
num2 = 0;
is_num1 = false;
dot_num = 1;
is_dot = false;
is_fushu = false;
doted = false;

}
}
}


2007-08-22 00:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-164454-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.832813 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved