C语言进阶-C程序控制
9unk Lv5

课上代码

fig04_01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*程序名:fig04_01.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int counter = 1;

while (counter <= 10)
{
printf("%d\n", counter);
++counter;
}

return 0; //程序结束
}

fig04_02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*程序名:fig04_02.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int counter;

for (counter = 1; counter <= 10; counter++)
{
printf("%d\n", counter);
}

return 0; //程序结束
}

fig04_05

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*程序名:fig04_05.c*/
/*
使用 for 循环求和;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int sum = 0;
int number;

for (number = 2; number <= 100; number += 2)
{
sum += number;
}

printf("Sum is %d\n", sum);

return 0; //程序结束
}

fig04_06

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
/*程序名:fig04_06.c*/
/*
计算账号10年中,每年的存款总额。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
double amount;
double principal = 1000.0;
double rate = .05;
int year;

printf("%4s%21s\n", "Year", "Amount on deposit");

for (year = 1; year <= 10; year++)
{
amount = principal * pow(1.0 + rate, year);

printf("%4d%21.2f\n", year, amount);
}

return 0; //程序结束
}

fig04_07

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*程序名:fig04_07.c*/
/*
统计不同档次考试成绩的学生人数。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int grade;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;

printf("Enter the letter grades.\n");
printf("Enter the EOF character to end input.\n");

while ((grade = getchar()) != EOF)
{
switch (grade)
{
case 'A':
case 'a':
++aCount;
break;

case 'B':
case 'b':
++bCount;
break;

case 'C':
case 'c':
++cCount;
break;

case 'D':
case 'd':
++dCount;
break;

case 'F':
case 'f':
++fCount;
break;

case '\n':
case '\t':
case ' ':
break;

default:
printf("Incorrect letter grade entered.");
printf("Enter a new grade.\n");
break;
}
}

printf("\nTotals for each letter grade are:\n");
printf("A: %d\n", aCount);
printf("B: %d\n", bCount);
printf("C: %d\n", cCount);
printf("D: %d\n", dCount);
printf("F: %d\n", fCount);
return 0; //程序结束
}

fig04_09

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*程序名:fig04_09.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int counter = 1;

do
{
printf("%d ", counter);
} while (++counter <= 10);

return 0; //程序结束
}

fig04_11

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
/*程序名:fig04_11.c*/
/*
使用 break 语句跳出循环。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int x;

for (x = 1; x <= 10; x++)
{
if (x == 5)
{
break;
}

printf("%d ", x);
}

printf("\nBroke out of loop at x == %d\n", x);
return 0; //程序结束
}

fig04_12

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
/*程序名:fig04_12.c*/
/*
使用 continue 语句略过printf语句,并开始下一轮循环。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int x;

for (x = 1; x <= 10; x++)
{
if (x == 5)
{
continue;
}

printf("%d ", x);
}

printf("\nUsed continue to skip printing the value 5\n", x);
return 0; //程序结束
}

continue 是 jmp 指令,默认跳转循环语句的开头。[continue 标号] 使用标号,指定跳转到任意位置。

课后练习

1
2
3
4
5
6
7
8
9
10
11

4.8

打印x列y行,以 “@” 符号形成的矩形。

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
30
31
/*程序名:4.8.c*/
/*
打印x列y行,以 "@" 符号形成的矩形。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int x;
int y;
int i;
int j;

printf("Enter two integers n the range 1-20:");
scanf("%d%d", &x, &y);

for (i = 1; i <= y; i++)
{
for (j = 1; j <= x; j++)
{
printf("@");
}
printf("\n");
}

printf("\nUsed continue to skip printing the value 5\n", x);
return 0; //程序结束
}

4.9

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
/*程序名:4.9.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int x=0;
int y=0;

printf("Enter the integers:");
scanf("%d", &x);

for (int i = 0; i < x; i++)
{
scanf("%d", &y);
}

printf("\n\"%d\" 表示以上序列有%d个整数。\n", x, x);
return 0; //程序结束
}

4.10

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
/*程序名:4.10.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int x=0;
int i=0;
int sum = 0;

printf("Enter the integers:");

for (; x != 9999; i++)
{
scanf("%d", &x);
if (x != 9999)
{
sum += x;
}
}
i--;
printf("\n%d 个整数的平均值是%d。\n", i, sum/i);
return 0; //程序结束
}

4.11

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
30
31
32
33
34
35
36
37
38
39
/*程序名:4.11.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int x;
int i;
int temp = 0;
int min;

printf("Enter the integers:");
scanf("%d", &x);

for (i=0; i<x; i++)
{

if (i == 0)
{
scanf("%d", &min);
}
else
{
scanf("%d", &temp);
if (temp < min)
{
min = temp;
}
}
}

printf("\n%d 个整数中最小值是%d。\n", x, min);
return 0; //程序结束
}

4.12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*程序名:4.12.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int i;
int sum=0;

for (i=2; i<=30; i+=2)
{
sum += i;
}

printf("\n2~30的偶数的和是%d。\n", sum);
return 0; //程序结束
}

4.13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*程序名:4.13.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int i;
int sum=1;

for (i=1; i<=15; i+=2)
{
sum *= i;
}

printf("\n1~15的奇数的乘积是%d。\n", sum);
return 0; //程序结束
}

4.14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*程序名:4.14.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
long long total = 1;

for (int i = 1; i <= 20; i++)
{
total *= i;
printf("%d的阶乘=%lld\n", i, total);

}

printf("%d\n", sizeof(long long));
return 0; //程序结束
}

int 变量只有4字节,存储不了20阶乘这样大的数字。

4.15

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
30
31
32
/*程序名:4.15.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
double amount;
double principal = 1000.0;
double rate;
int year;

printf("%4s%21s\n", "Year", "Amount on deposit");

for (rate = .05; rate <= .1; rate += .01)
{
for (year = 1; year <= 10; year++)
{
amount = principal * pow(1.0 + rate, year);
printf("%4d%21.2f\n", year, amount);
}
printf("\n");
}


return 0; //程序结束
}

4.16

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*程序名:4.16.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
for (int i = 1; i <= 10; i++)
{
for (int j = 0; j < i; j++)
{
printf("*");
}
printf("\n");
}

printf("\n");
for (int i = 10,k=1; i >= 1; i--,k++)
{
for (int j = 0; j < i; j++)
{
printf("*");
}

for (int l = 0; l < k; l++)
{
printf(" ");
}

for (int j = 0; j < i; j++)
{
printf("*");
}

for (int l = 0; l < i; l++)
{
printf(" ");
}

for (int j = 0; j < k; j++)
{
printf("*");
}

printf("\n");
}




return 0; //程序结束
}

4.17

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*程序名:4.17.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
int info[3][3] = { 0 };
int account;
int credit;
int balance;

//输入的原数据
for (int i = 0; i < 3; i++)
{
printf("请输入客户账号:");
scanf("%d", &account);

printf("请输入顾客原信贷余额:");
scanf("%d", &credit);

printf("请输入顾客当前余额:");
scanf("%d", &balance);

info[i][0] = account;
info[i][1] = credit;
info[i][2] = balance;

printf("\n");
}

//输出的新数据
printf("个用户更新数据:\n");
for (int i = 0; i < 3; i++)
{
info[i][1] = info[i][1] / 2;

printf("账号:%d\n",info[i][0]);
printf("信贷余额:%d\n",info[i][1]);
printf("当前余额:%d\n", info[i][2]);

if (info[i][2] > info[i][1])
{
printf("当前余额已超出信贷限额。\n");
}

printf("\n");
}

return 0; //程序结束
}

4.18

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
/*程序名:4.18.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
int number=0;

//输入的原数据
for (int i = 0; number != -1; i++)
{
printf("请输入一个数字(-1 to END):");
scanf("%d", &number);

for (int j = 0; j < number; j++)
{
printf("*");
}
printf("\n");
}
return 0; //程序结束
}

4.19

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*程序名:4.19.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
int number;
int number1 = 0;
double sum = 0;

for (int i=0;i < 7;i++)
{
number = 0;
while (number != -1)
{
printf("请输入产品号1~5(END to -1):");
scanf("%d", &number);

if (number == -1)
{
break;
}
printf("请输入%d天的销售数量:", i+1);
scanf("%d", &number1);

//输入的原数据
switch (number)
{
case 1: sum += 2.98 * number1; break;
case 2: sum += 4.5 * number1; break;
case 3: sum += 9.98 * number1; break;
case 4: sum += 4.49 * number1; break;
case 5: sum += 6.87 * number1; break;
}
}
printf("\n");
}


printf("上周出售商品的总价值 %lf",sum);
return 0; //程序结束
}

4.20

条件1 条件2 条件1&&条件2
0 0 0
0 非零值 0
非零值 0 0
非零值 非零值 1
条件1 条件2 条件1||条件2
0 0 0
0 非零值 1
非零值 0 1
非零值 非零值 1
条件1 !条件1
0 1
非零值 0

4.21

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*程序名:4.21.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int counter = 1;

for (; counter <= 10; counter++)
{
printf("%d\n", counter);
}

return 0; //程序结束
}

4.22

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*程序名:4.22.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int grade;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;

int total = 0;
int count = 0;
printf("请输入学生成绩等级。\n");
printf("请输入EOF结束符终止输入。\n");

while ((grade = getchar()) != EOF)
{
switch (grade)
{
case 'A':
case 'a':
++aCount;
break;

case 'B':
case 'b':
++bCount;
break;

case 'C':
case 'c':
++cCount;
break;

case 'D':
case 'd':
++dCount;
break;

case 'F':
case 'f':
++fCount;
break;

case '\n':
case '\t':
case ' ':
break;

default:
printf("输入错误,请输入新的成绩。\n");
break;
}
}

count = aCount + bCount + cCount + dCount + fCount;
total = aCount * 'A' + bCount * 'B' + cCount * 'C' + dCount * 'D' + fCount * 'F';

printf("\n输出学生成绩等级统计结果。\n");
printf("A: %d\n", aCount);
printf("B: %d\n", bCount);
printf("C: %d\n", cCount);
printf("D: %d\n", dCount);
printf("F: %d\n", fCount);

return 0; //程序结束
}

4.23

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
/*程序名:4.23.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
float amount;
int principal = 100000;
double rate = .05;
int year,amount_int,amount_dec;

printf("%4s%21s\n", "Year", "Amount on deposit");

for (year = 1; year <= 10; year++)
{
amount = principal * pow(1.0 + rate, year);
amount_int = (int)amount / 100;
amount_dec = (int)amount % 100;
printf("%4d%21d.%d\n", year, amount_int,amount_dec);
}

return 0; //程序结束
}

4.24

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
/*程序名:4.24.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
int i = 1, j = 2, k = 3, m = 2;
printf("%d\n", i == 1);
printf("%d\n", j == 3);
printf("%d\n", i >= 1 && j < 4);
printf("%d\n", m <= 99 && k < m);
printf("%d\n", j >= i && k == m);
printf("%d\n", k + m < j || 3 - j >= k);
printf("%d\n", !m);
printf("%d\n", !(j - m));
printf("%d\n", !(k > m));
printf("%d\n", !(j < k));
return 0; //程序结束
}

4.25

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
30
31
32
33
34
35
/*程序名:4.25.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
int bin[9] = { 0 };
printf("分别打印1~256二进制、八进制、十六进制数:\n");
for (int i = 1; i <= 256; i++)
{
for (int j=i,k = 0;k < 9; j /= 2,k++)
{
bin[k] = j % 2;
}

//打印二进制
for (int m = 8; m >= 0; m--)
{
printf("%d", bin[m]);
}

//打印八进制
printf("\n%o", i);

//打印十六进制
printf("\n%x\n\n",i);
}
return 0; //程序结束
}

4.26

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
30
31
32
33
34
35
36
37
38
/*程序名:4.26.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
double pi = 0.00;
double temp = 4.00;
double result = 0.00;

for (int i=1,k=1; pi != 3.141592;i+=2,k++)
{

if (k % 2 == 0)
{
pi -= temp / i;
}
else
{
pi += temp / i;
}

if ((int)(pi*100) == 314)
{
printf("使用第%d项:4/%d计算得出结果。", k,i);
break;
}

}

return 0; //程序结束
}

4.27

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
30
/*程序名:4.27.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>


int main(void)
{
//其中一条直角边
for (int i = 1; i < 500; i++)
{
//第二条直角边
for (int j = 1;j < 500; j++)

//第三条斜边
for (int k = 1; k < 500; k++)
{
if (i * i + j * j == k * k)
{
printf("长为 %3d %3d %3d 的三条边可构成直角三角形。\n", i, j, k);
}
}
}

return 0; //程序结束
}

4.28

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*程序名:4.28.c*/
/*
略。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int n;
int hour;
int count;
int sum;

printf("请输入要计算的员工类型编码:");
switch (getchar())
{
case 1:
printf("经理这周固定工资%d",n);
break;
case 2:
printf("请输入这周员工工作时间:");
scanf("%d", &hour);
if (hour > 40)
{
printf("计时工这周工资%d", n + n * (hour - 40) * 1.5);
}
else
{
printf("计时工这周工资%d", n);
}
break;
case 3:
printf("请输入这周的销售量:");
scanf("%d", &sum);

printf("任务工这周的工资%lf", 250 + sum * 0.057);
break;
case 4:
printf("请输入这周员工生产产品数量:");
scanf("%d", &count);

printf("计件工这周的工资%lf", count * 0.5);
}

return 0; //程序结束
}

4.29

1
2
3
4
5
6
7
8
9
10
11
a)!(x<5) && !(y>=7)
!((x<5) && (y>=7))

b)!(a == b) || !(g != 5)
!((a == b) || (g != 5))

c)!((x <= 8) && (y > 4))
!(x <= 8) && !(y > 4)

d)!((i >4 ) || (j <= 6))
!(i >4) || !(j <= 6)

4.30

4.30a

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*程序名:4.30a.c*/
/*
使用嵌套 if...else... 语句修改4.7
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int grade;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;

printf("Enter the letter grades.\n");
printf("Enter the EOF character to end input.\n");

while ((grade = getchar()) != EOF)
{
//getchar() 函数会依次读取输入缓冲区字符(包括换行符)
//例如:输入 【s+回车】,此时缓冲区会存储【s+'\n'】。因此这里使用 continue 跳过对换行符的判断循环
if (grade == '\n')
{
continue;
}
//if语句中如果要对 A-Z 和 a-z 中的一个字母做判断,需要分别跟在对应的大写或小写的判断表达式后
if ((grade != 'E') && (('A' <= grade)&& (grade <= 'F')) || (('a' <= grade)&&(grade <= 'f')) && (grade != 'e'))
{
if ((grade == 'A') || (grade == 'a'))
{
++aCount;
}
else
{
if ((grade == 'B') || (grade == 'b'))
{
++bCount;
}
else
{
if ((grade == 'C') || (grade == 'c'))
{
++cCount;
}
else
{
if ((grade == 'D') || (grade == 'd'))
{
++dCount;
}
else
{
if ((grade == 'F') || (grade == 'f'))
{
++fCount;
}
}
}
}
}
}
else
{
printf("Incorrect letter grade entered.\n");
printf("Enter a new grade.\n");
}
}



printf("\nTotals for each letter grade are:\n");
printf("A: %d\n", aCount);
printf("B: %d\n", bCount);
printf("C: %d\n", cCount);
printf("D: %d\n", dCount);
printf("F: %d\n", fCount);
return 0; //程序结束
}

4.30b

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*程序名:4.30b.c*/
/*
使用非嵌套 if 语句修改4.7
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int grade;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;

printf("Enter the letter grades.\n");
printf("Enter the EOF character to end input.\n");

while ((grade = getchar()) != EOF)
{
if (grade == '\n')
{
continue;
}
if ((grade == 'A') || (grade == 'a'))
{
++aCount;
}
else if ((grade == 'B') || (grade == 'b'))
{
++bCount;
}
else if ((grade == 'C') || (grade == 'c'))
{
++cCount;
}
else if ((grade == 'D') || (grade == 'd'))
{
++dCount;
}
else if ((grade == 'F') || (grade == 'f'))
{
++fCount;
}
else
{
printf("Incorrect letter grade entered.\n");
printf("Enter a new grade.\n");
}
}



printf("\nTotals for each letter grade are:\n");
printf("A: %d\n", aCount);
printf("B: %d\n", bCount);
printf("C: %d\n", cCount);
printf("D: %d\n", dCount);
printf("F: %d\n", fCount);
return 0; //程序结束
}

4.31

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
30
31
32
33
34
35
36
37
38
39
/*程序名:4.31.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
for (int i = 1,k=0; i <= 9; i+=2,k++)
{
for (int j = 4-k; j >= 0; j--)
{
printf(" ");
}
for (int m = 1; m <= i; m++)
{
printf("*");
}
printf("\n");
}

for (int i = 9, k = 0; i >=1; i -= 2, k++)
{
if (i == 9) continue;
for (int j = k; j > 0; j--)
{
printf(" ");
}
for (int m = 1; m <= i; m++)
{
printf("*");
}
printf("\n");
}
return 0; //程序结束
}

4.32

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*程序名:4.32.c*/
/*
略;
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
int line = 0;
printf("请输入要输出菱形的行数:");
scanf("%d", &line);

for (int i = 1,k=0; i <= line; i+=2,k++)
{
for (int j = (line/2-1)-k; j >= 0; j--)
{
printf(" ");
}
for (int m = 1; m <= i; m++)
{
printf("*");
}
printf("\n");
}

for (int i = line, k = 0; i >=1; i -= 2, k++)
{
if (i == line) continue;
for (int j = k; j > 0; j--)
{
printf(" ");
}
for (int m = 1; m <= i; m++)
{
printf("*");
}
printf("\n");
}
return 0; //程序结束
}

4.33

12
13

罗马数字对照表

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*程序名:4.33.c*/
/*
请编写一个程序,打印出一个与1到100的所有十进制数等值的罗马数字表。
*/
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")

#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>

int main(int argc, char* argv[])
{
int loopnum = 0;
char str1[10][5] = {"I" ,"II","III","IV","V","VI","VII","VIII","IX","X"};

for (int i = 0; i < 101; i++)
{
if (i < 10)
{
puts(str1[i]);
}
else if (10 < i && i <= 20)
{
printf("X");
printf("%s\n",str1[loopnum]);
loopnum++;
if (loopnum == 10)
{
loopnum = 0;
}
}
else if (20 < i && i <= 30)
{
printf("XX");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 10)
{
loopnum = 0;
}
}
else if (30 < i && i < 40)
{
printf("XXX");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 9)
{
loopnum = 0;
}
}
else if (i == 40)
{
printf("XL\n");
}
else if (40 < i && i < 50)
{
printf("XL");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 9)
{
loopnum = 0;
}
}
else if (i == 50)
{
printf("L\n");
}
else if (50 < i && i < 59)
{
printf("L");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 8)
{
loopnum = 0;
}
}
else if (i == 59)
{
printf("LVIX\n");
}
else if (i == 60)
{
printf("LX\n");
}
else if (60 < i && i <= 70)
{
printf("LX");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 10)
{
loopnum = 0;
}
}
else if (70 < i && i <= 80)
{
printf("LXX");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 10)
{
loopnum = 0;
}
}
else if (70 < i && i < 90)
{
printf("LXXX");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 9)
{
loopnum = 0;
}
}
else if (i == 90)
{
printf("XC\n");
}
else if (90 < i && i < 100)
{
printf("XC");
printf("%s\n", str1[loopnum]);
loopnum++;
if (loopnum == 9)
{
loopnum = 0;
}
}
else if (i == 100)
{
printf("C\n");
}

}

return 0; //程序结束
}

4.34

while 是在循环开始测试循环条件,而 do..while 是在循环结尾测试循环条件。

当判断条件不成立时 do…while 语句会比 while 多执行一次语句块,要保证两条语句输出相同,可以将while循环条件加 1 。

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
30
31
32
33
34
35
/*程序名:4.34.c*/
/*
略。
*/
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")

#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>

int main(int argc, char* argv[])
{
int a = 1;

do
{
printf("a");
a++;
} while (a < 1);

printf("\n---------------------------------\n");

a = 1;
//while (a < 1)
while (a < 2)
{
printf("a");
a++;
}

return 0; //程序结束
}

4.35

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*程序名:4.35.c*/
/*
请编写一个程序,输入从1994年到1999年的年份,使用for循环输出日历,注意闰年情况。
*/
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")

#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>

//判断该年份的1月1日是周几
int spaces(int year)
{
//以 1949 年引入公历时间为准,当年1月1日为星期六
int week = 6;

//flag 标记这一年日期是否要多加1天
int flag = 0;

for (int i = 1949; i < year; i++)
{
//每闰一年,下一年会多增加一天
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
{
//下一年
if (week > 7)
{
week -= 7;
}
else
{
week++;
}

flag = 1;

}
else
{
if (flag == 1)
{
week += 2;
flag = 0;
}
else
{
week++;
}

if (week > 7)
{
week -= 7;
}
}
}

return week;
}

//返回每个月的天数
int month_days(int year,int month)
{
int n = 0;
switch (month)
{
case 1:
n = 31;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
n = 29;
}
else
{
n = 28;
}
break;
case 3:
n = 31;
break;
case 4:
n = 30;
break;
case 5:
n = 31;
break;
case 6:
n = 30;
break;
case 7:
n = 31;
break;
case 8:
n = 31;
break;
case 9:
n = 30;
break;
case 10:
n = 31;
break;
case 11:
n = 30;
break;
case 12:
n = 31;
break;
}

return n;
}


//判断每个月的第一天是星期几
//year=年;start_week=1月1日是周几;month=月份;n=打印的天数;n1=累加的天数(用来判断这月是从周几开始)
void month_week(int year, int start_week, int month, int n, int n1)
{
int week = 0;

// 判断该月份1号在星期几
if (month == 1)
{
week = start_week;
}
else
{
week = start_week + (n1 % 7);
if (week > 7)
{
week = week - 7;
}
}

//打印第一行空格
for (int m = 1; m < week; m++)
{
printf(" ");
}

//打印后续日历
for (int i = 0; i < n; i++)
{
if ((week + i) % 7 == 1)
{
printf("\n");
}
if (i < 10)
{
printf(" %2d",i+1);
}
else
{
printf(" %2d",i+1);
}
}




}

//打印一年的日历
void printf_year(int start)
{
int days = 0;
char strings[12][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

printf("\n------------%d------------\n",start);
printf(" 一 二 三 四 五 六 日\n");
printf("----------------------------");


//使用循环判断累计days
for (int i = 1; i <= 12; i++)
{
days += month_days(start, i-1);
//打印1月份
printf("\n\n\t %s \t\n",strings[i-1]);
month_week(start, spaces(start), i, month_days(start, i), days);
}

}

int main(void)
{
int start = 0;
int end = 0;

printf("请输入开始年份:");
scanf("%d", &start);

printf("请输入结束年份:");
scanf("%d", &end);

for (int i = 0; i <= end - start; i++)
{
printf_year(start+i);
printf("\n");
}
return 0;
}

4.36

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
30
/*程序名:4.36.c*/
/*
人们对break语句和contiune语句的批评就是这两种都是非结构化的(不是用if等其他结构语句,而是使用jmp直接跳转)。
实际上,我们始终都可以用结构化的语句来取代break语句和contiune语句,
尽管这么做可能非常笨拙。简述一些如何在一个程序中从循环中删除break语句,
并使用结构化的等价语句来取代该语句。(提示:break语句会从循环体内部退出循环过程。
另外一种离开循环的方法就是使用循环条件测试失败。
请考虑在循环条件测试中使用二次测试,二次测试表示“由一个break条件而过早退出循环”。)
使用在这里开发的技术把break语句从图 fig04_11.c 中剔除。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int main(void)
{
int x;

for (x = 1; x <= 10 && x != 5; x++)
{
//if (x == 5) /*条件判断*/
//{
//break; /*x=5时终止循环*/
//}
printf("%d ", x);
}

printf("\nBroke out of loop at x == %d\n", x);
return 0; //程序结束
}

4.37

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
30
31
32
33
34
/*程序名:4.37.c*/
/*
请简述以下程序段的功能。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>


int main(void)
{
/*
打印5个长4宽3,以星号组成的长方形。
****
****
****

****
****
****
*/
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 3; j++)
{
for (int k = 1; k <= 4; k++)
printf("*");
printf("\n");
}
printf("\n");
}

return 0; //程序结束
}

4.38

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
/*程序名:4.38.c*/
/*
概述用结构化的等价结构将 continue 语句从循环程序中剔除的方法。
使用该技术,剔除 fig04_12.c 程序中的 continue 语句。
*/

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int main(void)
{
int x;

for (x = 1; x <= 10; x++)
{
if (x != 5)
{
printf("%d ", x);
}

}

printf("\nUsed continue to skip printing the value 5\n", x);
return 0; //程序结束
}
  • 本文标题:C语言进阶-C程序控制
  • 本文作者:9unk
  • 创建时间:2023-11-16 18:18:00
  • 本文链接:https://9unkk.github.io/2023/11/16/c-yu-yan-jin-jie-c-cheng-xu-kong-zhi/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!