2024.3.28

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);
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值