单臂路由的设置注意事项
2009-09-25 09:46
477 查看
#include<stdio.h> #include<string.h> #include<ctype.h> #define MAXSIZE 40 /*******************************************************************************/ typedef char ElemType; typedef struct Stack{//定义栈结构 ElemType data[MAXSIZE]; int top; }Stack,*pStack; void initStack(pStack pS){//初始化栈 pS->top = -1; } bool push(pStack pS,ElemType e){//入栈 if(pS->top == MAXSIZE - 1){ puts("栈满,无法入栈"); return false; } pS->data[++(pS->top)] = e; return true; } bool pop(pStack pS,ElemType *e){//出栈 if(pS->top == -1){ puts("栈空,无法出栈"); return false; } *e = pS->data[(pS->top)--]; return true; } bool getTop(pStack pS,ElemType *e){//获得栈顶元素 if(pS->top == -1){ puts("栈空,无法取得栈顶元素"); return false; } *e = pS->data[pS->top]; return true; } void printStack(pStack pS){//打印栈 int index = pS->top; while(index != -1){ printf("%c",pS->data[index--]); } puts(""); } /*******************************************************************************/ void handleOperator(pStack pS,char chInput,char *systemOutput,int *indexOutput); int getPrior(char ch); void converToLastInfix(char *userInput,char *systemOutput); /*******************************************************************************/ int main(void){ char userInput[MAXSIZE];//存储用户输入 char systemOutput[MAXSIZE];//存储后缀表达式 int index; puts("Enter a string:"); gets(userInput); converToLastInfix(userInput,systemOutput);//中缀转换为后缀 for(index=0;index<strlen(systemOutput);index++){ printf("%c",systemOutput[index]); } getchar(); return 0; } /*******************************************************************************/ void converToLastInfix(char *userInput,char *systemOutput){//中缀转换为后缀 Stack S1;//符号栈 pStack pS1 = &S1; initStack(pS1); char ch;//存储用户输入一个字符 int index;//遍历用户输入用 int indexOutput = 0;//用于存储系统输出 bool tag;//标志位,用于 for(index=0;index<strlen(userInput);index++){ ch = userInput[index]; if(isdigit(ch)){ tag = true; systemOutput[indexOutput++] = ch; continue; } else{ if(tag){ systemOutput[indexOutput++] = '#'; tag = false; } if(strchr("+-*/()",ch)){ handleOperator(pS1,ch,systemOutput,&indexOutput); } else{ continue; } } } while(pS1->top != -1){ if(tag){ systemOutput[indexOutput++] = '#'; tag = false; } char chPop; pop(pS1,&chPop); systemOutput[indexOutput++] = chPop; systemOutput[indexOutput++] = '#'; } systemOutput[indexOutput++] = '\0'; } /*******************************************************************************/ void handleOperator(pStack pS,char chInput,char *systemOutput,int *indexOutput){//处理运算符 ElemType chTop;//存储栈顶元素 ElemType chPop;//存储出栈元素 int priorInput;//输入元素的优先级 int priorTop;//栈顶元素的优先级 if(pS->top == -1){//栈空的情况直接入栈并退出函数 push(pS,chInput); return; } if(chInput == '('){//如果是左括号直接入栈并退出函数 push(pS,chInput); return; } if(chInput == ')'){//如果是右括号则一直出栈输出知道碰到'(' while(pop(pS,&chPop)){//取出栈顶元素 if(chPop == '('){ break;//退出 } else{ systemOutput[(*indexOutput)++] = chPop; //输出 systemOutput[(*indexOutput)++] = '#'; continue;//继续 } } return ; } while(true){ getTop(pS,&chTop);//取得栈顶元素 priorInput = getPrior(chInput);//取得输入元素的优先级 priorTop = getPrior(chTop);//取得栈顶元素的优先级 if(priorInput>priorTop){//如果输入元素的优先级高于栈顶元素 push(pS,chInput);//入栈 break;//退出循环 } else{ pop(pS,&chPop); systemOutput[(*indexOutput)++] = chPop; systemOutput[(*indexOutput)++] = '#'; if(pS->top == -1){//栈空的情况直接入栈并退出循环 push(pS,chInput); break; } } } } /*******************************************************************************/ int getPrior(char ch){//获得优先级 switch(ch){ case '+': case '-': return 1; case '*': case '/': return 2; case '(': return 0; } } /*******************************************************************************/本文出自 “狂战” 博客,请务必保留此出处http://fanaticssk.blog.51cto.com/4148464/835860
相关文章推荐
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- 设置Symantc内部LiveUpdate服务器注意事项
- 神经网络参数设置注意事项
- 在tableView中,使用xib自定义view设置tableHeaderView时注意事项
- cocos2d-x 2.0.3 设置高清模式注意事项(已移除-hd方式)
- 【Linux】OpenWRT的无线设置注意事项——从2.4G到5G,hwmode不简单
- 允许某个应用发送通知设置及java字符串分割注意事项
- 控件属性设置注意事项
- redis集群鉴权密码密码设置及哨兵设置注意事项
- ST17H26上下拉电阻设置注意事项
- ABAP设置输入焦点和表格控件行数的注意事项
- win 2003设置注意事项
- tableview中cell设置的注意事项
- CSS中关于不同分辨率下的样式设置注意事项
- Qt中设置Widget背景颜色及图片的注意事项
- 在设置UIView及子视图的frame属性的注意事项
- 关于设置了setMaxAge(0)而浏览器未成功删除Cookie的注意事项
- 设置全局键盘钩子 和 注意事项
- 设置Java类路径的注意事项
- Adblock Plus在IE浏览器中设置方法以及IE10安装需要注意的事项