Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "mystack.h"
- void polska(char ar1[], char ar2[]);
- main()
- {
- char infix[SIZE], postfix[SIZE];
- FILE *ifp, *ofp;
- int i = 0;
- ifp = fopen("input.txt", "r");
- while ( !feof(ifp) ) {
- fscanf(ifp, "%c", &infix[++i]);
- }
- fclose(ifp);
- infix[0] = i;
- polska(infix, postfix);
- ofp = fopen("output.txt", "w");
- for (i = 1; i <= postfix[0]; i++) {
- fprintf(ofp, "%c ", postfix[i]);
- }
- fprintf(ofp, "\n");
- fclose(ofp);
- return 0;
- }
- int prior(char sign)
- {
- switch(sign) {
- case '*':case '/': return 2;
- break;
- case '+':case '-': return 1;
- break;
- }
- return 0;
- }
- void polska(char inf[], char post[])
- {
- Stack S;
- int i, j = 0;
- char curr_char, smbl;
- init(&S);
- for (i = 1; i <= inf[0]; i++) {
- switch ( curr_char = inf[i] ) {
- case '0':case '1':case '2':case '3':case '4':case '5':
- case '6':case '7':case '8':case '9':
- post[++j] = curr_char;
- break;
- case '+':case '-':case '*':case '/':
- if ((empty(&S) == 1) || (prior(curr_char) > prior(showtop(&S))))
- push(&S, curr_char);
- else {
- while (prior(curr_char) <= prior(showtop(&S)))
- post[++j] = pop(&S);
- push(&S, curr_char);
- }
- break;
- case '(': push(&S, curr_char);
- break;
- case ')': while ( (smbl = pop(&S)) != '(' ) {
- post[++j] = smbl;
- }
- break;
- default: break;
- }
- }
- while ( empty(&S) != 1)
- post[++j] = pop(&S);
- post[0] = j;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment