Chinaunix首页 | 论坛 | 博客
  • 博客访问: 156308
  • 博文数量: 36
  • 博客积分: 802
  • 博客等级: 准尉
  • 技术积分: 717
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-02 22:47
文章分类
文章存档

2012年(36)

分类: C/C++

2012-08-27 15:17:49

在看堆栈的时候,看到堆栈应用中有一个中缀式转换后缀式,于是自己写了一个。

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. #define TRUE 1
  4. #define FALSE 0

  5. typedef struct Node
  6. {
  7.     char data;
  8.     struct Node *next;
  9. }LinkStackNode,*LinkStack;

  10. //初始化
  11. void InitStack(LinkStack *S)
  12. {
  13.     *S=(LinkStack)malloc(sizeof(LinkStackNode));
  14.     (*S)->next=NULL;
  15. }
  16. //判断是否为空
  17. int Isempty(LinkStack S)
  18. {
  19.     if(S->next==NULL)
  20.         return TRUE;
  21.     else
  22.         return FALSE;
  23. }
  24. //进栈操作
  25. int Push(LinkStack S,char c)
  26. {
  27.     LinkStackNode *temp;
  28.     temp=(LinkStackNode *)malloc(sizeof(LinkStackNode));
  29.     if(temp)
  30.     {
  31.         temp->data=c;
  32.         temp->next=S->next;
  33.         S->next=temp;
  34.         return TRUE;
  35.     }
  36.     else
  37.         return FALSE;
  38. }

  39. //出栈操作

  40. int Pop(LinkStack S,char *c)
  41. {
  42.     LinkStackNode *temp;
  43.     temp=S->next;
  44.     if(temp)
  45.     {
  46.         *c=temp->data;
  47.          S->next=temp->next;
  48.         free(temp);
  49.         return TRUE;
  50.     }
  51.     else
  52.         return FALSE;
  53. }

  54. //取得栈顶指针
  55. int Gettop(LinkStack S,char *c)
  56. {
  57.     LinkStackNode *temp;
  58.     temp=S->next;
  59.     if(temp)
  60.     {
  61.         *c=temp->data;
  62.         return *c;
  63.     }
  64.     else
  65.         return FALSE;
  66. }

  67. void Print(LinkStack S)
  68. {
  69.     LinkStackNode *temp;
  70.     temp=S->next;
  71.     while(temp)
  72.     {
  73.         printf("%c",temp->data);
  74.         temp=temp->next;
  75.     }
  76.     printf("\n");
  77. }
  78. int Match(char a,char b)
  79. {
  80.     if(a=='('&&b==')')
  81.         return TRUE;
  82.     else if(a=='['&&b==']')
  83.         return TRUE;
  84.     else if(a=='{'&&b=='}')
  85.         return TRUE;
  86.     else
  87.         return FALSE;
  88. }
  89. void BreakMatch(char *str)
  90. {
  91.     LinkStack S;
  92.     int i;
  93.     char ch;
  94.     InitStack(&S);
  95.     for(i=0;str[i]!='\0';i++)
  96.     {
  97.         switch(str[i])
  98.         {
  99.             case '(':
  100.             case '[':
  101.             case '{':
  102.                 Push(S,str[i]);
  103.                 break;
  104.             case ')':
  105.             case ']':
  106.             case '}':
  107.                 if(Isempty(S))
  108.                 {
  109.                     printf("\n右括号多余!\n");
  110.                     return;
  111.                 }
  112.                 else
  113.                 {
  114.                     Gettop(S,&ch);
  115.                     if(Match(ch,str[i]))
  116.                         Pop(S,&ch);
  117.                     else
  118.                     {
  119.                         printf("\n对应的左右括号不是同类!\n");
  120.                         return;
  121.                     }
  122.                 }
  123.                 
  124.         }
  125.     }
  126.     if(Isempty(S))
  127.         printf("\n括号匹配!\n");
  128.     else
  129.         printf("\n左括号多余!\n");

  130. }
  131. void Mid_end(LinkStack S,int n)
  132. {
  133.     char ch,sh;
  134.     char s[30];
  135.     int i=0;
  136.     LinkStack L;
  137.     InitStack(&L);
  138.     while(S||L)
  139.     {
  140.         if(S)
  141.             Gettop(S,&ch);
  142.         switch(ch)
  143.         {
  144.             case '+':
  145.             case '-':
  146.             case '*':
  147.             case '/':
  148.             case '(':
  149.                 Push(L,ch);
  150.                 Pop(S,&ch);
  151.                 break;
  152.             case ')':
  153.                 Pop(S,&ch);
  154.                 while(L->next!=NULL)
  155.                 {
  156.                     Gettop(L,&ch);
  157.                     if(ch=='(')
  158.                     {
  159.                         Pop(L,&ch);
  160.                         while(S->next==NULL&&L->next!=NULL)
  161.                         {
  162.                             Gettop(L,&ch);
  163.                             printf("%c",ch);
  164.                             Pop(L,&ch);
  165.                         }
  166.                         
  167.                         break;
  168.                     }
  169.                     printf("%c",ch);
  170.                     Pop(L,&ch);
  171.                 }
  172.                 break;
  173.             default:
  174.                 Gettop(S,&ch);
  175.                 printf("%c",ch);
  176.                 Pop(S,&ch);
  177.                 while(S->next==NULL&&L->next!=NULL)
  178.                 {
  179.                     Gettop(L,&ch);
  180.                     printf("%c",ch);
  181.                     Pop(L,&ch);
  182.                 }
  183.                 break;
  184.         }
  185.         if(S->next==NULL&&L->next==NULL)
  186.             break;
  187.     }
  188.     printf("\n");
  189. }
  190. int main()
  191. {
  192.     LinkStack S;
  193.     char ch;
  194.     int i;
  195.     int n;
  196.     InitStack(&S);
  197.     printf("please input the string you want:");
  198.     for(i=0;i<30;i++)
  199.     {
  200.         ch=getchar();
  201.         if(ch=='#')
  202.             break;
  203.         Push(S,ch);
  204.         i++;
  205.     }
  206.     n=i;
  207.     Mid_end(S,n);
  208. //    BreakMatch(str);
  209. }

问题是:由于堆栈的先进后出,所以在输入的时候我都是反着输入的
eg:A+(B-C/D)*E#
我在输入的时候都是E*)D/C-B(+A#来输入的
——————————————————————————————————
对于一个堆栈 输入ABC,输出的所有选项有
A进A出,B进B出,C进C出(顺序是ABC)
A进B进,B出C进,C出A出(顺序是BCA)
A进B进,C进,C出B出A出(顺序是CBA)
A进A出,B进C进,C出B出(顺序是ACB)
A进B进,B出A出,C进C出(顺序是BAC)
就是不可能为CAB
阅读(1512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~