4
C C
C
if switch
4.1
4.2
4.3 if
4.4 switch
4.5
──
[Return]
4.1
a > b” >”
a 5 b 3 >”
a 2 b 3
>”
4.1.1
1
C 6
<(),<=(),>(),
>=(),==(),!=()
= =”
=,
2
1 4 2 4
2
2
4.1.2
1
a>b a+b>c-d (a=3)<=(b=5) 'a'>='b' (a>b)= =(b>c)
2 ——
num1=3 num2=4 num3=5
1 num1>num2 =0
2 (num1>num2)!=num3 =1
3 num1<num2<num3 =1
n um1 num2
4 ( num1<num2)+num3 = 6 n um1<num2
=1 1+5=6
C 1” 0”
[Return]
4.2
x>=0”
x>=0” x<10”
4.2.1
1 C
&&
||
!
(x>=0) && (x<10) (x<1) || (x>5) ! (x= =0)
(year%4==0)&&(year%100!=0)||(year%400==0)
2
1 &&
2 ||
3 !
x=5 ( x>=0) && (x<10)
(x<-1) || (x>5)
2
1
→ && → ||
2
→ → → &&→ || →
4.2.2
1
1
C
(year%4==0)&&(year%100!=0)||(year%400==0)
2 ──
n um=12 n um = n um>=1
num<=31 = num || num>31 =1
3
1
2
1
2
n 1 n2 n3 n4 x y 1 2 3
4 1 1 ( x=n1>n2)&&(y=n3>n4)” x
y 1
[Return]
4.3 if
4.3.1 if
[4.1] n u m1 n um2 n um3
/* AL4_1.C */
/* if */
main()
{int num1,num2,num3,max;
printf("Please input three numbers:");
scanf("%d,%d,%d",&num1,&num2,&num3);
if (num1>num2)
max=num1;
else
max=num2;
if (num3>max)
max=num3;
printf("The three numbers are:%d,%d,%d\n",num1,num2,num3);
printf("max=%d\n",max);
}
[]
Please input three numbers:11,22,18
The three numbers are:11,22,18
max=22
1 if else
max=num1;
if(num2>max) max=num2;
max m a x
max max
max 3
3
[4.2] n u m1 n um2 n um3
/* AL4_2.C */
main()
{int num1,num2,num3,temp;
printf("Please input three numbers:");
scanf("%d,%d,%d",&num1,&num2,&num3);
if (num1>num2) {temp=num1;num1=num2;num2=temp;}
if (num2>num3) {temp=num2;num2=num3;num3=temp;}
if (num1>num2) {temp=num1;num1=num2;num2=temp;}
printf("Three numbers after sorted,%d,%d,%d\n",num1,num2,num3);
} []
Please input three numbers:11,22,18
Three numbers after sorted,11,18,22
1 if
if()
{1;}
[else
{2;} ]
1 if (” )”
2 e lse if if
3 if e lse
2 if
1 else
0
1 4 -
1(a)
2 else
0
1
2 4-1(b)
3 if
if if
1” 2” if
if e l se if
if
if
[4.3] 1 y ear 4
4
100 400
1
2 l eap 0
y ear l eap 1
/* AL4_3.C */
/* if */
main()
{int year,leap=0; /* leap=0 */
printf("Please input the year:");
scanf("%d",&year);
if (year % 4==0) {if (year % 100 != 0) leap=1;}
else {if (year%400==0) leap=1; }
if (leap) printf("%d is a leap year.\n",year);
else printf("%d is not a leap year.\n",year);
} []
main()
{int year;
printf("Please input the year:");
scanf("%d",&year);
if ((year%4==0 && year%100!=0)||(year%400==0))
printf("%d is a leap year.\n",year);
else
printf("%d is not a leap year.\n",year);
}
4
1 if
2 if
(2 3 )
3 1,2,
[4.1]
if (num1>num2) max=num1;
else max=num2;
if max=num1;” if
e l se 2 if e l se if
4.3.2
1 1 2 3
1,2,
3”
2
1,0 ( )
2,3,
4-2
3
[4.4]
/* AL4_4.C*/
main()
{ char ch;
printf("Input a character,");
scanf("%c",&ch);
ch=(ch>='A' && ch<='Z')? (ch+32),ch;
printf("ch=%c\n",ch);
} []
[Return]
4.4 switch
C switch
[4.5] s core
score≥90 A 80≤score<90 B 70≤score<80 C
60≤score<70 D score<60 E
/* AL4_5.C */
main()
{int score,grade;
printf(“Input a score(0~100):,);
scanf(“%d”,&score);
grade = score/10; /* 10 switch case */
switch (grade)
{case 10:
case 9,printf(“grade=A\n”); break;
case 8,printf("grade=B\n"); break;
case 7,printf("grade=C\n"); break;
case 6,printf("grade=D\n"); break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0,printf(“grade=E\n”); break;
default,printf(“The score is out of range!\n”);
}
} []
Input a score(0~100),85
grade=B
1 switch
switch( )
{ case 1 break
case 2 break;
......
case break
[default [break; ]]
}
2
1 s witch c a se
c a se
b r eak s witch
switch
2 c a se
d efault
switch
3
1 s witch i nt c har
2 c a se
3 c ase
b r eak
switch
[ 4.5] b r eak
75
4 c ase default
5 case
[ 4.5] case 10:,case 9:,
printf("grade=A\n"); break;” case 5:,~“case 0:,
printf("grade=E\n"); break;”
6 s witch
if if
[Return]
4.5
[4.6] ax2+bx+c=0 a≠0
/* AL4_6.C */
/* */
#include "math.h"
main()
{float a,b,c,disc,x1,x2,p,q;
scanf(“%f,%f,%f”,&a,&b,&c);
disc=b*b-4*a*c;
if (fabs(disc)<=1e-6) /*fabs() */
printf(“x1=x2=%7.2f\n”,-b/(2*a)); /* */
else
{ if (disc>1e-6)
{x1=(-b+sqrt(disc))/(2*a); /* */
x2=(-b-sqrt(disc))/(2*a);
printf("x1=%7.2f,x2=%7.2f\n",x1,x2);
}
else
{p=-b/(2*a); /* */
q=sqrt(fabs(disc))/(2*a);
printf(“x1=%7.2f + %7.2f i\n“,p,q); /* */
printf(”x2=%7.2f - %7.2f i\n“,p,q);
}
}
} []
disc 0 disc
10-6
a b c
disc 0
[ 4.7] 5 0 0
profit
profit≤1000
1000 profit≤2000 10%
2000 profit≤5000 15%
5000 profit≤10000 20%
10000 profit 25%
s witch p rofit
1 0 00
1000 2000 5000 …… profit 1000
profit≤1000 0 1
1000 profit≤2000 1 2
2000 profit≤5000 2 3 4 5
5000 profit≤10000 5 6 7 8 9 10
10000 profit 10 11 12 ……
profit 1 1000
profit≤1000 0
1000 profit≤2000 1
2000 profit≤5000 2 3 4
5000 profit≤10000 5 6 7 8 9
10000 profit 10 11 12 ……
/* AL4_7.C */
main()
{long profit;
int grade;
float salary=500;
printf("Input profit,");
scanf("%ld",&profit);
grade= (profit – 1) / 1000; /* - 1 1 0 00
switch case */
switch(grade)
{ case 0,break; /*profit≤1000 */
case 1,salary += profit*0.1; break; /*1000 profit≤2000 */
case 2:
case 3:
case 4,salary += profit*0.15; break; /*2000 profit≤5000 */
case 5:
case 6:
case 7:
case 8:
case 9,salary += profit*0.2; break; /*5000 profit≤10000 */
default,salary += profit*0.25; /*10000 profit */
}
printf("salary=%.2f\n",salary);
} []
[Return]
──
1
2
3
1
( )
2
C if switch
1 if
/*…… */
if( ) /* */
{……}
else /* */
{……}
2 switch
/*…… */
switch( )
{ case 1 /* */
……
case n /* */
default /* */
}
[Return]