Bonjour,
Je "tente" de manipuler des polynômes avec une liste simplement chaînée pour pouvoir multiplier deux polynomes, les additionner,etc...
J'essaye de lire une chaine de caracteres pour la transformer en polynome et travailler avec.
Mais déjà la premiere etape me pose probleme...
Je vous copie les 3 fichiers .c
poly.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "error.h" #include "poly.h" /*STRUCTURE DECLARATION*/ typedef struct poly_t *pointer; typedef struct poly_t{ int coeff; int exponent; pointer next; }poly; /*FUNCTION NEW POLY*/ poly_t* new_poly_from_string(const char* poly_string){ printf("test %s",poly_string); poly_t *new_poly = malloc(sizeof(poly)); poly_t *temp = new_poly; int i; int l; for (i=0;i<strlen(poly_string);i++){ if(poly_string[i]=='x'){ temp->coeff = (int) poly_string[i-1]; printf("AAcoeff %d",new_poly->coeff); while(isdigit(poly_string[--i])){ temp->coeff += poly_string[i]*10^i; } temp = temp->next; } if(poly_string[i]=='^'){ temp->exponent = (int) poly_string[i+1]; while(isdigit(poly_string[++i])){ l++; temp->coeff += poly_string[i]*10^(l-i); printf("Scoeff %d",temp->coeff); } } printf("poly string"); printf("coeff %d",new_poly->coeff); return new_poly; } } /*FUNCTION FREE POLY*/ void free_poly(poly_t* poly){ //poly_t* temp = poly; free(poly); // while(poly->next != NULL){ // free(temp->coeff); // free(temp->exponent); // free_poly(temp->next); // } } /*FUNCTION ADD*/ poly_t* add(poly_t* poly1, poly_t* poly2){ poly_t* add_poly; if(poly1->exponent==poly2->exponent){ add_poly->coeff = poly1->coeff + poly2->coeff ; } else if (poly1->exponent != poly2->exponent){ // JE VOIS PAS } return add_poly; } /*FUNCTION MUL*/ poly_t* mul(poly_t* poly1, poly_t* poly2){ poly_t* mul_poly; while(poly1->next != NULL){ mul_poly->coeff = poly1->coeff * poly2->coeff; mul_poly->exponent = poly1->exponent + poly2->exponent; mul_poly->next = mul(poly1->next,poly2); mul_poly = add(mul_poly,mul_poly->next); } return mul_poly; } /*FUNCTION PRINT_POLY*/ void print_poly(poly_t* poly){ if(poly->next == NULL){ printf("Null polynomial"); } else while (poly->next != NULL){ printf("%d x^ %d",poly->coeff,poly->exponent); poly = poly->next; } }
error.c
main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <stdarg.h> #include <stdlib.h> #include <stdio.h> #include "error.h" void (error)(char* file, int line, const char* func, char* msg, ...) { va_list ap; static char colon[] = ": "; if (progname == NULL) { progname = ""; colon[0] = 0; } va_start(ap, msg); fprintf(stderr, "%s%serror: detected in file \"%s\", line %u" " in function \"%s\": ", progname, colon, file, line, func); vfprintf(stderr, msg, ap); va_end(ap); fputc('\n', stderr); exit(EXIT_FAILURE); }
Un grand merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 #include <stdbool.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include "poly.h" char* progname; static void poly_test(char* a, char* b) { poly_t* p; /* Polynomial made from A. */ poly_t* q; /* Polynomial made from B. */ poly_t* r; /* Product of P and Q. */ printf("Begin polynomial test of (%s) * (%s)\n", a, b); p = new_poly_from_string(a); q = new_poly_from_string(b); /* print_poly(p); print_poly(q); r = mul(p, q); print_poly(r); free_poly(p); free_poly(q); free_poly(r); */ system("PAUSE"); printf("End polynomial test of (%s) * (%s)\n\n\n", a, b); } int main(int argc, char** argv) { progname = argv[0]; poly_test("x^2 - 7x + 1", "3x + 2"); poly_test("x^10000000 + 2", "2x^2 + 3x + 4"); return 0; }
Partager