4
CC
Cifswitch
4.1
4.2
4.3 if
4.4 switch
4.5
──
[Return]
4.1
a > b” >”a5b3 >” a2b3 >”
4.1.1
1
C6
<(),<=(),>(),
>=(),==(),!=()
= =” =,
2
14242
2
4.1.2
1
a>ba+b>c-d(a=3)<=(b=5)'a'>='b'(a>b)= =(b>c)
2——
num1=3 num2=4num3=5
1num1>num2 =0
2(num1>num2)!=num3 =1
3num1<num2<num3 =1
num1 num2
4(num1<num2)+num3 =6num1<num2 =11+5=6
C 1” 0”
[Return]
4.2
x>=0” x>=0” x<10”
4.2.1
1C
&&
||
!
(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
1C
(year%4==0)&&(year%100!=0)||(year%400==0)
2──
num=12 num= num>=1 num<=31 = num || num>31 =1
3
1
2
1
2
n1n2n3n4xy123411 (x=n1>n2)&&(y=n3>n4)”xy1
[Return]
4.3 if
4.3.1 if
[4.1] num1num2num3
/*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
1ifelse
max=num1;
if(num2>max) max=num2;
maxmaxmaxmaxmax33
[4.2]num1num2num3
/*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;} ]
1if (” )”
2elseifif
3ifelse
2if
1else
0 14-1(a)
2else
0 124-1(b)
3if
ifif 1” 2”if
ifelseifif
if
[4.3] 1year44100400
1
2leap0yearleap1
/*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
1if
2if(23)
3 1” 2”
[4.1]
if (num1>num2) max=num1;
else max=num2;
if max=num1;”ifelse2ifelseif
4.3.2
1 123
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
Cswitch
[4.5] scorescore≥90A80≤score<90B70≤score<80C60≤score<70D
score<60E
/*AL4_5.C*/
main()
{int score,grade;
printf(“Input a score(0~100):,);
scanf(“%d”,&score);
grade = score/10; /*10switchcase*/
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
1switch
switch( )
{ case 1break
case 2break;
......
case break
[default [break; ]]
}
2
1switch case casebreakswitchswitch
2case default switch
3
1switch intchar
2case
3casebreakswitch
[4.5]break75
4casedefault
5case
[4.5] case 10:,case 9:,printf("grade=A\n");
break;” case 5:,~“case 0:,printf("grade=E\n"); break;”
6switchifif
[Return]
4.5
[4.6] ax2+bx+c=0a≠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);
}
}
} []
disc0disc10-6
abcdisc0
[4.7] 500profit
profit≤1000
1000profit≤2000 10%
2000profit≤5000 15%
5000profit≤10000 20%
10000profit 25%
switchprofit1000100020005000…… profit1000
profit≤1000 01
1000profit≤2000 12
2000profit≤5000 2345
5000profit≤10000 5678910
10000profit 101112……
profit11000
profit≤1000 0
1000profit≤2000 1
2000profit≤5000 234
5000profit≤10000 56789
10000profit 101112……
/*AL4_7.C*/
main()
{long profit;
int grade;
float salary=500;
printf("Input profit,");
scanf("%ld",&profit);
grade= (profit – 1) / 1000; /*-11000 switchcase*/
switch(grade)
{ case 0,break; /*profit≤1000 */
case 1,salary += profit*0.1; break; /*1000profit≤2000 */
case 2:
case 3:
case 4,salary += profit*0.15; break; /*2000profit≤5000 */
case 5:
case 6:
case 7:
case 8:
case 9,salary += profit*0.2; break; /*5000profit≤10000 */
default,salary += profit*0.25; /*10000profit */
}
printf("salary=%.2f\n",salary);
} []
[Return]
──
123
1
()
2
Cifswitch
1 if
/*…… */
if( ) /**/
{……}
else /**/
{……}
2 switch
/*…… */
switch( )
{ case 1 /**/
……
case n /**/
default /**/
}
[Return]
CC
Cifswitch
4.1
4.2
4.3 if
4.4 switch
4.5
──
[Return]
4.1
a > b” >”a5b3 >” a2b3 >”
4.1.1
1
C6
<(),<=(),>(),
>=(),==(),!=()
= =” =,
2
14242
2
4.1.2
1
a>ba+b>c-d(a=3)<=(b=5)'a'>='b'(a>b)= =(b>c)
2——
num1=3 num2=4num3=5
1num1>num2 =0
2(num1>num2)!=num3 =1
3num1<num2<num3 =1
num1 num2
4(num1<num2)+num3 =6num1<num2 =11+5=6
C 1” 0”
[Return]
4.2
x>=0” x>=0” x<10”
4.2.1
1C
&&
||
!
(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
1C
(year%4==0)&&(year%100!=0)||(year%400==0)
2──
num=12 num= num>=1 num<=31 = num || num>31 =1
3
1
2
1
2
n1n2n3n4xy123411 (x=n1>n2)&&(y=n3>n4)”xy1
[Return]
4.3 if
4.3.1 if
[4.1] num1num2num3
/*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
1ifelse
max=num1;
if(num2>max) max=num2;
maxmaxmaxmaxmax33
[4.2]num1num2num3
/*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;} ]
1if (” )”
2elseifif
3ifelse
2if
1else
0 14-1(a)
2else
0 124-1(b)
3if
ifif 1” 2”if
ifelseifif
if
[4.3] 1year44100400
1
2leap0yearleap1
/*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
1if
2if(23)
3 1” 2”
[4.1]
if (num1>num2) max=num1;
else max=num2;
if max=num1;”ifelse2ifelseif
4.3.2
1 123
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
Cswitch
[4.5] scorescore≥90A80≤score<90B70≤score<80C60≤score<70D
score<60E
/*AL4_5.C*/
main()
{int score,grade;
printf(“Input a score(0~100):,);
scanf(“%d”,&score);
grade = score/10; /*10switchcase*/
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
1switch
switch( )
{ case 1break
case 2break;
......
case break
[default [break; ]]
}
2
1switch case casebreakswitchswitch
2case default switch
3
1switch intchar
2case
3casebreakswitch
[4.5]break75
4casedefault
5case
[4.5] case 10:,case 9:,printf("grade=A\n");
break;” case 5:,~“case 0:,printf("grade=E\n"); break;”
6switchifif
[Return]
4.5
[4.6] ax2+bx+c=0a≠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);
}
}
} []
disc0disc10-6
abcdisc0
[4.7] 500profit
profit≤1000
1000profit≤2000 10%
2000profit≤5000 15%
5000profit≤10000 20%
10000profit 25%
switchprofit1000100020005000…… profit1000
profit≤1000 01
1000profit≤2000 12
2000profit≤5000 2345
5000profit≤10000 5678910
10000profit 101112……
profit11000
profit≤1000 0
1000profit≤2000 1
2000profit≤5000 234
5000profit≤10000 56789
10000profit 101112……
/*AL4_7.C*/
main()
{long profit;
int grade;
float salary=500;
printf("Input profit,");
scanf("%ld",&profit);
grade= (profit – 1) / 1000; /*-11000 switchcase*/
switch(grade)
{ case 0,break; /*profit≤1000 */
case 1,salary += profit*0.1; break; /*1000profit≤2000 */
case 2:
case 3:
case 4,salary += profit*0.15; break; /*2000profit≤5000 */
case 5:
case 6:
case 7:
case 8:
case 9,salary += profit*0.2; break; /*5000profit≤10000 */
default,salary += profit*0.25; /*10000profit */
}
printf("salary=%.2f\n",salary);
} []
[Return]
──
123
1
()
2
Cifswitch
1 if
/*…… */
if( ) /**/
{……}
else /**/
{……}
2 switch
/*…… */
switch( )
{ case 1 /**/
……
case n /**/
default /**/
}
[Return]