# include<string.h>. R. F& v3 U0 d/ c
# include<stdio.h>
s! q9 @! f* t, H2 C4 s/ Bcheck(p1,p2,compare)+ U% F" L# k g( j0 L2 e
char *p1,*p2;
% ?* g: U) H! D" Uint (*compare)();
9 ]/ l0 y+ r. @{
2 @" d; k6 f4 \7 I6 _ if(!(*compare)(p1,p2)) Z% t! @. s; e6 ]3 ?
printf("\n The two strings are equal!");& b+ n. S7 h! }; c
else1 G& G$ H' d& `( k/ b8 S. y
printf("\n The two strings are not equal!");* N3 S2 ~. S$ N; C2 h0 d
}5 \3 Y' r( j, J# G6 Y* s7 H* m- U
numcmp(a,b): w" }) |/ J$ ?( G
char *a,*b;
$ O$ C n1 o2 T4 q5 o, P ?{
- o& _# V# Q9 [+ K- W/ o0 `; `0 ]- I if(atoi(a)==atoi(b))" B/ p; t8 ^' D* C) w/ l
return(0);8 G: `& ^: m* _# t5 n7 W
else) j. c% c, }8 S- `2 Q
return(1);
1 h+ w) q- ?# r* i- P# l2 M8 [, @) @}
& ?( S: C) [8 M3 U% Zmain()4 _( g ^. I. T5 x) x- h
{/ ]3 w6 n O5 [$ y
int strcmp(),numcmp();
1 U2 X. R" o# n char str1[100],str2[100];$ w/ A& x9 g" |( E3 x9 o4 B
printf("\n Please enter two strings or numbers:");" a3 o# @! x+ a7 v! M! P7 R
scanf("%s",str1);& P( _) `* {$ F
scanf("%s",str2);( x% d8 @3 ^6 d6 G7 V( g$ S
printf("\n Please enter 'n' for number compare or enter 'c' for strings compare:");& c$ E& i: l& h+ `' S
if(getchar()=='n')7 m7 I$ n0 t7 ?* ^# ?9 m
check(str1,str2,numcmp);
$ i& ~( x# S2 M. l/ d: d else8 I( `* I. U8 T. M. l2 O" ]) F
check(str1,str2,strcmp);
0 B; y% @, |, f! S: I5 R}
/ w* J2 Y$ s8 O7 B
: m) P" y( D5 a8 \% n 这个程序的题目:检查两个输入数或字符串是否相等。/ Z$ r) f1 }: C+ N T$ y
其中
5 l$ S1 _3 L7 ~# G4 \( h; ]% knumcmp(a,b)8 X6 T5 \# A/ H" {/ H/ I4 g2 j
char *a,*b;
) i4 W: F5 c( k( R+ `{9 E" r* }- U: w; G( p& x* v) R x
if(atoi(a)==atoi(b))2 a1 B! w7 y i- y$ |
return(0);
% M ~, P1 G% f; f else' n. i4 ]3 p2 P% a( P
return(1);
8 @4 g! ~/ g; x7 b, h}
! D% J' V/ {# U- a- m这是一段数比较函数,那么a,b都应该是数,比方说一个2,一个3,为什么这两个又是指针?那么atoi(a)的意思,: [ T0 E5 E3 p: |
就是把a指针所指的那个数变换,然后进行比较。可是a并没有所指的数呀?
1 D, R# u, Q0 g4 ~' Y; t况且这是数的比较,为什么要用atoi函数(字符转数字)?4 O6 l( q9 H2 b9 D" t3 f+ b
还有,整个程序运行不能正确运行,估计是: {6 [$ K' g% H$ B$ F( v. f. Z
main()
3 _3 |1 w3 t) x: t- |. b{
7 V% q4 w9 T. j5 s) ~ int strcmp(),numcmp();0 l5 D$ w: f/ ]+ q7 v
char str1[100],str2[100];
: R: M$ A8 Z2 v. a% R printf("\n Please enter two strings or numbers:");& o2 h4 o* j7 U* j
scanf("%s",str1);
! r/ M* N% j0 G/ R9 V- h% U1 @$ ~ scanf("%s",str2);
8 }% @' M& o9 V' b printf("\n Please enter 'n' for number compare or enter 'c' for strings compare:"); O) V! q+ G& H
if(getchar()=='n')
|- f! r4 c; j6 g+ S check(str1,str2,numcmp);
) X; c3 H8 v' U- H else
x7 D8 A* {2 C check(str1,str2,strcmp);% }% _' Y$ h! g- `. i
}3 Q- |2 J+ M1 L+ f/ r' d: R
这一段的毛病,+ ?1 P$ e( l J, J
printf("\n Please enter 'n' for number compare or enter 'c' for strings compare:");
- W) Y* ?7 S) k$ w9 w" c if(getchar()=='n')
! n; H g; t/ F# B1 _1 S( F% k根本就没有机会输入n or c?% \ ?% A1 P3 j6 K
请各位大哥挑一下这个程序的毛病?