输入3个值switch(c)case '+':return a+b;break;...其他照样了 像这样的话 就只能用栈实现了
输入的时候 要这么输..1*(2+1) 不能输等号...#include ""#include<>#include<>char s[100];struct my{ char *s; int v;};void f2(char *s, int f, int f4);int f1(char *p);struct my *ff(char *p, int f);void main( ){ int i; int f; char *p; p=s; gets(s); while((f=f1(s))!=-1) { f2(s,f,1); } }struct my *ff(char *p, int f){ char s1[100]; struct my *r; char *s; int i=0, j=1, k=0; s=p;strcpy(s1,s); r=malloc(sizeof(struct my)); if(f==0) { p--; while(*p>='0'&&*p<='9') { i+=(*p-'0')*j; j=j*10; p--; } p++; r->v=i; r->s=p; return r; } if(f==1) { p++; while(*p>='0'&&*p<='9') {p++; k++;} p=s; p++; for(f=0; f
你可以另外写四个函数(加减乘除),然后再main函数里调用它#include#includeintAdd(double*result,doubleleft,doubleright){*result=left+right;return1;}intSub(double*result,doubleleft,doubleright){*result=left-right;return1;}intMul(double*result,doubleleft,doubleright){*result=left*right;return1;}intDiv(double*result,doubleleft,doubleright){if(right==0){printf("出书不能为零,请核实
你就用楼下的程序吧!!/*** 一共包含四个文件 *************| | | | | 这里的代码考虑到通用性,对代码的可| 重用性作了细致的考虑,略显冗余,核心算法:| 里的函数STATUS EvaluateExpression(float*fResult,char*strExpression)**********************************/// ""extern EXPRESSION_DEBUG;STATUS EvaluateExpression(float*,char*);void Usage(char *);int HandleOptions(int,char **);int main(int argc,char*argv[ ]){char strLine[30]={0};float fResult=0;/* handle the program options */HandleOptions(argc,argv);fprintf(stderr,"Input cls to clear the screen\n");fprintf(stderr," debug to show stack change\n");fprintf(stderr," nodebug to show no stack change\n");fprintf(stderr," end to exit\n");while(TRUE){ printf("Input:\n"); gets(strLine); if(!strcmp(strLine,"end")) break; if(!strcmp(strLine,"cls")) { system("cls"); continue; } if(!strcmp(strLine,"debug")){ EXPRESSION_DEBUG=TRUE; continue; } if(!strcmp(strLine,"nodebug")){ EXPRESSION_DEBUG=FALSE; continue; } EvaluateExpression(&fResult,strLine); printf("Ans=%f\n",fResult);}return OK;}void Usage(char *programName){fprintf(stderr,"%s usage:%s [-d][-h/?]\n",programName,programName);fprintf(stderr,"-d Test program, calculate expression and\n");fprintf(stderr," see changes in the stack at the same time.\n");exit(OK);}/* returns the index of the first argument that is not an option; . does not start with a dash or a slash*/int HandleOptions(int argc,char *argv[]){int i,firstnonoption=0;for (i=1; i< argc;i++) { if (argv[i][0] == '/' || argv[i][0] == '-') { switch (argv[i][1]) { /* An argument -? means help is requested */ case '?': case 'h': case 'H': Usage(argv[0]); break; case 'd': case 'D': EXPRESSION_DEBUG=TRUE; break; default: fprintf(stderr,"unknown option %s\n",argv[i]); break; } } else { firstnonoption = i; break; }}return firstnonoption;}// - definitions/declarations for symbols used by other C Header files#ifndef SYMBOL_H#define SYMBOL_H#define STACK_H#define TRUE 1#define OK 1#define YES 1#define FALSE 0#define ERROR 0#define NO 0#define OVERFLOW -1#ifndef NULL#define NULL 0#endiftypedef unsigned int UINT;typedef int STATUS;typedef int BOOL;#endif /*SYMBOL_H*/// - definitions/declarations for stack operation#ifndef STACK_H#define STACK_H#include ""#include <>#include <>#include <>#include <>#include <>#define S_CHAR 1#define S_SHORT 2#define S_INT 3#define S_FLOAT 4#define S_DOUBLE 5//-- struct tagNode{void*pData;struct tagNode*pNext;}Node,*PNode;typedef struct tagStack{UINT uType;/*1 char*2 short/short int*3 int*4 float*5 double*/UINT uLength;struct tagNode*pFirst;struct tagNode*pTop;}Stack,*PStack;STATUS InitStack(PStack pStack,UINT uType){pStack->uType=uType;pStack->uLength=0;pStack->pFirst=pStack->pTop=NULL;return OK;}STATUS ShowStack(PStack pStack){PNode pNode=pStack->pFirst;while(pNode){ switch(pStack->uType) { case S_CHAR: // char printf("%c ",*(char*)(pNode->pData));break; case S_FLOAT: // float printf("% ",*(float*)(pNode->pData)); } pNode=pNode->pNext;}putchar(10);return OK;}STATUS Push(PStack pStack,void*pData){PNode pNode=(PNode)malloc(sizeof(Node));if(!pNode){ printf("\nmalloc error!\n"); fflush(stdin); getch(); exit(ERROR);}if(pStack->uType==1){ pNode->pData=(char*)malloc(sizeof(char)); *(char*)(pNode->pData)=*(char*)pData;}else if(pStack->uType==3){ pNode->pData=(int*)malloc(sizeof(int)); pNode->pData=(int*)malloc(sizeof(int)); *(int*)(pNode->pData)=*(int*)pData;}else if(pStack->uType==4){ pNode->pData=(float*)malloc(sizeof(float)); pNode->pData=(float*)malloc(sizeof(float)); *(float*)(pNode->pData)=*(float*)pData;}else if(pStack->uType==5){ pNode->pData=(double*)malloc(sizeof(double)); pNode->pData=(double*)malloc(sizeof(double)); *(double*)(pNode->pData)=*(double*)pData;}pNode->pNext=NULL;if(!pStack->pTop) pStack->pTop=pStack->pFirst=pNode;else{ pStack->pTop->pNext=pNode; pStack->pTop=pNode;}pStack->uLength++;return OK;}STATUS Pop(PStack pStack,void*pData){PNode pPre=pStack->pFirst;if(pStack->pTop!=pStack->pFirst) while(pPre->pNext!=pStack->pTop) pPre=pPre->pNext;else pPre=NULL;if(pStack->uType==1) *(char*)(pData)=*(char*)(pStack->pTop->pData);else if(pStack->uType==3) *(int*)(pData)=*(int*)(pStack->pTop->pData);else if(pStack->uType==4) *(float*)(pData)=*(float*)(pStack->pTop->pData);else if(pStack->uType==5) *(double*)(pData)=*(double*)(pStack->pTop->pData);free(pStack->pTop->pData);free(pStack->pTop);pStack->pTop=pPre;if(pPre) pStack->pTop->pNext=NULL;else pStack->pFirst=NULL;pStack->uLength--;return OK;}STATUS GetTop(PStack pStack,void*pData){if(pStack->uType==1) *(char*)(pData)=*(char*)(pStack->pTop->pData);else if(pStack->uType==3) *(int*)(pData)=*(int*)(pStack->pTop->pData);else if(pStack->uType==4) *(float*)(pData)=*(float*)(pStack->pTop->pData);else if(pStack->uType==5) *(double*)(pData)=*(double*)(pStack->pTop->pData);return OK;}STATUS DestroyStack(PStack pStack){PNode pPre1,pPre2;pPre1=pPre2=pStack->pFirst;while(pPre1){ pPre1=pPre1->pNext; free(pPre2->pData); free(pPre2); pPre2=pPre1;}pStack->pFirst=pStack->pTop=NULL;pStack->uLength=0;return OK;}#endif /* STACK_H */// EXPRESSION_H#define EXPRESSION_H#include ""typedef struct tagOptr{char cOptr;UINT uPriority;}Optr,*POptr;BOOL EXPRESSION_DEBUG=FALSE;Optr pOptr[8]={{0,7},{')',1},{'*',2},{'/',2},{'+',3},{'-',3},{'(',4},{'#',4}};STATUS Operate(float*fTemp3,float fTemp1,char theta,float fTemp2){switch(theta){case '+':*fTemp3=fTemp1+fTemp2;break;case '-':*fTemp3=fTemp1-fTemp2;break;case '*':*fTemp3=fTemp1*fTemp2;break;case '/': if(fTemp2!=0) *fTemp3=fTemp1/fTemp2; else { printf("\n0 can not be divisor!\n\nPress any key to continue...\n"); fflush(stdin); getch(); exit(ERROR); }// else break;}return OK;}int Precede(char cOptrTop,char cChar){UINT i,j;if(cOptrTop=='#'&&cChar=='#') return 0;if(cChar=='(') return -1;if(cChar==')') if(cOptrTop=='(') return 0; else return 1;for(i=1;i<=pOptr[0].uPriority;i++) if(pOptr[i].cOptr==cOptrTop) { i=pOptr[i].uPriority; break; }for(j=1;j<=pOptr[0].uPriority;j++) if(pOptr[j].cOptr==cChar) { j=pOptr[j].uPriority; break; }if(i<=j) return 1;else return -1;return -2;}STATUS IsIn(char cChar){if(cChar>='0'&&cChar<='9'||cChar=='.') return YES;return NO;}STATUS Debug(PStack stackOptr,PStack stackOpnd,char*strExpression,int i){// --debugif(EXPRESSION_DEBUG){ printf("-------------------------------\n"); printf("%s\n",strExpression); printf("Optr:"); ShowStack(stackOptr); printf("Opnd:"); ShowStack(stackOpnd);}return OK;}STATUS EvaluateExpression(float*fResult,char*strExpression){char cChar='#',cOptrTop=0,theta=0;float fTemp1,fTemp2,fTemp3,fTemp4;int i=0,iTemp;Stack stackOptr,stackOpnd;InitStack(&stackOptr,S_CHAR);InitStack(&stackOpnd,S_FLOAT);Push(&stackOptr,&cChar);GetTop(&stackOptr,&cOptrTop);strcat(strExpression,"#");if(strExpression[0]=='-'){ fTemp1=0; Push(&stackOpnd,&fTemp1);}cChar=strExpression[0];while(cChar!='#'||cOptrTop!='#'){ Debug(&stackOptr,&stackOpnd,strExpression,i); if(IsIn(cChar)) { fTemp1=0; fTemp3=10; fTemp4=1; while(IsIn(cChar)) { if(cChar=='.'){ fTemp3=1; cChar=strExpression[++i]; continue; } fTemp2=(float)(cChar-'0'); if(fTemp3==1) { iTemp=i; while(IsIn(strExpression[i])){ fTemp2/=10; i++; } i=iTemp; } fTemp1=fTemp1*fTemp3+fTemp2; cChar=strExpression[++i]; } Push(&stackOpnd,&fTemp1); } else { switch(Precede(cOptrTop,cChar)) { case -1:Push(&stackOptr,&cChar);cChar=strExpression[++i];break; case 0:Pop(&stackOptr,&cChar);cChar=strExpression[++i];break; case 1: Pop(&stackOptr,&theta); Pop(&stackOpnd,&fTemp2);Pop(&stackOpnd,&fTemp1); Operate(&fTemp3,fTemp1,theta,fTemp2); Push(&stackOpnd,&fTemp3); break; }// switch GetTop(&stackOptr,&cOptrTop); }// else}// whilePop(&stackOptr,&cChar);Debug(&stackOptr,&stackOpnd,strExpression,i);GetTop(&stackOpnd,fResult);DestroyStack(&stackOptr);DestroyStack(&stackOpnd);return OK;}// EvaluateExpression#endif // EXPRESSION_H
~~rand()%100用来产生0~100以内的随机数,不能每次都存入一样的数吧,所以用随机数好些,。。,,我学的也是谭浩强的,没有rand()用于产生随机数,cystem(“pause”)用于暂停,相当于getchar()或getch(),cystem("cls")用于清屏,可以去掉,这些东西都是我百度上百得。,。呵呵谭浩强书上确实没有这些东西。。把问题先想清楚是一种很好的习惯,,呵呵/* Note:Your choice is C IDE */#include ""#include""int right=0,wrong=0;void add(){ int a,b,c; a=rand()%100; b=rand()%100; printf("请回答:%d+%d=",a,b); scanf("%d",&c); if(a+b!=c){printf("回答错误\n");wrong++;} else {printf("回答正确\n");right++;} }void minu(){ int a,b,c; a=rand()%100; b=rand()%100; printf("请回答:%d-%d=",a,b); scanf("%d",&c); if(a-b!=c){printf("回答错误\n");wrong++;} else {printf("回答正确\n");right++;} }void mul(){ int a,b,c; a=rand()%100; b=rand()%100; printf("请回答:%d*%d=",a,b); scanf("%d",&c); if(a*b!=c){printf("回答错误\n");wrong++;} else {printf("回答正确\n");right++;} }void di(){ int a,b,c; a=rand()%100; b=rand()%100; printf("请回答:%d/%d=",a,b); scanf("%d",&c); if(a/b!=c){printf("回答错误\n");wrong++;} else {printf("回答正确\n");right++;} }void main(){ int choise,con=0; printf("\n\n\t\t欢迎光临我的C语言四则运算程序\n"); system("pause"); system("cls"); while(1) { printf("\n\n\t\t请选择:\n加(输入1)\n减(输入2)\n乘(输入3)\n除(输入4)\n"); if(con==0)scanf("%d",&choise); switch(choise) { case 1:add();break; case 2:minu();break; case 3:mul();break; case 4:di();break; } printf("请问您想继续进行这个运算还是重新选择其他运算还是退出程序?\n继续(输入1),重新(输入2),退出(输入3)"); scanf("%d",&con); if(con==1)con=1; if(con==2)con=0; if(con==3)break; } printf("您总做了%d个题,正确%d的道,错误%d道!\n",right+wrong,right,wrong); system("pause");} 编译器不同可能不能运行,这是通用的的编译器。另外,采用了 100之内的随机数做的题目
1.判断四则运算字符串的括号是否匹配,可以用以个栈来计算,开始栈是空,从头遍历所有字符,找到'(',压栈,找到')'弹出一个元素,遍历完成以后如果栈是空表示正常,非空,或则中间出现问题都表示括号不正常。2.分解四则运算中的所有元素,把所有分解的元素放在队列中,遍历整个字符串,有switch分支,如果是0-9的字符用一个字符串存储并和前面的字符组合起来,如果是'+''-''*''/'就把前面存储数字的字符串当成一个元素放进队列,并把当前运算法符当成一个元素也放进队列,遍历完成以后所有的数据和运算符都分开了,比如234+456,会分解成234,+,456三个元素。3.然后分解队列,用两个栈,一个存放所有元素用A表示,一个存放运算符号用B表示(这个是临时变量),从头开始遍历整个队列,如果是数据就把数据压到A栈。如果元素是运算符就分开处理,如果是'(''+''-''*''/'')'处理方法都不一样,详细说明太复杂,整体的思想就是把数据和运算符的顺序排列好(上面的元素排列以后变成234,456,+;可以参考后序排列)。4.计算:依次弹出A中的元素,如果是数据把数据压在栈C中,如果是运算符,从C中弹出该运算符需要的数据,然后把计算结果再次压进栈C,当A遍历完以后,C顶元素就是计算结果。整个大题思路:1.检查,2.分解,3.中序排列,4.后序排列,5.计算
80 浏览 4 回答
140 浏览 5 回答
183 浏览 5 回答
96 浏览 3 回答
334 浏览 5 回答
249 浏览 6 回答
235 浏览 6 回答
241 浏览 3 回答
313 浏览 3 回答
253 浏览 5 回答
294 浏览 9 回答
103 浏览 2 回答
125 浏览 3 回答
223 浏览 7 回答
177 浏览 7 回答