Advertisement
LipAnn

Untitled

Feb 21st, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct node
  6. {
  7.     char el;
  8.     struct node *left, *right;
  9. } node;
  10.  
  11. node* create_node(char c)
  12. {
  13.     node *my = malloc(sizeof(*my));
  14.     my->el = c;
  15.     my->left = NULL;
  16.     my->right = NULL;
  17.     return my;
  18. }
  19.  
  20. bool is_digit(char c)
  21. {
  22.     return (c >= '0' && c <= '9');
  23. }
  24.  
  25. void error(int code)
  26. {
  27.     if (code == 0)
  28.         printf("UNMATCHED BRACKETS\n");
  29.     if (code == 1)
  30.         printf("INCORRECT EXPRESSION\n");
  31.     if (code == 2)
  32.         printf("INCORRECT SYMBOL\n");
  33.     exit(0);
  34. }
  35.  
  36. char op;
  37.  
  38. node* expression(void);
  39. node* summand(void);
  40. node* multi(void);
  41.  
  42. node* build_tree(void)
  43. {
  44.     scanf("%c", &op);
  45.     node *root = expression();
  46.     if (op == '(' || op == ')')
  47.         error(0);
  48.     if (op == '*' || op == '+' || op == '-' || op == 'x' || op == 'X' || is_digit(op))
  49.         error(2);
  50.     if (op != '.')
  51.         error(1);
  52.     return root;
  53. }
  54.  
  55. void print_tree(node *cur)
  56. {
  57.     if (cur == NULL)
  58.         return;
  59.     print_tree(cur->left);
  60.     printf("%c", cur->el);
  61.     print_tree(cur->right);
  62. }
  63.  
  64. int calc_expression(node *cur, int x)
  65. {
  66.     if (cur == NULL)
  67.         return 0;
  68.     if (cur->left == NULL && cur->right == NULL)
  69.     {
  70.         if (cur->el == 'x' || cur->el == 'X')
  71.             return x;
  72.         return cur->el - '0';
  73.     }
  74.     int op1 = calc_expression(cur->left, x);
  75.     int op2 = calc_expression(cur->right, x);
  76.     if (cur->el == '+')
  77.         return op1 + op2;
  78.     if (cur->el == '-')
  79.         return op1 - op2;
  80.     return op1 * op2; //if cur->el == '*'
  81. }
  82.  
  83. node* expression(void)
  84. {
  85.     node* left = summand();
  86.     while (op == '+' || op == '-')
  87.     {
  88.         node *cur = create_node(op);
  89.         scanf("%c", &op);
  90.         node *right = summand();
  91.         cur->left = left;
  92.         cur->right = right;
  93.         left = cur;
  94.     }
  95.     return left;
  96. }
  97.  
  98. node* summand(void)
  99. {
  100.     node* left = multi();
  101.     while (op == '*')
  102.     {
  103.         node *cur = create_node(op);
  104.         scanf("%c", &op);
  105.         node *right = multi();
  106.         cur->left = left;
  107.         cur->right = right;
  108.         left = cur;
  109.     }
  110.     return left;
  111. }
  112.  
  113. node* multi(void)
  114. {
  115.     if (is_digit(op) || op == 'x' || op == 'X')
  116.     {
  117.         node *cur = create_node(op);
  118.         scanf("%c", &op);
  119.         return cur;
  120.     }
  121.     if (op == '(')
  122.     {
  123.         scanf("%c", &op);
  124.         node *cur = expression();
  125.         if (op != ')')
  126.             error(0);
  127.         scanf("%c", &op);
  128.         return cur;
  129.     }
  130.     if (op == ')')
  131.         error(0); //unmatched brackets
  132.     if (op == '.' || op == '+' || op == '-' || op == '*')
  133.         error(1); //incorrect expresion
  134.     error(2); //incorrect symbol in expression
  135.     return NULL;
  136. }
  137.  
  138. int main(void)
  139. {
  140.     printf("ENTER ARITHMETIC EXPRESSION WITH VARIABLE X\n");
  141.     printf("EXPRESSION MUST CONSIST ONLY OF '*', '+', '-', digits, '(', ')' AND VARIABLES 'x' (or 'X'):\n");
  142.     node *root = build_tree();
  143.     printf("ENTER THE VALUE OF VARIABLE 'x' ('X'):\n");
  144.     int x;
  145.     scanf("%d", &x);
  146.     //print_tree(root);
  147.     int ans = calc_expression(root, x);
  148.     printf("THE VALUE OF EXPRESSION AT POINT X IS %d", ans);
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement