C primer Plus Chap 06
C primer Plus Chap 06
C primer Plus 第六章代码与练习记录。
C语言控制语句:循环
- fx1.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12#include <stdio.h>
int main(void)
{
int quack = 2;
// printf("%d\n", quack+=5);
// printf("%d\n", quack*=5);
// printf("%d\n", quack-=6);
// printf("%d\n", quack/=8);
printf("%d\n" ,quack%=3);
return 0;
}1
2
- fx2.c输出结果:
1
2
3
4
5
6
7
8
9
10
11#include <stdio.h>
int main(void)
{
// int value;
double value;
for(value=36; value>0; value/=2)
printf("%10.2f\t", value);
printf("\n");
return 0;
}1
36.00 18.00 9.00 4.50 2.25 1.12 0.56 0.28 0.14 0.07 0.04 0.02 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
- fx6.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12#include <stdio.h>
int main(void)
{
int i, j;
for(i=1; i<=4; i++)
{
for(j=1; j<=8; j++)
printf("$");
printf("\n");
}
return 0;
}1
2
3
4$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$ - fx7a.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <stdio.h>
int main(void)
{
int i = 0;
while(++i<4)
{
printf("%d",i);
printf("hi!\t");
}
printf("\n");
do
printf("bye!\t");
while(i++<8);
printf("\n");
return 0;
}1
21hi! 2hi! 3hi!
bye! bye! bye! bye! bye! - fx7b.c输出结果:
1
2
3
4
5
6
7
8
9
10
11#include <stdio.h>
int main(void)
{
int i;
char ch;
for(i=0,ch='A'; i<4; i++, ch+=2*i)
printf("%c", ch);
printf("\n");
return 0;
}1
ACGM
- fx8a.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
while(ch != 'g')
{
printf("%c", ch);
scanf("%c", &ch);
}
printf("\n");
return 0;
}1
2
3
4
5
6
7123
123
abc
abc
def
def
^C - fx8b.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
while(ch!='g')
{
printf("%c", ++ch);
scanf("%c", &ch);
}
printf("\n");
return 0;
}1
2
3
4
5
6
7123
234
asd
bte
qwer
rxfs
^C - fx8c.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
int main(void)
{
char ch;
do
{
scanf("%c", &ch);
printf("%c", ch);
}while(ch!='g');
printf("\n");
return 0;
}1
2
3
4qqww
qqww
g
g - fx8d.c输出结果:
1
2
3
4
5
6
7
8
9
10#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
for(ch='$'; ch!='g'; scanf("%c", &ch))
putchar(ch);
return 0;
}1
2change
$han% - fx9.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <stdio.h>
int main(void)
{
int n, m;
n = 30;
while(++n <= 33)
printf("%d|", n);
n = 30;
do
printf("%d|", n);
while(++n<=33);
printf("%d", n);
printf("\n***\n");
for(n=2, m=6; n<m; n*=2, m+=2)
printf("%d %d\n", n, m);
printf("\n***\n");
for(n=5; n>0; n--)
{
for(m=0; m<=n; m++)
printf("=");
printf("\n");
}
return 0;
}1
2
3
4
5
6
7
8
9
10
11
1231|32|33|30|31|32|33|34
***
2 6
4 8
8 10
***
======
=====
====
===
== - other.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h>
int main(void)
{
int i, j;
printf("-------九九乘法表-------\n");
for(i=1; i<=9; i++)
{
for(j=1; j<=i; j++)
printf("%2d*%2d=%2d ", j, i, j*i);
printf("\n");
}
return 0;
}1
2
3
4
5
6
7
8
9
10-------九九乘法表-------
1* 1= 1
1* 2= 2 2* 2= 4
1* 3= 3 2* 3= 6 3* 3= 9
1* 4= 4 2* 4= 8 3* 4=12 4* 4=16
1* 5= 5 2* 5=10 3* 5=15 4* 5=20 5* 5=25
1* 6= 6 2* 6=12 3* 6=18 4* 6=24 5* 6=30 6* 6=36
1* 7= 7 2* 7=14 3* 7=21 4* 7=28 5* 7=35 6* 7=42 7* 7=49
1* 8= 8 2* 8=16 3* 8=24 4* 8=32 5* 8=40 6* 8=48 7* 8=56 8* 8=64
1* 9= 9 2* 9=18 3* 9=27 4* 9=36 5* 9=45 6* 9=54 7* 9=63 8* 9=72 9* 9=81 - xt1.c输出结果:
1
2
3
4
5
6
7
8
9#include <stdio.h>
int main(void)
{
int i;
for(i=1; i<=26; i++)
printf("%c\t", 96+i);
printf("\n");
return 0;
}1
a b c d e f g h i j k l m n o p q r s t u v w x y z
- xt1a.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h>
int main(void)
{
int i=1, ch[26];
while(i<=26)
{
ch[i]=i+96;
printf("%c\t", ch[i]);
i++;
}
printf("\n");
return 0;
}1
a b c d e f g h i j k l m n o p q r s t u v w x y z
- xt2.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
int main(void)
{
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
printf("$");
printf("\n");
}
return 0;
}1
2
3
4
5$
$$
$$$
$$$$
$$$$$ - xt3.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
int main(void)
{
int i, j;
for(i=1; i<=6; i++)
{
for(j=1; j<=i; j++)
printf("%c", 71-j);
printf("\n");
}
return 0;
}1
2
3
4
5
6F
FE
FED
FEDC
FEDCB
FEDCBA - xt4.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <stdio.h>
int main(void)
{
int i, j, k;
printf("please enter a number:\n");
scanf("%d", &i);
for(j=1; j<=i; j++)
{
for(k=1; k<=i+1-j; k++)
printf(" ");
for(k=1; k<=j; k++)
printf("%c", k+64);
for(k=1; k<=j-1; k++)
printf("%c", 64+j-k);
printf("\n");
}
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
1312
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
ABCDEFGHIJKJIHGFEDCBA
ABCDEFGHIJKLKJIHGFEDCBA - xt5.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <stdio.h>
int main(void)
{
int ulimt, dlimt;
do
{
printf("please enter the number of downlimt and uplimt(uplimt < downlimt):\n");
scanf("%d %d", &dlimt, &ulimt);
}while(dlimt>=ulimt);
printf("%d %d\n", dlimt, ulimt);
while(dlimt<=ulimt)
{
printf("%d is square=%d and cubed=%d.\n", dlimt, dlimt*dlimt, dlimt*dlimt*dlimt);
dlimt++;
}
printf("\n");
return 0;
}1
2
3
4
5
6
7
8
9please enter the number of downlimt and uplimt(uplimt < downlimt):
3
7
3 7
3 is square=9 and cubed=27.
4 is square=16 and cubed=64.
5 is square=25 and cubed=125.
6 is square=36 and cubed=216.
7 is square=49 and cubed=343. - xt6.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h>
#include <string.h>
int main(void)
{
char ch[10];
int i;
printf("please enter a word:\n");
scanf("%s", ch);
printf("%s\n", ch);
for(i=strlen(ch)-1; i>=0; i--)
printf("%c", ch[i]);
printf("\n");
return 0;
}1
2
3
4please enter a word:
hello
hello
olleh - xt7.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h>
int main(void)
{
float number1, number2;
printf("please enter two float number:\n");
while(scanf("%f %f", &number1, &number2)==2)
{
printf("%f\n", (number1-number2)/(number1*number2));
printf("please agin(q to exit the program):\n");
}
printf("the end \n");
return 0;
}1
2
3
4
5
6please enter two float number:
12.3 3.14
0.237171
please agin(q to exit the program):
q
the end - xt8.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <stdio.h>
#include <float.h>
float result(float x, float y);
int main(void)
{
float number1, number2;
printf("please enter two number:\n");
while(scanf("%f %f", &number1, &number2)==2)
{
printf("(%f-%f)/(%f*%f)=%f\n", number1,number2,number1,number2,result(number1, number2));
printf("please try agin(q to exit):\n");
}
return 0;
}
float result(float x, float y)
{
return((x-y)/(x*y));
}1
2
3
4
5please enter two number:
12 4
(12.000000-4.000000)/(12.000000*4.000000)=0.166667
please try agin(q to exit):
q - xt9.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <stdio.h>
int result(int x, int y);
int main(void)
{
int lower, upper;
printf("please enter lower and upper limits:");
while(scanf("%d %d", &lower, &upper)==2)
{
printf("the sums of the squares form %d to %d is %d\n", lower, upper, result(lower, upper));
printf("enter the next set of limits:");
scanf("%d %d", &lower, &upper);
}
printf("the end.\n");
return 0;
}
int result(int x, int y)
{
int sums=0;
while(x<y)
{
sums = sums + x*x;
x++;
}
return sums;
}1
2
3
4please enter lower and upper limits:1 4
the sums of the squares form 1 to 4 is 14
enter the next set of limits:2 5
^C - xt10.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <stdio.h>
int main(void)
{
int i, number[10];
printf("please enter ten number:\n");
for(i=1; i<=10; i++)
{
scanf("%d", &number[i]);
}
printf("input complated\n");
for(i=10; i>=1; i--)
printf("%d ",number[i]);
printf("\n");
return 0;
}1
2
3
4please enter ten number:
1 2 3 4 5 6 7 8 9 10
input complated
10 9 8 7 6 5 4 3 2 1 - xt11.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <stdio.h>
int main(void)
{
double sum1=0, sum2=0;
int i, count, sign=1;
printf("please enter a number:\n");
scanf("%d", &count);
for(i=1.0; i<=count; i++, sign*=-1)
{
sum1+=1.0/i;
sum2+=sign/i;
}
printf("1+1/2+1/3+1/4...=%f\n", sum1);
printf("1-1/2+1/3-1/4...=%f\n", sum2);
return 0;
}1
2
3
4please enter a number:
6
1+1/2+1/3+1/4...=2.450000
1-1/2+1/3-1/4...=1.000000 - xt12.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13#include <stdio.h>
#include <math.h>
int main(void)
{
double i=0;
do
{
printf("%.0f\t", pow(2.0,i));
}while(++i<8);
printf("\n");
return 0;
}1
1 2 4 8 16 32 64 128
- xt12a.c输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <stdio.h>
int main(void)
{
int num[8], i;
for(i=0; i<8; i++)
num[i]=2*i;
i=0;
do
{
printf("%d\t", num[i]);
}while(i++<8);
printf("\n");
return 0;
}1
0 2 4 6 8 10 12 14 0
C primer Plus Chap 06
https://ywmy.xyz/2022/05/14/C-primer-Plus-Chap-06/