Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <malloc.h>
  7. #include <locale.h>
  8. #define pi 3.14159265358979323846
  9. #define STACK_OVERFLOW -100
  10. #define STACK_UNDERFLOW -101
  11. #define NMAX 100
  12. typedef long double Dig;
  13. typedef char Char;
  14. char str[100];
  15. char str1[100];
  16. bool exit1=false;
  17. bool exit2=false;
  18. bool exit6=false;
  19. int quantity=0;
  20. int prior=0;
  21. int stack_amount=0;
  22. typedef struct number
  23. {
  24.     Dig valueNum;
  25.     struct number *nextNum;
  26. } number_t;
  27.  
  28. typedef struct sign
  29. {
  30.     Char valueOper;
  31.     struct sign *nextOper;
  32. } oper_t;
  33.  
  34. void push_num(number_t **headNum, Dig value)
  35. {
  36.     number_t *tmp = (struct number*)malloc(sizeof(number_t));
  37.     if (tmp == NULL) {
  38.         exit(STACK_OVERFLOW);
  39.     }
  40.     tmp->nextNum = *headNum;
  41.     tmp->valueNum = value;
  42.     *headNum = tmp;
  43. }
  44.  
  45. void push_oper(oper_t **headOper, Char value)
  46. {
  47.     oper_t *tmp = (struct sign*)malloc(sizeof(oper_t));
  48.     if (tmp == NULL) {
  49.         exit(STACK_OVERFLOW);
  50.     }
  51.     stack_amount++;
  52.     tmp->nextOper = *headOper;
  53.     tmp->valueOper = value;
  54.     *headOper = tmp;
  55. }
  56.  
  57.  
  58. Dig pop_num(number_t **headNum)
  59. {
  60.     number_t *out;
  61.     Dig value;
  62.     if (*headNum == NULL)
  63.     {
  64.         exit(STACK_UNDERFLOW);
  65.     }
  66.     out = *headNum;
  67.     *headNum = (*headNum)->nextNum;
  68.     value = out->valueNum;
  69.     free(out);
  70.     return value;
  71. }
  72.  
  73. Char pop_oper(oper_t **headOper)
  74. {
  75.     oper_t *out;
  76.     Char value;
  77.     if (*headOper == NULL)
  78.     {
  79.         exit(STACK_UNDERFLOW);
  80.     }
  81.  
  82.     out = *headOper;
  83.     *headOper = (*headOper)->nextOper;
  84.     value = out->valueOper;
  85.     stack_amount--;
  86.     free(out);
  87.     return value;
  88. }
  89. void PrintStack(const oper_t* headOper)
  90. {
  91.     while (headOper)
  92.     {
  93.         printf("%c ", headOper->valueOper);
  94.         headOper = headOper->nextOper;
  95.     }
  96. }
  97.  
  98.  
  99. size_t GetSize_num(const number_t *headNum) {
  100.     size_t size = 0;
  101.     while (headNum) {
  102.         size++;
  103.         headNum = headNum->nextNum;
  104.     }
  105.     return size;
  106. }
  107.  
  108. size_t GetSize_oper(const oper_t *headOper) {
  109.     size_t size = 0;
  110.     while (headOper) {
  111.         size++;
  112.         headOper = headOper->nextOper;
  113.     }
  114.     return size;
  115. }
  116. void summa()
  117. {
  118.  
  119. }
  120. void raznost()
  121. {
  122.  
  123. }
  124. void mult()
  125. {
  126.  
  127. }
  128. void del()
  129. {
  130.  
  131. }
  132. void factorial()
  133. {
  134.  
  135. }
  136. void power()
  137. {
  138.  
  139. }
  140. void sinus()
  141. {
  142.  
  143. }
  144. void cosinus()
  145. {
  146.  
  147. }
  148. void tang()
  149. {
  150.  
  151. }
  152. void ctang()
  153. {
  154.  
  155. }
  156. void arcsin()
  157. {
  158.  
  159. }
  160. void arccosinus()
  161. {
  162.  
  163. }
  164. void arctang()
  165. {
  166.  
  167. }
  168. void arcctang()
  169. {
  170.  
  171. }
  172. int priority(char c)
  173. {
  174.     if (c=='(')
  175.     {
  176.         //printf("^");
  177.         prior= 1;
  178.     }
  179.     if (c=='+' || c=='-')
  180.     {
  181.         //printf("+-");
  182.         prior= 2;
  183.     }
  184.     if (c=='*' || c=='/')
  185.     {
  186.         //printf("*");
  187.         prior= 3;
  188.     }
  189.     if (c=='^' || c== 's' ||)
  190.     {
  191.         //printf("^");
  192.         prior= 4;
  193.     }
  194.     return prior;
  195. }
  196. void treatment(struct number **headNum, struct sign **headOper)
  197. {
  198.     int elem=0;
  199.     int skobki=0;
  200.    
  201.     //push_oper(headOper, '*');
  202.     int exit4=0;
  203.     int number_1=0;
  204.     char in, out, out1;
  205.     int j=0;
  206.     int i=0;
  207.     for (int i=0; i<quantity-1; i++)
  208.     {
  209.         if (str[i]=='!')
  210.         {
  211.             str1[j]=str[i];
  212.             number_1=0;
  213.         }
  214.         if (str[i]=='.')
  215.         {
  216.             str1[j]=str[i];
  217.         }
  218.         if (str[i]=='0' || str[i]=='1' || str[i]=='2' || str[i]=='3' || str[i]=='4' || str[i]=='5' || str[i]=='6' || str[i]=='7' ||str[i]=='8' || str[i]=='9')
  219.         {
  220.             if (number_1==0)
  221.             {
  222.                 str1[j]='|';
  223.                 j++;
  224.             }
  225.             str1[j]=str[i];
  226.             number_1=1;
  227.         }
  228.         if (str[i]=='*' || str[i]=='/' || str[i]=='+' || str[i]=='-' || str[i]=='^')
  229.         {
  230.             if (stack_amount!=0)
  231.             {
  232.                 out=pop_oper(headOper);
  233.                 //int x=priority(out);
  234.                 //printf("%d", x);
  235.                 out1=out;
  236.                 while (priority(out)>=priority(str[i]))
  237.                 {
  238.                     str1[j]=out;
  239.                     j++;
  240.                     exit4++;
  241.                    // stack_amount--;
  242.                     //pop_oper(headOper);
  243.                     /*if (stack_amount==0)
  244.                     {
  245.                         break;
  246.                     }*/
  247.                     //printf("(priority(out)=%d   ", priority(out));
  248.                   //  printf("(priority(str[i])=%d \n", priority(str[i]));
  249.                     if (stack_amount>0)
  250.                     {
  251.                      out=pop_oper(headOper);
  252.                      if (out=='(')
  253.                      {
  254.                          push_oper(headOper, out);
  255.                      }
  256.                      //stack_amount--;
  257.                     }
  258.                     else
  259.                     {
  260.                         break;
  261.                     }
  262.                 }
  263.                 if (exit4==0)
  264.                 {
  265.                     push_oper(headOper, out1);
  266.                     //stack_amount++;
  267.                 }
  268.             }
  269.             push_oper(headOper, str[i]);
  270.            // stack_amount++;
  271.             number_1=0;
  272.             exit4=0;
  273.             j--;
  274.         }
  275.         if (str[i]=='(')
  276.         {
  277.             push_oper(headOper, str[i]);
  278.            // stack_amount++;
  279.             j--;
  280.             number_1=0;
  281.         }
  282.         if (str[i]==')')
  283.         {
  284.             while(!exit6)
  285.             {
  286.                 out=pop_oper(headOper);
  287.                 if (out=='(')
  288.                 {
  289.                    // stack_amount--;
  290.                     exit6=true;
  291.                     break;
  292.                 }
  293.                 str1[j]=out;
  294.                // stack_amount--;
  295.                 j++;
  296.                
  297.             }
  298.             j--;
  299.             number_1=0;
  300.         }
  301.         if (str[i]=='s' && str[i+1]=='i' && str[i+2]=='n')
  302.         {
  303.            
  304.         }
  305.        
  306.         printf("%d\n", stack_amount);
  307.         PrintStack(*headOper);
  308.         printf("\n");
  309.  
  310.         j++;
  311.         exit6=false;
  312.     }
  313.     //j--;
  314.      printf("stack amount=%d\n", stack_amount);
  315.     while (stack_amount!=0)
  316.     {
  317.         out=pop_oper(headOper);
  318.         str1[j]=out;
  319.         j++;
  320.         //stack_amount--;
  321.     }
  322.     for (int i=0; i<j+2; i++)
  323.     {
  324.         printf("%c", str1[i]);
  325.     }
  326.     printf("\n");
  327.     printf("%d\n", stack_amount);
  328.     //PrintStack(*headOper);
  329.     //push_oper(headOper, str[coord]);
  330.     /*for(int i=0; i<quantity; i++)
  331.     {
  332.         printf("%c", exp[i]);
  333.     }*/
  334. }
  335. int main()
  336. {
  337.     struct number *headNum = NULL;
  338.     struct sign *headOper = NULL;
  339.     setlocale(LC_ALL, "Rus");
  340.     printf("Введите выражение: \n");
  341.     int i=0;
  342.     while (!exit1)
  343.     {
  344.         scanf("%c", &str[i]);
  345.         if (str[i]=='=')
  346.         {
  347.             exit1=true;
  348.         }
  349.         i++;
  350.     }
  351.     quantity=i;
  352.     printf("\n");
  353.     for(int i=0; i<quantity; i++)
  354.     {
  355.         str1[i]=0;
  356.     }
  357.     treatment(&headNum, &headOper);
  358.     /*for (int i=0; i<quantity; i++)
  359.     {
  360.         printf("%c", str[i]);
  361.     }*/
  362.     //printf("lol");
  363.     _getch();
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement