C#实训报告书--薛波 下载本文

内容发布更新时间 : 2024/11/15 3:45:34星期一 下面是文章的全部内容请认真阅读。

对23个button控件和4个radiobutton控件生成对应的事件,编写代码

public partial class MainForm : Form {

public MainForm() {

InitializeComponent(); }

char oper = '\\0'; double num1 = 0; double num2 = 0;

bool isSecond = false; bool isDone = false; int x;

private void btnEqual_Click(object sender, EventArgs e) {

SetBtnFun(char.Parse(((Button)sender).Text)); }

private void MainForm_KeyPress(object sender,

16

KeyPressEventArgs e) {

SetBtnFun(e.KeyChar); }

void SetBtnFun(char c) {

if (char.IsDigit(c)) {

if (isSecond || isDone) { lblResult.Text = \; isSecond = false; isDone = false; }

string s = lblResult.Text + c.ToString(); s = double.Parse(s).ToString();

lblResult.Text = s; }

else if (c == '+' || c == '-' || c == '*' || c == '/') {

num1 = double.Parse(lblResult.Text); oper = c;

isSecond = true; }

else if (c == '=') {

num2 = double.Parse(lblResult.Text); double result = 0; switch (oper) {

case '+':

result = num1 + num2; break; case '-':

result = num1 - num2; break; case '*':

result = num1 * num2; break; case '/':

17

result = num1 / num2; break; }

lblResult.Text = result.ToString(); num1 = result; isSecond = false; isDone = true; }

else if (c == '.') {

if (lblResult.Text.IndexOf(\) == -1) {

lblResult.Text += \; } }

x = int.Parse(lblResult.Text); }

private void rdoShiliu_CheckedChanged_1(object sender, EventArgs e) {

this.btnA.Enabled = true; this.btnB.Enabled = true; this.btnC.Enabled = true; this.btnD.Enabled = true; this.btnE.Enabled = true; this.btnF.Enabled = true; this.btn2.Enabled = true; this.btn3.Enabled = true; this.btn4.Enabled = true; this.btn5.Enabled = true; this.btn6.Enabled = true; this.btn7.Enabled = true; this.btn8.Enabled = true; this.btn9.Enabled = true;

lblResult.Text = Convert.ToString(x, 16); }

private void rdoShi_CheckedChanged_1(object sender, EventArgs e) {

18

this.btnA.Enabled = false; this.btnB.Enabled = false; this.btnC.Enabled = false; this.btnD.Enabled = false; this.btnE.Enabled = false; this.btnF.Enabled = false; this.btn2.Enabled = true; this.btn3.Enabled = true; this.btn4.Enabled = true; this.btn5.Enabled = true; this.btn6.Enabled = true; this.btn7.Enabled = true; this.btn8.Enabled = true; this.btn9.Enabled = true;

lblResult.Text = Convert.ToString(x, 10); }

private void rdoBa_CheckedChanged_1(object sender, EventArgs e) {

this.btnA.Enabled = false; this.btnB.Enabled = false; this.btnC.Enabled = false; this.btnD.Enabled = false; this.btnE.Enabled = false; this.btnF.Enabled = false; this.btn2.Enabled = true; this.btn3.Enabled = true; this.btn4.Enabled = true; this.btn5.Enabled = true; this.btn6.Enabled = true; this.btn7.Enabled = true; this.btn8.Enabled = false; this.btn9.Enabled = false;

lblResult.Text = Convert.ToString(x, 8); }

private void rdoEr_CheckedChanged_1(object sender, EventArgs e) {

this.btnA.Enabled = false; this.btnB.Enabled = false;

19

this.btnC.Enabled = false; this.btnD.Enabled = false; this.btnE.Enabled = false; this.btnF.Enabled = false; this.btn2.Enabled = false; this.btn3.Enabled = false; this.btn4.Enabled = false; this.btn5.Enabled = false; this.btn6.Enabled = false; this.btn7.Enabled = false; this.btn8.Enabled = false; this.btn9.Enabled = false;

lblResult.Text = Convert.ToString(x, 2); }

private void btnClear_Click(object sender, EventArgs e) {

this.btnA.Enabled = true; this.btnB.Enabled = true; this.btnC.Enabled = true; this.btnD.Enabled = true; this.btnE.Enabled = true; this.btnF.Enabled = true; this.btn2.Enabled = true; this.btn3.Enabled = true; this.btn4.Enabled = true; this.btn5.Enabled = true; this.btn6.Enabled = true; this.btn7.Enabled = true; this.btn8.Enabled = true; this.btn9.Enabled = true; lblResult.Text = \; }

}

项目演示

输入一个数12,四种进制运行结果;

20