Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <mem.h>
- #define STACK_MAX_SIZE 1000
- char *str;
- int code = 0;
- int DEFAULT[127];
- int PRIORITY[127];
- int state = 0;
- typedef int(*t_func)(char**pos);
- typedef struct Stack_E{
- double data[STACK_MAX_SIZE];
- size_t size;
- }t_stack_E;
- typedef struct Stack_T{
- char data[STACK_MAX_SIZE];
- size_t size;
- }t_stack_T;
- t_stack_E E;
- t_stack_T T;
- int isEmpty(t_stack_E *stack){
- if(stack->size <= 0){
- return -1;
- } else return 0;
- }
- int push_E(double value){
- if(E.size >= STACK_MAX_SIZE){
- return -1;
- }
- E.data[E.size] = value;
- E.size++;
- return 0;
- }
- int push_T(char value){
- if(T.size >= STACK_MAX_SIZE){
- return -1;
- }
- T.data[T.size] = value;
- T.size++;
- return 0;
- }
- double pop_E(){
- if(E.size <= 0){
- return -1;
- }
- E.size--;
- return E.data[E.size];
- }
- char pop_T(){
- if(T.size <= 0){
- return -1;
- }
- T.size--;
- return T.data[T.size];
- }
- double peek_E(){
- if(E.size <= 0){
- return -1;
- }
- return E.data[E.size - 1];
- }
- char peek_T(){
- if(T.size <= 0){
- return -1;
- }
- return T.data[T.size-1];
- }
- int RS(char**ptr){
- while(isspace(**ptr) && (**ptr != '\0'))
- ++(*ptr);
- return 0;
- }
- int RN(char**ptr){
- double d = strtod(*ptr,ptr);
- push_E(d);
- return 0;
- }
- int RU(char**ptr){
- char c = **ptr;
- ++(*ptr);
- if(c == '-'){
- c = '!';
- push_T(c);
- }else{
- push_T(c);
- }
- return 0;
- }
- int ERR(char**ptr){
- return -1;
- }
- void show(){
- int s = T.size;
- printf("\nELEM OF T: ");
- while (T.size != 0) {
- printf("%c ", peek_T());
- --T.size;
- }
- T.size = s;
- s = E.size;
- printf("\nELEM OF E: ");
- while (E.size != 0) {
- printf("%f ", peek_E());
- --E.size;
- }
- E.size = s;
- }
- int operation(char ch){
- double a, b;
- switch(ch) {
- case '+':
- if (E.size > 1){
- a = pop_E();
- b = pop_E();
- a += b;
- push_E(a);
- break;
- }else return -1;
- case '-':
- if (E.size > 1){
- a = pop_E();
- b = pop_E();
- a = b - a;
- push_E(a);
- break;
- } else return -1;
- case '!':
- if(E.size > 0){
- a = pop_E();
- a *= -1;
- push_E(a);
- break;
- }else return -1;
- case '*':
- if (E.size > 1) {
- a = pop_E();
- b = pop_E();
- a *= b;
- push_E(a);
- break;
- } else return -1;
- case '/':
- if (E.size > 1){
- a = pop_E();
- if(a != 0){
- b = pop_E();
- a = b / a;
- push_E(a);
- break;
- }else{
- code = 1;
- return -1;
- }
- } else return -1;
- case '(':
- if (code == 0)
- code = 2;
- break;
- }
- }
- int RB(char**ptr) {
- char c = **ptr;
- if (c != '(') {
- ++(*ptr);
- if(c == ')') {
- while( T.size > 0 && (peek_T()!='(')&&(PRIORITY[peek_T()] >= PRIORITY[c])) {
- operation(pop_T());
- }
- if(peek_T() == '('){
- pop_T();
- }
- else{
- return 2;
- }
- } else if(peek_T()!='(') {
- while((PRIORITY[peek_T()] >= PRIORITY[c] && T.size > 0 && peek_T() != '(')) {
- operation(pop_T());
- }
- }
- }
- if(PRIORITY[c] >= 0) {
- push_T(c);
- }
- return 0;
- }
- const static t_func FUNC[3][8]={
- {&RS,&RN,&RU,&ERR,&RU,&ERR,&ERR,&ERR},
- {&RS,&ERR,&RB,&RB,&ERR,&RB,&RB,&ERR},
- {&RS,&RN,&ERR,&ERR,&RU,&ERR,&ERR,&ERR}
- };
- const static int STATE[3][8]={
- {0,1,2,4,0,4,4,4},
- {1,4,2,2,4,1,-1,4},
- {2,1,4,4,0,4,4,4}
- };
- void initialize_pr(int *arr){
- arr['!'] = 3;
- arr['-'] = 1;
- arr['+'] = 1;
- arr['/'] = 2;
- arr['*'] = 2;
- arr['('] = 4;
- arr[')'] = -4;
- arr['\n'] = -1;
- arr['\0'] = -1;
- }
- void initialize(int *arr){
- memset(arr,255,255* sizeof(int));
- arr[32] = 0;
- for (int i = 48; i < 58; ++i) {
- arr[i] = 1;
- }
- arr[45] = 2;
- arr[42] = 3;
- arr[43] = 3;
- arr[47] = 3;
- arr[40] = 4;
- arr[41] = 5;
- arr['\0'] = 6;
- arr['\n'] = 6;
- }
- int function(char**ptr, double* x){
- initialize(DEFAULT);
- initialize_pr(PRIORITY);
- int oldstate = 0;
- while (state != -1){
- if (DEFAULT[**ptr]==-1){
- code = 4;
- break;
- }
- else {
- state = STATE[oldstate][DEFAULT[**ptr]];
- if (state == 4) {
- code = 3;
- break;
- } else {
- int tt = FUNC[oldstate][DEFAULT[**ptr]](ptr);
- oldstate = state;
- if (tt) {
- code = tt;
- break;
- }
- }
- }
- }
- if (!code && T.size != 0){
- code = RB(ptr);
- }
- if (T.size) {
- code = 2;
- }
- *x = peek_E();
- printf("\n%f", peek_E());
- return code;
- }
- int main() {
- double x;
- FILE *f = fopen("input.txt", "r");
- unsigned int N = 10, delta = 10, i = 0;
- char *str = (char *) malloc(sizeof(char) * N);
- while ((str[i] = fgetc(f)) != EOF) {
- if (++i >= N) {
- N += delta;
- str = (char *) realloc(str, sizeof(char) * N);
- }
- }
- fclose(f);
- str[i] = '\0';
- printf("\n%d",function(&str,&x));
- free(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement