高级语言程序设计第八次作业
这个作业属于哪个课程:2026高级语言程序设计
这个作业要求在哪里:高级语言程序设计课程第八次个人作业
学号:052402239
姓名:陈逸轩
1. 读取指定数量的字符
设计并测试一个函数,读取指定数量的字符(包括空白、制表符和换行符),存入传入的数组。
#include <stdio.h>
#define N 8
int read_chars(char s[], int n) {
int ch, i = 0;
while (i < n && (ch = getchar()) != EOF) s[i++] = (char)ch;
return i;
}
int main(void) {
char text[N];
int count;
printf("请输入%d个字符:", N);
count = read_chars(text, N);
printf("\n读取到的字符:");
fwrite(text, 1, count, stdout);
printf("\n共读取%d个字符。\n", count);
return 0;
}

2. 读取字符直到空白
修改上一题的函数,读取 n 个字符后停止,或遇到空格、制表符、换行符时停止,不能只使用 scanf()。
#include <stdio.h>
#include <ctype.h>
#define N 10
int read_word(char s[], int n) {
int ch, i = 0;
while (i < n && (ch = getchar()) != EOF && !isspace((unsigned char)ch))
s[i++] = (char)ch;
s[i] = '\0';
return i;
}
int main(void) {
char text[N + 1];
printf("请输入一串字符:");
int count = read_word(text, N);
printf("读取结果:%s\n字符数:%d\n", text, count);
return 0;
}

3. 读取一行中的第一个单词
设计并测试一个函数,跳过前导空白,将一行中的第一个单词读入数组,并丢弃该行剩余字符。
#include <stdio.h>
#include <ctype.h>
#define SIZE 100
void first_word(char word[], int size) {
int ch, i = 0;
while ((ch = getchar()) != EOF && ch != '\n' && isspace((unsigned char)ch)) {}
while (ch != EOF && ch != '\n' && !isspace((unsigned char)ch)) {
if (i < size - 1) word[i++] = (char)ch;
ch = getchar();
}
word[i] = '\0';
while (ch != EOF && ch != '\n') ch = getchar();
}
int main(void) {
char word[SIZE];
printf("请输入一行文字:");
first_word(word, SIZE);
printf("第一个单词:%s\n", word);
return 0;
}

4. 查找字符串中的字符
编写 is_within(),判断指定字符是否出现在字符串中,并在循环中测试。
#include <stdio.h>
#include <string.h>
int is_within(char ch, const char *s) {
while (*s) {
if (*s == ch) return 1;
s++;
}
return 0;
}
int main(void) {
char text[100], line[100];
while (1) {
printf("请输入字符串(空行结束):");
if (!fgets(text, sizeof text, stdin) || text[0] == '\n') break;
text[strcspn(text, "\n")] = '\0';
printf("请输入要查找的字符:");
if (!fgets(line, sizeof line, stdin)) break;
printf("查找结果:%s\n", is_within(line[0], text) ? "存在" : "不存在");
}
return 0;
}

5. 实现字符串定长复制
编写 mystrncpy(),按 strncpy() 的规则复制 n 个字符,并在循环中测试。
#include <stdio.h>
#include <string.h>
char *mystrncpy(char *dest, const char *source, size_t n) {
size_t i = 0;
while (i < n && source[i] != '\0') { dest[i] = source[i]; i++; }
while (i < n) dest[i++] = '\0';
return dest;
}
int main(void) {
char source[100], dest[101], line[32];
int n;
while (1) {
printf("请输入源字符串(空行结束):");
if (!fgets(source, sizeof source, stdin) || source[0] == '\n') break;
source[strcspn(source, "\n")] = '\0';
printf("请输入复制长度(0到100):");
if (!fgets(line, sizeof line, stdin) || sscanf(line, "%d", &n) != 1) break;
if (n < 0 || n > 100) { puts("长度无效。"); continue; }
memset(dest, 0, sizeof dest);
mystrncpy(dest, source, (size_t)n);
dest[n] = '\0';
printf("复制结果:%s\n", dest);
}
return 0;
}

6. 不使用全局变量改写程序
不使用全局变量,重写程序清单 12.4。
#include <stdio.h>
int critic(int *units) {
printf("不正确,请再试一次:");
return scanf("%d", units);
}
int main(void) {
int units;
printf("一 firkin 黄油有多少磅?");
if (scanf("%d", &units) != 1) return 0;
while (units != 56) if (critic(&units) != 1) return 0;
printf("回答正确!\n");
return 0;
}

7. 使用文件作用域变量计算油耗
实现油耗计算程序,提供头文件和源文件,并用具有内部链接的文件作用域变量保存模式、距离和燃料。
#include <stdio.h>
#include "pe12_2a.h"
int main(void)
{
int mode;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
set_mode(mode);
get_info();
show_info();
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf("%d", &mode);
}
printf("Done.\n");
return 0;
}
#ifndef PE12_2A_H
#define PE12_2A_H
void set_mode(int mode);
void get_info(void);
void show_info(void);
#endif
#include <stdio.h>
#include "pe12_2a.h"
static int mode = 0;
static double distance, fuel;
void set_mode(int value) {
if (value == 0 || value == 1) mode = value;
else printf("Invalid mode specified. Mode %d (%s) used.\n", mode, mode ? "US" : "metric");
}
void get_info(void) {
printf("Enter distance traveled in %s: ", mode ? "miles" : "kilometers");
scanf("%lf", &distance);
printf("Enter fuel consumed in %s: ", mode ? "gallons" : "liters");
scanf("%lf", &fuel);
}
void show_info(void) {
if (distance <= 0 || fuel <= 0) { puts("Distance and fuel must be positive."); return; }
if (mode) printf("Fuel consumption is %.1f miles per gallon.\n", distance / fuel);
else printf("Fuel consumption is %.2f liters per 100 km.\n", fuel / distance * 100);
}

8. 使用自动变量计算油耗
重新设计第 2 题,只使用自动变量,保留相同的用户界面和模式处理规则。
#include <stdio.h>
int set_mode(int mode, int old) {
if (mode == 0 || mode == 1) return mode;
printf("Invalid mode specified. Mode %d (%s) used.\n", old, old ? "US" : "metric");
return old;
}
int get_info(int mode, double *distance, double *fuel) {
printf("Enter distance traveled in %s: ", mode ? "miles" : "kilometers");
if (scanf("%lf", distance) != 1) return 0;
printf("Enter fuel consumed in %s: ", mode ? "gallons" : "liters");
return scanf("%lf", fuel) == 1;
}
void show_info(int mode, double distance, double fuel) {
if (distance <= 0 || fuel <= 0) { puts("Distance and fuel must be positive."); return; }
if (mode) printf("Fuel consumption is %.1f miles per gallon.\n", distance / fuel);
else printf("Fuel consumption is %.2f liters per 100 km.\n", fuel / distance * 100);
}
int main(void) {
int input, mode = 0;
double distance, fuel;
printf("Enter 0 for metric mode, 1 for US mode: ");
while (scanf("%d", &input) == 1 && input >= 0) {
mode = set_mode(input, mode);
if (!get_info(mode, &distance, &fuel)) break;
show_info(mode, distance, fuel);
printf("Enter 0 for metric mode, 1 for US mode (-1 to quit): ");
}
puts("Done.");
return 0;
}

9. 动态创建并显示整数数组
补全 make_array() 和 show_array():动态分配数组并初始化,每行显示 8 个数。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int *make_array(int elem, int val);
void show_array(const int ar[], int n);
int main(void)
{
int *pa;
int size;
int value;
printf("Enter the number of elements: ");
while (scanf("%d", &size) == 1 && size > 0)
{
printf("Enter the initialization value: ");
scanf("%d", &value);
pa = make_array(size, value);
if (pa)
{
show_array(pa, size);
free(pa);
}
printf("Enter the number of elements (<1 to quit): ");
}
printf("Done.\n");
return 0;
}
int *make_array(int elem, int val) {
if ((size_t)elem > SIZE_MAX / sizeof(int)) return NULL;
int *a = malloc((size_t)elem * sizeof *a);
if (a) for (int i = 0; i < elem; i++) a[i] = val;
return a;
}
void show_array(const int ar[], int n) {
for (int i = 0; i < n; i++)
printf("%d%c", ar[i], (i + 1) % 8 == 0 || i == n - 1 ? '\n' : ' ');
}

10. 动态保存输入的单词
询问单词数量,动态分配字符指针数组及每个单词所需的空间,读取并显示单词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int main(void) {
int count, i;
char temp[100];
printf("请输入单词数量:");
if (scanf("%d", &count) != 1 || count <= 0 ||
(size_t)count > SIZE_MAX / sizeof(char *)) return 0;
char **words = malloc((size_t)count * sizeof *words);
if (!words) { puts("内存分配失败。"); return 1; }
printf("请输入%d个单词:", count);
for (i = 0; i < count; i++) {
if (scanf("%99s", temp) != 1) break;
words[i] = malloc(strlen(temp) + 1);
if (!words[i]) break;
strcpy(words[i], temp);
}
printf("\n输入的单词:\n");
for (int j = 0; j < i; j++) { puts(words[j]); free(words[j]); }
free(words);
return 0;
}


浙公网安备 33010602011771号