Advertisement
nguyentruong98

Untitled

Dec 14th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. int main()
  2. {
  3.       Node node;
  4.       int i, j;
  5.       int total = 0;
  6.       int cho = 0;
  7.       int array1[1000];
  8.       int array2[1000];
  9.       Stack *s = makeStack();
  10.       FILE *f = fopen("expreval.txt", "r");
  11.       if (f == NULL)
  12.             printf("FILE ERROR\n");
  13.       else
  14.       {
  15.             fscanf(f, "%d", &n);
  16.             for (i = 1; i <= n; i++)
  17.             {
  18.                   fscanf(f, "%d", &array1[i]);
  19.             }
  20.             for (j = 1; j < n; j++)
  21.             {
  22.                   fscanf(f, "%d", &array2[j]);
  23.             }
  24.       }
  25.       for (i = 1; i <= n; i++)
  26.             for (j = 1; j < n; j++)
  27.             {
  28.                   pushStack(s, array1[i]);
  29.                   if (j == 5)
  30.                   {
  31.                         if (array2[j] == 2)
  32.                         {
  33.                               cho = topStack(s);
  34.                               popStack(s);
  35.                               cho = cho * array1[i];
  36.                               pushStack(s, cho);
  37.                         }
  38.                         pushStack(s, array1[i + 1]);
  39.                   }
  40.                   if (array2[j] == 2)
  41.                   {
  42.                         cho = topStack(s);
  43.                         popStack(s);
  44.                         cho = cho * array1[i];
  45.                         pushStack(s, cho);
  46.                   }
  47.             }
  48.       for (j = n - 1; j >= 1; j--)
  49.       {
  50.             if (array2[j] == 0)
  51.             {
  52.                   total = total - topStack(s);
  53.                   popStack(s);
  54.             }
  55.             if (array2[j] == 1)
  56.             {
  57.                   total = total + topStack(s);
  58.                   popStack(s);
  59.             }
  60.             if (array2[j] == 2)
  61.             {
  62.                   total = total + topStack(s);
  63.                   popStack(s);
  64.             }
  65.       }
  66.       printf("%d\n", total);
  67.       return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement