#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 20 #define MAX_ADDRESS_LEN 50 #define MAX_SEX_LEN 5 #define MAX_PHONE_LEN 11 #define MAX_STUDENT_NUM 1000 // 定义学生结构体 typedef struct { char name[MAX_NAME_LEN]; char address[MAX_ADDRESS_LEN]; char SEX[MAX_SEX_LEN]; char phone[MAX_PHONE_LEN]; } Student; // 定义通讯录结构体 typedef struct { Student students[MAX_STUDENT_NUM]; int num_students; } AddressBook;请写一段修改学生信息的代码
时间: 2024-02-13 18:00:23 浏览: 28
好的,可以参考下面的代码实现修改学生信息:
```c
void modifyStudentInfo(AddressBook *addressBook, int index) {
Student *student = &(addressBook->students[index]);
printf("请输入学生姓名:\n");
scanf("%s", student->name);
printf("请输入学生地址:\n");
scanf("%s", student->address);
printf("请输入学生性别:\n");
scanf("%s", student->SEX);
printf("请输入学生电话号码:\n");
scanf("%s", student->phone);
printf("修改成功!\n");
}
```
这个函数接受两个参数,一个是通讯录结构体指针,另一个是要修改的学生在通讯录中的下标。函数会根据用户输入来修改学生的信息,修改完成后会输出提示信息。注意,函数没有对用户输入进行校验,实际应用中需要加入相应的校验逻辑。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 20 struct Clothes { char id[10]; char brand[20]; char style[20]; char color[10]; int stock; float purchase_price; float sale_price; char payment_method[10]; }; void read_clothes(struct Clothes* clothes) { printf("请输入编号:"); scanf("%s", clothes->id); printf("请输入品牌:"); scanf("%s", clothes->brand); printf("请输入款式:"); scanf("%s", clothes->style); printf("请输入颜色:"); scanf("%s", clothes->color); printf("请输入库存量:"); scanf("%d", &clothes->stock); printf("请输入进货价格:"); scanf("%f", &clothes->purchase_price); printf("请输入销售价格:"); scanf("%f", &clothes->sale_price);为此代码写一段文件导入函数
可以使用C语言中的文件操作函数将文件中的数据导入到结构体数组中,下面是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
struct Clothes {
char id[10];
char brand[20];
char style[20];
char color[10];
int stock;
float purchase_price;
float sale_price;
char payment_method[10];
};
void read_clothes(struct Clothes* clothes) {
printf("请输入编号:");
scanf("%s", clothes->id);
printf("请输入品牌:");
scanf("%s", clothes->brand);
printf("请输入款式:");
scanf("%s", clothes->style);
printf("请输入颜色:");
scanf("%s", clothes->color);
printf("请输入库存量:");
scanf("%d", &clothes->stock);
printf("请输入进货价格:");
scanf("%f", &clothes->purchase_price);
printf("请输入销售价格:");
scanf("%f", &clothes->sale_price);
}
void import_clothes_from_file(struct Clothes* clothes, int* count) {
FILE* file = fopen("clothes.txt", "r");
if (file == NULL) {
printf("Failed to open file!");
return;
}
while (!feof(file)) {
fscanf(file, "%s %s %s %s %d %f %f %s", clothes[*count].id, clothes[*count].brand, clothes[*count].style, clothes[*count].color, &clothes[*count].stock, &clothes[*count].purchase_price, &clothes[*count].sale_price, clothes[*count].payment_method);
(*count)++;
}
fclose(file);
}
int main() {
struct Clothes clothes[100];
int count = 0;
import_clothes_from_file(clothes, &count);
for (int i = 0; i < count; i++) {
printf("编号:%s\n", clothes[i].id);
printf("品牌:%s\n", clothes[i].brand);
printf("款式:%s\n", clothes[i].style);
printf("颜色:%s\n", clothes[i].color);
printf("库存量:%d\n", clothes[i].stock);
printf("进货价格:%f\n", clothes[i].purchase_price);
printf("销售价格:%f\n", clothes[i].sale_price);
printf("支付方式:%s\n", clothes[i].payment_method);
printf("\n");
}
return 0;
}
```
在上面的示例代码中,我们定义了一个结构体数组clothes,用于存储从文件中读取的数据。我们使用import_clothes_from_file函数从文件中读取数据,并将其存储到clothes数组中。其中,我们使用fscanf函数逐行读取文件内容,并将其存储到对应的结构体中。
需要注意的是,读取字符串时需要使用%s格式,读取整数时需要使用%d格式,读取浮点数时需要使用%f格式。
在main函数中,我们调用import_clothes_from_file函数读取文件中的数据,并使用循环输出clothes数组中的每个结构体的内容。
需要注意的是,以上示例代码只是一个简单的例子,实际使用中还需要考虑文件不存在、读取格式错误等异常情况。
根据这段代码#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 20 #define MAX_ADDRESS_LEN 50 #define MAX_SEX_LEN 5 #define MAX_PHONE_LEN 11 #define MAX_STUDENT_NUM 1000 // 定义学生结构体 typedef struct { char name[MAX_NAME_LEN]; char address[MAX_ADDRESS_LEN]; char SEX[MAX_SEX_LEN]; char phone[MAX_PHONE_LEN]; } Student; // 定义通讯录结构体 typedef struct { Student students[MAX_STUDENT_NUM]; int num_students; } AddressBook;修改void modifyStudentInfo(AddressBook *book) { assert(book); char name[NAME_MAX]; printf("请输入需要修改信息用户的名字:>\n"); scanf("%s", name); int pos = FindByName(pc, name);//pos为要寻找的人的下标 if (pos == -1) { printf("查无此人\n"); } else { int num = 0; do { printf("请输入你想修改此用户的信息\n"); printf(" 0.退出 1.姓名 2.性别 3.年龄 4.电话 5.地址:>\n"); scanf("%d", &num); switch (num) { case NAME: printf("请输入你想修改的姓名:>\n"); scanf("%s", pc->data[pos].name); break; case SEX: printf("请输入你想修改的性别:>\n"); scanf("%s", pc->data[pos].sex); break; case AGE: printf("请输入你想修改的年龄:>\n"); scanf("%d", &(pc->data[pos].age)); break; case TELE: printf("请输入你想修改的电话:>\n"); scanf("%s", pc->data[pos].tele); break; case ADDR: printf("请输入你想修改的地址:>\n"); scanf("%s", pc->data[pos].addr); break; case 0: printf("不修改退回界面\n"); break; default:printf("无效操作数!\n"); } } while (num); } }这段代码
这段代码中有几个问题:
1. `name`数组的大小没有定义,应该改为`char name[MAX_NAME_LEN]`。
2. `FindByName`函数的参数应该是`book`而不是`pc`,因为`book`是传递给`modifyStudentInfo`函数的指针。
3. `sex`、`age`、`tele`和`addr`这些变量都没有定义,应该改为`SEX`、`AGE`、`TELE`和`ADDR`,并且在前面应该定义它们的最大长度。
4. 在`switch`语句中,`case`后的常量应该改为`NAME`、`SEX`、`AGE`、`TELE`和`ADDR`,因为这些是宏定义的常量。
5. 在`scanf`函数中,应该使用`%s`来读取字符串类型的变量,而不是使用`%d`。
6. 在修改`sex`、`age`、`tele`和`addr`时,应该使用结构体中定义的变量名,而不是使用错误的变量名。
修改后的代码如下所示:
```
void modifyStudentInfo(AddressBook *book) {
assert(book);
char name[MAX_NAME_LEN];
printf("请输入需要修改信息用户的名字:>\n");
scanf("%s", name);
int pos = FindByName(book, name);
if (pos == -1) {
printf("查无此人\n");
} else {
int num = 0;
do {
printf("请输入你想修改此用户的信息\n");
printf(" 0.退出 1.姓名 2.性别 3.年龄 4.电话 5.地址:>\n");
scanf("%d", &num);
switch (num) {
case NAME:
printf("请输入你想修改的姓名:>\n");
scanf("%s", book->students[pos].name);
break;
case SEX:
printf("请输入你想修改的性别:>\n");
scanf("%s", book->students[pos].SEX);
break;
case AGE:
printf("请输入你想修改的年龄:>\n");
scanf("%d", &(book->students[pos].AGE));
break;
case TELE:
printf("请输入你想修改的电话:>\n");
scanf("%s", book->students[pos].phone);
break;
case ADDR:
printf("请输入你想修改的地址:>\n");
scanf("%s", book->students[pos].address);
break;
case 0:
printf("不修改退回界面\n");
break;
default:
printf("无效操作数!\n");
}
} while (num);
}
}
```