nguyentruong98

Untitled

Dec 14th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. typedef struct node
  5. {
  6.       int value;
  7.       struct node *next;
  8. } Node;
  9. typedef struct stack
  10. {
  11.       Node *head;
  12.       Node *tail;
  13. } Stack;
  14. Node *makeNode(int value)
  15. {
  16.       Node *p = (Node *)malloc(sizeof(Node));
  17.       p->value = value;
  18.       p->next = NULL;
  19.       return p;
  20. }
  21. void printfnode(Node *p)
  22. {
  23.       if (p == NULL)
  24.             return;
  25.       printf("%d\n", p->value);
  26. }
  27. int topStack(Stack *s)
  28. {
  29.       if (s->tail == NULL)
  30.             return -1;
  31.       return s->tail->value;
  32. }
  33. Stack *makeStack()
  34. {
  35.       Stack *s;
  36.       s = (Stack *)malloc(sizeof(Stack));
  37.       s->head = NULL;
  38.       s->tail = NULL;
  39.       return s;
  40. }
  41. void popStack(Stack *s)
  42. {
  43.       Node *iterator;
  44.       if (s->tail == NULL)
  45.       {
  46.             printf("ERROR STACK");
  47.             return;
  48.       }
  49.       if (s->head == s->tail)
  50.       {
  51.             s->head = NULL;
  52.             s->tail = NULL;
  53.             return;
  54.       }
  55.       iterator = s->head;
  56.       while (iterator->next != s->tail)
  57.       {
  58.             iterator = iterator->next;
  59.       }
  60.       iterator->next = NULL;
  61.       s->tail = iterator;
  62.       return;
  63. }
  64. void pushStack(Stack *s, int value)
  65. {
  66.       Node *newNode = makeNode(value);
  67.       if (s->head == NULL)
  68.       {
  69.             s->head = newNode;
  70.             s->tail = newNode;
  71.             return;
  72.       }
  73.       s->tail->next = newNode;
  74.       s->tail = newNode;
  75.       return;
  76. }
  77. int n;
  78. int main()
  79. {
  80.       Node node;
  81.       int i, j;
  82.       int total = 0;
  83.       int cho = 0;
  84.       int array1[1000];
  85.       int array2[1000];
  86.       Stack *s = makeStack();
  87.       FILE *f = fopen("expreval.txt", "r");
  88.       if (f == NULL)
  89.             printf("FILE ERROR\n");
  90.       else
  91.       {
  92.             fscanf(f, "%d", &n);
  93.             for (i = 1; i <= n; i++)
  94.             {
  95.                   fscanf(f, "%d", &array1[i]);
  96.             }
  97.             for (j = 1; j < n; j++)
  98.             {
  99.                   fscanf(f, "%d", &array2[j]);
  100.             }
  101.       }
  102.       for (i = 1; i <= n; i++)
  103.             for (j = 1; j < n; j++)
  104.             {
  105.                   pushStack(s, array1[i]);
  106.                  if(array2[j] == 2)
  107.                  {
  108.                        cho = topStack(s);
  109.                        popStack(s);
  110.                        cho = cho*array1[i];
  111.                        pushStack(s, cho);
  112.                  }
  113.             }
  114.       for(j = n-1; j >= 1; j--)
  115.       {
  116.             if(array2[j] == 0)
  117.             total = total + topStack(s);
  118.             if(array2[j] == 1)
  119.             total = total - topStack(s);
  120.             if(array2[j] == 2)
  121.             total = total + topStack(s);
  122.       }
  123.       printf("%d\n", total);
  124.       return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment