【忠告】:1.记好了,大家上大学千万别【和鸡疯】!
【腹蛋】抓了好几个学生了!!!
2.抓学生有用吗????【妓院】【院长】却没被【靠】!不是,打错字了,是【没 被铐】,【靠你大爷的】!!!
///多重括号表达式运算(用递归)。。。
1.取所有最外层括号下标
2.用递归进行运算
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//string s = "(3*((1+2)*4/(1+1)))-(2*(4-2))";
//string s = "(1*(2+10)+1)-4";
//string s = "2*(6*(2-6)/(1+1))-((-2)*2)+(((1+1)+1)+1)";
string s = "(-12-3*(222-224))+(-5)*(-12)/(11-13)+(11+33)";
//string s = "-12+(-10)*(2-4)/(-2)";
char[] str = new char[s.Length];
for (int i = 0; i < s.Length; i++)
{
str[i] = s[i];
//Console.Write(str[i]);
}
int[,] KuoHao_Index = new int[10, 2];
//取括号下标(str, KuoHao_Index);
int a = 多重括号运算(str, str.Length);
//for(int i=0;
Console.WriteLine("ZhangBilei: " + a);
Console.ReadLine();
}
public static void 打()
{
Console.WriteLine("FSDFSDFSDFDSF");
}
public static void 取括号下标(char[] str, int[,] KuoHao_Index)
{
int label = 1;//label记录括号的层次
int length = str.Length;//记录str的长度
int count = 0;//记录括号的对数
bool left = false;//第一层中的当前左括号下标已经录入则left为true;
//bool right = false;//第一层中的当前右括号下标已经录入则right为true;
for (int i = 0; i < length; i++)
{
if (str[i] == '(')
{
if (left == false && label == 1)
{
KuoHao_Index[count, 0] = i;
left = true;
}
else
label++;
}
if (str[i] == ')')
{
if (left == true && label == 1)
{
KuoHao_Index[count, 1] = i;
count++;
left = false;
}
else
label--;
}
}
}
public static int 多重括号运算(char[] str, int length)
{
int[,] KuoHao_Index = new int[10, 2];//存括号的下标
取括号下标(str, KuoHao_Index);
//for (int i = 0; i < 10; i++)
//{
// Console.WriteLine(i + ":" + KuoHao_Index[i, 0] + " " + KuoHao_Index[i, 1]);
//}
int Index_KuoHao = 0;//记录当前括号(也就是第几对)
if (KuoHao_Index[0, 1] == 0)//取不到下标,说明没有括号了,可直接运算
{
return Get_Result(str, length);
}
取到下标,则重整表达式
else
{
char[] new_str = new char[length];//脱去括号后的表达式
int new_str_index = 0;//上面表达式的当前下标
for (int i = 0; i < length; i++)
{
if (i == KuoHao_Index[Index_KuoHao, 0] && KuoHao_Index[Index_KuoHao, 1] != 0)
{
i++;
char[] temp = new char[length];
int index_temp = 0;
while (i < KuoHao_Index[Index_KuoHao, 1])
{
temp[index_temp] = str[i];
index_temp++;
i++;
}//退出循环后i为右括号的下标
int x = 多重括号运算(temp, index_temp);
x<0是,new_str中添加负号,并把x变为正的
if (x < 0)
{
new_str[new_str_index] = '-';
new_str_index++;
x = 0 - x;
}
char[] ch = new char[10];/ 倒着存放整数x的各位数
int count = 0;while循环后count为x的位数
//Console.WriteLine("Print KuoHao li de jie guo:");
while (x != 0)
{
ch[count] = (char)(x % 10 + 48);
count++;
x = x / 10;
// Console.WriteLine("ch[{0}]:{1}",count,ch[count]);
}
while (count > 0)
{
new_str[new_str_index] = ch[count - 1];
count--;
new_str_index++;
}
Index_KuoHao++;
}
else//取最外层括号外的所有字符
if (i != KuoHao_Index[Index_KuoHao, 1])
{
new_str[new_str_index] = str[i];
new_str_index++;
}
}
return Get_Result(new_str, new_str_index);
}
}
public static int YouXianJi(char ch)//计算优先级
{
if (ch == '*' || ch == '/')
return 2;
else
return 1;
}
/// /// /// /// /// /// /// ///《《取无括号表达式优先级最高中的运算符中的第1个运算符》》 /// /// /// ///
/// 将此运算符传给current_op,其在字符数组中的下标传给index
///
/// /// /// /// /// /// /// /// /// /// /// /// ///
public static void Get_Operator(char[] czf, int czf_count, ref char current_op, ref int index)
{
int Max_YouXianJi = 0;
int i = 0;
for (; i < czf_count; i++)
{
if (Max_YouXianJi < YouXianJi(czf[i]))
{
Max_YouXianJi = YouXianJi(czf[i]);
index = i;
}
}
current_op = czf[index];
}
/// /// /// /// /// /// /// 《《对x和y运算》》 /// /// /// /// ///
///
///
/// /// /// /// /// /// /// /// /// /// /// /// ///
public static int Do_Operator(int x, int y, char ch)
{
switch (ch)
{
case '+':
return x + y;
break;
case '-':
return x - y;
break;
case '*':
return x * y;
break;
case '/':
return x / y;
break;
default:
return 0;
break;
}
}
/// <summary>
/// ///《《计算无括号表达式的值》》//
/// 直接利用数组实现无括号表达式优先级的运算/// /// ///
/// BiaoDaShi为无括号表达式
/// sub_length为BiaoDaShi的元素个数;
/// </summary>
///
///
///
///
///
///
///
/// <returns></returns>更改后可计算带负数的式子,
/// 如11+(-22)*(-44)会在函数【多重括号运算】中自动转化为“11+-22*-44”,
/// “11+-22*-44”可在本函数中直接计算
///
public static int Get_Result(char[] str, int sub_length)
{
//1.负数写在括号里,如(-74)可看成(0+-74)
//其他的如22+3*4,前面添上“0+”--->“0+22+3*4”结果依然不变。-11-2(“0+-11-2”)
char[] BiaoDaShi = new char[str.Length + 2];
BiaoDaShi[0] = '0';
BiaoDaShi[1] = '+';
for (int i = 2; i < BiaoDaShi.Length; i++)
{
BiaoDaShi[i] = str[i - 2];
}
int length = sub_length+2;
int[,] CZS = new int[10, 10];//(存放操作数数的各个位)最多可存放10个10位的操作数
int[] CaoZuoShu = new int[10];///存放操作数(实际的值)
bool []正负=new bool[10];
int[] WeiShu = new int[10];//存放各个操作数的位数
char[] CaoZuoFu = new char[length];///存放操作符
int czs_count = 0;/// 操作数的个数
int czs_index = 0;/// 操作数的个数
int czf_count = 0;/// 操作符的个数
/// /// /// /// ///
/// 将textBox1.Text中的字符分别存入二维int型数组(每行存一个多位操作数)CaoZuoShu和char型数组CaoZuoFu
/// /// /// /// /// ///
for (int i = 0; i < length; i++)
{
if (BiaoDaShi[i] <= '9' && BiaoDaShi[i] >= '0')
{
CZS[czs_index, WeiShu[czs_index]] = BiaoDaShi[i] - 48;/char类型转为int类型
WeiShu[czs_index]++;//第czs_count个操作数的位数增加1;
}
else
{
CaoZuoFu[czf_count] = BiaoDaShi[i];
czf_count++;
czs_index++;遇到操作符将进入下一个操作数
//遇到连续的运算符,表达式没输入错误的话,第二个就是负号,则跳过,下一操作数设为负。。。。
if (BiaoDaShi[i + 1] == '-')
{
i++;
正负[czs_index] = true;//下一操作数为负
}
}
}
czs_count = czs_index + 1;
///
///将操作数的各个位进行运算,把用各位数表示的数转换为整数,并存入CaoZuoShu数组中
///
//
for (int i = 0; i < czs_count; i++)czs_count为操作数的个数
{
for (int k = WeiShu[i] - 1; k >= 0; k--)k初始为第i操作数的最后一位数的下标,即从后往前,低位到高为
{
int n = WeiShu[i] - 1 - k; n初始为0,然后递增,最后为WeiShu[i] - 1
CaoZuoShu[i] += CZS[i, k] * Power(10, n);//第i个操作数从最低位运算到最高位,结果存入CaoZuoShu中
}
if (正负[i] == true)//如果第I个操作数为负
CaoZuoShu[i] = 0 - CaoZuoShu[i];
}
/// for (int i = 0; i < czs_count; i++)
{
/ Console.WriteLine("CaoZuoShu[{0}]:{1},", i, CaoZuoShu[i]);
}
/*int length = sub_length;
Console.WriteLine("SubString Length:" + length);
char[] CaoZuoFu = new char[length];///存放操作符
int[] CaoZuoShu = new int[length];///存放操作数
for (int i = 0; i < length; i++)
CaoZuoShu[i] = 0;
int czs_count = 0;/// 操作数的个数
int czf_count = 0;/// 操作符的个数
/// /// /// /// ///
/// 将textBox1.Text中的字符分别存入int型数组CaoZuoShu和char型数组CaoZuoFu
/// /// /// /// /// ///
for (int i = 0; i < length; i++)
{
if (BiaoDaShi[i] <= '9' && BiaoDaShi[i] >= '0')
{
CaoZuoShu[czs_count] = BiaoDaShi[i] - 48;
czs_count++;
}
else
{
CaoZuoFu[czf_count] = BiaoDaShi[i];
czf_count++;
}
}*/
/// /// /// /// /// /// /// ///
/// /// 操作数个数比操作符个数多1;第i操作符为第i操作数和第i+1操作数的操作符;
/// ///
/// /// /// /// /// /// /// /// ///
char Current_OP = ' ';/// /// CurrentOP记录当前用的运算符,
///
int OP_index = 0;/// ///OPindex记录CurrentOP在char数组中的位置
///
while (czf_count > 0)/// 当操作符数组不为空时
{
Get_Operator(CaoZuoFu, czf_count, ref Current_OP, ref OP_index);//用应用找出优先级最高的操作符及其下标
/// /// 操作数个数比操作符个数多1;第i操作符为第i操作数和第i+1操作数的操作符;计算结果存入第i+1操作数中
/// ///
CaoZuoShu[OP_index + 1] = Do_Operator(CaoZuoShu[OP_index], CaoZuoShu[OP_index + 1], Current_OP);
/// /// /// /// /// /// /// ///
/// /// 第i操作符用用过了,利用元素往后退将其删除
/// /// /// /// /// /// /// /// ///
for (int j = OP_index; j < czf_count - 1; j++)
{
CaoZuoFu[j] = CaoZuoFu[j + 1];
}
/// /// /// /// /// /// /// ///
/// /// 第i操作数用过了,利用元素往后退将其删除,完成后第i+1操作数将在i位置
/// /// /// /// /// /// /// /// ///
for (int k = OP_index; k < czs_count - 1; k++)
{
CaoZuoShu[k] = CaoZuoShu[k + 1];
}
/// /// /// /// /// /// /// ///
/// /// 完成每次操作,操作数和操作符的个数都减少1
/// /// /// /// /// /// /// /// ///
czf_count--;
czs_count--;
}
/// /// /// /// /// /// /// ///
/// 第i操作符为第i操作数和第i+1操作数的操作符;计算结果存入第i+1操作数中
/// /// 最后的操作符下标为0,最后两个操作数的下标分别为0和1,最终结果将存放在1中
/// /// /// /// /// /// /// /// ///
return CaoZuoShu[1];
}
public static int Power(int x, int n)
{
int sum = 1;
for (int i = 0; i < n; i++)
{
sum *= x;
}
return sum;
}
}
}