-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfonctions.cpp
More file actions
222 lines (202 loc) · 5.99 KB
/
fonctions.cpp
File metadata and controls
222 lines (202 loc) · 5.99 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <fstream>
#include <string>
#include "header.h"
#include <stdlib.h>
#define ISOPTION true
#define NOTOPTION false
using namespace std;
void help(void)
{
cout << "Utilisation : fibo [OPTION value]... " << endl << endl
<< "Cacule le nieme nombre de la suite de Fibonacci n etant fournie en entré ou les nombres " << endl
<< "de la suite dans un enterval d'indice le resultat peut etre stocker dans un fichier" << endl
<< " de taille non nulle de caractères délimités par une espace." << endl << endl
<< "Les options ci-dessous permettent de sélectionner les intervalles à afficher," << endl
<< " -f, --first afficher le nombre de caractères" << endl
<< " -s, --second afficher le nombre de sauts de lignes" << endl
<< " -F, --file afficher la largeur maximale d'affichage " << endl
<< " -S, --start afficher le nombre de mots" << endl
<< " -E, --end afficher le nombre de mots" << endl
<< " --space= pour preciser le separateur par defaut, c'est la tabulation" << endl
<< " --help afficher l'aide et quitter" << endl;
}
long maximum(long tab[], long length, long end ){
long max(0), i(0);
while(i<length){
max = (max < tab[i] )? tab[i] : max;
i++;
}
return (max < end )? end : max;
}
bool active_option(unsigned char option, int& i, int argc ,char** argv,long& first, long& seconde, long& start, long& end, string& file )
{
switch (option)
{
case 'f':
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
first = convertion(argv[++i]);
}
break;
case 's':
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
seconde = convertion(argv[++i]);
}
break;
case 'F':
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
file = argv[++i];
}
break;
case 'S':
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
start = convertion(argv[++i]);
if(start < 0)
{
cerr << "fibo : start doit avoir une valeur positive" << endl;
exit(-1);
}
}
break;
case 'E':
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
end = convertion(argv[++i]);
if(end < 0)
{
cerr << "fibo : end doit avoir une valeur positive" << endl;
exit(-1);
}
}
break;
default:
return NOTOPTION;
}
return ISOPTION;
}
bool active_long_option(string option, int& i, int argc ,char** argv,long& first, long& seconde, long& start, long& end, string& file)
{
if(option.compare((string)"--first") == EQUAL)
{
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
first = convertion(argv[++i]);
}
return ISOPTION;
}
if(option.compare((string)"--seconde") == EQUAL)
{
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
seconde = convertion(argv[++i]);
} return ISOPTION;
}
if(option.compare((string)"--file") == EQUAL)
{
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
file = argv[++i];
}
return ISOPTION;
}
if(option.compare((string)"--start") == EQUAL)
{
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(-1);
}
else
{
start = convertion(argv[++i]);
if(start < 0){
cerr << "fibo : start doit avoir une valeur positive" << endl;
exit(-1);
}
}
return ISOPTION;
}
if(option.compare((string)"--end") == EQUAL)
{
if( i == argc)
{
cerr << "fibo : usage option value" << endl;
exit(EXIT_FAILURE);
}
else
{
end = convertion(argv[++i]);
if(end < 0){
cerr << "fibo : end doit avoir une valeur positive" << endl;
exit(-1);
}
}
return ISOPTION;
}
return NOTOPTION;
}
long convertion(char * chaine)
{
unsigned i = 0;
long nombre = 0;
short signe = POSITIF;
if(chaine[0] == TIRET)
{
signe = NEGATIF;
i++;
}
while(chaine[i] != '\0'){
if( chaine[i] > 47 && chaine[i] < 59){
nombre = nombre * 10*i + chaine[i] - 48;
}else{
cerr <<" fibo : " << chaine << " is not a number" << endl;
exit(EXIT_FAILURE);
}
i++;
}
return nombre * signe;
}