高级语言第十一章,结构体与文件操作

1.定义结构体类型来描述如图所示的学生信息卡片

姓名:性别:学号:
出生日期:                           年         月         日
所在单位:                           学院         年级         班级
#include <stdio.h>
#include <stdbool.h>

typedef struct date{
	int year,month,day;
}date;

typedef struct affliation{
	char college[10];
	int grade,clas;
}affliation;

typedef struct student{
	char name[10];
	bool gender;
	int code;
	struct date birth;
	struct affliation address;
}student;

2.成绩信息包括学号,姓名,讨论成绩,报告成绩,测试成绩5项信息,规定实验成绩=20%讨论成绩+20%报告成绩+60%测试成绩,并对题中的链表按实验成绩从高到低排序

1)创建结点类型

2)加入所有信息都存储在文件2018Exp.txt中,创建链表

#include <stdio.h>
#include <stdlib.h>

typedef struct grade {
	int num;
	char name[10];
	int disscore;
	int repscore;
	int testscore;
	int totscore;
	struct grade *next;
} grade;

typedef struct grade *pgrade;
pgrade head = NULL;
int main() {
	FILE* fpin;
	fpin=fopen("2018Exp.txt","r");
	pgrade p;
	while(!feof(fpin)) {
		p = (pgrade)malloc(sizeof(struct grade));
		fscanf(fpin,"%d",&p->num);
		fscanf(fpin,"%s",&p->name);
		fscanf(fpin,"%d",&p->disscore);
		fscanf(fpin,"%d",&p->repscore);
		fscanf(fpin,"%d",&p->testscore);
		p->totscore=(int)(0.2*p->disscore+0.2*p->repscore+0.6*p->testscore);
		p->next=NULL;
		if(head==NULL)
			head=p;
		else {
			pgrade p1=head,p2=NULL;
			if(p1->totscore<p->totscore) {
				p->next=head;
				head=p;
			}
			while(p1->totscore>=p->totscore&&p1!=NULL) {
				p2=p1;
				p1=p1->next;
			}
			if(p1==NULL)
				p2->next=p;
			else{
				p2->next=p;
				p->next=p1;
			}
		}
	}
	fclose(fpin);
	return 0;
}

3.某班学生的信息包括学号,姓名,性别,成绩等信息

1)完成学生的结构定义

2)编一名为CreatList的函数实现建立具有n个节点的链表来存储这个班的学生信息

3)编一函数WriteFile,将该班的信息存入文件

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define n 20

typedef struct user {
	int num;
	char sex;
	char name[20];
	int score;
	struct user* next;
} user;

typedef struct user *puser;
puser head=NULL;
puser CreatList() {
	puser p;
	int i=n;
	while(i) {
		p=(puser)malloc(sizeof(struct user));
		scanf("%d%c",&p->num,&p->sex);
		scanf("%s",&p->name);
		scanf("%d",&p->score);
		p->next=NULL;
		if(head==NULL) head=p;
		else {
			puser p1=head,p2=NULL;
			if(p1->num>p->num) {
				p->next=head;
				head=p;
			}
			while(p1->num<p->num&&p1!=NULL) {
				p2=p1;
				p1=p1->next;
			}
			if(p1==NULL)
				p2->next=p;
			else {
				p2->next=p;
				p->next=p1;
			}
		}
		i--;
	}
	return head;
}

bool WriteFile(puser p) {
	FILE* fpin;
	if((fpin=fopen("output.txt","w"))==NULL) {
		printf("FILE OPEN ERROR");
		return false;
	}
	while(p) {
		fprintf(fpin,"%d     ",p->num);
		fprintf(fpin,"%s     ",p->name);
		fprintf(fpin,"%c     ",p->sex);
		fprintf(fpin,"%d\n",p->score);
		p=p->next;
	}
	fclose(fpin);
	return 0;
}

4.编一函数,用冒泡法实现由整数组成的单向链表的排序

#include <stdio.h>
#include <stdlib.h>
#define len 10

typedef struct node {
	int data;
	struct node *next;
} node;

struct node *bubble(struct node *head) {
	struct node *p1,*p2;
	for(int i=0; i<len-1; i++) {
		p1=head;
		p2=p1->next;
		for(int j=0; j<len-i-1; j++) {
			if(p1->data>p2->data) {
				int temp=p1->data;
				p1->data=p2->data;
				p2->data=temp;
			}
			p1=p1->next;
			p2=p2->next;
		}
	}
	return head;
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MIN 0

typedef struct node {
	int data;
	struct node *next;
} ListNode;

ListNode* bubble(ListNode* head) {
	ListNode *r0,*p0,*p,*q,*rs;
	r0=(ListNode*)malloc(sizeof(ListNode));
	r0->next=head;
	r0->data=MIN;
	bool flag=true;
	while(flag) {
		q=r0;
		p0=r0->next;
		flag=false;
		while(p!=NULL) {
			if(p0->data>p->data) {
				q->next=p;
				p->next=p0;
				flag=true;
			}
			q=q->next;
			p0=q->next;
			p=p0->next;
		}
	}
	rs=r0->next;
	free(r0);
	return rs;
}

5.每个学生信息卡片包括学生姓名和出生日期,从键盘一次输入若干学生信息,来创建一个用于管理学生信息的单向链表

1)请给出该链表中每个结点的数据类型定义

2)请编写程序,按照姓名的次序,依次插入每个学生信息,最后将链表中所有学生信息存入文本文件student.txt中

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct date {
	int year,month,day;
} date;

typedef struct card {
	char name[20];
	struct date birth;
	struct card* next;
} card;

struct card* insert(card* head,card* newcard) {
	if(head==NULL) {
		head=newcard;
		head->next=NULL;
		return head;
	}
	card* p=head,*p1=NULL;
	while(p!=NULL&&(strcmp(p->name,newcard->name)==-1)) {
		p1=p;
		p=p->next;
	}
	if(p1==NULL) {
		head=newcard;
		newcard->next=p;
	} else {
		p1->next=newcard;
		newcard->next=p;
	}
	return head;
}

void write(card* head) {
	FILE *fp;
	card *p=head;
	if((fp=fopen("student.txt","w"))==NULL)
		printf("CAN NOT OPEN FILE");
	else {
		while(p!=NULL) {
			if(fprintf(fp,"%s\t%d-%d-%d\n",p->name,p->birth.year,p->birth.month,p->birth.day))
				printf("FILE WRITE ERROR");
			p=p->next;
		}
		fclose(fp);
	}
}

int main() {
	int i;
	card *p,*head=NULL;
	printf("1-continue input 2-end");
	scanf("%d",&i);
	while(i==1) {
		p=(card*)malloc(sizeof(card));
		p->next=NULL;
		printf("NAME:?");
		scanf("%s",p->name);
		printf("year:?");
		scanf("%d",&p->birth.year);
		printf("month:?");
		scanf("%d",&p->birth.month);
		printf("day:?");
		scanf("%d",&p->birth.day);
		head=insert(head,p);
	}
	write(head);
	return 0;
}

6.从键盘上输入一个字符串,把该字符串的小写字母转换为大写字母,并将转换后的字符串输出到文件test.txt中,编程实现上述功能

#include <stdio.h>
int main() {
	FILE *fp;
	char str[100];
	int i=0;
	printf("input a string\n");
	scanf("%s",&str);
	while(str[i]!='\0') {
		if(str[i]>='a'&&str[i]<='z')
			str[i]=str[i]-'a'+'A';
		i++;
	}
	if((fp=fopen("text.txt","w"))==NULL)
		printf("CAN NOT OPEN");
	else if(fprintf(fp,"%s\n",str)==0)
		printf("FILE WRITE ERROR");
	fclose(fp);
	return 0;
}

7.把一个由数字和字母构成的字符串用单向链表存储,每个结点存储一个字符,请完成如下功能:

1)请给出节点的数据类型的定义

2)编写一个函数将链表中的所有数字串到前面,而字母串到后面,且数字与数字,字母与字母之间的相对顺序不变

#include <stdio.h>
#include <stdlib.h>

typedef struct item {
	char ch;
	struct item *next;
} item;

typedef struct item *pitem;
pitem filter(pitem head) {
	pitem p0,p,p1;
	pitem q0,q,q1;
	pitem tmp,r;
	p0=(pitem)malloc(sizeof(struct item));
	p0->next=head;
	p=p0;
	p1=head;
	while((p1->ch<='9')&&(p1->ch>-'0')) {
		p=p->next;
		p1=p1->next;
	}
	q0=p1;
	q=p;
	q1=p1;
	while((q1->ch<='z')&&(q1->ch>='a')&&(q1->ch<='A')&&(q1->ch>='Z')) {
		q=q->next;
		q1=q1->next;
	}
	tmp=q1;
	while(tmp!=NULL) {
		if((tmp->ch<='9')&&(tmp->ch>='0')) {
			p->next=tmp;
			p=tmp;
			q->next=tmp->next;
			tmp=tmp->next;
		} else {
			q=q->next;
			tmp=tmp->next;
		}
	}
	p->next=q0;
	r=p0->next;
	free(p0);
	return r;
}

8.CCF会员的信息卡至少包括:会员号(5位数字)、姓名、会员类别、有效期(包含年、月、日)等信息。

例如:15316、吕帅、高级会员、20181231.

1)定义存储CCF会员上述信息的单向链表的结点类型

2)编写函数,依次由文件in.txt中读入所有会员的信息(文件中每个会员信息包括会员号、姓名、有效期、;会员类别为冗余信息,由会员号的最后一位可以推导出),创建一个用于管理会员信息的单向链表,使得该链表有序;

3)假定管理会员信息的单向链表已经按照会员号的5位数字(唯一标识)从小到大排序。编写函数,将该单向链表修改为:首先按照会员类别排序,相同类别的会员按照5位数字从小到大排序

#include <stdio.h>
#include <stdlib.h>

typedef struct user {
	int num;
	char kind;
	char name[20];
	int date;
	struct user* next;
} user;

typedef struct user* puser;
puser head=NULL;
void creat() {
	FILE* fpin;
	fpin=fopen("in.txt","r");
	puser p;
	while(!feof(fpin)) {
		p=(puser)malloc(sizeof(struct user));
		fscanf(fpin,"%d%c",&p->num,&p->kind);
		fscanf(fpin,"%s",&p->name);
		fscanf(fpin,"%d",&p->date);
		p->next=NULL;
		if(head==NULL) head=p;
		else {
			puser p1,p2=NULL;
			if(p1->num>p->num) {
				p->next=head;
				head=p;
			}
			while(p1->num<p->num&&p1!=NULL) {
				p2=p1;
				p1=p1->next;
			}
			if(p1==NULL) p2->next=p;
			else {
				p2->next;
				p->next=p1;
			}
		}
	}
	fclose(fpin);
	return;
}

void sort() {
	puser p,q,p1=NULL,q1=NULL;
	for(p=head; p!=NULL; p=p->next) {
		for(q=p->next,q1=p; q!=NULL; q=q->next) {
			if(p->kind>q->kind) {
				if(p1==NULL) {
					head=q;
					q1->next=q->next;
					q->next=p;
				} else {
					q1->next=q->next;
					p1->next=q;
					q->next=p;
				}
				p=q;
			}
			q1=q;
		}
		p1=p;
	}
}

9.构造一个表示教师的结构体(包含3个字段:姓名、性别、年龄)。编写函数,读入M个教师的信息,存入一个结构图数组中,如下图所示。

例如:一个教师的信息位张三、true、50,另一个教师的信息位李四、false、37,

张三李四.......赵九
男(true)女(false)男(true)
503729

设有一个保存教师信息的单链表(每个结点包含4个字段:姓名、性别、年龄、后继指针)。构造该链表中一节的数据类型声明:编写函数,在给定链表上查找所有女教师的信息,并存储到指定文件output.txt中

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct node {
	char name[10];
	bool sex;
	int age;
	struct node* next;
} node;

void save(struct node * head) {
	FILE *file;
	if((file=fopen("output.txt","w"))==NULL)
		printf("FILE OPEN ERROR");
	while(head!=NULL) {
		if(head->sex==false) {
			fprintf(file,"%10s",head->name);
			fprintf(file,"%5d\n",head->age);
		}
		head=head->next;
	}
	fclose(file);
}

10.职工的信息卡至少包括工号、姓名和出生年月等信息。限定:工号为整型且不超过整型的取值范围。

1)定义存储职工信息的单向链表的结点类型

2)编写函数,由文件in.txt中一次读入k个职工的完整信息,创建一个用于管理职工信息的单向链表

3)假定管理职工信息的单向链表已经按照工号从小到大排序。编写函数,由键盘键入1个职工的信息,按照工号顺序插入到用于管理职工信息的单向链表中

#include <stdio.h>
#include <stdlib.h>

typedef struct Date {
	int y,m;
} Date;

typedef struct node {
	int num;
	char name[10];
	struct Date birthday;
	struct node* next;
} node;

struct node* create(int k) {
	FILE* file;
	if((file=fopen("in.txt","w"))==NULL) {
		printf("FILE OPEN ERROR");
		exit(0);
	}
	struct node* head=NULL,*p;
	for(int i=0; i<k; i++) {
		p=(node*)malloc(sizeof(node));
		fscanf(file,"%d%10s%d%d",&(p->num),p->name,&(p->birthday.y),&(p->birthday.m));
		p->next=head;
		head=p;
	}
	fclose(file);
	return head;
}

struct node* search(struct node* head) {
	struct node *temp=(node*)malloc(sizeof(node));
	scanf("%d%10s%d%d",&(temp->num),temp->name,&(temp->birthday.y),&(temp->birthday.m));
	if(temp->num<head->num) {
		temp->next=head;
		return temp;
	}
	struct node* p=head->next,*q=head;
	while(p!=NULL&&temp->num>p->num) {
		q=p;
		p=p->next;
	}
	q->next=temp;
	temp->next=p;
	return head;
}

11.每个学生的信息卡片包括学号,姓名和年龄三项。定义存储学生信息的单向链表的结点类型:编写函数,由键盘依次键入n(n>=1)个学生的信息,创建一个用于管理学生信息的单向链表。

编写函数,把上题创建的单向链表中删除所有年龄为z的结点(z的值由用户从键盘输入,且年龄为z的结点可能有多个),将处理后的单向链表的所有学生信息存储到文件名为output.txt的文本文件中

#include <stdio.h>
#include <stdlib.h>

typedef struct student {
	int id;
	char name;
	int age;
	struct student* next;
} student;

struct student* create(int n) {
	struct student* head=NULL;
	struct student *p1,*p2;
	for(int i=0; i<n; i++) {
		p1=(struct student *)malloc(sizeof(struct student));
		scanf("%d%s%d",&p1->id,&p1->name,&p1->age);
		if(i==0) head=p1;
		else p2->next=p1;
		p2=p1;
	}
	p2->next=NULL;
	return head;
}

void save(struct student* head,int z) {
	FILE *file;
	if((file=fopen("output.txt","w"))==NULL)
		printf("FILE OPEN ERROR");
	while(head!=NULL&&head->age==z)
		head=head->next;
	struct student* p=head,*q=NULL;
	while(p!=NULL) {
		if(p->age!=z)
			q=p;
		else {
			q->next=p->next;
			p=p->next;
		}
		while(head!=NULL) {
			fprintf(file,"%5d\n",head->id);
			fprintf(file,"%10s\n",head->name);
			fprintf(file,"%5d\n",head->age);
			head=head->next;
		}
		fclose(file);
	}
}

12.已知一个链表,链表中的每节是一个字符,要求:

1)根据给定的链表定义结点的结构体类型

2)编写函数删除链表中所有数字字符的结点

#include <stdio.h>

typedef struct node {
	char key;
	struct node* next;
} node;

struct node* del(struct node* head) {
	while(head->key>='0'&&head->key<='9')
		head=head->next;
	struct node* p=head;
	while(p!=NULL) {
		if(p->next->key>='0'&&p->next->key<='9')
			p->next=p->next->next;
		else
			p=p->next;
	}
	return head;
}

13.构造一个链表(每个结点包含2个字段:整数信息、后继指针)。编写函数,从单链表的头结点依次处理每个结点,仅保留其整数信息大于当前前驱结点的整数信息的结点,使得处理后的单链表中整数信息满足递增顺序

#include <stdio.h>

typedef struct node {
	char key;
	struct node* next;
} node;

void del(struct node *head) {
	if(head==NULL)
		return;
	struct node *p=head,*q=head->next;
	while(q!=NULL) {
		if(p->key>q->key)
			p->next=q->next;
		else
			p=q;
		p=p->next;
	}
}

14.写一个结构体,要求是一个单链表,里面装的是学生的学号,姓名,成绩,还要把这个链表按照成绩由低到高排序

在上一道题的基础上,有一个和上一道题一样的单链表,读取一个文件里面的学生成绩等信息放到新生成的链表里,最后再把两道题的链表递归排序

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MIN 0

typedef struct node {
	int num;
	char name[10];
	int grade;
	struct node* next;
} ListNode;

ListNode* bubble(ListNode* head) {
	ListNode *r0,*p0,*p,*q,*rs;
	r0=(ListNode*)malloc(sizeof(ListNode));
	r0->next=head;
	r0->grade=MIN;
	bool flag=true;
	while(flag) {
		q=r0;
		p0=r0->next;
		flag=false;
		while(p!=NULL) {
			if(p0->grade>p->grade) {
				q->next=p;
				p->next=p0;
				flag=true;
			}
			q=q->next;
			p0=q->next;
			p=p0->next;
		}
	}
	rs=r0->next;
	free(r0);
	return rs;
}

ListNode* create(int k) {
	FILE *file;
	if((file=fopen("in.txt","r"))==NULL) {
		printf("OPEN FILE ERROR");
		exit(0);
	}
	ListNode* head=NULL,*p;
	for(int i=0; i<k; i++) {
		p=(ListNode*)malloc(sizeof(ListNode));
		fscanf(file,"%d%10s%d",&(p->num),p->name,&(p->grade));
		p->next=head;
		head=p;
	}
	fclose(file);
	return head;
}

ListNode *Merge(ListNode* phead1,ListNode* phead2) {
	if(phead1==NULL)
		return phead2;
	if(phead2==NULL)
		return phead1;
	ListNode* mergehead=NULL;
	if(phead1->grade<phead2->grade) {
		mergehead=phead1;
		mergehead->next=Merge(phead1->next,phead2);
	} else {
		mergehead=phead2;
		mergehead->next=Merge(phead1,phead2->next);
	}
	return mergehead;
}
  • 41
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值