这是本文档旧的修订版!
c语言程序设计基础实验
点击下载
以下是根据题目要求设计的两个C语言程序:
(1) insert函数
设计一个char* insert(char* s1, char* s2, int n)函数,在字符串s1的第n个位置插入字符串s2:
#include <stdio.h> #include <string.h> #include <stdlib.h>
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, s1, n); result[n] = '\0';
// 拼接s2 strcat(result, s2);
// 拼接s1剩余部分 strcat(result, s1 + n);
return result;
}
int main() {
char s1[100], s2[100]; int n;
printf("请输入字符串s1: ");
scanf("%s", s1);
printf("请输入字符串s2: ");
scanf("%s", s2);
printf("请输入插入位置n: ");
scanf("%d", &n);
char* result = insert(s1, s2, n);
printf("结果字符串: %s\n", result);
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++) != '\0'); // 逐字符复制 return p;
}
int main() {
char s1[100], s2[100];
printf("请输入源字符串s2: ");
scanf("%s", s2);
strcpy_custom(s1, s2);
printf("复制结果s1: %s\n", s1);
return 0;
}
以上代码分别完成了两个任务,运行后输入测试数据即可查看结果。
根据图片中的要求,以下是各题的 C 语言程序实现:
(1) 学生信息的结构体
#include <stdio.h>
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("输入学生%d的姓名、学号和年龄:\n", i + 1);
scanf("%s %d %d", students[i].name, &students[i].id, &students[i].age);
sum_age += students[i].age;
if (students[i].age < students[min_age_index].age) {
min_age_index = i;
}
}
printf("\n平均年龄: %.2f\n", sum_age / 5.0);
printf("年龄最小的学生信息: 姓名: %s, 学号: %d, 年龄: %d\n",
students[min_age_index].name, students[min_age_index].id, students[min_age_index].age);
return 0;
}
(2) Student_Record 的结构体和排序
#include <stdio.h> #include <string.h>
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("输入第%d个学生的姓名、出生日期和总分:\n", i + 1);
scanf("%s %s %f", students[i].name, students[i].birth_date, &students[i].total_score);
}
// 按总分排序
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("\n按总分排序的学生信息:\n");
for (int i = 0; i < 10; i++) {
printf("姓名: %s, 出生日期: %s, 总分: %.2f\n",
students[i].name, students[i].birth_date, students[i].total_score);
}
return 0;
}
(3) 城市信息的排序
#include <stdio.h> #include <string.h>
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, cities[j].name) > 0) ||
(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("输入第%d个城市的名称、人口和文化程度:\n", i + 1);
scanf("%s %d %d", cities[i].name, &cities[i].population, &cities[i].culture_level);
}
// 排序并显示
for (int i = 1; i <= 3; i++) {
sortCities(cities, 5, i);
printf("\n排序方式 %d 的结果:\n", i);
for (int j = 0; j < 5; j++) {
printf("城市: %s, 人口: %d, 文化程度: %d\n",
cities[j].name, cities[j].population, cities[j].culture_level);
}
}
return 0;
}
(4) 书名排序
#include <stdio.h> #include <string.h>
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, books[j].title) > 0) {
Book temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}
}
int main() {
Book books[10];
// 输入书名
for (int i = 0; i < 10; i++) {
printf("输入第%d本书的书名:\n", i + 1);
scanf("%s", books[i].title);
}
sortBooks(books, 10);
printf("\n排序后的书名:\n");
for (int i = 0; i < 10; i++) {
printf("%s\n", books[i].title);
}
return 0;
}
(5-7) 单向链表操作
#include <stdio.h> #include <stdlib.h>
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("输入第%d个节点的姓名、年龄和工资:\n", i + 1);
scanf("%s %d %f", newNode->name, &newNode->age, &newNode->salary);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp->next = newNode;
}
temp = newNode;
}
return head;
}
void displayList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("姓名: %s, 年龄: %d, 工资: %.2f\n", temp->name, temp->age, temp->salary);
temp = temp->next;
}
}
Node* insertNode(Node* head, int position, char name[], int age, float salary) {
Node* newNode = (Node*)malloc(sizeof(Node)); strcpy(newNode->name, name); newNode->age = age; newNode->salary = salary;
if (position == 0) {
newNode->next = head;
return newNode;
}
Node* temp = head;
for (int i = 0; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL) {
newNode->next = temp->next;
temp->next = newNode;
}
return head;
}
Node* deleteNode(Node* head, int position) {
if (position == 0 && head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
return head;
}
Node* temp = head;
for (int i = 0; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL && temp->next != NULL) {
Node* toDelete = temp->next;
temp->next = temp->next->next;
free(toDelete);
}
return head;
}
int main() {
Node* head = createList();
printf("\n链表内容:\n");
displayList(head);
head = insertNode(head, 2, "新节点", 25, 5000.0);
printf("\n插入后的链表:\n");
displayList(head);
head = deleteNode(head, 2);
printf("\n删除后的链表:\n");
displayList(head);
return 0;
}
以上程序覆盖了所有要求,如有需要调整的部分,请告诉我!
评论