Article View: fr.comp.lang.c
Article #15466Re: conjugaison et autres exos sur les chaines [Was : Un système de conj...]
From: Emmanuel Delahaye
Date: Wed, 21 Sep 2005 20:25
Date: Wed, 21 Sep 2005 20:25
154 lines
6253 bytes
6253 bytes
Pierre Maurette wrote on 21/09/05 :
> candide, le 21/09/2005, a �crit :
>> "Emmanuel Delahaye" <emdel@YOURBRAnoos.fr> wrote in
>> news:mn.94c47d590202e83a.15512@YOURBRAnoos.fr:
>>
>>>
>>>> $ carre 7
>>>> 49
>>>
>>> Evidemment, si la chaine repr�sente une valeur num�rique, elle sera
>>> convertie en binaire par la fonction ad�quate (sscanf(), strtoul() etc.)
>>>
>>
>> mais pour un entier (<> flottant) on peut faire comme suit, non ?
>>
>> /*------------- icarre.c -------------------------*/
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main (int argc, char *argv[])
>> {
>> int entier;
>>
>> if (argc !=2) return;
>> sscanf(argv[1],"%d",&entier);
>> printf("%d\n",entier*entier);
>> return(0);
>> } /*-----------------------------------------------*/
>>
>>> icarre 32
>> 1024
> Il est d'usage d'utiliser une fonction usage() qui affiche la syntaxe de la
> ligne de commande en cas de mauvais arguments. �a permet d'ailleurs de
> clarifier pour soi-m�me ce qu'on attend de ces param�tres, ce qu'on accepte,
> ce qqu'on modifie par une valeur par d�faut, et ce qu'on refuse.
> De plus, il faut un minimum de contr�le, particuli�rment � ce niveau, puisque
> n'importe quoi peut arriver par la ligne de commande. Si j'�cris une fonction
> appel�e exclusivement par du code que je ma�trise, je pense qu'on peut ne pas
> multiplier les contr�les, quitte � noter en commentaire ce qu'on suppose
> vrai. Pour r�pondre � l'argument habituel de la r�utilisation de la fonction
> six mois plus tard. Mais ici, c'est diff�rent.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <inttypes.h>
>
> void usage(void)
> {
> puts("Syntaxe: icarre valeur (repr�sentant un entier)\nMais Auchan vend des
> trucs qui le font\npour moins d'un euro, et sans ordinateur.");
> exit(EXIT_SUCCESS);
> }
>
> int main(int argc, char * argv[])
> {
> uintmax_t entier;
> char * pvaleur = NULL;
> const uintmax_t borne = sqrtl((long double)UINTMAX_MAX);
>
> if(argc != 2) usage();
> pvaleur = argv[1];
> while(pvaleur[0] == '-' || isspace(pvaleur[0])) pvaleur++;
> entier = strtoumax(pvaleur, NULL, 0);
> if(errno || (entier > borne))
> puts("Oh my God, c'est h�naurme !");
> else
> printf("%" PRIuMAX "\n", entier * entier);
> return EXIT_SUCCESS;
> }
Un peu brut de d�coffrage...
main.c:13: warning: no previous prototype for 'usage'
main.c: In function `usage':
main.c:16: warning: function might be possible candidate for attribute
`noreturn'
main.c: In function `main_':
main.c:23: error: implicit declaration of function `sqrtl'
main.c:23: warning: nested extern declaration of `sqrtl'
<internal>:0: warning: redundant redeclaration of 'sqrtl'
main.c:27: error: implicit declaration of function `isspace'
main.c:27: warning: nested extern declaration of `isspace'
Petite mise au point...
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <limits.h>
#include <ctype.h>
#if 1
#if !defined (__STDC_VERSION__)\
|| (defined (__STDC_VERSION__) && __STDC_VERSION__ < 199901)
#error must be a C99 compiler
#endif
#endif
static void usage (void)
{
puts ("Syntaxe: icarre valeur (representant un entier)\n"
"Mais Auchan vend des trucs qui le font\n"
"pour moins d'un euro, et sans ordinateur.");
}
int main (int argc, char *argv[])
{
int ret = EXIT_SUCCESS;
if (argc == 2)
{
const uintmax_t borne = sqrtl ((long double) UINTMAX_MAX);
char const *pvaleur = argv[1];
while (pvaleur[0] == '-' || isspace (pvaleur[0]))
{
pvaleur++;
}
uintmax_t entier = strtoumax (pvaleur, NULL, 0);
if (errno || (entier > borne))
{
puts ("Oh my God, c'est henaurme !");
}
else
{
printf ("%" PRIuMAX "\n", entier * entier);
}
}
else
{
usage ();
ret = EXIT_FAILURE;
}
return ret;
}
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Technical Details
Message-ID:
<mn.ad417d590cf08264.15512@YOURBRAnoos.fr>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!grolier!oleane.net!oleane!feed.ac-versailles.fr!proxad.net!infeed-4.proxad.net!news18-e.free.fr!not-for-mailReferences:
<1126342327.583568.21420@g43g2000cwa.googlegroups.com> <4329e5d4$0$7846$8fcfb975@news.wanadoo.fr> <dge4b6$fqa$1@reader1.imaginet.fr> <mn.837a7d59123d924a.15512@YOURBRAnoos.fr> <432adddf$0$17220$8fcfb975@news.wanadoo.fr> <mn.84407d597cfa6e6e.15512@YOURBRAnoos.fr> <432b2a92$0$7861$8fcfb975@news.wanadoo.fr> <432c19e0$0$1710$8fcfb975@news.wanadoo.fr> <432cad90$0$27422$8fcfb975@news.wanadoo.fr> <432d1b7c$0$1743$8fcfb975@news.wanadoo.fr> <432d3117$0$1742$8fcfb975@news.wanadoo.fr> <mn.93877d590449a7bc.15512@YOURBRAnoos.fr> <432d7667$0$17248$8fcfb975@news.wanadoo.fr> <mn.94047d599f0cc076.15512@YOURBRAnoos.fr> <432da70c$0$1735$8fcfb975@news.wanadoo.fr> <mn.94c47d590202e83a.15512@YOURBRAnoos.fr> <43315679$0$27421$8fcfb975@news.wanadoo.fr> <mn.ad0b7d59687aaf3b.31483@laposte.net>Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,b21b26167a936e28 X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Subject: Re: conjugaison et autres exos sur les chaines [Was : Un système de conj...] From: "Emmanuel Delahaye" <emdel@YOURBRAnoos.fr> References: <1126342327.583568.21420@g43g2000cwa.googlegroups.com> <4329e5d4$0$7846$8fcfb975@news.wanadoo.fr> <dge4b6$fqa$1@reader1.imaginet.fr> <mn.837a7d59123d924a.15512@YOURBRAnoos.fr> <432adddf$0$17220$8fcfb975@news.wanadoo.fr> <mn.84407d597cfa6e6e.15512@YOURBRAnoos.fr> <432b2a92$0$7861$8fcfb975@news.wanadoo.fr> <432c19e0$0$1710$8fcfb975@news.wanadoo.fr> <432cad90$0$27422$8fcfb975@news.wanadoo.fr> <432d1b7c$0$1743$8fcfb975@news.wanadoo.fr> <432d3117$0$1742$8fcfb975@news.wanadoo.fr> <mn.93877d590449a7bc.15512@YOURBRAnoos.fr> <432d7667$0$17248$8fcfb975@news.wanadoo.fr> <mn.94047d599f0cc076.15512@YOURBRAnoos.fr> <432da70c$0$1735$8fcfb975@news.wanadoo.fr> <mn.94c47d590202e83a.15512@YOURBRAnoos.fr> <43315679$0$27421$8fcfb975@news.wanadoo.fr> <mn.ad0b7d59687aaf3b.31483@laposte.net> Newsgroups: fr.comp.lang.c Message-ID: <mn.ad417d590cf08264.15512@YOURBRAnoos.fr> X-Newsreader: MesNews/1.04.00.00-fr Date: Wed, 21 Sep 2005 22:25:33 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15"; format=flowed Content-Transfer-Encoding: 8bit Lines: 156 Organization: Guest of ProXad - France NNTP-Posting-Date: 21 Sep 2005 22:25:36 MEST NNTP-Posting-Host: 81.65.109.78 X-Trace: 1127334336 news18-e.free.fr 7380 81.65.109.78:14333 X-Complaints-To: abuse@proxad.net Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!grolier!oleane.net!oleane!feed.ac-versailles.fr!proxad.net!infeed-4.proxad.net!news18-e.free.fr!not-for-mail