include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct stu{
char name[20];
int age;
int score;
struct stu* next;
}Stu,*node_p;
node_p Stu_head();
node_p Stu_create(char* name,int age,int score);
void head_install(node_p H,char* name,int age,int score);
void File_install(node_p H);
void File_scanf(node_p H);
void show(node_p H);
//main
int main(int argc, const char *argv[])
{
//struct
node_p H=Stu_head();
head_install(H,"zhangsan",20,98);
head_install(H,"wangwu",19,91);
head_install(H,"lisi",21,87);
head_install(H,"huawei",20,100);
//File
//File_install(H);
show(H);
//printf("%s\n",H->name);
//printf("%s\n",H->next->name);
printf("*******************************************\n");
File_scanf(H);
show(H);
return 0;
}
//function
node_p Stu_head(){
node_p H=(node_p)malloc(sizeof(Stu));
if(H==NULL){
printf("error\n");
return NULL;
}
H->next=NULL;
return H;
}
node_p Stu_create(char* name,int age,int score){
node_p new=(node_p)malloc(sizeof(Stu));
if(new==NULL){
printf("error\n");
return NULL;
}
for(int i=0;i<strlen(name);i++){
new->name[i]=name[i];
}
new->age=age;
new->score=score;
new->next=NULL;
return new;
}
void head_install(node_p H,char* name,int age,int score){
if(H==NULL){
printf("error\n");
return;
}
node_p new=Stu_create(name,age,score);
new->next=H->next;
H->next=new;
}
void show(node_p H){
if(NULL == H)
{
printf("1111111111111\n");
return ;
}
node_p P=H;
while(1){
if(P->next==NULL){break;}
printf("%s ",P->next->name);
printf("%d ",P->next->age);
printf("%d\n",P->next->score);
P=P->next;
}
}
void File_install(node_p H){
FILE* fp=fopen("./score.txt","w");
if(fp==NULL){
printf("ERROR\n");
return;
}
while(1){
node_p P=H;
if(P->next==NULL){break;}
fprintf(fp,"%s ",P->next->name);
fprintf(fp,"%d ",P->next->age);
fprintf(fp,"%d\n",P->next->score);
P=P->next;
}
fclose(fp);
}
void File_scanf(node_p H){
FILE* fp=fopen("./score.txt","r");
if(fp==NULL){
printf("error\n");
return;
}
while(1){
char name[20]={0};
int age;
int score;
int res=fscanf(fp,"%s",name);
if (res==-1){break;}
fscanf(fp,"%d",&age);
fscanf(fp,"%d",&score);
head_install(H,name,age,score);
}
}
2024.3.28
最新推荐文章于 2024-09-12 19:02:20 发布