您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > C语言计算器程序源代码
//strcmp(s1,s2)当s1大于s2时,返回1,s1小于s2时,返回-1,相等时,返回0#includestdio.h#includectype.h#includestring.h#includemath.h#defineMAX256#defineSTACK_SIZE128#defineWORD_LEN8#definePOP1#definePUSH0#defineERR-1#defineEND2#defineOPER0#defineNUM1#defineWORD2#defineADD1#defineSUB2#defineMUL3#defineDIV4#definePOW5#defineFAC6#defineBRA_L7#defineBRA_R8#defineSIN9#defineCOS10#defineTAN11#defineCTG12#defineLG13//以10为底的常用对数//#defineLN14//#defineLOG15//行标为当前操作符代号,列标为栈顶元素代号//2表示计算结束,0表示当前操作符进栈,1表示栈顶操作符出栈//\0+-*/^!()sincostgctglgintPriority[14][14]={2,1,1,1,1,1,1,-1,-1,1,1,1,1,1,/*\0*/0,1,1,1,1,1,1,0,-1,1,1,1,1,1,/*+*/0,1,1,1,1,1,1,0,-1,1,1,1,1,1,/*-*/0,0,0,1,1,1,1,0,-1,1,1,1,1,1,/***/0,0,0,1,1,1,1,0,-1,1,1,1,1,1,/*/*/0,0,0,1,1,1,1,0,-1,0,0,0,0,0,/*^*/0,0,0,0,0,0,1,0,-1,0,0,0,0,0,/*!*/0,0,0,0,0,0,-1,0,-1,0,0,0,0,0,/*(*/-1,1,1,1,1,1,1,1,-1,1,1,1,1,1,/*)*/0,0,0,0,0,0,-1,0,-1,0,0,0,0,0,/*sin*/0,0,0,0,0,0,-1,0,-1,0,0,0,0,0,/*cos*/0,0,0,0,0,0,-1,0,-1,0,0,0,0,0,/*tg*/0,0,0,0,0,0,-1,0,-1,0,0,0,0,0,/*ctg*/0,0,0,0,0,0,-1,0,-1,0,0,0,0,0};/*lg*/charKeyWord[36][WORD_LEN+1]={sin,//前12个为函数,多余的用于扩展cos,tan,tg,ctg,lg,,,,,,,,,,help,//后面为命令,多余的为扩展version,set,digit,//精度,小数点后的位数color,radian,//弧度degree,//角度file,clr,clear,window,//窗口模式fullscr,//全屏模式,,,,,,,,};intOperCode(charc){intcode;switch(c){case'\0':code=0;break;case'+':code=1;break;case'-':code=2;break;case'*':code=3;break;case'/':code=4;break;case'^':code=5;break;case'!':code=6;break;case'(':code=7;break;case')':code=8;break;case's':code=9;break;//sincase'c':code=10;break;//coscase't':code=11;break;//tgcase'C':code=12;break;//ctgcase'l':code=13;break;//logdefault:code=-1;break;};returncode;}intWordCode(char*word){inti;for(i=0;i25;i++)if(strcmp(KeyWord[i],word)==0)break;if(i=25)return-1;elsereturni;}voidhelp(){printf(显示帮助信息!\n);return;}voidversion(){printf(显示版本信息!\n);return;}voidErr(interrcode,intposition,char*p){printf(\nERR:%dPosition:%d%s,errcode,position,p);return;}doublelongfactorial(inti){if(i==1||i==0)return(1.0);elsereturn(i*factorial(i-1));}main(){charExpression[MAX+1];intOperator[STACK_SIZE];intOperStackTop;doublelongNumber[STACK_SIZE];intNumStackTop;doublelongNumList[STACK_SIZE];intNumCursor,NumListSize;intOperList[STACK_SIZE];intOperCursor,OperListSize;intWordList[STACK_SIZE];intWordCursor,WordListSize;intIndex[MAX+1];intIndexCursor,IndexSize;charWord[WORD_LEN+1];doublelongnum,num1,num2,weight,tempnum;intOper;intisDecimal,isErr,isNumber,isEnd;charCurrentOper;inti,j,k,m,n;charch;num=0.0;num1=0.0;num2=0.0;tempnum=0.0;Oper=-1;while(1){for(i=0;i=MAX;i++)//表达式初始化,中间表索引初始化{Expression[i]='\0';Index[i]=-1;}for(i=0;iSTACK_SIZE;i++)//操作符栈和数字栈初始化{Operator[i]='0';Number[i]=0.0;NumList[i]=0.0;OperList[i]=-1;WordList[i]=-1;}NumStackTop=-1;//栈顶指针初始化OperStackTop=0;//操作符栈压入\0Operator[OperStackTop]=OperCode('\0');NumCursor=0;//各种中间表指针初始化,各种中间表的长度初始化NumListSize=0;OperCursor=0;OperListSize=0;//操作符表中先写入第一个操作符'\0'WordCursor=0;WordListSize=0;IndexCursor=0;IndexSize=0;//Index[0]=OPER;printf(Cal);//初始化完成,输出提示符i=0;while((ch=getchar())!='\n'){if(iMAX)/*输入超长,则出错*/{Err(0,i,输入的表达式长度超过规定值!\n);isErr=1;break;}if(isupper(ch))ch=tolower(ch);Expression[i]=ch;i++;}if(isErr==1){isErr=0;continue;}if(strlen(Expression)==0)//直接回车continue;if(strcmp(end,Expression)==0||strcmp(exit,Expression)==0||strcmp(quit,Expression)==0)break;//一下代码为编译预处理,主要处理负号,并检查括号是否配对k=0;for(i=0;Expression[i]!='\0';i++){if((i==0&&Expression[i]=='-')||(i0&&Expression[i]=='-'&&Expression[i-1]=='(')){for(j=strlen(Expression);ji;j--)Expression[j]=Expression[j-1];Expression[i]='0';}if(Expression[i]=='(')//检查括号k++;if(Expression[i]==')')k--;}if(k0)//如果括号不配对{Err(1,-1,缺少右括号)\n);continue;}if(k0){Err(1,-1,缺少左括号(\n);continue;}//编译预处理结束i=0;//词法分析while(1){if(Expression[i]=='\0'){OperList[OperListSize]=OperCode(Expression[i]);OperListSize++;Index[IndexSize]=OPER;IndexSize++;//printf(IndexSize=%d,Index[IndexSize]=%d,Expression[i]=%c\n,IndexSize,Index[IndexSize],Expression[i]);break;}isDecimal=0;isNumber=0;while(isdigit(Expression[i])||Expression[i]=='.')//读取数字{isNumber=1;if(Expression[i]=='.'){if((i(MAX-1)&&!isdigit(Expression[i+1]))||(i+1)==MAX)//不正确的小数点位置{Err(2,i,小数点位置不正确!\n);isErr=1;isNumber=0;i++;break;}isDecimal=1;weight=0.1;i++;continue;}if(isDecimal==0)num=num*10.0+(doublelong)(Expression[i]-'0');else{num=num+(doublelong)(Expression[i]-'0')*weight;weight=weight*0.1;}i++;}//数字读完if(isErr==1)break;if(isNumber==1)//如果刚才成功读取了数字,则数字入栈{NumList[NumListSize]=num;NumListSize++;isNumber=0;num=0.0;Index[IndexSize]=NUM;//printf(IndexSize=%d,Index[IndexSize]=%d\n,IndexSize,Index[IndexSize]);IndexSize++;}for(k=0;k=WORD_LEN;k++)Word[k]='\0';j=0;while(isalpha(Expression[i])){if(j=WORD_LEN)//超过长度仍然未匹配,则出错{Err(3,i,单词长度超过规定值/未定义的单词:);printf(%s\n,Word);isErr=1;break;}Word[j]=Expression[i];j++;//printf(WORD:%s\n,Word);if(WordCode(Word)==-1)//匹配不成功{if(!isalpha(Expression[i+1]))//匹配不成功,但是下一个字符已经不是字母,{Err(4,i,未定义的单词:);//则出错,并跳出循环printf(%s\n,Word);isErr=1;break;}//匹配不成功且还能继续读取字符,则继续读取下一个字母i++;continue;}else//匹配成功,则单词入表,读取下一个字符{switch(WordCode(Word)){case0:ch='s';break;case1:ch='c';b
本文标题:C语言计算器程序源代码
链接地址:https://www.777doc.com/doc-6253836 .html