//================================================================================= int CPU_VALID (cpu_t* cpu) { if (cpu == NULL) return CPU_NOT_INITIALIZED; if (cpu->max_counter < 0) return INCORRECT_MAX_COUNTER; if (cpu->PM == NULL) return NULL_PM_POINTER; if (cpu->prgm_counter < 0) return INCORRECT_PROGRAM_COUNTER; int check_stack = stack_ok(&cpu->Stack); if (check_stack != OK && check_stack != FULL && check_stack != EMPTY) return STACK_ERROR; check_stack = stack_ok(&cpu->Address_stack); if (check_stack != OK && check_stack != FULL && check_stack != EMPTY) return STACK_ERROR; return CPU_OK; }
Stack* stack_ctor(void){ Stack* st = (Stack* ) calloc(1,sizeof(Stack)); assert(!stack_ok(st)); st->szStack = SIZE_OF_STACK; st->top = 0; st->data = (int* )calloc(SIZE_OF_STACK, sizeof(int)); assert(!stack_ok(st)); return st; }
int mul(stackt *st)//a*b { if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; double a=st->value[--st->Count]; double b=st->value[--st->Count]; st->value[st->Count++]=a*b; if(st->value[st->Count-1]==-0.0)st->value[st->Count-1]=0.0; if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; return 0; }
int stack_push(stackt *st,double value) { if (!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; st->value=(double*)realloc(st->value,sizeof(double)*(st->Count+1)); assert(st->value); st->value[st->Count++]=value; if (!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; return 0; }
int sqrt(stackt *st)//a+b { if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; double a=st->value[--st->Count]; st->value[st->Count++]=sqrt(a); if(st->value[st->Count-1]==-0.0)st->value[st->Count-1]=0.0; if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; return 0; }
int stack_print(stackt *st) { if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; for(int i=st->Count-1; i>=0; i--)printf("COUNT=%d VALUE=%lg\n",i,st->value[i]); if (st->Count<0)printf("STACK ISNT CREATED!\n"); if (st->Count==0)printf("STACK IS EMPTY!\n"); }
int stack_dtor(stackt *st) { if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; st->Count=-2; free(st->value); return 0; }
//================================================================================= int CPU_default (cpu_t* cpu) { if (cpu == NULL) return CPU_NOT_INITIALIZED; cpu->prgm_counter = 0; for (int i = 0; i < MAX_NUM_OF_REGISTERS; i++) cpu->reg[i] = 0; while (stack_ok(&cpu->Address_stack) != EMPTY) pop(&cpu->Address_stack, NULL); while (stack_ok(&cpu->Stack) != EMPTY) pop(&cpu->Stack, NULL); return CPU_OK; }
int pop(Stack* st){ if(is_full(st)) return -1; else{ assert(!stack_ok(st)); return st->data[--st->top]; } }
int stack_ctor(stackt *st) { if(!st)return NULL_ARGUMENT; st->Count=0; st->value=(double*) calloc (1,sizeof(double)); assert(st->value); if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; return 0; }
int push (struct stek* stek_push, char x) { if (stek_push->counter >= stek_push->max_size - sizeof(double)) { char* P = (char*) realloc((void*) stek_push->data, (stek_push->max_size+=100)*sizeof(*stek_push->data)); if (P!=NULL) stek_push->data = P; else { printf("Memory is over, you can read the data.\n"); abort(); return 1; } } if (!stack_ok(stek_push)) { stack_dump(stek_push); printf("INVALID STACK\n"); abort(); } if (!stack_ok(stek_push)) { stack_dump(stek_push); printf("INVALID STACK\n"); abort(); } stek_push->data[stek_push->counter++] = x; if (!stack_ok(stek_push)) { stack_dump(stek_push); printf("INVALID STACK\n"); abort(); } return 0; }
int push(Stack* st, int value){ if(is_empty(st)) return -1; else{ st->data[st->top++] = value; assert(!stack_ok(st)); return 0; } }
int div(stackt *st)//a*b { if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; double a = st->value[--st->Count]; double b = st->value[--st->Count]; if (a != 0) { st->value[st->Count++] = b / a; if (st->value[st->Count] == -0.0)st->value[st->Count] = 0.0; if(!st)return NULL_ARGUMENT; if (stack_ok(st)==1) return 1; return 0; } return 1; }
void stack_dump(stack* st) { if (stack_ok(st)) { int i = st -> data; (st -> data) --; for (; (st -> data) >= 0 ; (st -> data)--) printf("a[%d] = %d\n", st -> data , st -> name[st -> data]); st -> data = i; } else printf("You will not work with incorrect stack\n"); }
void stack_dtor(stack* st) { int i = 0; if(stack_ok(st)) for(i = 0; i < st -> data; i++) st -> name[i] = 0xBAD; else exit(0); free(st -> name); st -> name = NULL; st -> size = 0; st -> data = -1; }
void stack_dump(stack* st) { if (stack_ok(st)) { int i = st -> data; for (st -> data -- ; st -> data >= 0 ; (st -> data)--) //decrementing data twice? or what is the first argument of cycle? printf("a[%d] = %d\n", st -> data , st -> name[st -> data]); st -> data = i; } else printf("You don`t work with incorrect stack\n"); }
double stack_pop(stackt *st) { if(!st)return FAIL_POP; if (stack_ok(st)==1) return 1; if (st->Count>0) return st->value[--st->Count]; else { return FAIL_POP; } }
int push(stack* st, elem_t a) { if (stack_ok(st)) { if (st -> data < st -> size) // is is always true stack_resize(st); // you always resize??? st -> name[st -> data] = a; st -> data++ ; return 0; } else { printf("You don`t work with incorrect stack\n"); return 0; } }
int push(stack* st, elem_t a) { if (stack_ok(st)) { if (st -> data == st -> size) stack_resize(st); st -> name[st -> data] = a; st -> data++ ; return 0; } else { printf("You will not work with incorrect stack\n"); return 0; } }
int stack_dump(FILE* stream, stack* user_stack) { fprintf(stream, "\nI'm stack\n"); if (!(user_stack)) return ERR_NULL; fprintf(stream,"stack[%08x] %s\n" "Maxsize = %d\n" "Size = %d\n", user_stack, stack_ok(user_stack) ? "BAD" : "OK!", user_stack -> maxsize, user_stack -> size); for (int i = user_stack -> maxsize - 1; i >= 0; --i) { fprintf(stream, "%.3d: ", i + 1); if (fprintf(stream, "%c %lg \n", ((user_stack -> size > i)&&(i >= 0)) ? '*' : ' ', user_stack -> data[i]) < 0 ) return ERR_PRINT; } fprintf(stream, "\nStack: end of dump\n"); return OK; }
double stack_pop(struct stek* stek_pop) { if (!stack_ok(stek_pop)) { stack_dump(stek_pop); printf("INVALID STACK"); abort(); } if (stek_pop->counter == 0) { printf("Stack is empty, first enter the data\n"); return 1; } if ((stek_pop->counter+200 < stek_pop->max_size) && (stek_pop->counter >0)) stek_pop->data = (double*) realloc(stek_pop->data, (stek_pop->max_size-=10)*sizeof(*stek_pop->data)); assert(stek_pop->data); return stek_pop->data[--stek_pop->counter]; }
int stack_dump (struct stek* stek_push) { if (stack_ok(stek_push)) printf("Good stack "); else printf("Bad stack"); printf(" [0x%lx] \n", (unsigned long)stek_push); printf("counter = %ld \nmax_size = %ld\n", stek_push->counter, stek_push->max_size); printf("DATA [0x%lx]:\n", (unsigned long)stek_push->data); if (stek_push->data) { for (long i = 0; i < stek_push->max_size;i++) { printf(" data[%ld] = %lg ",i,stek_push->data[i]); if (i >= stek_push->counter) { printf("*"); } printf("\n"); } } printf("END OF DATA\n"); return 0; }
void stack_push(stack* user_stack, double input) { VERIFY(stack_ok(user_stack) == OK); //what means that size < maxsize user_stack -> data[(user_stack -> size)++] = input; //where new size <= maxsize - correct VERIFY(stack_ok(user_stack) == OK); }
double stack_pop(stack* user_stack) { VERIFY(stack_ok(user_stack) == OK); return user_stack -> data[--(user_stack -> size)]; }