- UID
- 418
- 精华
- 积分
- 4003
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
发表于 2014-11-18 22:43:08
|
显示全部楼层
基于栈的表达式计算,弄了好几个月,现在分享一下
- /*
- Calculation Engine Kernel v1.0[Static Edition]
- Created By CYY 20140201
- Copyright (C) 2014 Ctechnology Corporation
- All Rights Resaved.
- [email]cyycoish@hotmail.com[/email]
- 201402042139
- 201402241352
- 201403061734--func
- 201403140144--x1
- 201403202105--no add end
- 201404091752SAT--ALL END
- 注意:AddConstants必须在AddFunc前使用
- */
- //包含部分
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#include <Windows.h>
- #include <math.h>
- #include <time.h>
- //#include <iostream>
- //宏定义部分
- #define N 4096//输入总长
- #define M 1024//输入总长
- #define S_NUM_MAX 4096//单个数字大小
- #define S_OPR_MAX 4096//单个运算符大小
- #define OFUN_NUM 63 //运算符数量
- #define CST_CTR 5 //常数个数
- #define FUN_MAX 1024//运算符数量
- #define PAR_MAX 1024//函数参数数量
- #define UFN_MAX 1024//自定义函数数量
- #define MPI 3.1415926535897932384626433832795028841971693993751058
- #define MEE 2.7182818284590452353602874713526624977572470936999595
- //#define SFUN_NUM 10//自定义运算符数量
- //声明部分
- double CALC_main(char *exp);
- char *InputScanner(char *exp);
- void Debug_Print_Para(void);
- //
- int intFunParCount = 0;//函数参数个数
- double Answ = 0;//保存前一个运算结果
- double pa[M] = { 0 };//函数参数
- int ix = 0;//位置标记
- int iUserFun = 0;//自定义函数数量
- int iParCtr = 0;//函数参数数量
- int iUserCnt = 0;//自定义常量数量
- enum DEGSCALE{//角度度量
- S_Rad,//弧度制
- S_Deg,//角度制
- S_Gra //梯度制
- };
- DEGSCALE degScale = S_Rad;//角度度量默认弧度
- //错误代码
- int ERR_NUM = 0;
- enum ERRORNUM{
- NoError,
- MathError,
- StackError,
- SyntaxError,
- UnknownError,
- };
- typedef struct{
- ERRORNUM num;
- int pos;
- }ERRSTRU;
- ERRSTRU Error(ERRORNUM E){
- ERRSTRU ERRNUMr;
- ERRNUMr.num = E;
- ERRNUMr.pos = ix;
- return ERRNUMr;
- }
- //常数
- typedef struct{
- char *name;
- double value;
- int number;
- }CONSTANT;
- //运算符结合性
- enum OPRCAL{
- DoubleCalOpr,//双目运算符
- FrontCalOpr, //前缀运算符
- RearCalOpr, //后缀运算符
- IsBracket, //是括号
- Specific, //特殊 ,
- Functio, //函数
- UserFun, //自定义函数
- //VarCon //变量/常数
- };
- //运算符类型
- typedef struct{
- char *name;//运算符名称
- int level;//优先级
- OPRCAL ctype;//运算符结合性
- /*
- ctype取值:
- 0:双目运算符
- 1.前缀运算符
- 2.后缀运算符
- 3.括号
- */
- int number;//标识符
- char *expr;//函数表达式
- //double value;
- }OTYPE;
- //数栈
- typedef struct{
- double stack[N];
- int top;
- }NumSTACK;
- //二级数栈存放函数参数[操作对象函数CALC_fun]
- typedef struct{
- double stack[N];
- int top;
- }NumSTACK_fun;
- //符栈
- typedef struct{
- OTYPE stack[M];
- int top;
- }OprSTACK;
- //初始化数栈
- void InitNumSTACK(NumSTACK* s){
- (*s).top = 0;
- }
- //进数栈
- bool NPush(NumSTACK* s, double n)
- {
- //如果栈满则报错退出程序
- if ((*s).top == N - 1){
- Error(StackError);
- return false;
- }
- (*s).stack[(*s).top] = n;
- (*s).top++;
- return true;
- }
- //出数栈
- double NPop(NumSTACK* s)
- {
- //如果栈空则报错退出程序
- if ((*s).top == 0){
- Error(StackError);
- return 0;
- }
- else{
- (*s).top--;
- return (*s).stack[(*s).top];
- }
- }
- //初始化符栈
- void InitOprSTACK(OprSTACK* s){
- (*s).top = 0;
- }
- //进符栈
- bool OPush(OprSTACK* s, OTYPE* sign)
- {
- //如果栈满则报错退出程序
- if ((*s).top == M - 1){
- Error(SyntaxError);
- return false;
- }
- (*s).stack[(*s).top] = *sign;
- (*s).top++;
- return true;
- }
- //出符栈
- OTYPE OPop(OprSTACK *s)
- {
- //栈空则报错退出程序
- if ((*s).top == 0){
- Error(SyntaxError);
- }
- else{
- (*s).top--;
- //return (*s).stack[M-((*s).top)];
- return (*s).stack[(*s).top];
- }
- }
- //运算符表
- OTYPE oper[FUN_MAX];
- bool InitOperTable(void){
- /*
- oper[0].name="#"; oper[0].level=1; oper[0].ctype=DoubleCalOpr; oper[0].number=1;
- oper[1].name="NOT"; oper[1].level=2; oper[1].ctype=FrontCalOpr; oper[1].number=2;
- oper[2].name="~NOT"; oper[2].level=7; oper[2].ctype=FrontCalOpr; oper[2].number=3;
- oper[3].name="^"; oper[3].level=14; oper[3].ctype=DoubleCalOpr; oper[3].number=4;
- oper[4].name="!"; oper[4].level=15; oper[4].ctype=RearCalOpr; oper[4].number=5;
- oper[5].name="*"; oper[5].level=13; oper[5].ctype=DoubleCalOpr; oper[5].number=6;
- oper[6].name="/"; oper[6].level=13; oper[6].ctype=DoubleCalOpr; oper[6].number=7;
- oper[7].name="MOD"; oper[7].level=13; oper[7].ctype=DoubleCalOpr; oper[7].number=8;
- oper[8].name="AND"; oper[8].level=8; oper[8].ctype=DoubleCalOpr; oper[8].number=9;
- oper[9].name="~AND"; oper[9].level=3; oper[9].ctype=DoubleCalOpr; oper[9].number=10;
- oper[10].name="+"; oper[10].level=12;oper[10].ctype=DoubleCalOpr;oper[10].number=11;
- //oper[11].name="-"; oper[11].level=4;oper[11].ctype=DoubleCalOpr;oper[11].number=12;
- oper[11].name="OR"; oper[11].level=9; oper[11].ctype=DoubleCalOpr;oper[11].number=12;
- oper[12].name="~OR"; oper[12].level=4; oper[12].ctype=DoubleCalOpr;oper[12].number=13;
- oper[13].name="XOR"; oper[13].level=10;oper[13].ctype=DoubleCalOpr;oper[13].number=14;
- oper[14].name="~XOR"; oper[14].level=5; oper[14].ctype=DoubleCalOpr;oper[14].number=15;
- oper[15].name="XNOR"; oper[15].level=11;oper[15].ctype=DoubleCalOpr;oper[15].number=16;
- oper[16].name="~XNOR";oper[16].level=6; oper[16].ctype=DoubleCalOpr;oper[16].number=17;
- oper[17].name="="; oper[17].level=16;oper[17].ctype=DoubleCalOpr;oper[17].number=18;
- oper[18].name=";"; oper[18].level=17;oper[18].ctype=RearCalOpr; oper[18].number=19;
- oper[19].name="("; oper[19].level=-1;oper[19].ctype=IsBracket; oper[19].number=20;
- oper[20].name=")"; oper[20].level=0; oper[20].ctype=IsBracket; oper[20].number=21;
- oper[21].name=","; oper[21].level=0; oper[21].ctype=IsBracket; oper[21].number=22;
- */
- oper[0].name = "NAN"; oper[0].level = 100; oper[0].ctype = Specific; oper[0].number = -1;
- oper[1].name = "NOT"; oper[1].level = 25; oper[1].ctype = FrontCalOpr; oper[1].number = 2;
- oper[2].name = "~NOT"; oper[2].level = 24; oper[2].ctype = FrontCalOpr; oper[2].number = 3;
- oper[3].name = "^"; oper[3].level = 14; oper[3].ctype = DoubleCalOpr; oper[3].number = 4;
- oper[4].name = "!"; oper[4].level = 15; oper[4].ctype = RearCalOpr; oper[4].number = 5;
- oper[5].name = "*"; oper[5].level = 13; oper[5].ctype = DoubleCalOpr; oper[5].number = 6;
- oper[6].name = "/"; oper[6].level = 13; oper[6].ctype = DoubleCalOpr; oper[6].number = 7;
- oper[7].name = "MOD"; oper[7].level = 13; oper[7].ctype = DoubleCalOpr; oper[7].number = 8;
- oper[8].name = "AND"; oper[8].level = 23; oper[8].ctype = DoubleCalOpr; oper[8].number = 9;
- oper[9].name = "~AND"; oper[9].level = 22; oper[9].ctype = DoubleCalOpr; oper[9].number = 10;
- oper[10].name = "+"; oper[10].level = 12; oper[10].ctype = DoubleCalOpr; oper[10].number = 11;
- oper[11].name = "OR"; oper[11].level = 21; oper[11].ctype = DoubleCalOpr; oper[11].number = 12;
- oper[12].name = "~OR"; oper[12].level = 20; oper[12].ctype = DoubleCalOpr; oper[12].number = 13;
- oper[13].name = "XOR"; oper[13].level = 19; oper[13].ctype = DoubleCalOpr; oper[13].number = 14;
- oper[14].name = "~XOR"; oper[14].level = 18; oper[14].ctype = DoubleCalOpr; oper[14].number = 15;
- oper[15].name = "XNOR"; oper[15].level = 17; oper[15].ctype = DoubleCalOpr; oper[15].number = 16;
- oper[16].name = "~XNOR"; oper[16].level = 16; oper[16].ctype = DoubleCalOpr; oper[16].number = 17;
- oper[17].name = ">>"; oper[17].level = 9; oper[17].ctype = DoubleCalOpr; oper[17].number = 18;
- oper[18].name = ";"; oper[18].level = 17; oper[18].ctype = RearCalOpr; oper[18].number = 19;
- oper[19].name = "("; oper[19].level = -1; oper[19].ctype = IsBracket; oper[19].number = 20;
- oper[20].name = ")"; oper[20].level = 0; oper[20].ctype = IsBracket; oper[20].number = 21;
- oper[21].name = ","; oper[21].level = 0; oper[21].ctype = IsBracket; oper[21].number = 22;
- oper[22].name = "ADD"; oper[22].level = 99; oper[22].ctype = Functio; oper[22].number = 23;
- oper[23].name = "SIN"; oper[23].level = 99; oper[23].ctype = FrontCalOpr; oper[23].number = 24;
- oper[24].name = "%"; oper[24].level = 26; oper[24].ctype = RearCalOpr; oper[24].number = 1;
- oper[25].name = "COS"; oper[25].level = 99; oper[25].ctype = FrontCalOpr; oper[25].number = 25;
- oper[26].name = "TAN"; oper[26].level = 99; oper[26].ctype = FrontCalOpr; oper[26].number = 26;
- oper[27].name = "~>"; oper[27].level = 11; oper[27].ctype = DoubleCalOpr; oper[27].number = 27;
- oper[28].name = "~<"; oper[28].level = 11; oper[28].ctype = DoubleCalOpr; oper[28].number = 28;
- oper[29].name = ">="; oper[29].level = 11; oper[29].ctype = DoubleCalOpr; oper[29].number = 27;
- oper[30].name = "<="; oper[30].level = 11; oper[30].ctype = DoubleCalOpr; oper[30].number = 30;
- oper[31].name = "=="; oper[31].level = 10; oper[31].ctype = DoubleCalOpr; oper[31].number = 31;
- oper[32].name = "<>"; oper[32].level = 10; oper[32].ctype = DoubleCalOpr; oper[32].number = 32;
- oper[33].name = "ARCSIN"; oper[33].level = 99; oper[33].ctype = FrontCalOpr; oper[33].number = 33;
- oper[34].name = "ARCCOS"; oper[34].level = 99; oper[34].ctype = FrontCalOpr; oper[34].number = 34;
- oper[35].name = "ARCTAN"; oper[35].level = 99; oper[35].ctype = FrontCalOpr; oper[35].number = 35;
- oper[36].name = "ABS"; oper[36].level = 99; oper[36].ctype = FrontCalOpr; oper[36].number = 36;
- oper[37].name = "SINH"; oper[37].level = 99; oper[37].ctype = FrontCalOpr; oper[37].number = 37;
- oper[38].name = "COSH"; oper[38].level = 99; oper[38].ctype = FrontCalOpr; oper[38].number = 38;
- oper[39].name = "TANH"; oper[39].level = 99; oper[39].ctype = FrontCalOpr; oper[39].number = 39;
- oper[40].name = "ARCSINH"; oper[40].level = 99; oper[40].ctype = FrontCalOpr; oper[40].number = 40;
- oper[41].name = "ARCCOSH"; oper[41].level = 99; oper[41].ctype = FrontCalOpr; oper[41].number = 41;
- oper[42].name = "ARCTANH"; oper[42].level = 99; oper[42].ctype = FrontCalOpr; oper[42].number = 42;
- oper[43].name = "LG"; oper[43].level = 99; oper[43].ctype = FrontCalOpr; oper[43].number = 43;
- oper[44].name = "LN"; oper[44].level = 99; oper[44].ctype = FrontCalOpr; oper[44].number = 44;
- oper[45].name = "LOG"; oper[45].level = 99; oper[45].ctype = Functio; oper[45].number = 45;
- oper[46].name = "SGN"; oper[46].level = 99; oper[46].ctype = FrontCalOpr; oper[46].number = 46;
- oper[47].name = "SQR"; oper[47].level = 99; oper[47].ctype = FrontCalOpr; oper[47].number = 47;
- oper[48].name = "INT"; oper[48].level = 99; oper[48].ctype = FrontCalOpr; oper[48].number = 48;
- oper[49].name = "SEC"; oper[49].level = 99; oper[49].ctype = FrontCalOpr; oper[49].number = 49;
- oper[50].name = "CSC"; oper[50].level = 99; oper[50].ctype = FrontCalOpr; oper[50].number = 50;
- oper[51].name = "COT"; oper[51].level = 99; oper[51].ctype = FrontCalOpr; oper[51].number = 51;
- oper[52].name = "ARCSEC"; oper[52].level = 99; oper[52].ctype = FrontCalOpr; oper[52].number = 52;
- oper[53].name = "ARCCSC"; oper[53].level = 99; oper[53].ctype = FrontCalOpr; oper[53].number = 53;
- oper[54].name = "ARCCOT"; oper[54].level = 99; oper[54].ctype = FrontCalOpr; oper[54].number = 54;
- oper[55].name = "SECH"; oper[55].level = 99; oper[55].ctype = FrontCalOpr; oper[55].number = 55;
- oper[56].name = "CSCH"; oper[56].level = 99; oper[56].ctype = FrontCalOpr; oper[56].number = 56;
- oper[57].name = "COTH"; oper[57].level = 99; oper[57].ctype = FrontCalOpr; oper[57].number = 57;
- oper[58].name = "ARCSECH"; oper[58].level = 99; oper[58].ctype = FrontCalOpr; oper[58].number = 58;
- oper[59].name = "ARCCSCH"; oper[59].level = 99; oper[59].ctype = FrontCalOpr; oper[59].number = 59;
- oper[60].name = "ARCCOTH"; oper[60].level = 99; oper[60].ctype = FrontCalOpr; oper[60].number = 60;
- oper[61].name = "ROOT"; oper[61].level = 99; oper[61].ctype = Functio; oper[61].number = 61;
- oper[62].name = "AVG"; oper[62].level = 99; oper[62].ctype = Functio; oper[62].number = 62;
- oper[63].name = "INT"; oper[63].level = 99; oper[63].ctype = FrontCalOpr; oper[63].number = 63;
- //oper[25].name = "PI"; oper[25].level = 100; oper[25].ctype = VarCon; oper[25].number = 25; oper[25].value = 3.14;
- return true;
- }
- //常数表
- CONSTANT constants[CST_CTR];//常数变量
- CONSTANT functpara[PAR_MAX];//函数参数
- void InitParaTable(void){
- register int i = 0;
- for (i = 0; i < PAR_MAX; i++){
- functpara[i].name = "";
- functpara[i].number = 0;
- functpara[i].value = 0;
- }
- }
- bool InitConstTable(void){
- constants[0].name = "NULL"; constants[0].value = 0; constants[0].number = 1;
- constants[1].name = "PI"; constants[1].value = MPI; constants[1].number = 2;
- constants[2].name = "E"; constants[2].value = MEE; constants[2].number = 3;
- constants[3].name = "ANS"; constants[3].value = 0; constants[3].number = 4;
- constants[4].name = "RND"; constants[4].value = 0; constants[4].number = 5;
- return true;
- }
- /////////////////
- double Sgn(double x){
- if (x > 0)
- return 1.0;
- else if (x == 0)
- return 0.0;
- else
- return -1.0;
- }
- //运算符运算过程表
- double DOprCal(double b, double a, int number){//双目运算符运算
- switch (number){
- case 4://^
- //printf("%s\n","hello^");////////////////////
- return powf(a, b);
- case 6://*
- //printf("%s\n","hello*");////////////////////
- return a*b;
- case 7:///
- if (b != 0)
- return a / b;
- else{
- Error(MathError);
- return 0;
- }
- case 8://MOD
- return (int)a % (int)b;
- case 9://AND
- return a&&b;
- case 10://~AND
- return (int)a&(int)b;
- case 11://+
- //printf("%s\n","hello+");////////////////////
- return a + b;
- //case 12://-
- //return a-b;
- case 12://OR
- //printf("%s\n","hello or");////////////////////
- return a || b;
- case 13://~OR
- return (int)a | (int)b;
- case 14://XOR
- if (a == b)
- return 0;
- else
- return 1;
- case 15://~XOR
- return (int)a ^ (int)b;
- case 16://XNOR
- if (a == b)
- return 1;
- else
- return 0;
- case 17://~XNOR
- return ~((int)a ^ (int)b);
- case 18://=
- if (a == b)
- return 1;
- else
- return 0;
- case 27:
- if (a > b)
- return 1;
- else
- return 0;
- case 28:
- if (a < b)
- return 1;
- else
- return 0;
- case 29:
- if (a >= b)
- return 1;
- else
- return 0;
- case 30:
- if (a <= b)
- return 1;
- else
- return 0;
- case 31:
- if (a == b)
- return 1;
- else
- return 0;
- case 32:
- if (a != b)
- return 1;
- else
- return 0;
- }
- }
- double FOprCal(int number, double a = 0){//单目前缀运算符运算
- int j = 0; double x = 0; char tStr[N] = { '\0' };
- //printf("入FO的数字代码:%s\n",number);/////////////////////////
- switch (number){
- case 2://NOT
- if (a != 0)
- return 0;
- else
- return 1;
- case 3://~NOT
- return ~((int)a);
- case 23://add
- for (j = 0; j <= intFunParCount; j++)
- x += pa[j];
- return x;
- case 24://sin
- switch (degScale){
- case S_Rad:
- return sin(a);
- case S_Deg:
- return sin(MPI / 180 * a);
- case S_Gra:
- return sin(MPI / 200 * a);
- }
- case 25://cos
- switch (degScale){
- case S_Rad:
- return cos(a);
- case S_Deg:
- return cos(MPI / 180 * a);
- case S_Gra:
- return cos(MPI / 200 * a);
- }
- case 26:
- switch (degScale){
- case S_Rad:
- return tan(a);
- case S_Deg:
- return tan(MPI / 180 * a);
- case S_Gra:
- return tan(MPI / 200 * a);
- }
- case 33:
- x = asin(a);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 34:
- x = acos(a);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 35:
- x = atan(a);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 36:
- return fabs(a);
- case 37://sinh
- return (exp(a) - exp(-a)) / 2;
- case 38://cosh
- return (exp(a) + exp(-a)) / 2;
- case 39://tanh
- return (exp(a) - exp(-a)) / (exp(a) + exp(-a));
- case 40://arcsinh
- return log(a + sqrt(a * a + 1));
- case 41://arccosh
- return log(a + sqrt(a * a - 1));
- case 42://arctanh
- return log((1 + a) / (1 - a)) / 2;
- case 43://lg
- return log(a) / log(10.0);
- case 44://ln
- return log(a) / log(MEE);
- case 45://log
- return log(pa[0]) / log(pa[1]);
- case 46://sgn
- return Sgn(a);
- case 47://sqr
- return sqrt(a);
- case 48://int
- return (double)int(a);
- case 49://sec
- if (a == 0)
- return 1.0;
- else
- x = 1 / cos(a);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 50://csc
- x = 1 / sin(a);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 51://cot
- x = 1 / tan(a);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 52://arc sec
- if (a == 1)
- return 0.0;
- else
- x = atan(a / sqrt(a * a - 1)) + Sgn((a)-1) * (2 * atan(1.0));
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 53://arc csc
- x = atan(a / sqrt(a * a - 1)) + (Sgn(a) - 1) * (2 * atan(1.0));
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 54://arc cot
- x = atan(a) + 2 * atan(1.0);
- switch (degScale){
- case S_Rad:
- return x;
- case S_Deg:
- return MPI / 180 * x;
- case S_Gra:
- return MPI / 200 * x;
- }
- case 55://sech
- if (a == 0)
- return 1;
- else
- x = 2 / (exp(a) + exp(-a));
- case 56://scsh
- x = 2 / (exp(a) - exp(-a));
- case 57://coth
- x = (exp(a) + exp(-a)) / (exp(a) - exp(-a));
- case 58://arc sech
- x = log((sqrt(-a * a + 1) + 1) / a);
- case 59://arc scsh
- x = log((Sgn(a) * sqrt(a * a + 1) + 1) / a);
- case 60://arc coth
- x = log((a + 1) / (a - 1)) / 2;
- case 61:
- return powf(pa[0], 1 / pa[1]);
- case 62:
- for (j = 0; j <= intFunParCount; j++)
- x += pa[j];
- return x / (intFunParCount + 1);
- case 63:
- return (double)int(a);
- default:
- //Debug_Print_Para();
- //double xxxz = 0;
- //char *cah;
- strcpy(tStr, InputScanner(oper[number].expr));
- //printf("\tA1st::\'%s\'\n", tStr);/////////////////////////////////////////InputScanner(oper[number].expr)
- return CALC_main(tStr);
- }
- }
- double ROprCal(double a, int number){//单目后缀运算符运算
- int k;
- double r = 1.0;
- switch (number){
- case 1:
- return (a / 100);
- //printf("hello");
- case 5://!
- for (k = 1; k <= (int)a; k++)
- r = r*(double)k;
- return r;
- case 19://;
- ;
- }
- }
- //查找运算符
- int IsOper(char *s){
- register int i = 0;
- //printf("iUserFun:%d\n", iUserFun);////////////////////
- for (i = 0; i <= (OFUN_NUM + iUserFun); i++){
- //printf("比较前:%s\n", oper[i].name);
- //if (i == 63) printf("hello\n");//////////////////////////////////
- if ((s[0] != '\0') && (strcmp(s, oper[i].name) == 0)){
- //printf("找到的:%d\n",i);/////////////////////////////////////////
- return i;
- }
- }
- //printf("LAST:%d\n", i);/////////////////////////////////////////
- return 0;
- }
- //查找函数参数变量
- int IsPara(char *s){
- register int i = 1;
- for (i = 1; i <= iParCtr; i++){
- if ((s[0] != '\0'))
- if ((strcmp(s, functpara[i].name) == 0))
- return i;
- }
- return 0;
- }
- //查找常数
- int IsConst(char *s){
- register int i = 1;
- for (i = 1; i < CST_CTR + iUserCnt; i++){
- if ((s[0] != '\0') && (strcmp(s, constants[i].name) == 0)){
- return i;
- }
- }
- return 0;
- }
- //符栈看栈顶
- OTYPE OPeek(OprSTACK *s){
- OTYPE r;
- if ((*s).top == 0){//判栈空,空则赋等级0值
- r.level = 0;
- return r;
- }
- else
- return(*s).stack[(*s).top - 1];
- }
- ///////////////////////////////
- //字符串函数
- char *Mid(char *String, long Start, long Length = -1){
- long i = 0, j = 0;
- char string[N] = { '\0' }; strcpy(string, String);
- if (Length == -1)
- Length = strlen(string) - Start + 1;
- string[Start + Length - 1] = '\0';
- for (i = 0, j = Start - 1; i <= strlen(string); i++, j++)
- string[i] = string[j];
- return string;
- }
- ///////////////////////////////
- bool AddFunc(char *name, char para[][M], char *expr){
- int i = 0;
- if (IsOper(name) != 0)
- return false;
- ++iUserFun;
- //printf("\t\\%s\\\n", oper[OFUN_NUM + iUserFun].name);///////////////////////////////////
- oper[OFUN_NUM + iUserFun].ctype = UserFun;
- oper[OFUN_NUM + iUserFun].level = 99;
- oper[OFUN_NUM + iUserFun].name = name;
- oper[OFUN_NUM + iUserFun].number = OFUN_NUM + iUserFun;
- oper[OFUN_NUM + iUserFun].expr = expr;
- //printf("自定义函数:"%s"\n", oper[OFUN_NUM + iUserFun].name);////////////////////
- //oper[OFUN_NUM + iUserFun]
- for (i = 0; i < PAR_MAX; i++){
- //printf("参数:"%s"\n", para[i]);////////////////////////////////////////
- if ((IsConst(para[i]) == 0) && (IsPara(para[i]) == 0) && (para[i][0] != '\0')){
- //printf("参数:"%s"\n", para[i]);////////////////////////////////////////
- functpara[i + 1].name = para[i];
- //iParCtr++;
- }
- else{
- break;
- }
- }
- return true;
- //printf("自定义函数:%s\n", oper[OFUN_NUM + iUserFun].name);////////////////////
- }
- void SetPara(double value){
- ++iParCtr;
- functpara[iParCtr].number = iParCtr;
- functpara[iParCtr].value = value;
- }
- bool ClearAllFunc(void){
- int i = 0;
- for (i = 0; i <= iUserFun; i++){
- oper[OFUN_NUM + iUserFun].name = "";
- oper[OFUN_NUM + iUserFun].number = 0;
- }
- iUserFun = 0;
- iParCtr = 0;
- return true;
- }
- //////
- bool AddConstant(char *cname, double value){
- int i = 0;
- if (IsConst(cname) != 0)
- return false;
- if (IsPara(cname) != 0)
- return false;
- if (IsOper(cname) != 0)
- return false;
- ++iUserCnt;
- //printf("CST HELLO!\t%d\n", CST_CTR + iUserCnt);///////////////////////////////
- constants[CST_CTR + iUserCnt].name = cname;
- constants[CST_CTR + iUserCnt].value = value;
- constants[CST_CTR + iUserCnt].number = CST_CTR + iUserCnt;
- return true;
- }
- bool ClearAllConst(void){
- int i = 0;
- for (i = 0; i <= iUserCnt; i++){
- constants[CST_CTR + iUserCnt].name = "";
- constants[CST_CTR + iUserCnt].number = 0;
- }
- iUserCnt = 0;
- return true;
- }
- ///////////////////////////////
- void CALC_simple(OTYPE OTOpr, NumSTACK* dblStack, bool isFun = false){
- double tmpNumA = 0.0, tmpNumB = 0.0, tmpNumR = 0.0;
- int j = 0;
- //printf("CALC_simple hello\n");///////////////////////////////
- //printf("\t运算符出栈:%s\n",OTOpr.name);/////////////////////////////////
- switch (OTOpr.ctype){//检测运算符类型
- case DoubleCalOpr:
- tmpNumA = NPop(dblStack);//取出一个操作数
- //printf("\t数栈出栈A:%f\n",tmpNumA);////////////////////////////
- tmpNumB = NPop(dblStack);//取出一个操作数
- //printf("\t数栈出栈B:%f\n",tmpNumB);////////////////////////////
- tmpNumR = DOprCal(tmpNumA, tmpNumB, OTOpr.number);//双目运算符运算
- break;
- case FrontCalOpr:
- tmpNumA = NPop(dblStack);//取出一个操作数1
- //printf("\t数栈出栈A2:%f\n",tmpNumA);////////////////////////////
- tmpNumB = NPop(dblStack);//取出一个操作数2
- //printf("\t数栈出栈B2:%f\n",tmpNumB);////////////////////////////
- tmpNumR = FOprCal(OTOpr.number, tmpNumA);
- NPush(dblStack, tmpNumB);//操作数1进栈
- //printf("\t内进栈:%f\n",tmpNumB);////////////////////////////
- break;
- case RearCalOpr:
- tmpNumA = NPop(dblStack);//取出一个操作数1
- tmpNumR = ROprCal(tmpNumA, OTOpr.number);
- break;
- case Functio:
- for (j = 0; j <= intFunParCount; j++){
- pa[j] = NPop(dblStack);//取出一个操作数1
- //printf("\t数栈出栈AF:%f\n", tmpNumA);////////////////////////////
- }
- tmpNumR = FOprCal(OTOpr.number);
- break;
- case UserFun:
- for (j = 0; j <= intFunParCount; j++)
- SetPara(NPop(dblStack));//取出一个操作数
- tmpNumR = FOprCal(OTOpr.number);
- break;
- }
- NPush(dblStack, tmpNumR);//运算结果进栈
- //printf("数栈进栈 内:%f\n",tmpNumR);////////////////////////////
- }
- double CALC_main(char *exp){//计算
- //初始化
- //InitOperTable();
- //定义栈
- NumSTACK nStack;
- OprSTACK oStack;
- //初始化栈
- InitNumSTACK(&nStack);
- InitOprSTACK(&oStack);
- char sTmpNum[S_NUM_MAX] = { 0 };//用于[数字整合]的临时变量
- char sTmpOpr[S_OPR_MAX] = { 0 };//用于[运算符整合]临时变量
- char cTmpDblChar[2] = { 0, 0 };//临时存放单个字符
- OTYPE y, w;//
- int iWhichOpr = 0;//记录运算符返回值
- //主循环
- //ix = 0;//位置标记
- if (exp == "")return 0;//空串返零
- //exp = strupr(exp);//全转为大写
- for (ix = 0; ix <= strlen(exp); ix++){
- if ((exp[ix] >= '0'&&exp[ix] <= '9') || exp[ix] == '-' || exp[ix] == '.'){//区分操作数和运算符及其进栈
- //数字整合
- cTmpDblChar[0] = exp[ix];
- strcat(sTmpNum, cTmpDblChar);
- if ((exp[ix + 1] >= '0'&&exp[ix + 1] <= '9') || exp[ix + 1] == '-' || exp[ix + 1] == '.'){
- continue;
- }
- else{//数字进栈
- double x = atof(sTmpNum);//转换
- NPush(&nStack, x);//数字进栈
- //printf("数字进栈A:%f\n",x);/////////////////////////////////
- sTmpNum[0] = '\0';//进栈后的临时数串清零
- }
- }
- else{//运算符整合及运算
- //运算符整合
- cTmpDblChar[0] = exp[ix];
- strcat(sTmpOpr, cTmpDblChar);
- //printf("符号:%s\n", sTmpOpr);////////////////////////////////////////////////////////
- //printf("yname==%d\n", IsOper(sTmpOpr));/////////////////////////////////////////////////////
- if (iWhichOpr = IsOper(sTmpOpr)){
- y = oper[iWhichOpr];
- //printf("yname==%s\n",y.name);/////////////////////////////////////////////////////
- sTmpOpr[0] = '\0';
- if (y.ctype != IsBracket){
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- while (OPeek(&oStack).level >= y.level&&oStack.top > 0 && OPeek(&oStack).level > 0){//当栈中符的等级大于当前等级时则取出符进行运算
- w = OPop(&oStack);//出栈一个运算符
- //printf("\t运算符出栈1:%s\n",w.name);/////////////////////////////////
- CALC_simple(w, &nStack);
- intFunParCount = 0;
- }
- OPush(&oStack, &y);//直到无法运算将当前符放入栈中
- //printf("运算符进栈1:%s\n",y.name);/////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- else{
- if (y.number == 20){//如果是左括号则无条件进栈
- OPush(&oStack, &y);//进栈后等级为-1,以便遇到右括号出栈
- //printf("运算符进栈2:%s\n",y.name);/////////////////////////////////
- }
- else if (y.number == 21){//右括号计算
- while (OPeek(&oStack).level != -1){//遇到右括号则计算直到左括号
- w = OPop(&oStack);//出栈一个运算符
- //printf("\t运算符出栈2:%s\n", w.name);/////////////////////////////////
- CALC_simple(w, &nStack);
- }
- OPop(&oStack);//直到遇到左括号将左括号出栈
- }
- else if (y.number == 22){
- intFunParCount++;
- }
- }
- }
- else{
- continue;
- }
- }
- }
- while (OPeek(&oStack).level != 0){//表达式结束后对栈进行操作直到栈空
- if (OPeek(&oStack).level == -1){//如果有为用掉的左括号则报错退出程序
- Error(SyntaxError);
- return 0;
- }
- //printf("\t\t运算符1:%s\n",OPeek(&oStack).name);/////////////////////////////////
- w = OPop(&oStack);//取出两个数和一个符进行计算
- //printf("\t运算符出栈3:%s\n",w.name);/////////////////////////////////
- CALC_simple(w, &nStack);
- intFunParCount = 0;
- }
- Answ = NPop(&nStack);
- //constants[3].value = Answ;
- return Answ;
- }
- ///////////////////////////////
- //功能
- //角度弧度梯度制转换
- inline void ChangeScaleEnv(int x){
- if (x == 0)
- degScale = S_Rad;
- else if (x == 2)
- degScale = S_Deg;
- else
- degScale = S_Gra;
- }
- //Preprocesser Debugger
- char *InputScanner(char *exp){
- srand((int)time(NULL));
- register int i = 0, j = 0, z = 0, h = 0;//循环计数器
- //初始化
- //InitOperTable();
- //InitConstTable();
- constants[4].value = rand();
- if (InitConstTable) constants[3].value = Answ;
- char sTmpOpr[S_OPR_MAX] = { 0 };//用于[运算符整合]临时变量
- char cTmpDblChar[2] = { 0, 0 };//临时存放单个字符
- register int iWhichOpr = 0;//记录运算符返回值
- //int iWhichCst = 0;//记录常数
- OTYPE y; bool bEnvIsTrue = false; CONSTANT c;
- int absCtrAll = 0;//记录"|"的总数
- char tStr[N] = { '\0' }, ttStr[N] = { '\0' }, ttStrB[N] = { '\0' };
- for (i = 0; i <= strlen(exp); i++){
- if (exp[i] == ' ')//跳过空格
- continue;
- if (exp[i] >= 'a'&&exp[i] <= 'z'){
- tStr[j] = exp[i] - 32;
- }
- else{
- tStr[j] = exp[i];
- }
- if (exp[i] == '|')
- absCtrAll++;
- j++;
- }
- tStr[j + 1] = '\0';
- strcpy(ttStr, tStr);
- //处理绝对值 将|-1|转换为ABS(-1)
- if (absCtrAll % 2 != 0){
- Error(SyntaxError);
- return "";
- }
- for (i = 0; i <= strlen(tStr); i++){
- if (tStr[i] == '|'){
- if (h < absCtrAll / 2){
- z = 0;
- strcpy(ttStr, Mid(tStr, 1, i));
- strcpy(ttStrB, Mid(tStr, i + 2));
- tStr[0] = '\0';
- strcat(tStr, ttStr);
- strcat(tStr, "ABS(");
- strcat(tStr, ttStrB);
- //printf("\t%s\n", tStr);//////////////////
- h++;
- }
- else
- tStr[i] = ')';
- }
- }
- /*
- for (i = 0; i <= strlen(tStr); i++)
- if (tStr[i] == '|')
- tStr[i] = ')';
- */
- //
- i = 0; j = 0; z = 0; h = 0;
- ttStr[0] = '\0'; ttStrB[0] = '\0';
- //将减法转换为加法 a-b == a+-b
- for (i = 0; i <= strlen(tStr); i++){
- if ((tStr[0] != NULL) && (i >= 1) && ((tStr[i - 1] >= '0'&&tStr[i - 1] <= '9' || tStr[i - 1] == ')') && (tStr[i] == '-') && ((tStr[i + 1] >= '0'&&tStr[i + 1] <= '9') || tStr[i + 1] == '.' || tStr[i + 1] == '('))){
- z = 0;
- ttStr[i] = '+';
- for (z = i; z <= strlen(tStr); z++)
- ttStr[z + 1] = tStr[z];
- ttStr[z + 1] = '\0';
- for (z = 0; z <= strlen(ttStr); z++)
- tStr[z] = ttStr[z];
- i++;
- }
- }
- //将+-()转换为+-1*()
- for (i = 0; i <= strlen(tStr); i++){
- if ((tStr[0] != NULL) && (tStr[i - 1] == '-' && (tStr[i] == '(' || (tStr[i] >= 'a'&&tStr[i] <= 'z' || tStr[i] >= 'A'&&tStr[i] <= 'Z')))){
- z = 0;
- for (z = 0; z < i; z++)
- ttStr[z] = tStr[z];
- ttStr[z] = '1';
- ttStr[++z] = '*';
- for (z = i + 2; tStr[z - 2] != NULL; z++)
- ttStr[z] = tStr[z - 2];
- strcpy(tStr, ttStr);
- i += 2;
- }
- }
- //printf("1 hello!!\n");//////////////////////////////////////
- //将:2sin变成2*sin
- for (i = 0; i <= strlen(tStr); i++){
- if ((tStr[i] >= '0'&&tStr[i] <= '9') || tStr[i] == '-' || tStr[i] == '.'){//区分操作数和运算符
- bEnvIsTrue = true;
- sTmpOpr[0] = '\0';///////////////////////////WARNING!!!
- }
- else{
- cTmpDblChar[0] = tStr[i];
- strcat(sTmpOpr, cTmpDblChar);
- if (iWhichOpr = IsOper(sTmpOpr)){
- y = oper[iWhichOpr];
- sTmpOpr[0] = '\0';
- if ((y.ctype == FrontCalOpr || y.ctype == Functio) && bEnvIsTrue){
- z = 0;
- ttStr[i - strlen(y.name) + 1] = '*';
- for (z = i - strlen(y.name) + 1; z <= strlen(tStr); z++)
- ttStr[z + 1] = tStr[z];
- ttStr[z + 1] = '\0';
- for (z = 0; z <= strlen(ttStr); z++)
- tStr[z] = ttStr[z];
- i++;
- }
- bEnvIsTrue = false;
- }
- }
- }
- //printf("2 hello!!\n");//////////////////////////////////////
- //printf("3 hello!!\n");//////////////////////////////////////
- //将2pi转换成2*pi
- bEnvIsTrue = false; sTmpOpr[0] = '\0';
- for (i = 0; i <= strlen(tStr); i++){
- if ((tStr[i] >= '0'&&tStr[i] <= '9') || tStr[i] == '-' || tStr[i] == '.'){//区分操作数和运算符
- bEnvIsTrue = true;
- sTmpOpr[0] = '\0';
- }
- else{
- //printf("hello\n");///////////////////////////
- cTmpDblChar[0] = tStr[i];
- strcat(sTmpOpr, cTmpDblChar);
- //printf("%s\n",sTmpOpr);////////////////////////////
- if (iWhichOpr = IsConst(sTmpOpr)){
- //printf("hello\n");///////////////////////////
- c = constants[iWhichOpr];
- sTmpOpr[0] = '\0';
- if (bEnvIsTrue){
- /*
- z = 0;
- ttStr[i - strlen(c.name) + 1] = '*';
- for (z = i - strlen(c.name) + 1; z <= strlen(tStr); z++)
- ttStr[z + 1] = tStr[z];
- ttStr[z + 1] = '\0';
- for (z = 0; z <= strlen(ttStr); z++)
- tStr[z] = ttStr[z];
- i++;
- */
- strcpy(ttStr, Mid(tStr, 1, i - strlen(c.name) + 1));
- //printf("A\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- strcat(ttStr, "*");
- //printf("B\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- //printf("BA\tttStr %s\n", Mid(tStr, i)); system("pause");////////////////////////////////////
- strcat(ttStr, Mid(tStr, i));
- //printf("C\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- i = i - strlen(c.name) + 1;
- strcpy(tStr, ttStr);
- }
- bEnvIsTrue = false;
- }
- }
- }
- //printf("\t"%s"\n", tStr);/////////////////////////////////////////////////////////
- //
- //printf("::::%s\n", tStr);/////////////////////////////////////////////////////i <= strlen(tStr)
- //将函数参数替换成数值
- sTmpOpr[0] = '\0'; bEnvIsTrue = true;
- for (i = 0; i <= strlen(tStr); i++){
- if ((tStr[i] >= '0'&&tStr[i] <= '9') || tStr[i] == '-' || tStr[i] == '.'){//区分操作数和运算符
- //bEnvIsTrue = true;
- sTmpOpr[0] = '\0';
- }
- else{
- //printf("tstr %s\n", tStr);
- //printf("i==%d\n", i);///////////////////////////////////////////////////////////////////////
- //printf("\t%c\n",tStr[i]);////////////////////////////////////////////
- //if ((tStr[i] >= 'A') && (tStr[i] <= 'Z')){//区分操作数和运算符
- //printf("1A,%d====%s\n", i, sTmpOpr);///////////////////////////////////////////
- cTmpDblChar[0] = tStr[i];
- //if (cTmpDblChar[0] >= '0' || cTmpDblChar[0] <= '9')sTmpOpr[0] = '\0';///////////////////////////////////////////
- strcat(sTmpOpr, cTmpDblChar);
- //printf("XX %s\n",sTmpOpr);///////////////////////////////////////////////////////////
- //printf("IsPara %d\n", IsPara(sTmpOpr));////////////////////////////////////////////////
- //printf("\t\tEnv %d\n", bEnvIsTrue);/////////////////////////////////////////////////////
- //if (strcmp(sTmpOpr, "PI") == 0) printf("ok\n");
- if (IsOper(sTmpOpr)){
- sTmpOpr[0] = '\0';
- bEnvIsTrue = true;
- }
- else if ((iWhichOpr = IsConst(sTmpOpr)) && bEnvIsTrue){
- //printf("sTmpOpr \'%s\'\n", sTmpOpr);//////////////////////////////////////////////////显示字符串
- sTmpOpr[0] = '\0';
- bEnvIsTrue = false;
- c = constants[iWhichOpr];
- //printf("替换hello\n");
- printf("替换 %s\n",c.name);
- strcpy(ttStr, Mid(tStr, 1, i - strlen(c.name) + 1));
- //printf("A\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- j = sprintf(ttStrB, "%.16f", c.value);
- strcat(ttStr, ttStrB);
- //printf("B\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- strcat(ttStr, Mid(tStr, i + strlen(c.name)));
- //printf("C\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- i = i - strlen(c.name) + j;
- strcpy(tStr, ttStr);
- //printf("tStr %s\n", tStr);
- //printf("i %d\n", i + strlen(c.name)+j);
- //i += (j + (i - (strlen(c.name) + 1)));
- //i += strlen(c.name) + j;
- }
- }
- }
- //将函数参数替换成数值B
- sTmpOpr[0] = '\0'; bEnvIsTrue = true;
- for (i = 0; i <= strlen(tStr); i++){
- if ((tStr[i] >= '0'&&tStr[i] <= '9') || tStr[i] == '-' || tStr[i] == '.'){//区分操作数和运算符
- sTmpOpr[0] = '\0';
- }
- else{
- cTmpDblChar[0] = tStr[i];
- strcat(sTmpOpr, cTmpDblChar);
- if (IsOper(sTmpOpr)){
- sTmpOpr[0] = '\0';
- bEnvIsTrue = true;
- }
- else if ((iWhichOpr = IsPara(sTmpOpr)) && bEnvIsTrue){
- sTmpOpr[0] = '\0';
- bEnvIsTrue = false;
- c = functpara[iWhichOpr];
- strcpy(ttStr, Mid(tStr, 1, i - strlen(c.name) + 1));
- //printf("A\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- j = sprintf(ttStrB, "%.16f", c.value);
- strcat(ttStr, ttStrB);
- //printf("B\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- strcat(ttStr, Mid(tStr, i + strlen(c.name)));
- //printf("C\tttStr %s\n", ttStr); system("pause");////////////////////////////////////
- i = i - strlen(c.name) + j;
- strcpy(tStr, ttStr);
- }
- }
- }
- return tStr;
- }
- ///////////////////////////////
- void Debug_Print_Para(void){
- int i = 0;
- for (i = 0; i <= CST_CTR + iUserCnt; i++){
- //printf("参数:"%s"\n", para[i]);////////////////////////////////////////
- //printf("参数:"%s"\n", para[i]);////////////////////////////////////////
- //printf("\t%s %f\n", functpara[i].name, functpara[i].value);
- printf("\t%s\n", constants[i].name);
- //iParCtr++;
- }
- }
- //主函数
- void PrintTitle(void){
- printf("Calculation Engine Kernel Alpha\n");
- printf("Copyright (C) 2014 Ctechnology Corporation & TZR Lab.\n");
- printf("All Rights Resaved.\n");
- }
- int main(){
- //////freopen("C:\\Users\\Cosh Cage\\Desktop\\t.txt","r",stdin);
- //////char* a="16.714+56";
- InitOperTable();
- InitConstTable();
- InitParaTable();
- char a[N] = { 0 };
- //Debug_Print_Para();
- AddConstant("ZZZ", 3.0);
- //Debug_Print_Para();
- char par[10][M] = { '\0' };
- strcat(par[0], "AA");
- strcat(par[1], "BB");
- strcat(par[2], "CC");
- AddFunc("SSUM", par, "AA+BB+CC");
- //printf("??%d\n", IsConst("ZZ"));//////////////////////////////////
- //printf("!! %s\n", constants[5].name);/////////////////////////////////////////////
- //ClearAllFunc();
- //printf("Calculation Engine Kernel Alpha\n");
- //printf("Copyright (C) 2014 Ctechnology Corporation & TZR Lab.\n");
- //printf("All Rights Resaved.\n");
- /*
- do{
- printf("?"); gets(a);
- //////////std::cin >> a;
- strcpy(a, InputScanner(a));
- printf("%s\n", a);
- printf("ANS=%f\n", CALC_main(a));
- } while (a[0] != NULL);
- */
- //15+6-8/2*5\n3+8*6-13\n9-2*3*3+8-6+5
- //printf("2*(1+2)\n");
- //printf("9-2-6\n");
- //8+2-(7-4)*2//3+2*(5-2)-4//3+4-(6+2)*2
- /////////////////////InitConstTable();
- //////////////////////printf("%d\n", IsConst("PIP\0"));//////////////////
- //strcpy(a, InputScanner("zz+1"));
- //printf(""%s"\n", a);
- //printf("ANS=%f\n", CALC_main(a));
- //Debug_Print_Para();
- //printf("IsPara %d\n", IsPara("AA"));////////////////////////////////////////////////
- //printf("\n");
- //printf("End:%s\n", InputScanner("AA+BB+CC"));
- //system("Pause");
- int i = 0; double localAns;
- char *fn; char str[M];
- PrintTitle();
- do{
- printf("?"); gets(a);
- if (strcmp(strupr(a), "ADDFUN") == 0){
- do{
- printf("\tFunction Name?"); gets(a);
- fn = a;
- for (i = 0; i < 10; i++){
- printf("\tFunction Parament?"); gets(a);
- if (a[0] == '\0'){
- break;
- }
- else{
- strcat(par[0], a);
- }
- }
- printf("\tFunction Expression?"); gets(a);
- AddFunc(fn, par, a); printf("\tAdded OK!\n");
- printf("\tFunction Name?"); gets(a);
- } while (a[0] != NULL);
- printf("?"); gets(a);
- }
- else if (strcmp(strupr(a), "ADDVAR") == 0){
- do{
- printf("\tVar Name?"); gets(a);
- fn = a;
- printf("\tVar Value?"); gets(a);
- AddConstant(fn, atof(a)); printf("\tAdded OK!\n");
- printf("\tVar Name?"); gets(a);
- } while (a[0] != NULL);
- printf("?"); gets(a);
- }
- else if (strcmp(strupr(a), "DELALLFUN") == 0){
- ClearAllFunc(); printf("Done!\n");
- printf("?"); gets(a);
- }
- else if (strcmp(strupr(a), "DELALLVAR") == 0){
- ClearAllConst(); printf("Done!\n");
- printf("?"); gets(a);
- }
- else if (strcmp(strupr(a), "SCALE") == 0){
- do{
- printf("\t\t0:Rad\n\t\t2:Deg\n\t\tAny:Gra\n\tScale?"); gets(a);
- ChangeScaleEnv(atoi(a));
- } while (a[0] != NULL);
- printf("?"); gets(a);
- }
- else if (strcmp(strupr(a), "CLS") == 0){
- system("cls");
- PrintTitle();
- }
- else{
- //////////std::cin >> a;
- strcpy(a, InputScanner(a));
- localAns = CALC_main(a);
- //printf("%s\n", a);
- str[0] = '\0';
- if (ERR_NUM != NoError){
- for (i = 1; i <= ix - 2; i++){
- strcat(str, " ");
- }
- strcat(str, "^\n");
- if (ERR_NUM == MathError){
- strcat(str, "MathError!");
- }
- else if (ERR_NUM == StackError){
- strcat(str, "StackError!");
- }
- else if (ERR_NUM == SyntaxError){
- strcat(str, "SyntaxError!");
- }
- else if (ERR_NUM == UnknownError){
- strcat(str, "UnknownError!");
- }
- printf("%s\n", a);
- printf("%s\n", str);
- }
- printf("ANS=%f\n", localAns);
- }
- } while (a[0] != NULL);
- return 0;
- }
- //////////END///////////////////
复制代码 |
|