| 这个作业属于哪个课程 | <班级的链接> |
|---|---|
| 这个作业要求在哪里 | <作业链接> |
| 学号 | 092300303 |
| 姓名 | 池博洋 |
@
- 11.13
- 1
- 2
- 3
- 6
- 7
- 12.9
- 1
- 2
- 3
- 8
- 9
11.13
1
点击查看代码
#include <stdio.h>char *get_n_chars(char *str, int n) {int i = 0;char ch;printf("请输入最多 %d 个字符(包括空白、制表符、换行符):\n", n);while (i < n) {ch = getchar();if (ch == EOF) {break;}str[i++] = ch;}str[i] = '\0';return str;
}int main() {char str[100];int n;printf("请输入要读取的字符数:");scanf("%d", &n);getchar(); // 清除换行符get_n_chars(str, n);printf("你输入的内容是:\n%s\n", str);return 0;
}
结果:

2
点击查看代码
#include <stdio.h>char *get_n_chars_mod(char *str, int n) {int i = 0;char ch;printf("请输入字符(最多 %d 个字符,或遇到空白、制表符、换行符停止):\n", n);while (i < n) {ch = getchar();if (ch == EOF || ch == ' ' || ch == '\t' || ch == '\n') {break;}str[i++] = ch;}str[i] = '\0';// 如果停止是因为空白字符,需要清除输入缓冲区中的剩余字符if (ch != EOF && ch != '\n' && ch != ' ' && ch != '\t') {while (getchar() != '\n') {continue;}}return str;
}int main() {char str[100];int n;printf("请输入最大字符数:");scanf("%d", &n);getchar(); // 清除换行符get_n_chars_mod(str, n);printf("读取的结果是:\"%s\"\n", str);return 0;
}
结果:

3
点击查看代码
#include <stdio.h>
#include <ctype.h>char *get_word(char *str) {char ch;int i = 0;printf("请输入一行文本:\n");// 跳过前面的空白字符while ((ch = getchar()) != EOF && isspace(ch)) {continue;}// 如果第一个非空白字符是EOF,返回空字符串if (ch == EOF) {str[0] = '\0';return str;}// 读取单词直到遇到空白字符do {str[i++] = ch;} while ((ch = getchar()) != EOF && !isspace(ch));str[i] = '\0';// 丢弃输入行中的其余字符if (ch != EOF && ch != '\n') {while (getchar() != '\n') {continue;}}return str;
}int main() {char str[100];printf("=== 单词读取测试 ===\n");get_word(str);printf("读取的单词是:\"%s\"\n", str);return 0;
}
结果:

6
点击查看代码
#include <stdio.h>int is_within(char ch, const char *str) {while (*str) {if (*str == ch) {return 1; // 找到字符,返回真}str++;}return 0; // 未找到字符,返回假
}int main() {char str[100];char ch;char choice;do {printf("请输入一个字符串:");fgets(str, sizeof(str), stdin);// 移除fgets读取的换行符int i = 0;while (str[i] != '\n' && str[i] != '\0') {i++;}str[i] = '\0';printf("请输入要查找的字符:");scanf(" %c", &ch);getchar(); // 清除换行符if (is_within(ch, str)) {printf("字符 '%c' 在字符串 \"%s\" 中\n", ch, str);} else {printf("字符 '%c' 不在字符串 \"%s\" 中\n", ch, str);}printf("是否继续测试?(y/n): ");scanf(" %c", &choice);getchar(); // 清除换行符} while (choice == 'y' || choice == 'Y');printf("测试结束\n");return 0;
}
结果:

7
点击查看代码
#include <stdio.h>
#include <string.h>char *mystrncpy(char *s1, const char *s2, int n) {int i;// 拷贝字符for (i = 0; i < n && s2[i] != '\0'; i++) {s1[i] = s2[i];}// 如果拷贝的字符数小于n,且s2长度小于n,则添加空字符// 注意:标准strncpy在s2长度>=n时不会添加空字符if (i < n && s2[i] == '\0') {s1[i] = '\0';}return s1;
}int main() {char src[100], dest[100];int n;char choice;do {printf("请输入源字符串:");fgets(src, sizeof(src), stdin);// 移除fgets读取的换行符int i = 0;while (src[i] != '\n' && src[i] != '\0') {i++;}src[i] = '\0';printf("请输入要拷贝的字符数:");scanf("%d", &n);getchar(); // 清除换行符// 使用自定义函数拷贝mystrncpy(dest, src, n);// 确保dest以空字符结尾(当n小于dest大小时)if (n < sizeof(dest)) {dest[n] = '\0';} else {dest[sizeof(dest) - 1] = '\0';}printf("源字符串: \"%s\"\n", src);printf("拷贝 %d 个字符到目标字符串: \"%s\"\n", n, dest);printf("是否继续测试?(y/n): ");scanf(" %c", &choice);getchar(); // 清除换行符} while (choice == 'y' || choice == 'Y');printf("测试结束\n");// 额外的对比测试printf("\n=== 与标准strncpy函数的对比测试 ===\n");char test_src[] = "Hello World!";char my_dest[20], std_dest[20];// 测试1:拷贝少于源字符串长度的字符mystrncpy(my_dest, test_src, 5);my_dest[5] = '\0';strncpy(std_dest, test_src, 5);std_dest[5] = '\0';printf("测试1(拷贝5个字符):\n");printf("mystrncpy结果: \"%s\"\n", my_dest);printf("strncpy结果 : \"%s\"\n", std_dest);// 测试2:拷贝等于源字符串长度的字符mystrncpy(my_dest, test_src, 13);strncpy(std_dest, test_src, 13);printf("\n测试2(拷贝13个字符):\n");printf("mystrncpy结果: \"%s\"\n", my_dest);printf("strncpy结果 : \"%s\"\n", std_dest);return 0;
}
结果:

12.9
1
点击查看代码
#include <stdio.h>// 函数原型声明
void critic(int *units_ptr);int main(void)
{int units; // 局部变量printf("How many pounds to a firkin of butter?\n");scanf("%d", &units);// 当猜错时,调用critic函数while (units != 56) {critic(&units); // 传递units的地址}printf("You must have looked it up!\n");return 0;
}void critic(int *units_ptr)
{printf("No luck, my friend. Try again.\n");scanf("%d", units_ptr); // 直接修改main中的units
}
结果:

2
头文件
点击查看代码
/* p12-2a.h -- 油耗计算程序头文件 */
#ifndef P12_2A_H
#define P12_2A_H/* 文件作用域变量声明 */
extern int mode; // 0=公制,1=美制
extern float distance; // 行驶距离
extern float fuel; // 消耗的燃油/* 函数原型 */
void set_mode(int m);
void get_info(void);
void show_info(void);#endif
源文件
点击查看代码
/* p12-2a.c -- 油耗计算程序实现 */
#include <stdio.h>
#include "p12-2a.h"/* 文件作用域变量定义 */
int mode = 0; // 默认使用公制模式
float distance = 0;
float fuel = 0;/* 设置模式函数 */
void set_mode(int m)
{static int last_mode = 0; // 上一次有效的模式if (m == 0 || m == 1) {mode = m;last_mode = m;} else {printf("Invalid mode specified. Mode %d(%s) used.\n",last_mode, last_mode == 0 ? "metric" : "US");mode = last_mode;}
}/* 获取信息函数 */
void get_info(void)
{if (mode == 0) {// 公制模式:升/100公里printf("Enter distance traveled in kilometers: ");while (scanf("%f", &distance) != 1 || distance <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue; // 清理输入缓冲区}printf("Enter fuel consumed in liters: ");while (scanf("%f", &fuel) != 1 || fuel <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}} else {// 美制模式:英里/加仑printf("Enter distance traveled in miles: ");while (scanf("%f", &distance) != 1 || distance <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}printf("Enter fuel consumed in gallons: ");while (scanf("%f", &fuel) != 1 || fuel <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}}
}/* 显示信息函数 */
void show_info(void)
{if (mode == 0) {// 公制模式:计算升/100公里float consumption = (fuel / distance) * 100;printf("Fuel consumption is %.2f liters per 100 km.\n", consumption);} else {// 美制模式:计算英里/加仑float consumption = distance / fuel;printf("Fuel consumption is %.1f miles per gallon.\n", consumption);}
}
主程序
点击查看代码
/* main.c -- 油耗计算主程序 */
#include <stdio.h>
#include "p12-2a.h"int main(void)
{int user_mode;printf("Enter 0 for metric mode, 1 for US mode: ");scanf("%d", &user_mode);while (user_mode >= 0) {set_mode(user_mode);get_info();show_info();printf("\nEnter 0 for metric mode, 1 for US mode (-1 to quit): ");scanf("%d", &user_mode);}printf("Done.\n");return 0;
}
3
头文件
点击查看代码
/* p12-3.h -- 油耗计算程序头文件(使用自动变量) */
#ifndef P12_3_H
#define P12_3_H/* 函数原型 */
void set_mode_local(int m, int *mode);
void get_info_local(int mode, float *distance, float *fuel);
void show_info_local(int mode, float distance, float fuel);#endif
源文件
点击查看代码
/* p12-3.c -- 油耗计算程序实现(使用自动变量) */
#include <stdio.h>
#include "p12-3.h"/* 设置模式函数(使用自动变量) */
void set_mode_local(int m, int *mode)
{static int last_mode = 0; // 静态局部变量,记录上一次有效模式if (m == 0 || m == 1) {*mode = m;last_mode = m;} else {printf("Invalid mode specified. Mode %d(%s) used.\n",last_mode, last_mode == 0 ? "metric" : "US");*mode = last_mode;}
}/* 获取信息函数(使用自动变量) */
void get_info_local(int mode, float *distance, float *fuel)
{if (mode == 0) {// 公制模式:升/100公里printf("Enter distance traveled in kilometers: ");while (scanf("%f", distance) != 1 || *distance <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}printf("Enter fuel consumed in liters: ");while (scanf("%f", fuel) != 1 || *fuel <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}} else {// 美制模式:英里/加仑printf("Enter distance traveled in miles: ");while (scanf("%f", distance) != 1 || *distance <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}printf("Enter fuel consumed in gallons: ");while (scanf("%f", fuel) != 1 || *fuel <= 0) {printf("Please enter a positive number: ");while (getchar() != '\n') continue;}}
}/* 显示信息函数(使用自动变量) */
void show_info_local(int mode, float distance, float fuel)
{if (mode == 0) {// 公制模式:计算升/100公里float consumption = (fuel / distance) * 100;printf("Fuel consumption is %.2f liters per 100 km.\n", consumption);} else {// 美制模式:计算英里/加仑float consumption = distance / fuel;printf("Fuel consumption is %.1f miles per gallon.\n", consumption);}
}
主程序
点击查看代码
/* main_local.c -- 油耗计算主程序(使用自动变量) */
#include <stdio.h>
#include "p12-3.h"int main(void)
{int user_mode;int current_mode = 0; // 局部变量float distance = 0; // 局部变量float fuel = 0; // 局部变量printf("Enter 0 for metric mode, 1 for US mode: ");scanf("%d", &user_mode);while (user_mode >= 0) {set_mode_local(user_mode, ¤t_mode);get_info_local(current_mode, &distance, &fuel);show_info_local(current_mode, distance, fuel);printf("\nEnter 0 for metric mode, 1 for US mode (-1 to quit): ");scanf("%d", &user_mode);}printf("Done.\n");return 0;
}
结果:
Enter 0 for metric mode, 1 for US mode: 0
Enter distance traveled in kilometers: 600
Enter fuel consumed in liters: 78.5
Fuel consumption is 13.08 liters per 100 km.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 1
Enter distance traveled in miles: 434
Enter fuel consumed in gallons: 12.7
Fuel consumption is 34.2 miles per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 3
Invalid mode specified. Mode 1(US) used.
Enter distance traveled in miles: 38
Enter fuel consumed in gallons: 1.5
Fuel consumption is 25.3 miles per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): -1
Done.
8
点击查看代码
#include <stdio.h>
#include <stdlib.h>/* 函数原型 */
int *make_array(int size, int value);
void show_array(const int arr[], 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("\nEnter the number of elements (<1 to quit): ");}printf("Done.\n");return 0;
}/* 创建并初始化数组的函数 */
int *make_array(int size, int value)
{int *arr = (int *)malloc(size * sizeof(int));if (arr != NULL) {for (int i = 0; i < size; i++) {arr[i] = value;}}return arr;
}/* 显示数组内容的函数 */
void show_array(const int arr[], int n)
{for (int i = 0; i < n; i++) {printf("%d ", arr[i]);// 每8个元素换一行if ((i + 1) % 8 == 0) {printf("\n");}}// 如果最后一行不满8个,也需要换行if (n % 8 != 0) {printf("\n");}
}
结果:

9
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main(void)
{int num_words;char **words; // 指向指针的指针,用于存储字符串数组char temp[100]; // 临时缓冲区用于读取单词// 询问用户需要输入多少个单词printf("How many words do you wish to enter? ");scanf("%d", &num_words);// 分配指针数组words = (char **)malloc(num_words * sizeof(char *));if (words == NULL) {printf("Memory allocation failed!\n");return 1;}// 读取单词printf("Enter %d words now:\n", num_words);for (int i = 0; i < num_words; i++) {scanf("%s", temp);// 为当前单词分配足够的内存words[i] = (char *)malloc((strlen(temp) + 1) * sizeof(char));if (words[i] == NULL) {printf("Memory allocation failed for word %d!\n", i);// 释放已分配的内存for (int j = 0; j < i; j++) {free(words[j]);}free(words);return 1;}// 复制单词到分配的内存strcpy(words[i], temp);}// 显示所有单词printf("Here are your words:\n");for (int i = 0; i < num_words; i++) {printf("%s\n", words[i]);}// 释放内存for (int i = 0; i < num_words; i++) {free(words[i]);}free(words);return 0;
}
结果:
