Skip to content
Snippets Groups Projects
Commit 7822da87 authored by thib's avatar thib
Browse files

rien qui marche :(

parent ff138532
Branches
No related tags found
No related merge requests found
cc=gcc
LIBS=-Wextra -Wall -g -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)
main: main.x
./main.x
main.x: calc.o main.c
$(cc) -o $@ $^ $(LIBS)
calc.o: calc.c calc.h
$(cc) -c $^ $(LIBS)
clean:
rm -f *.o *.x *.gch
#include "calc.h"
#include <stdlib.h>
// typedef struct _stack {
// void** data;
// int top;
// int capacity;
// } stack;
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_init(stack *stack, int size, int typeSize) {
stack->data = malloc(typeSize * size);
stack->top = -1;
stack->capacity = size;
}
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;
}
int getPrio(char opp) {
switch (opp) {
case '+':
case '-':
return 1;
case 'x':
case '/':
return 2;
case '^':
return 3;
default:
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;
}
#ifndef _CALC_H_
#define _CALC_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 max, int typeSize);
void stack_destroy(stack *stack);
void push(stack *stack, void* item);
void* pop(stack *stack);
char* toPostfix(char* infix,int len);
int getPrio(char opp);
#endif
\ No newline at end of file
#include "calc.h"
#include <stdio.h>
#include <string.h>
int main() {
// stack s;
// stack_init(&s, 5, sizeof(int));
// int *a = malloc(sizeof(int));
// *a = 3;
// push(&s, a);
// printf("%d \n", *(int *)peek(s));
// stack_destroy(&s);
// free(a);
char infix1[]="A+(B/C–D^E";
int len=strlen(infix1);
printf("%d\n",len);
//char infix[5]={'a','+','b','x','c'};
char *postfix=toPostfix(infix1, len);
for(int i=0;i<len;i++){
printf("%c ",postfix[i]);
}
printf("\n");
free(postfix);
// char *postfix1 = malloc(sizeof(char) * 5);
// stack s;
// int j = 0;
// stack_init(&s, 5, sizeof(char));
// postfix[0] = *((char *)pop(&s));
return EXIT_SUCCESS;
}
\ No newline at end of file
#include <stdlib.h>
#include "calc.h"
int main(){
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