PDA

View Full Version : مبتدی: مشکل با پیاده سازی ماشین حساب



jaykob
شنبه 29 اسفند 1388, 23:50 عصر
سلام

دوستان من کلا با پیاده سازی ماشین حساب در C# مشکل دارم با vb راحت پیاده سازی کردم قبلا اما الان با C# نمی تونم نمی خوام کلی سورس رو برام بزارید می خوام کم کم بگید هم خودم با شیوه حل کردنش آشنا بشم و هم یک چیزی یاد بگیریم من تا اون جایی جلو رفتم می زارم براتون تا اینجا فقط جمع درست کار می کنه :)




namespace Calculator
{
public partial class calculator : Form
{
double val, i;

public void Clear()
{
txtvalue.Text = string.Empty;
}
public calculator()
{
InitializeComponent();
}


private void btnone_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "1";
}

private void btntwo_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "2";
}

private void btnthree_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "3";
}

private void btnfour_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "4";
}

private void btnfive_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "5";
}

private void btnsix_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "6";
}

private void btnseven_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "7";
}

private void btneight_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "8";
}

private void button3_Click(object sender, EventArgs e)
{
txtvalue.Text = txtvalue.Text + "9";
}
private void btnmosavi_Click(object sender, EventArgs e)
{
double tmp,tmp2;
if (i == 1)
{
tmp2= double.Parse(txtvalue.Text);
tmp = val + tmp2;
txtvalue.Text = tmp.ToString();
val = 0;

}
else if (i == 2)
{
tmp2 = double.Parse(txtvalue.Text);
tmp = val - tmp2;
txtvalue.Text = tmp.ToString();
val = 0;
}
}

private void btnplus_Click(object sender, EventArgs e)
{
val +=double.Parse(txtvalue.Text);
i = 1;
Clear();
}

private void btnmenha_Click(object sender, EventArgs e)
{
val =double.Parse(txtvalue.Text);
i = 2;
Clear();

}

private void btnclear_Click(object sender, EventArgs e)
{
Clear();
val = 0;
}


}
}


ممنون می شم جوری بگید که با روش حل مسئله هم آشنا بشم .

:قلب:

exlord
یک شنبه 01 فروردین 1389, 10:58 صبح
اولا :

txtvalue.Text = txtvalue.Text + "1"; --> txtvalue.Text += "1";

خوب الان مشکلت چیه .... بقیشم همی جوری بنویس دیگه.....

Sirwan Afifi
یک شنبه 01 فروردین 1389, 19:36 عصر
این سورسو نگا کن ببین مشکلت حل میشه :

namespace Calculator
{
enum Operator
{
pluse,
minus,
multiply,
division,
noOp
}
public partial class Form1 : Form
{
long operand1 = 0, operand2 = 0;
string operation, preOperation;
bool clearNum = false;
bool firstTime = true;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
txt_Result.Text = "0";
}

private void txt_Result_TextChanged(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text.Trim();
if(txt_Result.Text.Length != 0) try
{
operand2 = Convert.ToInt64(txt_Result.Text);
}
catch
{
txt_Result.Text = txt_Result.Text.Substring(0, txt_Result.Text.Length - 1);
}
}

private void btn_num0_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "0";
}

private void btn_num1_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "1";
}

private void btn_num2_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "2";
}

private void btn_num3_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "3";
}

private void btn_num4_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "4";
}

private void btn_num5_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "5";
}

private void btn_num6_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "6";
}

private void btn_num7_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "7";
}

private void btn_num8_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "8";
}

private void btn_num9_Click(object sender, EventArgs e)
{
if (clearNum)
{
txt_Result.Text = "0";
clearNum = false;
}
if (txt_Result.Text == "0") txt_Result.Text = "";
txt_Result.Text = txt_Result.Text + "9";
}

private void btn_Plus_Click(object sender, EventArgs e)
{
clearNum = true;
preOperation = operation;
operation = Operator.pluse.ToString();
calculate();
}

private void calculate()
{
//MessageBox.Show(operand1.ToString() + " " + operation + " " + operand2.ToString()+ " " + firstTime.ToString());
long result = 0;
if (operation == Operator.noOp.ToString()) return;
try
{
if (operation != preOperation && preOperation != Operator.noOp.ToString() && preOperation != null)
{
if (preOperation == Operator.pluse.ToString()) result = operand1 + operand2;
else if (preOperation == Operator.minus.ToString()) result = operand1 - operand2;
else if (preOperation == Operator.multiply.ToString()) result = operand1 * operand2;
else if (preOperation == Operator.division.ToString()) result = operand1 / operand2;
operand1 = result;
txt_Result.Text = result.ToString();
}
else if (firstTime)
{
operand1 = operand2;
firstTime = false;
}
else
{
if (operation == Operator.pluse.ToString()) result = operand1 + operand2;
else if (operation == Operator.minus.ToString()) result = operand1 - operand2;
else if (operation == Operator.multiply.ToString()) result = operand1 * operand2;
else if (operation == Operator.division.ToString()) result = operand1 / operand2;
operand1 = result;
txt_Result.Text = result.ToString();
}
}
catch
{
string msg = "مقدار نتيجه بزرگ است يا تقسم بر صفر انجام گرفته است\n";
msg += "\n";
msg += " دوباره سعي كنيد";
MessageBox.Show(msg);
txt_Result.Text = "0";
operand1 = 0;
operand2 = 0;
operation = Operator.noOp.ToString();
preOperation = operation;
}

}

private void btn_Clear_Click(object sender, EventArgs e)
{
firstTime = true;
txt_Result.Text = "0";
operand1 = 0;
operand2 = 0;
operation = Operator.noOp.ToString();
}

private void btn_Back_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text.Substring(0, txt_Result.Text.Length - 1);
}

private void btn_Minus_Click(object sender, EventArgs e)
{
clearNum = true;
preOperation = operation;
operation = Operator.minus.ToString();
calculate();
}

private void btn_multiply_Click(object sender, EventArgs e)
{
clearNum = true;
preOperation = operation;
operation = Operator.multiply.ToString();
calculate();
}

private void btn_Divide_Click(object sender, EventArgs e)
{
clearNum = true;
preOperation = operation;
operation = Operator.division.ToString();
calculate();
}

private void btn_Equal_Click(object sender, EventArgs e)
{
long result=0;
try
{
if (!firstTime)
{
if (operation == Operator.pluse.ToString()) result = operand1 + operand2;
else if (operation == Operator.minus.ToString()) result = operand1 - operand2;
else if (operation == Operator.multiply.ToString()) result = operand1 * operand2;
else if (operation == Operator.division.ToString()) result = operand1 / operand2;
operand1 = result;
txt_Result.Text = result.ToString();
}
}
catch
{
string msg = "مقدار نتيجه بزرگ است يا تقسم بر صفر انجام گرفته است\n";
msg += "\n";
msg += " دوباره سعي كنيد";
MessageBox.Show(msg);
txt_Result.Text = "0";
operand1 = 0;
operand2 = 0;
operation = Operator.noOp.ToString();
preOperation = operation;
}
firstTime = true;
operation = Operator.noOp.ToString();
}

}
}

daivid_ d31r1
دوشنبه 02 فروردین 1389, 02:07 صبح
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace calc
{
public partial class Form1 : Form
{
string Oprator;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
//1
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "1";
}
private void button1_Click(object sender, EventArgs e)
{
//2
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "2";
}
private void button6_Click(object sender, EventArgs e)
{
//3
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "3";
}
private void button5_Click(object sender, EventArgs e)
{
//4
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "4";
}
private void button4_Click(object sender, EventArgs e)
{
//5
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "5";
}
private void button9_Click(object sender, EventArgs e)
{
//6
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "6";
}
private void button8_Click(object sender, EventArgs e)
{
//7
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "7";
}
private void button7_Click(object sender, EventArgs e)
{
//8
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "8";
}
private void button11_Click(object sender, EventArgs e)
{
//9
if (Oprator == "=")
{ textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "9";
}
private void button3_Click(object sender, EventArgs e)
{
//0
if (Oprator == "=")
{
textBox1.ResetText();
Oprator = "";
}
textBox1.Text += "0";
}
private void button16_Click(object sender, EventArgs e)
{
// /
textBox1.Tag = textBox1.Text;
textBox1.ResetText();
Oprator = "/";
}
private void button15_Click(object sender, EventArgs e)
{
// *
textBox1.Tag = textBox1.Text;
textBox1.ResetText();
Oprator = "*";
}

private void button10_Click(object sender, EventArgs e)
{
//+
textBox1.Tag = textBox1.Text;
textBox1.ResetText();
Oprator = "+";

}
private void button12_Click(object sender, EventArgs e)
{
//-
textBox1.Tag = textBox1.Text;
textBox1.ResetText();
Oprator = "-";
}
private void button13_Click(object sender, EventArgs e)
{
//=
decimal eqaql =0;
try
{
switch (Oprator)
{
case "+":
eqaql = Convert.ToDecimal(textBox1.Tag.ToString()) + Convert.ToDecimal(textBox1.Text);
textBox1.Text = eqaql.ToString();
Oprator = "+";
break;
case "-":
eqaql = Convert.ToDecimal(textBox1.Tag.ToString()) - Convert.ToDecimal(textBox1.Text);
textBox1.Text = eqaql.ToString();
Oprator = "-";
break;
case "*":
eqaql = Convert.ToDecimal(textBox1.Tag.ToString()) * Convert.ToDecimal(textBox1.Text);
textBox1.Text = eqaql.ToString();
Oprator = "*";
break;
case "/":
eqaql = Convert.ToDecimal(textBox1.Tag.ToString()) / Convert.ToDecimal(textBox1.Text);
textBox1.Text = eqaql.ToString();
Oprator = "/";
break;
default:
Oprator = "";
break;
}
Oprator = "=";
}
catch (Exception) { }
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.ResetText();
textBox1.Tag = null;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(100, 100);
}
}
}