Skip to content
Snippets Groups Projects
Commit 954a1170 authored by thib's avatar thib
Browse files

debug plz

parent e5d98251
Branches
No related tags found
No related merge requests found
cc=gcc
LIBS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak -lm
CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak
LFLAGS=-fsanitize=address -fsanitize=leak -lm
test :test.x
./test.x
test.x: calc.o test.o
$(cc) -o $@ $^ $(LIBS)
test.o: test.c
$(cc) -c $^ $(LIBS)
test.x: calc.o test.o stack.o
$(cc) -o $@ $^ $(CFLAGS) $(LFLAGS)
main: main.x
./main.x
main.x: calc.o main.c
$(cc) -o $@ $^ $(LIBS)
calc.o: calc.c calc.h
$(cc) -c $^ $(LIBS)
main.x: calc.o main.o stack.o
$(cc) -o $@ $^ $(CFLAGS) $(LFLAGS)
clean:
rm -f *.o *.x *.gch
#include "calc.h"
#include "stack.h"
#include <stdlib.h>
#include <string.h>
// typedef struct _stack {
// void** data;
// int top;
// int capacity;
// } stack;
void stack_init(stack *stack, int size, int typeSize) {
stack->data = malloc(typeSize * size);
stack->top = -1;
stack->capacity = size;
void malloc_push(stack *s, char item) {
char *c = malloc(sizeof(char));
*c = item;
push(s, c);
}
bool is_empty(stack stack) { return stack.top == -1; }
bool is_full(stack stack) { return stack.capacity - 1 == stack.top; }
void *peek(stack stack) {
if (is_empty(stack)) {
return NULL;
}
return stack.data[stack.top];
}
int stack_count(stack stack) { return stack.top + 1; }
void stack_destroy(stack *stack) {
free(stack->data);
stack->top = -1;
stack->capacity = -1;
// void opp(char c, stack s) {
// if (!is_empty(s) && (getPrio(c) > getPrio(*(char *)peek(s)))) {
// push(&s, &c);
// }
// while (!is_empty(s) && (3 <= getPrio(*(char *)peek(s)))) {
// postfix[j++] = *(char *)pop(&s);
// }
// }
bool isOpperator(char ch) {
return ch == '+' || ch == '-' || ch == 'x' || ch == '/' || ch == '^' ||
ch == '*';
}
char *infix_to_postfix(char *infix) {
int infix_size=strlen(infix);
int postfix_size = infix_size + 1;
stack s;
stack_init(&s, infix_size);
// get final size
// for (int i = 0; i < infix_size; i++) {
// if (infix[i] == '(' || infix[i] == ')') {
// postfix_size--;
// }
// }
char *postfix = malloc(sizeof(char) * postfix_size);
int j = 0; // index for returned array
for (int i = 0; i < infix_size; i++) {
if (infix[i] >= '0' && infix[i] <= '9') { // number
postfix[j] = infix[i];
j++;
} else if (infix[i] == '(') { // (
push(&s, &infix[i]);
} else if (infix[i] == ')') { // )
while (!is_empty(s) && *(char *)peek(s) != '(') {
printf("%c\n",*(char*)peek(s));
postfix[j] = *(char *)pop(&s);
j++;
}
if (!is_empty(s) && *(char *)peek(s) == '(') { //ici ?
pop(&s);
}
} else if (isOpperator(infix[i])) { // opp
void push(stack *stack, void *item) {
if (!is_full(*stack)) {
stack->top++;
stack->data[stack->top] = item;
while (!is_empty(s) && (getPrio(infix[i]) <= getPrio(*(char *)peek(s)))) {
postfix[j] = *(char *)pop(&s);
j++;
}
push(&s, &infix[i]);
}else{
printf("NULL ahhh!!\n");
free(postfix);
stack_destroy(&s);
return NULL;
}
}
}
void *pop(stack *stack) {
void *tmp;
tmp = stack->data[stack->top];
stack->top--;
return tmp;
while (!is_empty(s)) {
postfix[j] = *(char *)pop(&s);
j++;
}
printf("%d\n",j);
postfix[j]='\0';
printf("%s",postfix);
printf("\n");
// printf("%d\n",j);
stack_destroy(&s);
return postfix;
}
int getPrio(char opp) {
......@@ -62,61 +94,3 @@ int getPrio(char opp) {
return 0;
}
}
char *toPostfix(char *infix, int len) {
stack s;
char *postfix = malloc(sizeof(char) * len);
int j = 0;
stack_init(&s, len, sizeof(char*));
for (int i = 0; i < len; ++i) {
if (infix[i] == ')') {
while (*(char *)peek(s) != '(') {
postfix[j++] = *(char *)pop(&s);
}
pop(&s);
} else if (infix[i] == '(') {
push(&s, &infix[i]);
} else if (infix[i] == '+' || infix[i] == '-') {
if (!is_empty(s) && (1 > getPrio(*(char *)peek(s)))) {
push(&s, &infix[i]);
}
while (!is_empty(s) && (1 < getPrio(*(char *)peek(s)))) {
postfix[j++] = *(char *)pop(&s);
}
} else if (infix[i] == 'x' || infix[i] == '/') {
// printf("print %c\n\n\n\n",peek())
if (!is_empty(s)) {
printf("not empty\n");
if ((2 > getPrio(*(char *)peek(s)))) {
//push(&s, &infix[i]);
}
printf("before\n");
while (!is_empty(s)&&(2 < getPrio(*(char *)peek(s)))) {
printf("looooop\n");
postfix[j++] = *(char *)pop(&s);
}
}
} else if (infix[i] == '^') {
if (!is_empty(s) && (3 > getPrio(*(char *)peek(s)))) {
push(&s, &infix[i]);
}
while (!is_empty(s) && (3 < getPrio(*(char *)peek(s)))) {
postfix[j++] = *(char *)pop(&s);
}
} else { // numer
postfix[j++] = infix[i];
printf("nb %c\n", infix[i]);
}
printf("i :%d j: %d stack: %d\n", i, j, stack_count(s));
}
while (!is_empty(s)) {
postfix[j++] = *(char *)pop(&s);
}
free(s.data);
return postfix;
}
......@@ -7,31 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct _stack {
void **data;
int top;
int capacity;
} stack;
bool is_empty(stack stack);
bool is_full(stack stack);
void* peek(stack stack);
int stack_count(stack stack);
void stack_init(stack *stack, int max, int typeSize);
void stack_destroy(stack *stack);
void push(stack *stack, void* item);
void* pop(stack *stack);
char* infix_to_postfix(char* infix);
char* toPostfix(char* infix,int len);
int getPrio(char opp);
......
......@@ -3,14 +3,14 @@
#include <string.h>
int main() {
int len=5;
char infix[5]={'a','+','b','x','c'};
char *postfix=toPostfix(infix, len);
for(int i=0;i<len;i++){
printf("%c ",postfix[i]);
}
printf("\n");
char *infix="2/(7-3*2)";
char *postfix=infix_to_postfix(infix);
// for(int i=0;i<len;i++){
// printf("%c ",postfix[i]);
// }
// printf("\n");
free(postfix);
// // char *postfix1 = malloc(sizeof(char) * 5);
......
#include "stack.h"
#include <stdlib.h>
void stack_init(stack *stack, int size) {
stack->data = malloc(sizeof(void*)* size);
stack->top = -1;
stack->capacity = size;
}
bool is_empty(stack stack) { return stack.top == -1; }
bool is_full(stack stack) { return stack.capacity - 1 == stack.top; }
void *peek(stack stack) {
if (is_empty(stack)) {
return NULL;
}
return stack.data[stack.top];
}
int stack_count(stack stack) { return stack.top + 1; }
void stack_destroy(stack *stack) {
free(stack->data);
stack->top = -1;
stack->capacity = -1;
}
void push(stack *stack, void *item) {
if (!is_full(*stack)) {
stack->top++;
stack->data[stack->top] = item;
}
}
void *pop(stack *stack) {
void *tmp;
tmp = stack->data[stack->top];
stack->top--;
return tmp;
}
\ No newline at end of file
#ifndef _STACK_H_
#define _STACK_H_
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _stack {
void **data;
int top;
int capacity;
} stack;
bool is_empty(stack stack);
bool is_full(stack stack);
void* peek(stack stack);
int stack_count(stack stack);
void stack_init(stack *stack, int size);
void stack_destroy(stack *stack);
void push(stack *stack, void* item);
void* pop(stack *stack);
#endif
#include <stdlib.h>
#include "calc.h"
#include "stack.h"
#include <stdlib.h>
int main() {
stack s;
stack_init(&s, 3);
int main(){
stack s;
// stack_init(&s,1,sizeof(int*));
// int x=3;
// push(&s,&x);
// printf("%d",*(int*)pop(&s));
stack s2;
stack_init(&s2,1,sizeof(char*));
char c='a';
push(&s2,&c);
printf("%c",*(char*)pop(&s2));
stack_init(&s,5,sizeof(int*));
//char infix[5]={'a','+','b','x','c'};
char infix[5]={1,2,3,4,5};
for(int i=0;i<5;i++){
// char *c=malloc(sizeof(char));
// *c=infix[i];
push(&s,&infix[5]);
}
printf("%d",*(int*)pop(&s));
//char z=*(char*)pop(&s);
for(int i=0;i<5;i++){
// char *c=malloc(sizeof(char));
// *c=infix[i];
// printf("%c\n\n\n\n ",*(char*)pop(&s));
//printf("%c",*(char*)pop(&s));
}
stack_destroy(&s);
stack_destroy(&s2);
//free(c);
return EXIT_SUCCESS;
char *str = "abc";
push(&s, &str[0]);
push(&s, &str[1]);
push(&s, &str[2]);
printf("%c\n", *(char *)pop(&s));
printf("%c\n", *(char *)pop(&s));
printf("%c\n", *(char *)pop(&s));
stack_destroy(&s);
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment