li8489462
摸鱼党的胜利
2020-05-13 12:20

同学给了个代码,让我学下查找信息和修改信息怎么做,但是我做了半天,找了好多资料,代码一直显示错误,有大佬救救我么?万分感谢!

  • c++

#include
#include
#include
struct student{ //学生结构体
char num[20]; //学号
char name[20]; //姓名
int age; //年龄
char sex[20]; //性别
char tel[12]; // 手机号码
char address[20]; //地址
};

struct Node{
struct student data;
struct Node* next; //数据域
};
//创建表头
struct Node* createlist(){

struct Node* headNode=(struct Node*)malloc(sizeof(struct Node)); //指针变成变量
headNode->next=NULL; //表头数据不做初始化 或者:
return headNode;

}
//创建结点
struct Node* createNode(struct student data){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next = NULL;
return newNode;
}
//插入操作
void insertNodeByHead(struct Node* headNode,struct student data){
struct Node* newNode=createNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
//删除操作
void deleteNodeByNum(struct Node* headNode,char posNum){
struct Node
posNodeFront = headNode;
struct Node* posNode = headNode->next;
if(posNode==NULL){
printf("无相关数据,无法删除!\n");
system("pause");
return;
}
else{
while(strcmp(posNode->data.num,posNum)){
posNodeFront = posNode;
posNode = posNodeFront->next;
if(posNode == NULL){
printf("未找到相关数据,无法删除!\n");
system("pause");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
//查询操作
void findNodeByNum(struct Node* headNode,struct student data){
if(==NULL){
printf("学号输入错误,无法查找!");
system("pause");
return;
}
if()

}
//打印操作
void printlist(struct Node* headNode){
struct Node* pMove = headNode->next;
printf("\t学号\t姓名\t性别\t年龄\t电话\t地址\n");
while(pMove){
printf("%s\t%s\t%s\t%d\t%s\t%s\n",pMove->data.num,pMove->data.name,
pMove->data.sex,pMove->data.age,pMove->data.tel,pMove->data.address);
pMove = pMove->next;
}
printf("\n");
}

//链表读文件操作
void readStudentInfoFromFile(struct Node* headNode,const char *fileName){

//打开文件
FILE *read = fopen(fileName,"r");
if(read==NULL)

    read=fopen(fileName,"w+");
 //读文件
 struct student data;
 while(fscanf(read,"%s\t%s\t%s\t%d\t%s\t%s",data.num,data.name,data.sex,data.age,data.tel,data.address)!=EOF)
 {
    insertNodeByHead(headNode,data);
    memset(&data,0,sizeof(struct student));
 }
 //关闭文件
 fclose(read); 

}

void writeStudentInfoToFile(struct Node* headNode,const char fileName){
//打开文件
FILE *write=fopen(fileName,"w");
struct Node*pMove=headNode->next;
while(pMove){
fprintf(write,"%s\t%s\t%s\t%d\t%s\t%s\n",pMove->data.num,pMove->data.name,
pMove->data.sex,pMove->data.age,pMove->data.tel,pMove->data.address);
pMove=pMove->next;
}
fclose(write);
}
void menu()
{
printf("\t\t\t
欢迎来到学生信息管理系统 \n\n");
printf("\t
**********************************************************\n");
printf("\t* [1] 录入学生信息 \n");
printf("\t
[2] 浏览学生信息 \n");
printf("\t
[3] 删除学生信息 \n");
printf("\t
[4] 查找学生信息 \n");
printf("\t
[5] 修改学生信息 \n");
printf("\t
[6] 退出系统 \n");
printf("\t
*********************************************************\n");
printf("左边数字对应功能选择,请选1-6:");

}
struct Node* studentlist = createlist();
void keyDown(){
int userKey=0;
scanf("%d",&userKey);
struct student userData;
switch(userKey){
case 1:
printf("\t---------------录入信息---------------\n");
printf("请输入以下学生信息:\n");
printf("\t学号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t地址\n");
scanf("%s %s %s %d %s %s",userData.num,userData.name,userData.sex,&userData.age,userData.tel,userData.address);
insertNodeByHead(studentlist,userData);

break;
case 2:
printf("\t---------------浏览信息---------------\n");
printlist(studentlist);
break;
case 3:
printf("\t---------------删除信息---------------\n");
printf("请输入删除学生学号:");
scanf("%s",userData.num) ;
deleteNodeByNum(studentlist,userData.num);

        break;
    case 4:
        printf("\t---------------查询信息---------------\n");
        break;
    case 5:
        printf("\t---------------修改信息---------------\n");
        break;
    case 6:
        printf("\t---------------退出系统---------------\n");
        printf("正常退出,欢迎下次使用!\n");
        system("pause");
        exit(0);
        break;
    default:
        printf("输入错误,请重新输入!\n");
        break;      

}   

}

int main(){
readStudentInfoFromFile(studentlist,"studentInfo.txt");
while(1)
{
menu();
keyDown();
writeStudentInfoToFile(studentlist,"studentInfo.txt");
system("pause");
system("cls");

} 



system("pause");     
return 0;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答