root / pobysoC-4.0 / src / test-pobyso-subpoly.c @ 137
Historique | Voir | Annoter | Télécharger (2,78 ko)
1 | 136 | storres | /** @file test-pobyso-subpoly.c
|
---|---|---|---|
2 | 133 | storres | * Name & purpose
|
3 | 133 | storres | *
|
4 | 133 | storres | * @author
|
5 | 133 | storres | * @date
|
6 | 133 | storres | *
|
7 | 133 | storres | * @details Verbosity manipulation are performed to make sur that verbosity
|
8 | 133 | storres | * suppression is correctly managed in pobyso_is_constant_expression()
|
9 | 133 | storres | * function.
|
10 | 133 | storres | */
|
11 | 133 | storres | /******************************************************************************/
|
12 | 133 | storres | /* Headers, applying the "particular to general" convention.*/
|
13 | 133 | storres | |
14 | 133 | storres | #include "pobyso.h" |
15 | 133 | storres | |
16 | 133 | storres | /* includes of local headers */
|
17 | 133 | storres | |
18 | 133 | storres | /* includes of project headers */
|
19 | 133 | storres | |
20 | 133 | storres | /* includes of system headers */
|
21 | 136 | storres | #include <stdlib.h> |
22 | 133 | storres | /* Other declarations */
|
23 | 133 | storres | |
24 | 133 | storres | /* Internal prototypes */
|
25 | 137 | storres | int
|
26 | 136 | storres | read_long_decimal(long int* value, char* string); |
27 | 137 | storres | int
|
28 | 137 | storres | read_unsigned_long_decimal(long unsigned int* value, char* string); |
29 | 133 | storres | |
30 | 133 | storres | /* Types, constants and macros definitions */
|
31 | 133 | storres | |
32 | 133 | storres | /* Global variables */
|
33 | 133 | storres | |
34 | 133 | storres | /* Functions */
|
35 | 137 | storres | int
|
36 | 136 | storres | read_long_decimal(long int* returnValue, char* string) |
37 | 133 | storres | { |
38 | 136 | storres | char* endptr[1]; |
39 | 136 | storres | long int convertedValue = 0; |
40 | 133 | storres | |
41 | 136 | storres | convertedValue = strtoul(string, endptr, 10);
|
42 | 136 | storres | /* For a completely safe conversion *endptr must point to 0 value char. */
|
43 | 136 | storres | if (**endptr != '\0') |
44 | 133 | storres | { |
45 | 136 | storres | return 1; |
46 | 133 | storres | } |
47 | 133 | storres | else
|
48 | 133 | storres | { |
49 | 136 | storres | *returnValue = convertedValue; |
50 | 136 | storres | return 0; |
51 | 133 | storres | } |
52 | 136 | storres | } /* End read_long_decimal. */
|
53 | 133 | storres | |
54 | 137 | storres | int
|
55 | 137 | storres | read_unsigned_long_decimal(long unsigned int* returnValue, char* string) |
56 | 136 | storres | { |
57 | 136 | storres | char* endptr[1]; |
58 | 136 | storres | long int convertedValue = 0; |
59 | 136 | storres | |
60 | 136 | storres | /* Negative sign -> failure. */
|
61 | 136 | storres | if (*string == '-') |
62 | 133 | storres | { |
63 | 136 | storres | return 1; |
64 | 133 | storres | } |
65 | 136 | storres | convertedValue = strtoul(string, endptr, 10);
|
66 | 136 | storres | /* For a completely safe conversion *endptr must point to 0 value char. */
|
67 | 136 | storres | if (**endptr != '\0') |
68 | 133 | storres | { |
69 | 133 | storres | return 1; |
70 | 133 | storres | } |
71 | 133 | storres | else
|
72 | 133 | storres | { |
73 | 136 | storres | *returnValue = convertedValue; |
74 | 136 | storres | return 0; |
75 | 133 | storres | } |
76 | 136 | storres | return 0; |
77 | 136 | storres | } /* End read_unsigned_long_decimal. */
|
78 | 133 | storres | |
79 | 136 | storres | int
|
80 | 136 | storres | main(int argc, char** argv) |
81 | 136 | storres | { |
82 | 136 | storres | long int* expsArray = NULL; |
83 | 136 | storres | int i;
|
84 | 136 | storres | pobyso_func_exp_t polynomial; |
85 | 136 | storres | pobyso_func_exp_t subpoly; |
86 | 136 | storres | |
87 | 136 | storres | if (argc < 3) |
88 | 133 | storres | { |
89 | 136 | storres | fprintf(stderr, |
90 | 136 | storres | "Usage: %s polynomialAsString power1 [power2 [...[powern]..]\n",
|
91 | 136 | storres | argv[0]);
|
92 | 133 | storres | return 1; |
93 | 133 | storres | } |
94 | 133 | storres | |
95 | 136 | storres | pobyso_set_canonical_on(); |
96 | 136 | storres | polynomial = pobyso_parse_string(argv[1]);
|
97 | 136 | storres | if (polynomial == NULL) |
98 | 133 | storres | { |
99 | 136 | storres | fprintf(stderr, |
100 | 136 | storres | "%s: can't create the polynomial.\n",
|
101 | 136 | storres | argv[0]);
|
102 | 136 | storres | return 1; |
103 | 133 | storres | } |
104 | 136 | storres | pobyso_autoprint(polynomial); |
105 | 136 | storres | expsArray = (long int*) malloc((argc - 2) * sizeof(long int)); |
106 | 136 | storres | if (expsArray == NULL) |
107 | 133 | storres | { |
108 | 136 | storres | fprintf(stderr, |
109 | 136 | storres | "%s: can't create the exponents array.\n",
|
110 | 136 | storres | argv[0]);
|
111 | 133 | storres | return 1; |
112 | 133 | storres | } |
113 | 136 | storres | for (i = 2 ; i < argc ; i++) |
114 | 133 | storres | { |
115 | 136 | storres | if (read_long_decimal(&(expsArray[i - 2]), argv[i])) |
116 | 136 | storres | { |
117 | 136 | storres | fprintf(stderr, |
118 | 136 | storres | "%s: can't read exponent \"%s\".\n",
|
119 | 136 | storres | argv[0],
|
120 | 136 | storres | argv[i]); |
121 | 136 | storres | return 1; |
122 | 136 | storres | } |
123 | 133 | storres | } |
124 | 136 | storres | subpoly = pobyso_subpoly(polynomial, argc - 2, expsArray);
|
125 | 136 | storres | if (subpoly == NULL) |
126 | 133 | storres | { |
127 | 133 | storres | return 1; |
128 | 133 | storres | } |
129 | 133 | storres | else
|
130 | 133 | storres | { |
131 | 136 | storres | pobyso_autoprint(subpoly); |
132 | 136 | storres | return 0; |
133 | 133 | storres | } |
134 | 133 | storres | } /* End main */ |