using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 简易计算机
{
public partial class EasyCalculate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
int clear = 1;
protected void Button6_Click(object sender, EventArgs e)
{
try
{
if (TextBox1.Text == "")
{
}
else
{
string endChar = TextBox1.Text.Substring(TextBox1.Text.Length - 1);
if (endChar == "+" || endChar == "-")
{
Response.Write("请输入完整的计算表达式");
}
else
{
//计算的完整过程
#region
int result = 0;
string[] s = new string[100];
string[] syb = new string[100];
int sybIndex = 0;
int index = 0;
string textBox1 = TextBox1.Text.Trim();
//存取字符串
#region
int isFirst = 0;
for (int i = 0; i < textBox1.Length; i++)
{
if (textBox1.Substring(i, 1) != "+" && textBox1.Substring(i, 1) != "-")
{
if (isFirst == 0)//首次拿到了数字,吧isFirst赋值为1的目的是为了方便下面下次只要一拿到符号,就让s数组下标+1
{
isFirst = 1;
}
s[index] += textBox1.Substring(i, 1);//存储数字的字符串
continue;
}
else
{
syb[sybIndex] = textBox1.Substring(i, 1);
if (isFirst != 0)//!=0说明第一次取值的时候拿到是负号而不是数字;因为如果第一次拿到是数字的话在上面的代码中就将isFirst赋值为了1;
{
index++;//拿到了一个,记录数字的数组s应该加下,准备吧接下来拿到字符保存下来
}
// index++;
sybIndex++;//第一次拿到的是负号,那么就只让记录符号的数组下标+1
}
}
#endregion
int first = 1;
sybIndex = 0;
int sNum = 0;
int sybNum = 0;
for (int i = 0; i < s.Length && s[i] != null; i++)
{
sNum++;
}
for (int i = 0; i < syb.Length && syb[i] != null; i++)
{
sybNum++;
}
for (int i = 0; i < s.Length && s[i] != null; i++)
{
int a = int.Parse(s[i]);//拿一个数字,注意这里读了一个字符,不一定是数字,那么这么直接转为int类型肯定会出错
if (first == 1)//为了判断第一个数组是否为负数
{
if (sybNum == sNum)//判断要计算的式子第一位是否为“负号”,如果是负号则拿到的第一个数字之间取他的相反数
{
result = -a;
sybIndex++;//这里一开始我忘记写了,导致在从syb数组中拿符号的时候偏向左移了一位
first = 0;
}
else
{
result = a;//
first = 0;
}
}
else if (syb[sybIndex] == "+")
{
result = result + a;
sybIndex++;
}
else if (syb[sybIndex] == "-")
{
result = result - a;
sybIndex++;
}
}
TextBox1.Text = result.ToString();
#endregion
}
}
}
catch(Exception ee)
{
Response.Write(ee.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
// if (clear == 0)
// {
// TextBox1.Text = null;
// }
//}
TextBox1.Text += Button1.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
if (clear == 0)
{
TextBox1.Text = null;
}
TextBox1.Text += Button2.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
if (clear == 0)
{
TextBox1.Text = null;
}
TextBox1.Text += Button3.Text;
}
protected void Button4_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
}
else
{
string repeatPress = TextBox1.Text.Substring(TextBox1.Text.Length - 1);
if (repeatPress != "+" && repeatPress != "-")
{
TextBox1.Text += "+";
}
}
}
protected void Button5_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string repeatPress = TextBox1.Text.Substring(TextBox1.Text.Length - 1);
if (repeatPress != "+" && repeatPress != "-")
{
TextBox1.Text += "-";
}
}
else
{
TextBox1.Text = "-";
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 简易计算机
{
public partial class EasyCalculate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
int clear = 1;
protected void Button6_Click(object sender, EventArgs e)
{
try
{
if (TextBox1.Text == "")
{
}
else
{
string endChar = TextBox1.Text.Substring(TextBox1.Text.Length - 1);
if (endChar == "+" || endChar == "-")
{
Response.Write("请输入完整的计算表达式");
}
else
{
//计算的完整过程
#region
int result = 0;
string[] s = new string[100];
string[] syb = new string[100];
int sybIndex = 0;
int index = 0;
string textBox1 = TextBox1.Text.Trim();
//存取字符串
#region
int isFirst = 0;
for (int i = 0; i < textBox1.Length; i++)
{
if (textBox1.Substring(i, 1) != "+" && textBox1.Substring(i, 1) != "-")
{
if (isFirst == 0)//首次拿到了数字,吧isFirst赋值为1的目的是为了方便下面下次只要一拿到符号,就让s数组下标+1
{
isFirst = 1;
}
s[index] += textBox1.Substring(i, 1);//存储数字的字符串
continue;
}
else
{
syb[sybIndex] = textBox1.Substring(i, 1);
if (isFirst != 0)//!=0说明第一次取值的时候拿到是负号而不是数字;因为如果第一次拿到是数字的话在上面的代码中就将isFirst赋值为了1;
{
index++;//拿到了一个,记录数字的数组s应该加下,准备吧接下来拿到字符保存下来
}
// index++;
sybIndex++;//第一次拿到的是负号,那么就只让记录符号的数组下标+1
}
}
#endregion
int first = 1;
sybIndex = 0;
int sNum = 0;
int sybNum = 0;
for (int i = 0; i < s.Length && s[i] != null; i++)
{
sNum++;
}
for (int i = 0; i < syb.Length && syb[i] != null; i++)
{
sybNum++;
}
for (int i = 0; i < s.Length && s[i] != null; i++)
{
int a = int.Parse(s[i]);//拿一个数字,注意这里读了一个字符,不一定是数字,那么这么直接转为int类型肯定会出错
if (first == 1)//为了判断第一个数组是否为负数
{
if (sybNum == sNum)//判断要计算的式子第一位是否为“负号”,如果是负号则拿到的第一个数字之间取他的相反数
{
result = -a;
sybIndex++;//这里一开始我忘记写了,导致在从syb数组中拿符号的时候偏向左移了一位
first = 0;
}
else
{
result = a;//
first = 0;
}
}
else if (syb[sybIndex] == "+")
{
result = result + a;
sybIndex++;
}
else if (syb[sybIndex] == "-")
{
result = result - a;
sybIndex++;
}
}
TextBox1.Text = result.ToString();
#endregion
}
}
}
catch(Exception ee)
{
Response.Write(ee.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
// if (clear == 0)
// {
// TextBox1.Text = null;
// }
//}
TextBox1.Text += Button1.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
if (clear == 0)
{
TextBox1.Text = null;
}
TextBox1.Text += Button2.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
if (clear == 0)
{
TextBox1.Text = null;
}
TextBox1.Text += Button3.Text;
}
protected void Button4_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
}
else
{
string repeatPress = TextBox1.Text.Substring(TextBox1.Text.Length - 1);
if (repeatPress != "+" && repeatPress != "-")
{
TextBox1.Text += "+";
}
}
}
protected void Button5_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string repeatPress = TextBox1.Text.Substring(TextBox1.Text.Length - 1);
if (repeatPress != "+" && repeatPress != "-")
{
TextBox1.Text += "-";
}
}
else
{
TextBox1.Text = "-";
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}