Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- #define STACK struct stack
- #define STACK_M struct stack_m
- const char *filenameout = "out.txt";
- const char *filenamein = "in.txt";
- void writefile(const char* filename, char* content) // записывает в файл filename content
- {
- FILE *fout;
- fout = fopen(filename, "a");
- if (fout == NULL)
- {
- printf("file %s not opened",filenameout );
- return ;
- }
- if (content == "")
- fprintf(fout,"%s",content);
- else
- {
- fprintf(fout,"%s\n",content);
- fclose(fout);
- }
- }
- int Checksintax(char* code) // проверка на синтаксические ошибки
- {
- if (code == NULL)
- return - 21;
- int flag1 = 0, i = 0, k = 0, j1 = 0, j2 = 0, len = strlen(code);
- for(i=0;i<len;i++)
- {
- if ((code[i]>='0')&&(code[i]<='9'))
- j1++;
- if ((code[i] == '+')||(code[i] == '-')||(code[i] == '*')||(code[i] == '/'))
- {
- if(i == len-1)
- return -8;
- j2++;
- }
- if (code[i]=='(')
- {
- k++;
- if (code[i+1] == ')')
- return -5;
- }
- if (code[i]==')')
- {
- k--;
- if (code[i+1] == '(')
- return -9;
- }
- if (code[i]=='.')
- return -1;
- if (code[i]==' ')
- return -13;
- if ((code[i]>=65)&&(code[i]<=90))
- {
- flag1++;
- return -3;
- }
- if ((code[i]>=97)&&(code[i]<=122))
- {
- flag1++;
- return -4;
- }
- }
- if(flag1 > 0)
- return -20;
- if (j1 < j2+1)
- if (j2 != 0)
- return -65;
- if (k != 0)
- return -2;
- return 0;
- }
- void writefileint(const char* filename, int content) // вывод результата
- {
- FILE *fout;
- fout = fopen(filename, "w");
- if (fout == NULL)
- {
- printf("file %s not opened",filename);
- return ;
- }
- fprintf(fout," %d",content);
- fclose(fout);
- }
- STACK_M // лист содержащий указатель на голову стека
- {
- STACK* head;
- };
- STACK // стэк
- {
- char data;
- STACK* next;
- };
- void push(STACK_M* A, char *b) // новые эелменты стека
- {
- STACK* B;
- B = (STACK*)malloc(sizeof(STACK*));
- B->data = *b;
- B->next = A->head;
- A->head = B;
- }
- int tabl(char a) //приоритеты знаков
- {
- int b = -2;
- if (a == '(')
- b = 0;
- if (a == ')')
- b = 1;
- if ((a == '+')||(a == '-'))
- b = 2;
- if ((a == '*')||(a == '/'))
- b = 3;
- if (a == '^')
- b = 4;
- return b;
- }
- void pop(STACK_M* A, char *b) // достаем значение из головы стека
- {
- STACK* B;
- B = A->head;
- *b = A->head->data;
- if (A->head->next)
- A->head = A->head->next;
- else
- A->head = NULL;
- free(B);
- }
- void show(STACK_M* A) // выводит стек ( для отладки)
- {
- STACK* cur;
- cur = A->head;
- int i = 0;
- while(cur)
- {
- printf("\n data[%d] = %c",i,cur->data);
- cur = cur->next;
- i++;
- }
- printf("\n");
- }
- char* charcat(char* code, char data) //аналог "strcat" только к строке добавляется 1 символ и пробел
- {
- int len = strlen(code);
- code[len]=data;
- code[len+1] = ' ';
- code[len+2] = '\0';
- return code;
- }
- int tablstack(STACK_M*A) // приоритет знакак в стеке
- {
- STACK* cur;
- cur = A->head;
- char a;
- a = cur -> data;
- int tab;
- tab = tabl(a);
- return tab;
- }
- int calc(char* code, int N,int* flag) // калькулятор
- {
- char* tmp;
- tmp = (char*)malloc(N*sizeof(char*));
- tmp[0] = '\0';
- int count = 0;
- int* stak;
- stak = (int*)malloc(N*sizeof(int*));
- int i = 0;
- int count11 = 0;
- int len = strlen(code);
- for (;;)
- {
- if ((code[i]>='0')&&(code[i]<='9'))
- {
- int g = 0;
- while (code[i]!=' ')
- {
- tmp[g] = code[i];
- tmp[g+1]='\0';
- g++;
- i++;
- }
- int celo = atoi(tmp);
- stak[count11] = celo;
- tmp[0]='\0';
- count11++;
- }
- else
- {
- if (code[i]=='+')
- {
- stak[count11-2]=stak[count11-1]+stak[count11-2];
- count11--;
- }
- if (code[i]=='-')
- {
- stak[count11-2]=stak[count11-2]-stak[count11-1];
- count11--;
- }
- if (code[i]=='*')
- {
- stak[count11-2]=stak[count11-1]*stak[count11-2];
- count11--;
- }
- if (code[i]=='/')
- {
- if (stak[count11-1] == 0)
- {
- writefile(filenameout,"division by zero");
- *flag = 0;
- return -1;
- }
- stak[count11-2]=stak[count11-2]/stak[count11-1];
- count11--;
- }
- i++;
- }
- if (i == len-1 )
- break;
- }
- return stak[0];
- }
- int main()
- {
- STACK_M* A;
- A = (STACK_M*)malloc(sizeof(STACK_M*));
- STACK* D;
- D = (STACK*)malloc(sizeof(STACK*));
- D = NULL;
- A->head = D;
- char* ex;
- ex = (char*)malloc(1000*sizeof(char*));
- ex = strcpy(ex,"");
- FILE *fin;
- fin = fopen(filenamein, "r");
- if (fin == NULL)
- {
- printf("file not opened");
- return -1;
- }
- char *code;
- code = (char*)malloc(2560*sizeof(char));
- if(code == NULL)
- return -2;
- fscanf(fin,"%[zxcvbnmasdfghjklqwertyuiopQWERTYUIOPLKJHGFDSAZXCVBNM1234567890+-*/ ().]",code);
- printf("cpde = %s\n",code);
- fclose(fin);
- int Check1;
- Check1 = Checksintax(code);
- printf("ch = %d",Check1);
- if (Check1 < 0)
- {
- writefile(filenameout,"syntax error");
- return 0;
- }
- int len = strlen(code);
- int i;
- for(i=0;i<len;i++) // перевод в обратную польскую запись
- {
- int tab = tabl(code[i]);
- if ((code[i]>='0')&&(code[i]<='9'))
- {
- while((code[i]>='0')&&(code[i]<='9'))
- {
- ex = charcat(ex,code[i]);
- int len1 = strlen(ex);
- ex[len1-1] = '\0';
- i++;
- }
- int len1 = strlen(ex);
- ex[len1] = ' ';
- ex[len1+1] = '\0';
- i--;
- }
- if (code[i] == '(')
- push(A,&code[i]);
- if (code[i] == ')')
- {
- char b=0;
- while (b != '(')
- {
- if (A->head->data != '(')
- {
- pop(A,&b);
- ex = charcat(ex,b);
- }
- else
- pop(A,&b);
- }
- }
- if (tab > 1)
- {
- int tab2;
- if((A->head) != NULL)
- {
- tab2 = tabl(A->head->data);
- while (tab2>=tab)
- {
- char a;
- pop(A,&a);
- ex = charcat(ex,a);
- if ((A->head) == NULL)
- break;
- tab2 = tabl(A->head->data);
- }
- }
- push(A,&code[i]);
- }
- if (i == len-1)
- {
- while((A->head) != NULL)
- {
- char a;
- pop(A,&a);
- ex = charcat(ex,a);
- }
- break;
- }
- }
- printf("\n ex = %s",ex);
- int res;
- int flag = 1;
- res = calc(ex,strlen(ex),&flag);
- printf("\n flag = %d",flag);
- printf("\n res = %d",res);
- if (flag == 1)
- writefileint(filenameout,res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement