一个谜题 [英] A puzzle
问题描述
编写函数
int readINT(FILE * fp);
从fp读取一个int并返回它。
给出以下条件
a。不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你也不能使用任何一个循环。
e。你不能在其中使用任何库函数
除了fgetc()和ungetch()。
write the function
int readINT(FILE* fp);
that read an int from fp and return it.
given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.
e. you cannot use in it any library function
except fgetc() and ungetch().
推荐答案
RoSsIaCrIiLoIA< n@esiste.ee>写道:
RoSsIaCrIiLoIA <n@esiste.ee> writes:
写函数
int readINT(FILE * fp);
从fp读取一个int并返回它。
给出以下内容条件
不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你也不能使用任何一个循环。
e。你不能在其中使用任何库函数
除了fgetc()和ungetch()。
write the function
int readINT(FILE* fp);
that read an int from fp and return it.
given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.
e. you cannot use in it any library function
except fgetc() and ungetch().
更多的作业?
__________________________________________________ _____________________________
克里斯麦克唐纳博士电子邮件: ch***@csse.uwa.edu.au
计算机科学与技术学院软件工程
西澳大利亚大学WWW: http://www.csse.uwa.edu.au/~chris
克劳利,西澳大利亚州,6009 PH:+61 8 6488 2533,传真:+61 8 6488 1089
More homework?
__________________________________________________ _____________________________
Dr Chris McDonald EMAIL: ch***@csse.uwa.edu.au
School of Computer Science & Software Engineering
The University of Western Australia WWW: http://www.csse.uwa.edu.au/~chris
Crawley, Western Australia, 6009 PH: +61 8 6488 2533, FAX: +61 8 6488 1089
Chris McDonald写道:
Chris McDonald wrote:
RoSsIaCrIiLoIA< n@esiste.ee>写道:
RoSsIaCrIiLoIA <n@esiste.ee> writes:
写函数
int readINT(FILE * fp);
从fp读取一个int并返回它。
考虑到以下条件
a。不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你也不能使用任何一个循环。
e。你不能在其中使用任何库函数
除了fgetc()和ungetch()。
write the function
int readINT(FILE* fp);
that read an int from fp and return it.
given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.
e. you cannot use in it any library function
except fgetc() and ungetch().
更多的作业?
More homework?
看起来像相同的作业。
必须是一个完整的作弊者。
Looks like the same homework.
Must be a whole class full of cheaters.
On Sun,2004年4月25日21:57:30 GMT,RoSsIaCrIiLoIA < n@esiste.ee>写道:
On Sun, 25 Apr 2004 21:57:30 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:
写函数
int readINT(FILE * fp);
从fp读取一个int并返回它。
给出以下条件> a。不要使用数组
b。不要使用任何比较功能,如if / then
或switch-case
c。你只能使用指针
d。你也不能使用任何一个循环。
e。你不能在其中使用任何库函数
除了fgetc()和ungetch()。
write the function
int readINT(FILE* fp);
that read an int from fp and return it.
given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.
e. you cannot use in it any library function
except fgetc() and ungetch().
#include< stdio.h>
#include< assert.h>
#include< ctype.h>
#include< limits.h>
>
#define P printf
#define R return
int readINT(FILE * fp)
{静态int sign = + 1,r = 0,led = 0,max = 0;
int c;
断言(fp!= 0);
R((c = fgetc(fp))==''''|| c ==''\ t'')&& !led?
readINT(fp):
c =='' - ''&& !led?
(sign = -1,readINT(fp)):
!max&& ''0''< = c&& c< =''9''&& (INT_MAX - (c - ''0''))/ 10> r?
((r = 10 * r +(c - ''0'')),led = 1,readINT(fp)):
!max& ;&安培; ''0''< = c&& c< =''9''&& (INT_MAX - (c - ''0''))/ 10< = r?
((max = 1),(r = INT_MAX),readINT(fp)):
c == EOF?
((c = sign * r),(max = led = r = 0),(sign = +1),c):
(ungetc(c,fp),(c = sign * r),(max = led = r = 0),(sign = +1),c);
}
int main(无效)
{int x;
P(" A int>"); fflush(stdout);
x = readINT(stdin);
printf(" x =%d \ n",x);
R 0;
}
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#define P printf
#define R return
int readINT(FILE* fp)
{static int sign = +1, r = 0, led = 0, max = 0;
int c;
assert(fp!=0);
R ((c = fgetc(fp))=='' '' || c==''\t'') && !led ?
readINT(fp) :
c==''-'' && !led ?
(sign = -1, readINT(fp)):
!max && ''0''<=c && c<=''9'' && (INT_MAX - (c - ''0''))/10 > r ?
( (r = 10*r + (c-''0'')), led = 1, readINT(fp) ):
!max && ''0''<=c && c<=''9'' && (INT_MAX - (c - ''0''))/10 <= r ?
((max = 1), (r = INT_MAX), readINT(fp)):
c==EOF ?
( (c = sign*r), (max = led = r = 0), (sign = +1), c):
( ungetc(c, fp), (c = sign*r), (max = led = r = 0), (sign = +1), c);
}
int main(void)
{int x;
P("A int>"); fflush(stdout);
x = readINT(stdin);
printf("x=%d\n", x);
R 0;
}
这篇关于一个谜题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!