void rgUser() { char confPassword[20]; printf("请输入用户名:"); fflush(stdin); //清空缓存区 scanf_s("%s", cust.name); printf("请输入密码:"); fflush(stdin); //清空缓存区 scanf_S("%s", cust.password); printf("请确认密码:"); fflush(stdin); //清空缓存区 scanf_s("%s", &confPassword); if (strcmp(confPassword, cust.password) != 0) { printf("两次密码不相同,请重新输入密码\n"); } //判断密码是否相同 else { pFile = fopen("psMls.dat", "ab+");//ab+打开一个二进制文件,允许读和输入 if (pFile == NULL) { printf("文件打开失败"); system("pause"); //运行到这一行暂停,等用户按下任意键后继续执行程序 } rewind(pFile); //将指针指向文件的初始位置 if (fread(&tempUser, sizeof(struct customer), 1, pFile)) { fseek(pFile, -sizeof(struct customer), 2); fread(&tempUser, sizeof(struct customer), 1, pFile); cust.account = tempUser.account + 1; } else { cust.account = 0001; } fseek(pFile, 0, 2); fwrite(&cust, sizeof(struct customer), 1, pFile); printf("给您分配的账号是:%d\n", cust.account); system("pause"); fclose(pFile); } }
时间: 2024-04-21 08:29:44 浏览: 23
这段代码是一个注册用户的函数,它会提示用户输入用户名和密码,并确认密码是否一致。如果密码一致,它会打开一个二进制文件,检查文件中是否有已经注册的用户。如果有,会为新用户分配一个比最后一个用户账号号码加1的账号号码。如果没有已经注册的用户,会为新用户分配一个初始账号号码0001。最后,它会将新用户的信息写入二进制文件中,并显示给用户分配的账号号码。
相关问题
// 通过终点站查路线 void seek2() { char Name[20]; printf("\n\t输入终点站:"); scanf("%s", Name); int i; for (i = 0; i < line_number; i++) if (strcmp(l_List[i].TO, Name) == 0) { printf("班次信息\n"); printf("班次 发车时间 起点站 终点站 行车时间 额定载量 已订票人数\n"); printf("%-010d%-010s%-010s%-010s%-013.1f%-013d%-012d\n", l_List[i].Train_number, l_List[i].TIME, l_List[i].FROM, l_List[i].TO, l_List[i].Travel_time, l_List[i].Rated_seat, l_List[i].booked); printf("\n\n\t回车键返回"); fflush(stdin); // 清空输入缓冲区,通常是为了确保不影响后面的数据读取. getchar(); // 吃回车 return; } printf("\n\n\t指定信息不存在,回车键返回"); fflush(stdin); // 清空输入缓冲区,通常是为了确保不影响后面的数据读取. getchar(); // 吃回车 };
好的,下面是这段代码的注释说明:
// 通过终点站查路线
void seek2() {
char Name[20]; // 存储输入的终点站名称
printf("\n\t输入终点站:"); // 提示用户输入终点站
scanf("%s", Name); // 获取用户输入的终点站名称
int i; // 定义一个循环计数器
for (i = 0; i < line_number; i++) { // 遍历班次列表
if (strcmp(l_List[i].TO, Name) == 0) { // 如果找到了终点站相同的班次
printf("班次信息\n"); // 打印出班次信息的标题
printf("班次 发车时间 起点站 终点站 行车时间 额定载量 已订票人数\n"); // 打印出班次信息的表头
printf("%-010d%-010s%-010s%-010s%-013.1f%-013d%-012d\n", // 格式化输出班次信息
l_List[i].Train_number, l_List[i].TIME,
l_List[i].FROM, l_List[i].TO, l_List[i].Travel_time,
l_List[i].Rated_seat, l_List[i].booked);
printf("\n\n\t回车键返回"); // 提示用户按回车键返回
fflush(stdin); // 清空输入缓冲区,通常是为了确保不影响后面的数据读取
getchar(); // 吃掉用户按下的回车键
return; // 返回函数调用位置
}
}
printf("\n\n\t指定信息不存在,回车键返回"); // 如果没有找到相应班次,输出指定信息不存在
fflush(stdin); // 清空输入缓冲区,通常是为了确保不影响后面的数据读取
getchar(); // 吃掉用户按下的回车键
};
c语言代码函数名称:findBookByID * 函数功能:按书号检索图书 * 形式参数:p,book型指针,book型一维数组首地址 * 形式参数:num,int型,图书数量 * 形式参数:id,int型,书号 * 返 回 值:返回book型指针。若没有找到图书,则返回空指针NULL;否则,仅返回第1本符合检索条件的图书 */ struct book * findBookByID(struct book *p, int num, int id){ // 请编程实现本函数#include <stdio.h> #include <string.h> typedef struct book{ int id; // 书号 char name[30]; // 书名 char author[30]; //作者 float price; //价格 }BOOK; int main(){ /* 声明函数 */ void input(struct book *p, int num); void output(struct book *p, int num); struct book * findBookByID(struct book *p, int num, int id); struct book * findBookByName(struct book p, int num, char name[]); / 声明变量 */ const int NUM = 3; struct book myBooks[NUM]; struct book *pBook = myBooks; BOOK theBook; / 为myBooks数组的所有元素赋值 / input(pBook, NUM); / 按ID检索图书 / int bookID; scanf("%d", &bookID); fflush(stdin); /清空输入缓冲区/ theBook = findBookByID(pBook, NUM, bookID); /按书号检索/ output(theBook, 1); /输出检索结果/ / 输出myBooks数组的所有元素值 / output(pBook, NUM); return 0; } / *
根据您提供的代码,以下是完整的 C 语言程序:
```
#include <stdio.h>
#include <string.h>
typedef struct book{
int id; // 书号
char name[30]; // 书名
char author[30]; // 作者
float price; // 价格
}BOOK;
/* 按书号检索图书 */
struct book *findBookByID(struct book *p, int num, int id){
for (int i = 0; i < num; i++) {
if (p[i].id == id) {
return &p[i]; // 返回第1本符合检索条件的图书
}
}
return NULL; // 没有找到图书,返回空指针
}
/* 声明函数 */
void input(struct book *p, int num);
void output(struct book *p, int num);
struct book *findBookByName(struct book p, int num, char name[]);
int main(){
const int NUM = 3;
struct book myBooks[NUM];
struct book *pBook = myBooks;
BOOK theBook;
/* 为 myBooks 数组的所有元素赋值 */
input(pBook, NUM);
/* 按 ID 检索图书 */
int bookID;
scanf("%d", &bookID);
fflush(stdin); // 清空输入缓冲区
theBook = *findBookByID(pBook, NUM, bookID); // 按书号检索
output(&theBook, 1); // 输出检索结果
/* 输出 myBooks 数组的所有元素值 */
output(pBook, NUM);
return 0;
}
/* 从键盘输入图书信息 */
void input(struct book *p, int num){
for (int i = 0; i < num; i++) {
printf("请输入第 %d 本书信息:\n", i + 1);
printf("书号:");
scanf("%d", &p[i].id);
fflush(stdin); // 清空输入缓冲区
printf("书名:");
gets(p[i].name);
printf("作者:");
gets(p[i].author);
printf("价格:");
scanf("%f", &p[i].price);
fflush(stdin); // 清空输入缓冲区
}
}
/* 输出图书信息 */
void output(struct book *p, int num){
printf("图书信息如下:\n");
for (int i = 0; i < num; i++) {
printf("书号:%d,书名:%s,作者:%s,价格:%.2f\n", p[i].id, p[i].name, p[i].author, p[i].price);
}
}
/* 按书名检索图书 */
struct book *findBookByName(struct book *p, int num, char name[]){
for (int i = 0; i < num; i++) {
if (strcmp(p[i].name, name) == 0) {
return &p[i]; // 返回第1本符合检索条件的图书
}
}
return NULL; // 没有找到图书,返回空指针
}
```
该程序实现了按书号检索图书的功能,同时还包含了按书名检索图书的函数 `findBookByName`,以及从键盘输入图书信息和输出图书信息的函数 `input` 和 `output`。在 `main` 函数中,用户输入要检索的图书的书号,然后调用 `findBookByID` 函数查找图书并输出检索结果,最后输出所有图书的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)