c 四則混合運算

2021-10-07 15:52:35 字數 3427 閱讀 6944

最近因專案需求,做了乙個簡易的四則混合運算解析模組。例如計算表示式:34 * ((300+4) / 2) - ((300 - 4) % 2)

注:運算子僅支援: +  -  *  /   % 五種型別

public inte***ce imixedcalculate

t exec(string expression);

}介面實現類:

public class mixedcalculate : imixedcalculate

regex bracketrg = null;

///

/// 乘 - 除 - % 運算

///

regex calclevel1 = null;

///

/// 加 - 減 運算

///

regex calclevel2 = null;

const int max = 100;

public mixedcalculate()

bracketrg = new regex(@"\((?[^\(\)]+)\)", regexoptions.ignorecase);

calclevel1 = new regex(@"(?[0-9\.]+)\s*(?[\/\*\%])\s*(?[0-9\.]+)", regexoptions.ignorecase);

calclevel2 = new regex(@"(?[0-9\.]+)\s*(?[\+\-])\s*(?[0-9\.]+)", regexoptions.ignorecase);

t imixedcalculate.exec(string expression)

t val = default(t);

if (string.isnullorempty(expression)) return val;

bracketcalculate(ref expression);

level_calculate(calclevel1, ref expression);

level_calculate(calclevel2, ref expression);

string s = expression.trim();

object vobj = convertto(s); //這裡是基本資料型別轉換

if (null != vobj) val = (t)vobj;

return val;

object convertto(string vs)

type type = typeof(t);

object vobj = null;

if (typeof(int) == type)

int n = 0;

int.tryparse(vs, out n);

vobj = n;

else if(typeof(float) == type)

float fnum = 0;

float.tryparse(vs, out fnum);

vobj = fnum;

else if (typeof(double) == type)

double dbnum = 0;

double.tryparse(vs, out dbnum);

vobj = dbnum;

return vobj;

void level_calculate(regex rg, ref string expression)

string s = expression.trim();

if (string.isnullorempty(s)) return;

int n = 0;

string num1 = "";

string num2 = "";

string calcchar = "";

float n1 = 0;

float n2 = 0;

float num = 0;

match m = null;

while (rg.ismatch(s) && max > n)

m = rg.match(s);

num1 = m.groups["num1"].value;

num2 = m.groups["num2"].value;

calcchar = m.groups["calcchar"].value;

n1 = convert.tosingle(num1);

n2 = convert.tosingle(num2);

num = calculat(n1, n2, calcchar);

s = s.replace(m.groups[0].value, num.tostring());

n++;

expression = s;

void bracketcalculate(ref string expression)

string s = expression.trim();

if (string.isnullorempty(s)) return;

string calculateexp = "";

int n = 0;

match m = null;

while (bracketrg.ismatch(s) && max > n)

m = bracketrg.match(s);

calculateexp = m.groups["calculateexp"].value;

level_calculate(calclevel1, ref calculateexp);

level_calculate(calclevel2, ref calculateexp);

s = s.replace(m.groups[0].value, calculateexp);

n++;

expression = s;

float calculat(float num1, float num2, string calculatechar)

float v1 = 0;

if (string.isnullorempty(calculatechar)) return v1;

calculatechar = calculatechar.trim();

switch (calculatechar)

case "+":

v1 = num1 + num2;

break;

case "-":

v1 = num1 - num2;

break;

case "*":

v1 = num1 * num2;

break;

case "/":

v1 = num1 / num2;

break;

case "%":

v1 = num1 % num2;

break;

return v1;

四則混合運算類

using system namespace expressionoperate 對公式進行語法檢測 需要檢測的公式 public static bool isrexpression string expression case break case case case if rexpression...

C 實現計算器(四則混合運算)

計算器是帶括號的四則運算實際上利用的是棧後進先出的原則 轉換思想 入棧push int pop1 stack1 s,float ch 出棧 typedef struct stack2 void initstack2 stack2 s int push2 stack2 s,char ch 入棧push...

C 四則運算

一 問題及 檔名稱 兩個浮點數的四則運算 02.作 者 李欽 03.完成日期 2016 年 3 月 12 日 04.版 本 號 v1.0 05.對任務及求解方法的描述部分 06.輸入描述 07.問題描述 略 08.程式輸出 略 09.問題分析 略 10.演算法設計 略 11.includevoid ...