高级程序设计语言第八次个人作业

news/2025/12/7 17:12:34/文章来源:https://www.cnblogs.com/fantasyaaazzz/p/19318411
这个作业属于哪个课程 <班级的链接>
这个作业要求在哪里 <作业链接>
学号 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;
}

结果:

image

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;
}

结果:

image

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;
}

结果:

image

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;
}

结果:

image

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;
}

结果:

image

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
}

结果:

image

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, &current_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");}
}

结果:

image

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;
}

结果:

image

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/991602.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux 安装nginx - 华

1.解压安装包1tar -zxf nginx-1.25.1.tar.gz2.进入解压后文件夹cd nginx-1.25.13.配置nginx./configure4.编译安装make&&make install5.运行nginx进入目录 cd /usr/local/nginx/sbin启动nginx./nginx6.配置ngi…

网络流

网络流 Network flow flowchart LRS(Source)v1([V1])v2([V2])v3([V3])v4([V4])t(sink)S -->|4| v1S -->|2| v2v1 -->|1| v2v2 -->|2| v4v1 -->|4| v4v1 -->|2| v3v4-->|3|tv3-->|3|t 有向有权…

Godot OpenGL

Godot OpenGLGodot 中 OpenGL Godot 中的 Compatible 渲染后端用的是 OpenGL 直接写的,没有封装

昆明黄金店联系方式大全

主流品牌门店电话 廖金匠(昆明多家分店) 南亚风情第壹城店 电话:19969207928(微信同号) •地址:昆明市西山区福海街道办事处南亚风情第壹城A4幢1单元1-2层A4-1室2楼 昆明南亚廖金匠珠宝店:18808581681 中国黄金(昆…

Chrome 清除网站图标缓存,更新网站图标

Google Chrome 快速清除某一个网站图标补充: 好像这样操作之后我的浏览器的所有的缓存都被清除了,暂时还不确定是不是这个操作造成的在 Google Chrome 中清除某一个网站的图标缓存并更新网站图标:打开这个网站 Shif…

20232410 2025-2026-1 《网络与系统攻防技术》实验八实验报告1

Web前后端开发与网络安全测试实验报告 1. 实验内容 (1) Web前端HTML 能正常安装、启停Apache。理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML。 (2) Web前端javascript 理解JavaScript的基本功能,理…

SSO 方案

为多个域/子域、Web 与移动端,提供安全、可扩展、对外可接入的单点登录服务(SSO)。采用 OAuth2 Authorization Code + PKCE 与 OpenID Connect (OIDC) 标准,现在简单介绍一下:核心概念IdP(Identity Provider):…

全球AI一周动态(12月1日-7日):巨头战略博弈升级,技术爆发催生新生态

🔥 一、国家级战略:AI军备竞赛进入深水区 1. 中国五部门联合发布AI+医疗新政五部门联合发布《关于促进和规范"人工智能+医疗卫生"应用发展的实施意见》,提出两阶段目标,到2027年建立卫生健康行业高质量…

英语四级翻译

翻译必考点一节课搞定四六级翻译必考点_哔哩哔哩_bilibili 之一:one of 1.找主干 2.添加定语 3.检查主谓一致、时态、单复数、a/an 《水浒传》(Water Margin)是中国文学四大经典小说之一 Water Margin is one of th…

散修带你入门鸿蒙应用开发基础:启程篇(下) - 鸿蒙

鸿蒙应用开发基础:启程篇(下) 【课程目标】熟悉DevEco Studio界面布局与常用功能 掌握工程目录精简配置(忽略自动生成冗余文件) 了解工具内置汉化、代码提示等辅助功能 学会创建、管理ArkTS页面文件与组件【本节重…

多方案统一认证体系对比

在多系统、多子域、跨平台应用中,认证与登录状态同步是核心问题。不同架构阶段可采用不同方案,从传统 Session 模型到标准化 OAuth2 / OIDC SSO。域下的登录态共享可以看看之前文章提到了域登录态分享类 SSO。 现在简…

centos更新阿里源并同步更新系统时间

下面是如何替换为阿里源的步骤: 1. 备份原有的仓库配置文件 首先,建议备份原有的仓库配置文件,以防万一需要恢复。 sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup2. 下载阿…

齐次与非齐次的区别

啊丢,线性代数学了这么久,突然想到齐次和非齐次是啥区别。 齐次方程组的右边等于0,因此每个方程中的单项式可以看作次数相等,就叫齐次。 非齐次方程组右边不等于0,因此每个方程中有1次项也有0次项,次数不相等,就…

centos7 无法上网怎么办?

查看本机的IP 方法一:ifconfig查找 en0,inet 后面就是本机的IP方法二: 系统设置 -> WIFI -> 详细信息,弹出的页面也有IP地址信息parallels desktop的网络配置:centos虚拟机 -> 配置 -> 硬件 -> 网…

实用指南:[Linux命令分享]日志查看 — — less

实用指南:[Linux命令分享]日志查看 — — lesspre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &…

论文分享 |Spark-TTS:用解耦语音令牌实现高效可控的语音合成 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

昆明黄金推荐排行

昆明黄金推荐排行引言在昆明这个繁华的城市,黄金市场琳琅满目,消费者在选择黄金时往往会感到困惑。本文将为您提供一份昆明黄金推荐排行,帮助您在众多品牌中找到适合自己的黄金产品。一、品牌实力昆明廖金匠昆明廖金…

NOIP2025反思--杨芮溪

NOIP2025反思--杨芮溪NOIP2025反思

2025深圳/惠州组装线供应商TOP5评测!装配线/生产线/老化线/输送线等五大主流厂家推荐,技术创新+行业经验权威榜单发布,赋能工业自动化升级

随着制造业智能化转型加速,组装线、装配线及生产线设备作为工业生产的核心基础设施,其技术先进性与适配性直接影响企业生产效率与产品质量。本榜单基于技术实力、行业覆盖、服务能力三大维度,结合国内制造业协会数据…

一个很好的观察案例:成功究竟是因为我们比较牛,还是仅仅因为运气

雪球上一个热帖,喜提人生第一个100万作者在藏格上挣到100万,志得意满,分享喜悦。下面都是炫富大会,低于100w的都不好开口。我买过藏格,不过卖飞了,所以看到这个帖子难免心痛。。。不过除了商业互吹,还有个争论很…