差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
| aa [2024/12/02 18:30] – doge24190 | aa [2025/04/15 01:32] (当前版本) – doge24190 | ||
|---|---|---|---|
| 行 7: | 行 7: | ||
| 点击下载 | 点击下载 | ||
| - | 以下是根据题目要求设计的两个C语言程序: | + | {{tag>存档}} |
| - | + | ||
| - | (1) insert函数 | + | |
| - | + | ||
| - | 设计一个char* insert(char* s1, char* s2, int n)函数,在字符串s1的第n个位置插入字符串s2: | + | |
| - | + | ||
| - | #include < | + | |
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | char* insert(char* s1, char* s2, int n) { | + | |
| - | int len1 = strlen(s1); | + | |
| - | int len2 = strlen(s2); | + | |
| - | char* result = (char*)malloc(len1 + len2 + 1); // 分配新的内存 | + | |
| - | + | ||
| - | if (n > len1) n = len1; // 防止n超出范围 | + | |
| - | + | ||
| - | // 拷贝s1前n个字符 | + | |
| - | strncpy(result, | + | |
| - | result[n] = ' | + | |
| - | + | ||
| - | // 拼接s2 | + | |
| - | strcat(result, | + | |
| - | + | ||
| - | // 拼接s1剩余部分 | + | |
| - | strcat(result, | + | |
| - | + | ||
| - | return result; | + | |
| - | } | + | |
| - | + | ||
| - | int main() | + | |
| - | char s1[100], s2[100]; | + | |
| - | int n; | + | |
| - | + | ||
| - | printf(" | + | |
| - | scanf(" | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | + | ||
| - | char* result = insert(s1, s2, n); | + | |
| - | printf(" | + | |
| - | + | ||
| - | free(result); | + | |
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | (2) 改写strcpy函数 | + | |
| - | + | ||
| - | 利用指针实现char* strcpy(char* s1, char* s2)的功能: | + | |
| - | + | ||
| - | #include <stdio.h> | + | |
| - | + | ||
| - | char* strcpy_custom(char* s1, char* s2) { | + | |
| - | char* p = s1; // 保存起始地址 | + | |
| - | while ((*s1++ = *s2++) != ' | + | |
| - | return p; | + | |
| - | } | + | |
| - | + | ||
| - | int main() { | + | |
| - | char s1[100], s2[100]; | + | |
| - | + | ||
| - | printf(" | + | |
| - | scanf(" | + | |
| - | + | ||
| - | strcpy_custom(s1, | + | |
| - | printf(" | + | |
| - | + | ||
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | 以上代码分别完成了两个任务,运行后输入测试数据即可查看结果。 | + | |
| - | + | ||
| - | + | ||
| - | 根据图片中的要求,以下是各题的 C 语言程序实现: | + | |
| - | + | ||
| - | (1) 学生信息的结构体 | + | |
| - | + | ||
| - | #include < | + | |
| - | + | ||
| - | typedef struct { | + | |
| - | char name[50]; | + | |
| - | int id; | + | |
| - | int age; | + | |
| - | } Student; | + | |
| - | + | ||
| - | int main() { | + | |
| - | Student students[5]; | + | |
| - | int sum_age = 0, min_age_index = 0; | + | |
| - | + | ||
| - | // 输入学生信息 | + | |
| - | for (int i = 0; i < 5; i++) { | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | sum_age += students[i].age; | + | |
| - | if (students[i].age < students[min_age_index].age) { | + | |
| - | min_age_index = i; | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | printf(" | + | |
| - | printf(" | + | |
| - | | + | |
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | (2) Student_Record 的结构体和排序 | + | |
| - | + | ||
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | typedef struct { | + | |
| - | char name[50]; | + | |
| - | char birth_date[15]; | + | |
| - | float total_score; | + | |
| - | } StudentRecord; | + | |
| - | + | ||
| - | int main() { | + | |
| - | StudentRecord students[10]; | + | |
| - | + | ||
| - | // 输入学生信息 | + | |
| - | for (int i = 0; i < 10; i++) { | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | } | + | |
| - | + | ||
| - | // 按总分排序 | + | |
| - | for (int i = 0; i < 9; i++) { | + | |
| - | for (int j = i + 1; j < 10; j++) { | + | |
| - | if (students[i].total_score < students[j].total_score) { | + | |
| - | StudentRecord temp = students[i]; | + | |
| - | students[i] = students[j]; | + | |
| - | students[j] = temp; | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | printf(" | + | |
| - | for (int i = 0; i < 10; i++) { | + | |
| - | printf(" | + | |
| - | | + | |
| - | } | + | |
| - | + | ||
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | (3) 城市信息的排序 | + | |
| - | + | ||
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | typedef struct { | + | |
| - | char name[50]; | + | |
| - | int population; | + | |
| - | int culture_level; | + | |
| - | } City; | + | |
| - | + | ||
| - | void sortCities(City cities[], int n, int key) { | + | |
| - | for (int i = 0; i < n - 1; i++) { | + | |
| - | for (int j = i + 1; j < n; j++) { | + | |
| - | if ((key == 1 && strcmp(cities[i].name, | + | |
| - | (key == 2 && cities[i].population < cities[j].population) || | + | |
| - | (key == 3 && cities[i].culture_level < cities[j].culture_level)) { | + | |
| - | City temp = cities[i]; | + | |
| - | cities[i] = cities[j]; | + | |
| - | cities[j] = temp; | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | int main() { | + | |
| - | City cities[5]; | + | |
| - | + | ||
| - | // 随机输入城市信息 | + | |
| - | for (int i = 0; i < 5; i++) { | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | } | + | |
| - | + | ||
| - | // 排序并显示 | + | |
| - | for (int i = 1; i <= 3; i++) { | + | |
| - | sortCities(cities, | + | |
| - | printf(" | + | |
| - | for (int j = 0; j < 5; j++) { | + | |
| - | printf(" | + | |
| - | | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | (4) 书名排序 | + | |
| - | + | ||
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | typedef struct { | + | |
| - | char title[50]; | + | |
| - | } Book; | + | |
| - | + | ||
| - | void sortBooks(Book books[], int n) { | + | |
| - | for (int i = 0; i < n - 1; i++) { | + | |
| - | for (int j = i + 1; j < n; j++) { | + | |
| - | if (strcmp(books[i].title, | + | |
| - | Book temp = books[i]; | + | |
| - | books[i] = books[j]; | + | |
| - | books[j] = temp; | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | int main() { | + | |
| - | Book books[10]; | + | |
| - | + | ||
| - | // 输入书名 | + | |
| - | for (int i = 0; i < 10; i++) { | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | } | + | |
| - | + | ||
| - | sortBooks(books, | + | |
| - | + | ||
| - | printf(" | + | |
| - | for (int i = 0; i < 10; i++) { | + | |
| - | printf(" | + | |
| - | } | + | |
| - | + | ||
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | (5-7) 单向链表操作 | + | |
| - | + | ||
| - | #include < | + | |
| - | #include < | + | |
| - | + | ||
| - | typedef struct Node { | + | |
| - | char name[50]; | + | |
| - | int age; | + | |
| - | float salary; | + | |
| - | struct Node* next; | + | |
| - | } Node; | + | |
| - | + | ||
| - | Node* createList() { | + | |
| - | Node* head = NULL; | + | |
| - | Node* temp = NULL; | + | |
| - | + | ||
| - | for (int i = 0; i < 5; i++) { | + | |
| - | Node* newNode = (Node*)malloc(sizeof(Node)); | + | |
| - | printf(" | + | |
| - | scanf(" | + | |
| - | newNode-> | + | |
| - | + | ||
| - | if (head == NULL) { | + | |
| - | head = newNode; | + | |
| - | } else { | + | |
| - | temp-> | + | |
| - | } | + | |
| - | temp = newNode; | + | |
| - | } | + | |
| - | return head; | + | |
| - | } | + | |
| - | + | ||
| - | void displayList(Node* head) { | + | |
| - | Node* temp = head; | + | |
| - | while (temp != NULL) { | + | |
| - | printf(" | + | |
| - | temp = temp-> | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | Node* insertNode(Node* head, int position, char name[], int age, float salary) { | + | |
| - | Node* newNode = (Node*)malloc(sizeof(Node)); | + | |
| - | strcpy(newNode-> | + | |
| - | newNode-> | + | |
| - | newNode-> | + | |
| - | + | ||
| - | if (position == 0) { | + | |
| - | newNode-> | + | |
| - | return newNode; | + | |
| - | } | + | |
| - | + | ||
| - | Node* temp = head; | + | |
| - | for (int i = 0; i < position - 1 && temp != NULL; i++) { | + | |
| - | temp = temp-> | + | |
| - | } | + | |
| - | + | ||
| - | if (temp != NULL) { | + | |
| - | newNode-> | + | |
| - | temp-> | + | |
| - | } | + | |
| - | return head; | + | |
| - | } | + | |
| - | + | ||
| - | Node* deleteNode(Node* head, int position) { | + | |
| - | if (position == 0 && head != NULL) { | + | |
| - | Node* temp = head; | + | |
| - | head = head-> | + | |
| - | free(temp); | + | |
| - | return head; | + | |
| - | } | + | |
| - | + | ||
| - | Node* temp = head; | + | |
| - | for (int i = 0; i < position - 1 && temp != NULL; i++) { | + | |
| - | temp = temp-> | + | |
| - | } | + | |
| - | + | ||
| - | if (temp != NULL && temp-> | + | |
| - | Node* toDelete = temp-> | + | |
| - | temp-> | + | |
| - | free(toDelete); | + | |
| - | } | + | |
| - | return head; | + | |
| - | } | + | |
| - | + | ||
| - | int main() { | + | |
| - | Node* head = createList(); | + | |
| - | printf(" | + | |
| - | displayList(head); | + | |
| - | + | ||
| - | head = insertNode(head, | + | |
| - | printf(" | + | |
| - | displayList(head); | + | |
| - | + | ||
| - | head = deleteNode(head, | + | |
| - | printf(" | + | |
| - | displayList(head); | + | |
| - | + | ||
| - | return 0; | + | |
| - | } | + | |
| - | + | ||
| - | 以上程序覆盖了所有要求,如有需要调整的部分,请告诉我! | + | |