Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct node
- {
- int value;
- struct node *next;
- } Node;
- typedef struct stack
- {
- Node *head;
- Node *tail;
- } Stack;
- Node *makeNode(int value)
- {
- Node *p = (Node *)malloc(sizeof(Node));
- p->value = value;
- p->next = NULL;
- return p;
- }
- void printfnode(Node *p)
- {
- if (p == NULL)
- return;
- printf("%d\n", p->value);
- }
- int topStack(Stack *s)
- {
- if (s->tail == NULL)
- return -1;
- return s->tail->value;
- }
- Stack *makeStack()
- {
- Stack *s;
- s = (Stack *)malloc(sizeof(Stack));
- s->head = NULL;
- s->tail = NULL;
- return s;
- }
- void popStack(Stack *s)
- {
- Node *iterator;
- if (s->tail == NULL)
- {
- printf("ERROR STACK");
- return;
- }
- if (s->head == s->tail)
- {
- s->head = NULL;
- s->tail = NULL;
- return;
- }
- iterator = s->head;
- while (iterator->next != s->tail)
- {
- iterator = iterator->next;
- }
- iterator->next = NULL;
- s->tail = iterator;
- return;
- }
- void pushStack(Stack *s, int value)
- {
- Node *newNode = makeNode(value);
- if (s->head == NULL)
- {
- s->head = newNode;
- s->tail = newNode;
- return;
- }
- s->tail->next = newNode;
- s->tail = newNode;
- return;
- }
- int n;
- int main()
- {
- Node node;
- int i, j;
- int total = 0;
- int cho = 0;
- int array1[1000];
- int array2[1000];
- Stack *s = makeStack();
- FILE *f = fopen("expreval.txt", "r");
- if (f == NULL)
- printf("FILE ERROR\n");
- else
- {
- fscanf(f, "%d", &n);
- for (i = 1; i <= n; i++)
- {
- fscanf(f, "%d", &array1[i]);
- }
- for (j = 1; j < n; j++)
- {
- fscanf(f, "%d", &array2[j]);
- }
- }
- for (i = 1; i <= n; i++)
- for (j = 1; j < n; j++)
- {
- pushStack(s, array1[i]);
- if(array2[j] == 2)
- {
- cho = topStack(s);
- popStack(s);
- cho = cho*array1[i];
- pushStack(s, cho);
- }
- }
- for(j = n-1; j >= 1; j--)
- {
- if(array2[j] == 0)
- total = total + topStack(s);
- if(array2[j] == 1)
- total = total - topStack(s);
- if(array2[j] == 2)
- total = total + topStack(s);
- }
- printf("%d\n", total);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment