我是 C# 新手,我需要这方面的帮助。
我正在做一个基本的计算器,但我无法进行多重操作数计算。
示例:1+1+1 = 3。
如果我尝试这个,我得到 2 作为结果,因为它添加了最后 2 个值。
这是代码。
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 calculadora
{
public partial class Form1 : Form
{
double operador1 = 0;
double operador2 = 0;
char operando;
double resultado = 0;
double extra;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
lblpantalla.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
lblpantalla.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
lblpantalla.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
lblpantalla.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
lblpantalla.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
lblpantalla.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
lblpantalla.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
lblpantalla.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
lblpantalla.Text += "9";
}
private void button0_Click(object sender, EventArgs e)
{
lblpantalla.Text += "0";
}
private void button_clear_Click(object sender, EventArgs e)
{
lblpantalla.Text = string.Empty;
operador1 = 0;
operador2 = 0;
}
private void button_plus_Click(object sender, EventArgs e)
{
operador1 = Convert.ToDouble(lblpantalla.Text);
operando = '+';
lblpantalla.Text = string.Empty;
}
private void button_minus_Click(object sender, EventArgs e)
{
operador1 = Convert.ToDouble(lblpantalla.Text);
operando = '-';
lblpantalla.Text = string.Empty;
}
private void button_multy_Click(object sender, EventArgs e)
{
operador1 = Convert.ToDouble(lblpantalla.Text);
operando = '*';
lblpantalla.Text = string.Empty;
}
private void button_divide_Click(object sender, EventArgs e)
{
operador1 = Convert.ToDouble(lblpantalla.Text);
operando = '/';
lblpantalla.Text = string.Empty;
}
private void button_equal_Click(object sender, EventArgs e)
{
try
{
operador2 = Convert.ToDouble(lblpantalla.Text);
double ug;
switch (operando)
{
case '+': //suma
resultado = (operador1 + operador2);
break;
case '-': //resta
resultado = (operador1 - operador2);
break;
case '*': //multiply
resultado = (operador1 * operador2);
break;
case '/': //division
if (operador2 != 0)
{
resultado = (operador1 / operador2);
}
else
{
lblpantalla.Text = "Can't divide by 0";
}
break;
}
lblpantalla.Text = resultado.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error occured. Details: " +
ex.Message);
}
}
private void button_dot_Click(object sender, EventArgs e)
{
if (lblpantalla.Text.Contains("."))
{
return;
}
lblpantalla.Text += ".";
}
private void button_reciproco_Click(object sender, EventArgs e)
{
double opera1;
if (double.TryParse(lblpantalla.Text, out opera1))
{
lblpantalla.Text = (-opera1).ToString();
}
}
private void button_recipro_Click(object sender, EventArgs e)
{
double opera1;
if (double.TryParse(lblpantalla.Text, out opera1))
{
lblpantalla.Text = (1/opera1).ToString();
}
}
}
}
提前致谢。