C C C
(V
isu
al
C+
+
6.
0
3
(1)
(2)
(3)
(4)
3
(V
isu
al
C+
+
6.
0
3.2
3.3
3.4
3.1
(V
isu
al
C+
+
6.
0 3.1.2
3.1.3
3.1.1 C
3.1
3.1.1 C
(V
isu
al
C+
+
6.
0
3.1
/*l3_1.cpp*/
#include "stdio.h"
main()
{ float a,b,s;
scanf("%f%f",&a,&b);
s=a*b;
printf("The area of rectangle is %5.2f,\n",s);
}
3.0 4.0 < CR >
The area of rectangle is 12.00,
3.1
(V
isu
al
C+
+
6.
0
3.1.2 1
(V
isu
al
C+
+
6.
0
1.
sum=a+b; C3
(1) sum=a+b;
(2)11 i++;
(3) x=1,y=2;
2.;”
3.
"{""}"
{12… n}
{int a=0,b=1,sum;
sum=a+b;
printf("%d",sum);
}
C"}""int a=0,b=1,sum;""{}"
4.
scanf("%d%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
3.1.2 2
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
3.2abababab
/*l3_2.cpp*/
#include "stdio.h"
main()
{ int a,b,t;
printf("Enter a and b:\n");
scanf("%d%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
t=a;a=b;b=t;
printf("a=%d,b=%d\n",a,b);
}
Enter a and b:
123 456 < CR >
a=123,b=456
a=456,b=123
3.1.3 3.2
a b a=b;b=a;a b2378a=b;a78b=a;b78ab78taab
3,3double
/*l3_3.cpp*/
#include "stdio.h"
main()
{ double x;
printf("Input x:\n");
scanf("%lf",&x);
3.1.3 3.3
(V
isu
al
C+
+
6.
0
3.1.3 3,3
(V
isu
al
C+
+
6.
0
printf("(1) x=%f\n",x); /*x*/
x=x*100;
x=x+0.5;
x=(int)x;
x=x/100; /*x1000.5100*/
printf("(2) x=%f\n",x); /*x*/
}
Input x:3.14159< CR >
(1) x=3.141590
(2) x=3.140000
3.1.3 3.4
(V
isu
al
C+
+
6.
0
3.4
/*l3_4.cpp*/
#include "stdio.h"
main()
{ char c1,c2;
scanf("%c",&c1);
printf("The upper case is %c,\n",c1);
c2=c1+'a'-'A';
printf("The lower case is %c,\n",c2);
}
B<CR>
The upper case is B,
The lower case is b,
3.2
(V
isu
al
C+
+
6.
0
3.2.1 C
3.2.2 if
3.2.3 switch
3.2.4
3.2.1 C
(V
isu
al
C+
+
6.
0
3.5a,ba+ba b
/*l3_5.cpp*/
#include "stdio.h"
main()
{ int a,b,s1,s2;
printf("a,b");
scanf("%d,%d",&a,&b);
s1=a+b; s2=a*b; /**/
if(s1>s2)
printf("(a+b)>(a*b) /n"); /*A*/
else
printf(" (a+b)<=(a*b)\n"); /*B*/
}
(V
isu
al
C+
+
6.
0
a,b2,3<CR>
(a+b)<=(a*b)
ifs1>s2A(B),ABifif
3.2.1 C
3.2.2
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
if
1elseif
if(<>) <1>
if(a<b){t=a;a=b;b=t;}
ifCifif""
if ifif ifif
(V
isu
al
C+
+
6.
0
2elseif
if(<>)
<1>
[else
<2>]
<>""<1>""<1><2>if
(V
isu
al
C+
+
6.
0
3.6x≥0 y=2x+1 y=0
#include "stdio.h"
main() /*l3_6.cpp*/
{ int x,y; /*yf(x)*/
printf("x");
scanf("%d",&x);
if(x>=0)
y=2*x+1;
else
y=0;
printf("x=%d,f(x)=%d",x,y);
}
(V
isu
al
C+
+
6.
0
x3<CR>
x=3f(x)=7
x=3x>=02*3+1=7
C if
y=x>=0?2*x+1:0;
,
(V
isu
al
C+
+
6.
0
{}” {}”
(1)if<>
(2)ifelseifelseifififelse
(3)ifelse
(4)<1><2>
(5)ifC
(V
isu
al
C+
+
6.
0 3.10""/*l3_10.cpp*/
#include"stdio.h"
main()
{ int s;
if(s=2)
printf("hello");
else
printf("error");
}
<>/C
(V
isu
al
C+
+
6.
0
3.10s2if(2)printf("hello");,printf("error");if
3.11""
/*l3_11.cpp*/
#include "stdio.h"
main()
{ int x,y; scanf("%d",&x);
if(x)
y=1;
else
y=-1;
(V
isu
al
C+
+
6.
0
printf("y=%d\n",y);
}
if(x)if(x!=0)
if(x)if(x!=0)
if(!x)if(x==0)
(V
isu
al
C+
+
6.
0
3.12a,b
#include "stdio.h,/*l3_12.cpp*/
main()
{ int a,b; printf("a,b") ; scanf("%d%d",&a,&b);
if(a>b)
printf("a>b\n");
else
if(a<b)
printf("a<b\n");
else
printf("a=b\n");
}
(V
isu
al
C+
+
6.
0
if
ifelse
ifCelseelseififelseifelse"{}"
(V
isu
al
C+
+
6.
0
3.13
/*l3_13.cpp*/
#include"stdio.h"
main()
{ int x=4,y=5,z=2,max;
max=x;
if(z>y)
if(z>x)
max=z;
else
if(y>x)
max=y;
printf(" max=%d",max);
}
max=4
(V
isu
al
C+
+
6.
0
3.1490A60E10
#include"stdio.h"
main() /*l3_14.cpp*/
{ int g; printf("input g:");
scanf("%d",&g); printf("g=%d,",g);
if(g>=90)
printf("A\n");
else if(g>=80)
printf("B\n");
else if(g>=70)
printf("C\n");
else if(g>=60)
printf("D\n");
else
printf("E\n");
}
(V
isu
al
C+
+
6.
0
if if if 1if
78 g=78 g>=701"g=78 "Cif
30"g=30""E"if
3.2.3 switch
(V
isu
al
C+
+
6.
0
1 switch
(V
isu
al
C+
+
6.
0
switch
switch(<>)
{case <1><1>
case <2><2>
……
case <n><n>
[default<n+1>]
}
switch switch
switch<><>
(V
isu
al
C+
+
6.
0
case<>case<><>
int x=3,y=7,z;
switch(z)
{ case 1+2,/**/
case x+y,/**/
}
casecase""
defaultcasedefaultswitchdefault
<1><2>
case
1 switch
2 switch
(V
isu
al
C+
+
6.
0
<>switchcasecasedefaultswitchdefaultdefaultswitchdefaultswitch
switchbreak
breakcasebreakbreakswitchswitchbreakswitch
3 switch
(V
isu
al
C+
+
6.
0
3.17
/*l3_17.cpp*/
#include "stdio.h"
main()
{ int button;
printf("========== ==========\n");
printf("1.\n");
printf("2.\n");
printf("3.\n");
printf("4.\n");
printf("5.\n");
printf("1--5:\n");
scanf("%d",&button);
3 switch
(V
isu
al
C+
+
6.
0
switch(button)
{ case 1:printf("\n\n");break;
case 2:printf("\n\n");break;
case 3:printf("\n\n");break;
case 4:printf("\n\n");break;
case 5:printf("\n\n");break;
default:printf("\n!\n");break;
}
}
===========================
1.
2.
3.
4.
5.
1-53<CR>
(V
isu
al
C+
+
6.
0
3.3.2 while
3.3.3 do-while
3.3.1 C
3.3
3.3.4 for
3.3.5
3.3.6 breakcontinue
3.3.7
3.3.1 C
(V
isu
al
C+
+
6.
0
3.190.1
main() /*l3_14.cpp*/
{ double h=0.1; int n=0; /* n*/
while(h<=8848180)
{ h=h*2; n=n+1;
}
printf("%d,%.2f\n",n-1,h/2);
}
21
h=h*2; n=n+1;
……
hwhile
Cwhiledo-whilefor
3.3.2 while
(V
isu
al
C+
+
6.
0
.
while<>
<>
whileC<>C<>"{}""{}"
1while<>23
2<>1
3while
.
while
(V
isu
al
C+
+
6.
0
3.21while1100
/*l3_21.cpp*/
#include "stdio.h"
main() / *sumint*/
{ int sum=0,i=1; while(i<=100)
{ sum+=i;
i++;}
printf(" %d\n",sum);
/*sumld*/
}
5050
do
<>
while<>
doCwhiledo-whiledowhilewhilewhile
3.3.3 do-while
(V
isu
al
C+
+
6.
0
.
.
1do<>
2while<>13
3while
5050
whiledo-while
do-while
(V
isu
al
C+
+
6.
0
3.23do-while1+2+…+100
#include "stdio.h" /*l3_23.cpp*/
main()
{ int sum=0,i=1; / *sumint*/
do
{ sum+=i;
i++;} while(i<=100);
printf(" %d\n",sum);
/*sumld*/
}
3.3.4 for
(V
isu
al
C+
+
6.
0
forC
for
for<1><2><3>
<>
forC""Cfor<1><2><3><>whiledo-whilefor
for<><><>
<>
for<3>while""for<3>1100for
for(sum=0,i=1;i<=n;sum+=i,i++);
.
for
(V
isu
al
C+
+
6.
0
<1>
<2> <2> for
for
<3>
.
3.25for1-100
#include "stdio.h"
main() /*l3_25.cpp
{ int sum=0,i=1;
/ *sumint*/
for
(V
isu
al
C+
+
6.
0
for( ;i<=n;i++)
sum+=i;
printf(" %d\n",sum);
/*sumld*/
}
5050
for1"i<=n""sum+=i","i++","i<=n""i<=n"i
for
(V
isu
al
C+
+
6.
0
Cforfor
1<1>
1-100<1>
2<1><3>
3
for(t=0;t<time;t++);
4
for""
for
<>
3.3.5
(V
isu
al
C+
+
6.
0
forforforwhiledo-while
3.26
1*1=1
2*1=2 2*2=4
……
9*1=9 9*2=18 9*3=27 …… 9*9=81
for(i=1;i<=9;i++)
{ i}
"i"i*=
for(j=1;j <= i;j++)
{ j}
(V
isu
al
C+
+
6.
0
j
printf("%d*%d=%-3d",i,j,i*j);
j
printf("\n");
3.26 /*l3_26.cpp*/
#include "stdio.h"
main()
{ int i,j; /* i,j */
for(i=1;i<=9;i++)
{ for(j=1;j <= i;j++)
printf("%d*%d=%-3d",i,j,i*j);
printf("\n"); /**/
}
}
3.3.6 breakcontinue
(V
isu
al
C+
+
6.
0,
1,break
(V
isu
al
C+
+
6.
0
breakswitchbreak
break
break
3.28break
#include "stdio.h"
main() /*l3_28.cpp*/
{ int i,s=0;
for(i=1;i<=10;i++)
{ s=s+i;
if(s>5) break;
printf("s=%d\n",s);
}
printf("breaks%d\n",s);
}
break
(V
isu
al
C+
+
6.
0
s=1
s=3
breaks6
break10i=3s6ifs>5 1break fors6
break
1switchbreak
2breakswitchbreakswitchbreak
2,Continue
(V
isu
al
C+
+
6.
0
continue
continue;
3.303.28breakcontinue
#include "stdio.h"
main() /*l3_30.cpp*/
{ int i,s=0;
for(i=1;i<=10;i++)
{ s=s+i;
if(s>5) continue;
printf("s=%d\n",s);
}
printf("continues%d\n",s);
}
Continue
(V
isu
al
C+
+
6.
0
s=1
s=3
continues55
i=1,s=1i=2s=3i=3s6ifs>5 1continuefor"3""2""2"continuebreakcontinue
3.3.7 1
(V
isu
al
C+
+
6.
0
3.32
*
***
*****
*******
*********
#include "stdio.h"
main() /*l3_32.cpp*/
{ int i,j;
for(i=1;i<=5;i++)
{ for(j=1;j<=10-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}
3.3.7 2
(V
isu
al
C+
+
6.
0
3.333-100n=3100i=2n/2n
main() /*l3_27.cpp*/
{ int i,n,num;
for(n=3,num=0;n<=100;n++)
{ for(i=2;i<=n-1;i++)
if(n%i==0) break;
if(n==i) /*n=in2n-1n */
{ printf("%4d",n);
num++;
}
} printf("\n%d\n",num);
}
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
97
24
(V
isu
al
C+
+
6.
0
3.4.2
3.4.1
3.4
3.4.1
(V
isu
al
C+
+
6.
0
E.W.Dijkstra60,
C1234ifswitchforwhiledo-whilecontinuebreak
3.4.2 1
(V
isu
al
C+
+
6.
0
3.34e1e-6
e=1+1/1!+1/2!+1/3!+… +1/n!+…
/*l3_34.cpp e*/
#include "stdio.h"
main()
{ float e=1.0,t=1.0; /*etn!,n1*/
int n=1;
while(1/t>1e-6)
{ t=t*n; /*n!*/
e=e+1/t; /* n! */
n++;
}
printf("e=%8.6f\n",e);
}
e=2.718282
3.4.2 2
(V
isu
al
C+
+
6.
0
,t=t*x,s=s+x,t s,t=1,s=0,x
3.35""
cocks henschicks
cocks+hens+chicks=100
5*cocks+3*hens+chicks/3=100
cocks0-19
hens0-33
chicks0-100
cockshenschicks
3.4.2 3
(V
isu
al
C+
+
6.
0
/*l3_35.cpp*/
#include "stdio.h"
main()
{ int cocks,hens,chicks;
for(cocks=1;cocks<=19;cocks++)
for(hens=1;hens<=33;hens++)
{ chicks=100-cocks-hens;
if(5*cocks+3*hens+chicks/3.0==100)
printf("cocks=%d,hens=%d,chicks=%d\n",cocks,hens,chicks");
} }
cocks=4,hens=18,chicks=78
cocks=8,hens=11,chicks=81
cocks=12,hens=4,chicks=84
3.36
213254657
ladders,
1ladders % 2 =1
2ladders % 3 =2
3ladders % 5 =4
4ladders % 6 =5
5ladders % 7 =0
577717212835… 1
77+147+14+14… 234
3.4.2 4
(V
isu
al
C+
+
6.
0
3.4.2 5
(V
isu
al
C+
+
6.
0
/*l3_36.cpp*/
#include "stdio.h"
main()
{ int ladders=7;
while(ladders % 3!=2||ladders % 5!=4||ladders % 6!=5)
ladders+=14;
printf("ladders=%d\n",ladders);
}
ladders=119