Skip to content
Snippets Groups Projects
Commit f9f1679d authored by DARWICH ALI's avatar DARWICH ALI
Browse files

m

parent 884530fc
Branches
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
File added
File added
File added
File deleted
// fichier base.h
#ifndef BASE
#define BASE
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
typedef bool Bool;
#define faux false
#define vrai true
//#define vrai 1
//#define faux 0
//typedef unsigned char Bool;
typedef unsigned int Nat;
typedef int Ent;
typedef float Reel;
typedef float Rat;
typedef char Car;
typedef Car * Chaine;
#define MALLOC(type) ((type*)malloc(sizeof(type)))
#define MALLOCN(type, n) ((type*)calloc(n, sizeof(type)))
#define CALLOCN(type, n) ((type*)malloc(n * sizeof(type)))
#define REALLOC(t, type, n) ((type*)realloc(t, n * sizeof(type)))
#define FREE(t) free(t)
#endif
\ No newline at end of file
File added
#include "pile.h"
int main(){
S x = "30 15 7 12 + * + 11 8 +10 - + +" ;
//printf("%d\n",evalNPI(x));
printf("%d\n", det(x, 6));
return 0;
}
\ No newline at end of file
File added
CC = gcc -g
CFLAGS = -W -Wall
SOURCES = $(wildcard *.c)
OBJETS = $(SOURCES:.c=.o)
EXEC = main
$(EXEC) : $(OBJETS)
$(CC) $(CFLAGS) -o $@ $^
%.o : %.c
$(CC) $(CFLAGS) -c $<
clean:
rm $(OBJETS) $(EXEC)
#include "pile.h"
Pile pilenouv(){
return NULL;
}
Pile empiler(Pile p, S x ){
Pile p1 = malloc(sizeof(spile));
p1->v = x ;
p1->s = p ;
return p1 ;
}
Pile depiler(Pile p){
Pile t = p ;
p = p->s;
free(t);
return p ;
}
Pile remplacer(Pile p , S x){
p->v = x ;
return p ;
}
S sommet(Pile p ){
return p->v;
}
bool vide(Pile p){
return p == NULL;
}
Nat hauteur(Pile p ){
return vide(p) ? 0 : 1 + hauteur(p->s) ;
}
Nat det(char x[], int indice ){
int i ;
char c ;
int k ;
Nat result = 0;
int l = 0 ;
for(i = indice ; x[i] != ' ' ;l++ , i++);
int h =0;
for(k =1 ; h < l ; h++ , k*= 10);
while (k != 0){
c = x[indice];
switch (c) {
case '1' :{
result += 1 * k;
break ;
}
case '2' :{
result += 2 * k;
break ;
}
case '3':{
result += 3 * k;
break ;
}
case '4':{
result += 4 * k;
break ;
}
case '5':{
result += 5 * k;
break ;
}
case '6':{
result += 6 * k;
break ;
}
case '7':{
result += 7 * k;
break ;
}
case '8':{
result += 8 * k;
break ;
}
case '9':{
result += 9 * k;
break ;
}
case '0':{
result += 0 * k;
break ;
}
default :{
assert("error");
break;
}
}
indice++;
k /=10 ;
}
return result/10 ;
}
Nat evalNPI(S x){
Pile temp ;
Pile p2 ;
Nat op1 ;
Nat op2 ;
Nat result =0 ;
int indice = 0 ;
Nat i ;
for(i = 0 ;x[i] !='\0';i++);
if(i == 0 ){
result =0 ;
}
else{
for(int j = 0 ;x[j] !='\0';j++ ){
char h = x[j];
switch (h) {
case '*':{
op1 = temp->v;
op2 = temp->s->v;
result += op1 * op2 ;
p2 = temp ;
temp = temp->s ;
free(p2);
temp->v = result ;
break ;
}
case '+':{
op1 = temp->v;
op2 = temp->s->v ;
result += op1 + op2 ;
p2 = temp ;
temp = temp->s ;
free(p2);
temp->v = result ;
break ;
}
case '-':{
op1 = temp->v;
op2 = temp->s->v ;
result += op1 - op2 ;
p2 = temp ;
temp = temp->s ;
free(p2);
temp->v = result ;
break ;
}
case '/':{
op1 = temp->v;
op2 = temp->s->v+ ;
result += op1 / op2 ;
p2 = temp ;
temp = temp->s ;
free(p2);
temp->v = result ;
break ;
}
case ' ':{
indice++ ;
break ;
}
/*
case '1':{
}
case '2':{
}
case '3':{
}
case '4':{
}
case '5':{
}
case '6':{
}
case '7':{
}
case '8':{
}
case '9':{
}
*/
default:{
S ar = emp(*x , indice) ;
temp = empiler(temp , ar);
break ;
}
}
}
}
return result ;
}
S emp(S x, int i ){
int k ;
S temp ;
for(k = 0 ; i != 0 ; k++){
if(x[k] == ' '){
i-- ;
}
}
Nat espace ;
Nat h = k ;
while(x[h]!= ' '){
espace++ ;
}
for(int i = 0 ; i<(int ) (espace - k); i++){
temp[i] = x[k] ;
}
return temp ;
}
\ No newline at end of file
#ifndef __PILE__
#define __PILE__
#include "base.h"
#include <assert.h>
typedef char* S;
typedef struct spile { S v; struct spile *s; } spile, *Pile ;
Pile pilenouv() ;
Pile empiler(Pile p, S x );
Pile depiler(Pile p) ;
Pile remplacer(Pile p , S x) ;
S sommet(Pile p );
bool vide(Pile p);
Nat hauteur(Pile p );
Nat evalNPI(S);
Nat det(char x[], int indice );
S emp(S x, int i );
#endif
\ No newline at end of file
File added
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment