Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<ctype.h>
- #define BUFFER_SIZE 1024
- #define NUMBER_SIZE 32
- #define PATH_SIZE 1024
- struct _Cvor;
- typedef struct _Cvor *Pos;
- typedef struct _Cvor
- {
- int El;
- Pos Next;
- }_CVOR;
- int Push(Pos, int);
- int Pop(Pos, int *);
- int Calc_Posfix(char *, int *);
- int Calculate(int, int,char, int *);
- int ReadFromFile(char *, char *, int);
- int main(int argc, char *argv[])
- {
- int error_code = 0;
- int result = 0;
- char ime_dat[PATH_SIZE]={0};
- printf(" %c", 201);
- for(int i = 0; i< 39; i++)
- printf("%c", 205);
- printf("%c", 187);
- printf("\n %c\t***\t Vjezba 7\t ***\t %c", 186,186);
- printf("\n %c\t*** Racunanje posfix izraza ***\t %c\n",186,186);
- printf(" %c", 200);
- for(int i = 0; i< 39; i++)
- printf("%c", 205);
- printf("%c", 188);
- printf("\n Unesite ime datoteke s posfix izrazom:\n\t\t");
- scanf_s(" %s", ime_dat, PATH_SIZE);
- if(Calc_Posfix(ime_dat, &result) == 0)
- printf("\n\n\n\tRezultat je : %d\n\n", result);
- else
- printf("\n\n\nGreska u izrazu!\n\n");
- }
- int Calc_Posfix(char *ime_dat, int *result)
- {
- int error_code = 0;
- char *data = NULL;
- char *fdata = NULL;
- Pos head = NULL;
- data = (char*)malloc(sizeof(char) * BUFFER_SIZE);
- if(data == NULL)
- {
- printf("\nGRESKA!!!\nMemorija nije alocirana!");
- error_code = -2;
- }
- else
- {
- fdata = data;
- memset(fdata, '\0', BUFFER_SIZE);
- if(!ReadFromFile(ime_dat, data, BUFFER_SIZE))
- {
- head=(Pos)malloc(sizeof(_CVOR));
- if(head == NULL)
- {
- printf("\nGRESKA!!!\nMemorija nije alocirana!");
- error_code = -1;
- }
- else
- {
- head->Next = NULL;
- char *buff;
- buff = (char*)malloc(sizeof(char) * NUMBER_SIZE);
- if(buff == NULL)
- {
- printf("\nGRESKA!!!\nMemorija nije alocirana!");
- error_code = -2;
- }
- else
- {
- memset(buff, '\0', NUMBER_SIZE);
- sscanf_s(fdata, " %s", buff, NUMBER_SIZE);
- do
- {
- if(isdigit(*buff))
- {//operand - Push
- char operand[NUMBER_SIZE] = {0};
- strcpy_s(operand,NUMBER_SIZE, buff);
- error_code = Push(head, atoi(operand));
- if(error_code != 0)
- break;
- }
- else
- {//operacija - Pop x2 AND Calculate AND Push
- int x = 0;
- int y = 0;
- int r = 0;
- error_code = Pop(head,&y);
- if(error_code != 0)
- break;
- error_code = Pop(head,&x);
- if(error_code != 0)
- break;
- error_code = Calculate(x,y,*buff,&r);
- if(error_code != 0)
- break;
- error_code = Push(head, r);
- if(error_code != 0)
- break;
- }
- memset(buff, '\0', NUMBER_SIZE);
- if(strlen(fdata) > 0)
- {
- fdata = strchr(fdata+1, ' ');
- if(fdata != NULL)
- sscanf_s(fdata, "%s", buff, NUMBER_SIZE);
- }
- }while(strlen(buff) > 0);
- }
- free(buff);
- }
- }
- }
- printf("\n\n\t\tPosfix izraz :\n");
- printf("\t%s", data);
- free(data);
- if(error_code == 0)
- *result = head->Next->El;
- else
- *result = 0;
- free(head->Next);
- free(head);
- return error_code;
- }
- int Push(Pos P, int x)
- {
- int error_code = 0;
- Pos q;
- q=(Pos)malloc(sizeof(_CVOR));
- if(q == 0)
- {
- printf("\nGRESKA!!!\nMemorija nije alocirana!");
- error_code = -1;
- }
- else
- {
- q->El = x;
- q->Next = P->Next;
- P->Next = q;
- }
- return error_code;
- }
- int Pop(Pos P, int *x)
- {
- int error_code = 0;
- Pos tmp;
- if(P->Next == NULL)
- {
- printf("\nGRESKA!!!\nStog je prazan!");
- error_code = -1;
- }
- else
- {
- tmp = P->Next;
- P->Next = tmp->Next;
- *x = tmp->El;
- free(tmp);
- }
- return error_code;
- }
- int Calculate(int x, int y,char op, int *r)
- {
- int error_code = 0;
- switch(op)
- {
- case '+':
- *r = x+y;
- break;
- case '-':
- *r = x-y;
- break;
- case '*':
- *r = x*y;
- break;
- case '/':
- if(y == 0)
- {
- printf("\nGRESKA!!!\nDijeljenje s nulom!\n");
- error_code = -2;
- }
- else
- *r = x/y;
- break;
- default:
- printf("\nGRESKA!!!\nOperacija nije definirana!\n");
- error_code = -1;
- }
- return error_code;
- }
- int ReadFromFile(char *ime_dat, char *Data, int Len)
- {
- int error_code = 0;
- FILE *fp;
- fopen_s(&fp, ime_dat, "r");
- if(fp == NULL)
- {
- printf("\nGRESKA!!!\nDatoteka %s nije otvorena!\n", ime_dat);
- error_code = -1;
- }
- else
- {
- if(0 >= fread(Data, sizeof(char), Len, fp))
- error_code = -2;
- fclose(fp);
- }
- return error_code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement