高级语言程序设计课程第四次个人作业
这个作业属于哪个课程:福州大学 2026 高级语言程序设计
这个作业要求在哪里:高级语言程序设计课程第四次个人作业
学号:0102600222
姓名:陈键豪
运行环境:Embarcadero Dev-C++ 6.3,64 位 Release 模式。
一、指定程序的运行结果
(1)整数求和

(2)猜测圆周率近似值

二、第 2 章编程练习
2.1 计算圆周率近似值
使用 while 实现:
#include <stdio.h>
int main(void)
{
double temp = 1;
double sum = 0;
int i = 1;
int is_neg = 0;
while (1)
{
temp = 1.0 / i;
i += 2;
if (temp <= 0.0001)
{
break;
}
if (is_neg)
{
sum -= temp;
is_neg = 0;
}
else
{
sum += temp;
is_neg = 1;
}
printf("%d %lf %lf\n", i - 2, temp, sum);
}
printf("%lf\n", 4 * sum);
return 0;
}
使用 do-while 实现:
#include <stdio.h>
int main(void)
{
double temp = 1;
double sum = 0;
int i = 1;
int is_neg = 0;
do
{
temp = 1.0 / i;
i += 2;
if (temp <= 0.0001)
{
break;
}
if (is_neg)
{
sum -= temp;
is_neg = 0;
}
else
{
sum += temp;
is_neg = 1;
}
printf("%d %lf %lf\n", i - 2, temp, sum);
} while (1);
printf("%lf\n", 4 * sum);
return 0;
}
while 先判断条件再执行,do-while 先执行一次再判断条件;本题两者都依靠 break 结束。

2.2 验证逻辑表达式的短路求值
#include <stdio.h>
int check(void)
{
printf("右边的 check() 执行了\n");
return 1;
}
int main(void)
{
int a = 0;
printf("a = 0:\n");
printf("%d\n", a != 0 && check());
printf("%d\n\n", a == 0 || check());
a = 1;
printf("a = 1:\n");
printf("%d\n", a != 0 && check());
printf("%d\n", a == 0 || check());
return 0;
}

2.3 输出六行倒序字母
#include <stdio.h>
int main(void)
{
for (int i = 6; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
printf("%c", 'F' - j);
}
printf("\n");
}
return 0;
}

2.4 计算分组累加
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
int sum = 0;
for (int i = 1; i <= n; i++)
{
int part = 0;
for (int j = 1; j <= i; j++)
{
part += j;
}
sum += part;
}
printf("%d\n", sum);
return 0;
}

2.5 数组赋值、输出与求和
#include <stdio.h>
int main(void)
{
int a[8];
int sum = 0;
for (int i = 0; i < 8; i++)
{
a[i] = i + 1;
}
for (int i = 0; i < 8; i++)
{
printf("%d ", a[i]);
sum += a[i];
}
printf("\n%d\n", sum);
return 0;
}

2.6 多分支成绩判断
if-else 实现:
#include <stdio.h>
int main(void)
{
int score;
while (scanf("%d", &score) == 1)
{
if (score < 0)
{
printf("retype please\n");
}
else if (score < 60)
{
printf("you did not pass\n");
}
else if (score < 80)
{
printf("pass\n");
}
else
{
printf("excel\n");
}
}
return 0;
}
switch 实现:
#include <stdio.h>
int main(void)
{
int score;
while (scanf("%d", &score) == 1)
{
if (score < 0)
{
printf("retype please\n");
continue;
}
switch (score / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("you did not pass\n");
break;
case 6:
case 7:
printf("pass\n");
break;
case 8:
case 9:
case 10:
printf("excel\n");
break;
default:
printf("retype please\n");
break;
}
}
return 0;
}
条件运算符(?:)双分支:
#include <stdio.h>
int main(void)
{
int score;
scanf("%d", &score);
printf("%s\n", score >= 60 ? "pass or above" : "you did not pass");
return 0;
}

2.7 break 与 continue
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
for (int i = 1; ; i++)
{
if (i % 2 == 0)
{
continue;
}
if (i >= n)
{
break;
}
printf("%d ", i);
}
return 0;
}
输入 20 时,输出 1 3 5 7 9 11 13 15 17 19。

2.8 输出 100~200 之间的素数
#include <stdio.h>
int is_prime(int n)
{
if (n < 2)
{
return 0;
}
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
int main(void)
{
for (int i = 100; i <= 200; i++)
{
if (is_prime(i))
{
printf("%d\n", i);
}
}
return 0;
}

算法思想:
传统流程图:
NS 图:
三、第 6 章 6.16 编程练习
6.16 第 1 题
#include <stdio.h>
int main(void)
{
char chs[26] = {'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; chs[i] != '\0'; i++)
{
printf("%c", chs[i]);
}
return 0;
}

6.16 第 5 题
#include <stdio.h>
int main(void)
{
char ch;
scanf("%c", &ch);
for (int i = 0; i < ch - 'A' + 1; i++)
{
for (int j = 0; j < ch - 'A' - i; j++)
{
printf(" ");
}
for (int j = 'A'; j <= 'A' + i; j++)
{
printf("%c", j);
}
for (int j = 'A' + i - 1; j >= 'A'; j--)
{
printf("%c", j);
}
printf("\n");
}
return 0;
}

6.16 第 7 题
#include <stdio.h>
#include <string.h>
int main(void)
{
char word[101];
scanf("%100s", word);
for (int i = strlen(word) - 1; i >= 0; i--)
{
printf("%c", word[i]);
}
return 0;
}

6.16 第 10 题
#include <stdio.h>
int main(void)
{
int n1, n2;
printf("Enter lower and upper integer limits: ");
while (scanf("%d %d", &n1, &n2) == 2 && n1 < n2)
{
int sum = 0;
for (int i = n1; i <= n2; i++)
{
sum += i * i;
}
printf("The sum of the squares from %d to %d is %d\n",
n1 * n1, n2 * n2, sum);
printf("Enter next set of limits: ");
}
printf("Done\n");
return 0;
}

6.16 第 13 题
#include <stdio.h>
int main(void)
{
int nums[8];
int value = 1;
int i = 0;
do
{
value *= 2;
nums[i] = value;
i++;
} while (i < 8);
i = 0;
do
{
printf("%d\n", nums[i]);
i++;
} while (i < 8);
return 0;
}

6.16 第 16 题
#include <stdio.h>
int main(void)
{
double daphne = 100.0;
double deirdre = 100.0;
int years = 0;
while (deirdre <= daphne)
{
daphne += 10.0;
deirdre *= 1.05;
years++;
}
printf("After %d years, Deirdre has $%.2f.\n", years, deirdre);
printf("At that time, Daphne has $%.2f.\n", daphne);
return 0;
}

6.16 第 18 题
#include <stdio.h>
int main(void)
{
int friends = 5;
int week = 1;
while (friends <= 150)
{
friends = (friends - week) * 2;
printf("Week %d: %d friends\n", week, friends);
week++;
}
return 0;
}

四、第 7 章 7.12 编程练习
7.12 第 1 题
#include <stdio.h>
int main(void)
{
int ch;
int spaces = 0;
int newlines = 0;
int others = 0;
printf("Enter text (# to stop):\n");
while ((ch = getchar()) != '#' && ch != EOF)
{
if (ch == ' ')
{
spaces++;
}
else if (ch == '\n')
{
newlines++;
}
else
{
others++;
}
}
printf("Spaces: %d\n", spaces);
printf("Newlines: %d\n", newlines);
printf("Other characters: %d\n", others);
return 0;
}

7.12 第 2 题
#include <stdio.h>
int main(void)
{
int ch;
int count = 0;
printf("Enter text (# to stop):\n");
while ((ch = getchar()) != '#' && ch != EOF)
{
if (ch == '\n')
{
printf("\\n:%d ", ch);
}
else if (ch == '\t')
{
printf("\\t:%d ", ch);
}
else
{
printf("%c:%d ", ch, ch);
}
count++;
if (count % 8 == 0)
{
printf("\n");
}
}
if (count % 8 != 0)
{
printf("\n");
}
return 0;
}

7.12 第 4 题
#include <stdio.h>
int main(void)
{
int ch;
int count = 0;
printf("Enter text (# to stop):\n");
while ((ch = getchar()) != '#' && ch != EOF)
{
if (ch == '.')
{
putchar('!');
count++;
}
else if (ch == '!')
{
printf("!!");
count++;
}
else
{
putchar(ch);
}
}
printf("\n%d substitutions\n", count);
return 0;
}

7.12 第 5 题
#include <stdio.h>
int main(void)
{
int ch;
int count = 0;
printf("Enter text (# to stop):\n");
while ((ch = getchar()) != '#' && ch != EOF)
{
switch (ch)
{
case '.':
putchar('!');
count++;
break;
case '!':
printf("!!");
count++;
break;
default:
putchar(ch);
break;
}
}
printf("\n%d substitutions\n", count);
return 0;
}
输出效果与第 4 题相同。
7.12 第 7 题
#include <stdio.h>
#define BASE_PAY 10.00
int main(void)
{
double hours, gross, tax, net;
printf("Enter the number of hours worked in a week: ");
scanf("%lf", &hours);
if (hours <= 40)
{
gross = hours * BASE_PAY;
}
else
{
gross = 40 * BASE_PAY + (hours - 40) * BASE_PAY * 1.5;
}
if (gross <= 300)
{
tax = gross * 0.15;
}
else if (gross <= 450)
{
tax = 300 * 0.15 + (gross - 300) * 0.20;
}
else
{
tax = 300 * 0.15 + 150 * 0.20 + (gross - 450) * 0.25;
}
net = gross - tax;
printf("Gross pay: $%.2f\n", gross);
printf("Taxes: $%.2f\n", tax);
printf("Net pay: $%.2f\n", net);
return 0;
}

浙公网安备 33010602011771号