dzungchaos

CTDL&TT: Chuyển + Tính hậu tố

May 31st, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. #define MAX 200
  7. struct Stack{
  8.     float DATA[MAX];
  9.     int TOP;
  10. };
  11.  
  12. void init(Stack *S){
  13.     S->TOP = -1;
  14. }
  15.  
  16. int isEmpty(Stack *S){
  17.     if (S->TOP == -1)
  18.         return 1;
  19.     return 0;
  20. }
  21.  
  22. void Push(struct Stack* S, float item){
  23.     if (S->TOP == (MAX - 1))
  24.     {
  25.         printf("\nStack is full");
  26.     }
  27.     else
  28.     {
  29.         ++S->TOP;
  30.         S->DATA[S->TOP] = item;
  31.     }
  32. }
  33.  
  34. int top(Stack *S){
  35.     return (S->DATA[S->TOP]);
  36. }
  37.  
  38. float Pop(struct Stack* S){
  39.     float ret = -1;
  40.     if (S->TOP == -1)
  41.         printf("\nStack is empty");
  42.     else{
  43.         ret = S->DATA[S->TOP];
  44.         --S->TOP;
  45.     }
  46.     return ret;
  47. }
  48.  
  49. int Precedence(char x){
  50.     if (x == '(')
  51.         return 0;
  52.     if (x == '+' || x == '-')
  53.         return 1 ;
  54.     if (x == '*' || x == '/' || x == '%')
  55.         return 2;
  56.     return 3;
  57. }
  58.  
  59.  
  60. void InfixtoPostfix(char infix[], char postfix[]){
  61.     Stack S;
  62.     char x, token;
  63.     int i=0, j=0;    //i-index of infix,j-index of postfix
  64.     init(&S);
  65.     for (i = 0; infix[i] != '\0'; i++)
  66.     {
  67.         token = infix[i];
  68.         if (isalnum(token))
  69.             postfix[j++] = token;
  70.         else
  71.             if (token == '(')
  72.                 Push(&S, '(');
  73.             else
  74.                 if (token == ')')
  75.                     while ((x = Pop(&S)) != '(')
  76.                         postfix[j++] = x;
  77.                 else
  78.                 {
  79.                     while (Precedence(token) <= Precedence(top(&S)) && !isEmpty(&S))
  80.                     {
  81.                         x = Pop(&S);
  82.                         postfix[j++] = x;
  83.                     }
  84.                     Push(&S, token);
  85.                 }
  86.     }
  87.  
  88.     while (!isEmpty(&S))
  89.     {
  90.         x = Pop(&S);
  91.         postfix[j++] = x;
  92.     }
  93.  
  94.     postfix[j] = '\0';
  95. }
  96. float Evaluate(char *Postfix)
  97. {
  98.     struct Stack S;
  99.     char *p;
  100.     float op1, op2, result;
  101.     S.TOP = -1;
  102.     p = &Postfix[0];
  103.     while (*p != '\0')
  104.     {
  105.         while (*p == ' ' || *p == '\t')
  106.         {
  107.             p++;
  108.         }
  109.         if (isdigit(*p))
  110.         {
  111.             int num = 0;
  112.             while (isdigit(*p))
  113.             {
  114.                 num = num * 10 + *p - 48;
  115.                 *p++;
  116.             }
  117.             Push(&S, num);
  118.         }
  119.         else
  120.         {
  121.             op1 = Pop(&S);
  122.             op2 = Pop(&S);
  123.             switch (*p)
  124.             {
  125.             case '+':
  126.                 result = op2 + op1;
  127.                 break;
  128.             case '-':
  129.                 result = op2 - op1;
  130.                 break;
  131.             case '/':
  132.                 result = op2 / op1;
  133.                 break;
  134.             case '*':
  135.                 result = op2 * op1;
  136.                 break;
  137.             default:
  138.                 printf("\nInvalid Operator");
  139.                 return 0;
  140.             }
  141.             Push(&S, result);
  142.         }
  143.         p++;
  144.     }
  145.     result = Pop(&S);
  146.     return result;
  147. }
  148.  
  149. int main()
  150. {
  151.     char A[MAX], B[MAX];
  152.     printf("Infix : ");
  153.     gets(A);
  154.     InfixtoPostfix(A, B);
  155.  
  156.     printf("Postfix: %s\n", B);
  157.     printf("Equals is %f\n", Evaluate(&B[0]));
  158.     gets(A);
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment