Advertisement
Guest User

Untitled

a guest
May 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <mem.h>
  6.  
  7. #define STACK_MAX_SIZE 1000
  8. char *str;
  9. int code = 0;
  10. int DEFAULT[127];
  11. int PRIORITY[127];
  12. int state = 0;
  13. typedef int(*t_func)(char**pos);
  14. typedef struct Stack_E{
  15.     double data[STACK_MAX_SIZE];
  16.     size_t size;
  17. }t_stack_E;
  18.  
  19. typedef struct Stack_T{
  20.     char data[STACK_MAX_SIZE];
  21.     size_t size;
  22. }t_stack_T;
  23.  
  24. t_stack_E E;
  25. t_stack_T T;
  26.  
  27. int isEmpty(t_stack_E *stack){
  28.     if(stack->size <= 0){
  29.         return -1;
  30.     } else return 0;
  31. }
  32.  
  33. int push_E(double value){
  34.     if(E.size >= STACK_MAX_SIZE){
  35.         return -1;
  36.     }
  37.     E.data[E.size] = value;
  38.     E.size++;
  39.     return 0;
  40. }
  41.  
  42. int push_T(char value){
  43.     if(T.size >= STACK_MAX_SIZE){
  44.         return -1;
  45.     }
  46.     T.data[T.size] = value;
  47.     T.size++;
  48.     return 0;
  49. }
  50.  
  51. double pop_E(){
  52.     if(E.size <= 0){
  53.         return -1;
  54.     }
  55.     E.size--;
  56.     return E.data[E.size];
  57. }
  58.  
  59. char pop_T(){
  60.     if(T.size <= 0){
  61.         return -1;
  62.     }
  63.     T.size--;
  64.     return T.data[T.size];
  65. }
  66.  
  67. double peek_E(){
  68.     if(E.size <= 0){
  69.         return -1;
  70.     }
  71.     return E.data[E.size - 1];
  72. }
  73.  
  74. char peek_T(){
  75.     if(T.size <= 0){
  76.         return -1;
  77.     }
  78.     return T.data[T.size-1];
  79. }
  80.  
  81.  
  82. int RS(char**ptr){
  83.     while(isspace(**ptr) && (**ptr != '\0'))
  84.         ++(*ptr);
  85.     return 0;
  86. }
  87.  
  88. int RN(char**ptr){
  89.     double d = strtod(*ptr,ptr);
  90.     push_E(d);
  91.     return 0;
  92. }
  93.  
  94. int RU(char**ptr){
  95.     char c = **ptr;
  96.     ++(*ptr);
  97.     if(c == '-'){
  98.         c = '!';
  99.         push_T(c);
  100.     }else{
  101.         push_T(c);
  102.     }
  103.     return 0;
  104. }
  105.  
  106. int ERR(char**ptr){
  107.     return -1;
  108. }
  109.  
  110. void show(){
  111.     int s = T.size;
  112.     printf("\nELEM OF T: ");
  113.     while (T.size != 0) {
  114.         printf("%c ", peek_T());
  115.         --T.size;
  116.     }
  117.     T.size = s;
  118.     s = E.size;
  119.     printf("\nELEM OF E: ");
  120.     while (E.size != 0) {
  121.         printf("%f ", peek_E());
  122.         --E.size;
  123.     }
  124.     E.size = s;
  125. }
  126.  
  127. int operation(char ch){
  128.     double a, b;
  129.     switch(ch) {
  130.         case '+':
  131.             if (E.size > 1){
  132.                 a = pop_E();
  133.                 b = pop_E();
  134.                 a += b;
  135.                 push_E(a);
  136.                 break;
  137.             }else return -1;
  138.         case '-':
  139.             if (E.size > 1){
  140.                 a = pop_E();
  141.                 b = pop_E();
  142.                 a = b - a;
  143.                 push_E(a);
  144.                 break;
  145.             } else return -1;
  146.         case '!':
  147.             if(E.size > 0){
  148.                 a = pop_E();
  149.                 a *= -1;
  150.                 push_E(a);
  151.                 break;
  152.             }else return -1;
  153.         case '*':
  154.             if (E.size > 1) {
  155.                 a = pop_E();
  156.                 b = pop_E();
  157.                 a *= b;
  158.                 push_E(a);
  159.                 break;
  160.             } else return -1;
  161.         case '/':
  162.             if (E.size > 1){
  163.                 a = pop_E();
  164.                 if(a != 0){
  165.                     b = pop_E();
  166.                     a = b / a;
  167.                     push_E(a);
  168.                     break;
  169.                 }else{
  170.                     code = 1;
  171.                     return -1;
  172.                 }
  173.             } else return -1;
  174.         case '(':
  175.             if (code == 0)
  176.                 code = 2;
  177.             break;
  178.     }
  179. }
  180.  
  181. int RB(char**ptr) {
  182.     char c = **ptr;
  183.     if (c != '(') {
  184.         ++(*ptr);
  185.         if(c == ')') {
  186.             while( T.size > 0 && (peek_T()!='(')&&(PRIORITY[peek_T()] >= PRIORITY[c])) {
  187.                 operation(pop_T());
  188.             }
  189.             if(peek_T() == '('){
  190.                 pop_T();
  191.             }
  192.             else{
  193.                 return 2;
  194.             }
  195.         } else if(peek_T()!='(') {
  196.             while((PRIORITY[peek_T()] >= PRIORITY[c] && T.size > 0 && peek_T() != '(')) {
  197.                 operation(pop_T());
  198.             }
  199.         }
  200.     }
  201.     if(PRIORITY[c] >= 0) {
  202.         push_T(c);
  203.     }
  204.     return 0;
  205. }
  206. const static t_func FUNC[3][8]={
  207.     {&RS,&RN,&RU,&ERR,&RU,&ERR,&ERR,&ERR},
  208.     {&RS,&ERR,&RB,&RB,&ERR,&RB,&RB,&ERR},
  209.     {&RS,&RN,&ERR,&ERR,&RU,&ERR,&ERR,&ERR}
  210. };
  211.  
  212. const static int STATE[3][8]={
  213.         {0,1,2,4,0,4,4,4},
  214.         {1,4,2,2,4,1,-1,4},
  215.         {2,1,4,4,0,4,4,4}
  216. };
  217.  
  218.  
  219.  
  220. void initialize_pr(int *arr){
  221.     arr['!'] = 3;
  222.     arr['-'] = 1;
  223.     arr['+'] = 1;
  224.     arr['/'] = 2;
  225.     arr['*'] = 2;
  226.     arr['('] = 4;
  227.     arr[')'] = -4;
  228.     arr['\n'] = -1;
  229.     arr['\0'] = -1;
  230. }
  231.  
  232. void initialize(int *arr){
  233.     memset(arr,255,255* sizeof(int));
  234.     arr[32] = 0;
  235.     for (int i = 48; i < 58; ++i) {
  236.         arr[i] = 1;
  237.     }
  238.     arr[45] = 2;
  239.     arr[42] = 3;
  240.     arr[43] = 3;
  241.     arr[47] = 3;
  242.     arr[40] = 4;
  243.     arr[41] = 5;
  244.     arr['\0'] = 6;
  245.     arr['\n'] = 6;
  246. }
  247.  
  248. int function(char**ptr, double* x){
  249.     initialize(DEFAULT);
  250.     initialize_pr(PRIORITY);
  251.     int oldstate = 0;
  252.     while (state != -1){
  253.         if (DEFAULT[**ptr]==-1){
  254.             code = 4;
  255.             break;
  256.         }
  257.         else {
  258.             state = STATE[oldstate][DEFAULT[**ptr]];
  259.             if (state == 4) {
  260.                 code = 3;
  261.                 break;
  262.             } else {
  263.                 int tt = FUNC[oldstate][DEFAULT[**ptr]](ptr);
  264.                 oldstate = state;
  265.                 if (tt) {
  266.                     code = tt;
  267.                     break;
  268.                 }
  269.             }
  270.         }
  271.     }
  272.     if (!code && T.size != 0){
  273.         code = RB(ptr);
  274.     }
  275.     if (T.size) {
  276.         code = 2;
  277.     }
  278.     *x = peek_E();
  279.     printf("\n%f", peek_E());
  280.     return code;
  281. }
  282.  
  283. int main() {
  284.     double x;
  285.     FILE *f = fopen("input.txt", "r");
  286.     unsigned int N = 10, delta = 10, i = 0;
  287.     char *str = (char *) malloc(sizeof(char) * N);
  288.     while ((str[i] = fgetc(f)) != EOF) {
  289.         if (++i >= N) {
  290.             N += delta;
  291.             str = (char *) realloc(str, sizeof(char) * N);
  292.         }
  293.     }
  294.     fclose(f);
  295.     str[i] = '\0';
  296.     printf("\n%d",function(&str,&x));
  297.     free(str);
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement