Advertisement
agul

20121218

Dec 17th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. #define SIZE 100
  9.  
  10. char* POST_IN_INFIC(char*);
  11. int calculator(char*);
  12. void Infic_in_Post(char* s);
  13.  
  14. struct list_t {
  15.     char data;
  16.     list_t *next;
  17.     list_t *prev;
  18. };
  19.  
  20. list_t *head, *tail;
  21. int n;
  22.  
  23. void Push(char);
  24. void Pop(void);
  25. void Del(void);
  26. void Init(void);
  27. void Show(void);
  28. bool Empty(void);
  29. void Find(char,char);
  30.  
  31.  
  32. enum CHOICE {   // Выбор пользователя
  33.     QUIT = 0,
  34.     INFICINPOST,
  35.     CALCULATOR,
  36.     OTHER
  37. };
  38.  
  39. CHOICE DoMenu(void);
  40.  
  41. int main()
  42. {
  43.     freopen("err.txt", "w", stderr);
  44.     head=NULL;
  45.     char* s;
  46.     s=(char*)malloc(100*sizeof(char));
  47.     int choice = -1;
  48.  
  49.     while (choice != QUIT) {
  50.         choice = DoMenu();
  51.  
  52.         switch(choice) {
  53.         case INFICINPOST:
  54.             Init();
  55.             scanf("%s", s);
  56.             Infic_in_Post(s);
  57.             break;
  58.         case CALCULATOR:
  59.             scanf("%s", s);
  60.             calculator(s);
  61.         default:
  62.             break;
  63.         }
  64.     }
  65.     return 0;
  66. }
  67.  
  68. CHOICE DoMenu(void)
  69. {
  70.     CHOICE choice;
  71.  
  72.     printf( "\nMenu\n"
  73.             "\t%d quit\n"
  74.             "\t%d infic->post\n"
  75.             "\t%d calculator\n"
  76.             "Enter new line: ",
  77.             QUIT, INFICINPOST, CALCULATOR);
  78.  
  79.     scanf("%d", (int *)&choice);
  80.  
  81.     switch (choice) {
  82.     case QUIT:
  83.     case INFICINPOST:
  84.     case CALCULATOR:
  85.         return choice;
  86.     default:
  87.         return OTHER;
  88.     }
  89.    
  90.     return OTHER;
  91. }
  92.  
  93.  
  94. int PriorityComp(char a, char b)
  95. {
  96.     switch (a) {
  97.     case '(': case ')':
  98.         a = 0;
  99.         break;
  100.     case '+': case '-':
  101.         a = 1;
  102.         break;
  103.     case '*': case '/':
  104.         a = 2;
  105.         break;
  106.     default:
  107.         a = 3;
  108.         break;
  109.     }
  110.    
  111.     switch (b) {
  112.     case '(': case ')':
  113.         b = 0;
  114.         break;
  115.     case '+': case '-':
  116.         b = 1;
  117.         break;
  118.     case '*': case '/':
  119.         b = 2;
  120.         break;
  121.     default:
  122.         b = 3;
  123.         break;
  124.     }
  125.    
  126.     return a - b;
  127. }
  128.  
  129. void Infic_in_Post(char* s)
  130. {   list_t *p;
  131.     int j;
  132.     unsigned int len = strlen(s);
  133.     Init();
  134.    
  135.     for (unsigned int i = 0; i < len; i++) {
  136.         std::cerr << i << ") " << s[i] << std::endl;
  137.    
  138.         if (s[i] >= '0' && s[i] <= '9') {
  139.             printf("%c", s[i]);
  140.             if (i == len - 1 || (i < len - 1 && (s[i + 1] < '0' || s[i + 1] > '9'))) printf(" ");
  141.         }
  142.         else if (s[i] == '(') {
  143.             Push(s[i]);
  144.         } else if (s[i] == ')') {
  145.             while (!Empty() && head->data != '(')
  146.                 Pop();
  147.             Pop();
  148.         } else if (s[i]=='^') {
  149.             Push(s[i]); j = i+1;
  150.             while (j < len && s[j]<='9' && s[j]>='0') {
  151.                 printf("%c", s[j]); j++;}
  152.             i = j - 1;
  153.             Pop();
  154.         }
  155.         else if (s[i]==' ')
  156.             printf(" ");
  157.         else {
  158.             if (Empty()) Push(s[i]);
  159.             else {
  160.                 while (head!=NULL)
  161.                     if (PriorityComp(head->data, s[i]) >= 0) Pop(); else break;
  162.                 Push(s[i]);
  163.             }
  164.         }
  165.                
  166.        
  167.         /*else if (s[i]=='+' || s[i]=='-') {
  168.             Find('*','/');
  169.             Find ('+', '-');
  170.             Push (s[i]);
  171.         } else if (s[i]=='*' || s[i]=='/') {
  172.             Find ('*', '/');
  173.             Push (s[i]);
  174.         } */
  175.        
  176.     }
  177.     while (!Empty()) Pop();
  178.     Del();
  179. }
  180.  
  181. int calculator(char* s)
  182. {
  183.     int j;
  184.     unsigned int len = strlen(s);
  185.    
  186.     head->data=-1;
  187.     head->next->data=-1;
  188.    
  189.     for (unsigned int i=0; i<len; i++) {
  190.         if (s[i]=='+') head->data=(int)head->data+(int)head->next->data;
  191.         else if (s[i]=='-') head->data=(int)head->data-(int)head->next->data;
  192.         else if (s[i]=='*') head->data=(int)head->data*(int)head->next->data;
  193.         else if (s[i]=='/') head->data=(int)head->data/(int)head->next->data;
  194.         else if (s[i]=='^') head->data=(int)head->data^(int)head->next->data;
  195.         else if (s[i]<='9' && s[i]>='0') {
  196.             j=i--;
  197.             if (s[j]==' ') {
  198.                 if (head->data!=(-1))
  199.                     head->next->data=(int)s[i];
  200.                 else head->data=(int)s[i];
  201.             } else if (head->next->data!=-1)
  202.                 head->data=head->data*10+(int)s[i];
  203.             else
  204.                 head->next->data=head->next->data*10+(int)s[i];
  205.         }
  206.     }
  207.     return head->data;
  208. }
  209.  
  210. bool Empty(void)
  211. {
  212.     if (n == 0)
  213.         return true;
  214.     else
  215.         return false;
  216. }
  217.  
  218. void Push(char d)
  219. {
  220.     list_t *ins;
  221.  
  222.     ins = (list_t*)malloc(sizeof(list_t));
  223.     ins->data = d;
  224.     ins->prev = NULL;
  225.  
  226.     if (Empty()) {
  227.         ins->next = NULL;
  228.  
  229.     } else {
  230.         ins->next = head;
  231.  
  232.     }
  233.     ++n; head = ins;
  234. }
  235.  
  236. void Pop()
  237. {
  238.     char d;
  239.    
  240.     if (head == NULL)
  241.         return;
  242.    
  243.     if (n == 1) {
  244.         d = head->data;
  245.         //free(head);
  246.         head = NULL;
  247.        
  248.         if (d != '(' && d != ')')
  249.             printf("%c ", d);
  250.     } else {
  251.         d = head->data;
  252.         head = head->next;
  253.         //free(head->prev);
  254.         head->prev = NULL;
  255.        
  256.         if (d != '(' && d != ')')
  257.             printf("%c ", d);
  258.     }
  259.     --n;
  260. }
  261.  
  262. void Init(void)
  263. {
  264.     head = NULL;
  265.     n = 0;
  266. }
  267.  
  268. void Del(void)
  269. {
  270.     return;
  271.     list_t *p;
  272.     if (n == 1)
  273.         printf("%c", head->data);
  274.         free(head);
  275.     for (p = head->next; p != NULL; p = p->next)
  276.         {printf("%c", p->prev->data);
  277.         free(p->prev);}
  278.  
  279.     head = NULL;
  280.     n = 0;
  281. }
  282.  
  283. void Show(void)
  284. {
  285.     list_t *p;
  286.     if (Empty()) {
  287.         fprintf(stderr, "Steck is empty!\n");
  288.         return;
  289.     }
  290.     for (p = head; p != NULL; p = p->next)
  291.         printf("%c\t", p->data);
  292.     printf("\n");
  293. }
  294.  
  295. void Find (char a, char b)
  296. {
  297.     list_t *p;
  298.     p = head;
  299.     while (p!=NULL)
  300.     {
  301.         if (p->data==a || p->data==b)
  302.             printf("%c", p->data);
  303.         p=p->next;
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement