3
I have a calculator made in WPF C# with basic operations +,-,*,/, % . And now I wanted to try to improve my calculator.
namespace calculadora
{
public delegate float? dlgoperacao(float? a, float? b);
public partial class MainWindow : Window
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"[+-/*]", System.Text.RegularExpressions.RegexOptions.None);
const int PESO = 10;
int? divisor = null;
bool flag = false;
float? op1 = 0, op2 = null;
float? rslt = null;
dlgoperacao operacao = null;
public MainWindow()
{
InitializeComponent();
// img.Source = new BitmapImage(new Uri(@"imgs/2.jpg",UriKind.Relative));
}//Fim Construtor
protected float? calculaop(float? operori, ref int? div, char ch)
{
float? rslt = null;
if (ch == ',') { rslt = operori; div = 1; return rslt; }
else
{
int digito = Convert.ToInt32(ch - 48);
if (div == null) rslt = (operori == null) ? digito : (operori * PESO) + digito;
else
{
div = div * 10;
rslt = ((operori * div) + digito) / div;
}
return rslt;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button bt = (Button)sender;
char[] ch = bt.Content.ToString().ToCharArray();
if (char.IsDigit(ch[0].ToString(), 0) || ch[0] == ',')
{
if (flag)
{
op2 = calculaop(op2, ref divisor, ch[0]);
// int i = regex.Match(txt.Text).Index;
//txt.Text = txt.Text.Substring(0, i +1) + " " + op2.ToString();
txt.Text = op2.ToString();
}
else
{
op1 = calculaop(op1, ref divisor, ch[0]);
txt.Text = op1.ToString();
}
}//if ch[0] is digit
else
{
divisor = null;
switch (ch[0])
{
case 'c':
flag = false;
op1 = op2 = null;
operacao = null;
txt.Text = "";
break;
case '+':
if (op2 == null) flag = !flag;
else if (operacao != null)
{
op1 = operacao(op1, op2);
op2 = null;
flag = true;
}
operacao = (a, b) => a + b;
txt.Text = op1.ToString() + " + ";
break;
case '-':
if (op2 == null) flag = !flag;
else if (operacao != null)
{
op1 = operacao(op1, op2);
op2 = null;
flag = true;
}
operacao = (a, b) => a - b;
txt.Text = op1.ToString() + "-";
break;
case '*':
if (op2 == null) flag = !flag;
else if (operacao != null)
{
op1 = operacao(op1, op2);
op2 = null;
flag = true;
}
operacao = (a, b) => a * b;
txt.Text = op1.ToString() + "*";
break;
case '/':
try
{
if (op2 == null) flag = !flag;
else if (operacao != null)
{
op1 = operacao(op1, op2);
op2 = null;
flag = true;
}
operacao = (a, b) => a / b;
txt.Text = op1.ToString() + "/";
}
catch (Exception x)
{
txt.Text = x.Message;
}
break;
case '√':
break;
case '%':
try
{
if (op2 == null) flag = !flag;
else if (operacao != null)
{
op1 = operacao(op1, op2);
op2 = null;
flag = true;
}
operacao = (a, b) => a % b;
txt.Text = op1.ToString() + "%";
}
catch (Exception x)
{
txt.Text = x.Message;
}
break;
case '=':
if (operacao != null)
{
if (op2 != null) rslt = operacao(op1, op2);
else rslt = op1;
txt.Text = "Resultado -> " + rslt.ToString();
flag = false;
operacao = null;
op1 = op2 = null;
}
break;
}//switch
}//else ch[0] -> not digit
}
}
}
Now I want to put the square root. I don’t know if I will use the case
to make it work or if I’ll use another way.
The code is partial, post it in full.
– user6476
Complete code
– ChrisAdler
Um, why don’t you use Math? use command Sqrt (Example Math.Sqrt(9) and result 3) is root and square is Pow (Example: Math.Pow(9, 2) and result 81).. Follow tutorial http://www.dotnetperls.com/math. and even more!
– KingRider