📄 node76.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//FR">
<!--Converted with LaTeX2HTML 97.1 (release) (July 13th, 1997)
by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippman, Marek Rouchal, Martin Wilck and others -->
<HTML><BLOCKQUOTE><img src="logo.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/logo.gif" ALIGN=right></A>
<HEAD>
<TITLE>Exercice 2</TITLE>
<META NAME="description" CONTENT="Exercice 2">
<META NAME="keywords" CONTENT="Introduction_ANSI_C">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso_8859_1">
<LINK REL="STYLESHEET" HREF="Introduction_ANSI_C.css" tppabs="http://www.linux-kheops.com/doc/ansi-c/Introduction_ANSI_C.css">
<LINK REL="previous" HREF="node75.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node75.htm">
<LINK REL="up" HREF="node68.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node68.htm">
<LINK REL="next" HREF="node77.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node77.htm">
</HEAD>
<BODY BACKGROUND="marge.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/marge.gif" TEXT=#000000 LINK=#FF0000 VLINK=#1809BB >
<!--Navigation Panel-->
<A NAME="tex2html2313"
HREF="node77.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node77.htm">
<IMG WIDTH="77" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="next_motif.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/next_motif.gif"></A>
<A NAME="tex2html2309"
HREF="node68.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node68.htm">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="up_motif.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/up_motif.gif"></A>
<A NAME="tex2html2305"
HREF="node75.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node75.htm">
<IMG WIDTH="96" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="previous_motif.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/previous_motif.gif"></A>
<A NAME="tex2html2311"
HREF="node1.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node1.htm">
<IMG WIDTH="96" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="contents_motif.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/contents_motif.gif"></A>
<A NAME="tex2html2312"
HREF="node174.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node174.htm">
<IMG WIDTH="59" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
SRC="index_motif.gif" tppabs="http://www.linux-kheops.com/doc/ansi-c/icons.gif/index_motif.gif"></A>
<BR>
<B> Suivant:</B> <A NAME="tex2html2314"
HREF="node77.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node77.htm">Structures, unions et 閚um閞ations</A>
<B> D閎ut:</B> <A NAME="tex2html2310"
HREF="node68.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node68.htm">Les entr閑s-sorties</A>
<B> Pr閏閐ent:</B> <A NAME="tex2html2306"
HREF="node75.htm" tppabs="http://www.linux-kheops.com/doc/ansi-c/node75.htm">Exercice 1</A><HR>
<BR>
<BR>
<!--End of Navigation Panel-->
<H1><font COLOR=#1809BB><A NAME="SECTION00780000000000000000">
Exercice 2</A>
</font></H1>
Reprendre la calculette r閍lis閑 en fin de chapitre sur les pointeurs et y
rajouter une gestion correcte des erreurs. Si l'utilisateur tape une ligne
incorrecte, on d閟ire l'閙ission d'un message d'erreur, et une continuation
de la boucle de calcul.
<P><SMALL>
<PRE>
#include "stdio.h"
/*****************************************************************************/
/* */
/* main */
/* */
/*****************************************************************************/
int main()
{
FILE * fi;
char nom[80];
char article[80];
int nombre,prix;
if ((fi = fopen("exer6.data","r")) == NULL)
printf("Impossible d'ouvrir le fichier exer6.data\n");
else
{
while(fscanf(fi,"%s %s %d %d",nom,article,&nombre,&prix) != EOF)
printf("%s %s %d\n",nom,article,nombre * prix);
fclose(fi);
}
}
</PRE>
</SMALL>
<P><SMALL>
<PRE>
#include <stdio.h>
enum {FAUX, VRAI};
/*****************************************************************************/
/* */
/* main */
/* */
/*****************************************************************************/
int main()
{
int i,j,r; /* les op閞andes */
char c; /* l'op閞ateur */
char imp; /* bool閑n de demande d'impression du r閟ultat */
int ret; /* code de retour de scanf */
char buf_err[80];
while (1)
{
if ((ret = scanf("%d %c %d",&i,&c,&j)) != 3)
{
if (ret == EOF) exit(0);
scanf("%[^\n]",buf_err); /* on mange la partie erron閑 */
printf("Erreur de syntaxe : %s\n",buf_err);
continue;
}
imp = VRAI;
switch (c)
{
case '+' : r = i + j; break;
case '-' : r = i - j; break;
case '*' : r = i * j; break;
case '/' :
if ( j == 0)
{
printf("Division par z閞o\n");
imp = FAUX;
}
else r = i / j;
break;
case '%' : r = i % j; break;
default : printf("l'op閞ateur %c est incorrect\n",c); imp = FAUX;
} /* fin du switch */
if (imp) printf("%d\n",r);
}
}
</PRE>
</SMALL>
<P>
<BR><HR>
<ADDRESS>
<I></I>
<BR><I>30/9/1997</I>
</ADDRESS>
</BODY>
</BLOCKQUOTE></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -