Advertisement
agul

20121218

Dec 17th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
  179.  
  180. int calculator(char* s)
  181. {
  182.     int j;
  183.     unsigned int len = strlen(s);
  184.    
  185.     head->data=-1;
  186.     head->next->data=-1;
  187.    
  188.     for (unsigned int i=0; i<len; i++) {
  189.         if (s[i]=='+') head->data=(int)head->data+(int)head->next->data;
  190.         else 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]<='9' && s[i]>='0') {
  195.             j=i--;
  196.             if (s[j]==' ') {
  197.                 if (head->data!=(-1))
  198.                     head->next->data=(int)s[i];
  199.                 else head->data=(int)s[i];
  200.             } else if (head->next->data!=-1)
  201.                 head->data=head->data*10+(int)s[i];
  202.             else
  203.                 head->next->data=head->next->data*10+(int)s[i];
  204.         }
  205.     }
  206.     return head->data;
  207. }
  208.  
  209. bool Empty(void)
  210. {
  211.     if (n == 0)
  212.         return true;
  213.     else
  214.         return false;
  215. }
  216.  
  217. void Push(char d)
  218. {
  219.     list_t *ins;
  220.  
  221.     ins = (list_t*)malloc(sizeof(list_t));
  222.     ins->data = d;
  223.     ins->prev = NULL;
  224.  
  225.     if (Empty()) {
  226.         ins->next = NULL;
  227.  
  228.     } else {
  229.         ins->next = head;
  230.  
  231.     }
  232.     ++n; head = ins;
  233. }
  234.  
  235. void Pop()
  236. {
  237.     char d;
  238.  
  239.     if (head == NULL) return;
  240.  
  241.     if (n == 1) {
  242.         d = head->data;
  243.         free(head);
  244.         head = NULL;
  245.         if (d != '(' && d != ')')
  246.             printf("%c ", d);
  247.     } else {
  248.         d = head->data;
  249.         head = head->next;
  250.         free(head->prev);
  251.         head->prev = NULL;
  252.        
  253.         if (d != '(' && d != ')')
  254.             printf("%c ", d);
  255.     }
  256.     --n;
  257. }
  258.  
  259. void Init(void)
  260. {
  261.     head = NULL;
  262.     n = 0;
  263. }
  264.  
  265. void Show(void)
  266. {
  267.     list_t *p;
  268.     if (Empty()) {
  269.         fprintf(stderr, "Steck is empty!\n");
  270.         return;
  271.     }
  272.     for (p = head; p != NULL; p = p->next)
  273.         printf("%c\t", p->data);
  274.     printf("\n");
  275. }
  276.  
  277. void Find (char a, char b)
  278. {
  279.     list_t *p;
  280.     p = head;
  281.     while (p!=NULL)
  282.     {
  283.         if (p->data==a || p->data==b)
  284.             printf("%c", p->data);
  285.         p=p->next;
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement