私は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();
}
}
}
}
前もって感謝します。