您的位置:首页 > 其它

单臂路由的设置注意事项

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: