每个学生的信息卡片包括学号、姓名和成绩三项。定义存储学生信息的单向链表的结点类型;编写函 数,由文件依次读入 n(n≥0)个学生的信息,创建一个用于管理学生信息的单向链表;编写函数,对 该链表进行整理,保证该单向链表的结点顺序满足学号从小到大的顺序。
运行程序前,新建一个文本文件1.txt放在C盘下(或者修改下面的Read("c:\\1.txt", head);为你的文件名和路径)
文件内容:
1,aaa,82
2,bbb,99
3,ccc,75
4,ddd,63
5,eee,78
6,fff,60
7,test,92.5
以下是程序,用VS2010编译
// Q703974.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct Student
{
char no[10];
char name[10];
double score;
};
typedef struct Node
{
Node * next;
Student * s;
};
void Read(char * filename, Node * head)
{
FILE * fp;
fp = fopen(filename, "r");
fseek(fp, 0, SEEK_END);
long filesize = ftell(fp);
rewind(fp);
char * pBuff = (char *)malloc(sizeof(char) * filesize + 1);
memset(pBuff, 0, filesize + 1);
fread(pBuff, filesize, 1, fp);
pBuff[filesize] = '\0';
int offset = 0;
while (true)
{
Student * pstu = (Student *)malloc(sizeof(Student));
int n = sscanf(pBuff + offset, "%[^,],%[^,],%lf", pstu->no, pstu->name, &pstu->score);
if (n != 3) break;
Node * curr = head;
while (true)
{
if (curr->next == NULL)
{
head->next = (Node *)malloc(sizeof(Node));
head->next->next = NULL;
head->next->s = pstu;
break;
}
else if (curr->next->s->score > pstu->score)
{
Node * t = (Node *)malloc(sizeof(Node));
t->next = curr->next;
t->s = pstu;
curr->next = t;
break;
}
curr = curr->next;
}
while (pBuff[offset] != '\n' && pBuff[offset] != '\0')
offset++;
if (pBuff[offset - 1] == '\0') break; else offset++;
}
free(pBuff);
fclose(fp);
}
int _tmain(int argc, _TCHAR* argv[])
{
Node * head = (Node *)malloc(sizeof(Node));
head->next = NULL;
Read("c:\\1.txt", head);
while (head->next != NULL)
{
printf("%s\t%s\t%lf\n", head->next->s->no, head->next->s->name, head->next->s->score);
head = head->next;
}
return 0;
}
如果问题得到解决,麻烦点下采纳,采纳后额外赠送1000个C语言源代码(https://download.csdn.net/download/caozhy/9919273 )
之前看错了题目,一个小问题,是学号排序,不是成绩,只要改1行:
else if (curr->next->s->score > pstu->score)
修改为
lse if (strcmp(curr->next->s->no, pstu->no) > 0)
输入:
13 Bob 85
6 Tim 73
9 Jim 60
输出:
6 Tim 73
9 Jim 60
13 Bob 85
文件流我这里不方便操作,自己修改一下就行了,这里以输入3个学生为例
#include<stdio.h>
#include<malloc.h>
typedef struct student {
int id; //学号
char name[20]; //姓名
int mark; //成绩
};
typedef struct Node {
student t;
Node* next;
}*Linklist;
void initlist(Linklist list, int n) {
Node *p, *r;
r = list;
for (int i = 0; i < n; i++) {
p = (Linklist)malloc(sizeof(Node));
scanf("%d%s%d", &p->t.id, &p->t.name, &p->t.mark); //按顺序输入学号、姓名、成绩
p->next = NULL;
r->next = p;
r = p;
}
}
void sortlist(Linklist list) { //冒泡排序
student tmp;
for (Node *i = list->next; i!= NULL; i = i->next) {
for (Node *j = i->next; j != NULL; j = j->next) {
if (i->t.id > j->t.id) {
tmp = i->t;
i->t = j->t;
j->t = tmp;
}
}
}
}
void showlist(Linklist list) {
Node *p = list->next;
printf("\n");
while (p != NULL) {
printf("%d %s %d\n", p->t.id, p->t.name, p->t.mark);
p = p->next;
}
}
int main() {
Linklist list;
list = (Linklist)malloc(sizeof(Node));
int n = 3;
initlist(list,3); //输入3组数据
sortlist(list);
showlist(list);
return 0;
}