撰寫程式:接著任務二,假設存在一個文本檔案STUDENT.TXT,其中存盤了若干個學生的資訊,每個學生資訊包含:學號,姓名,及數學、政治和英語三門功課的成績及均分,但是學生人數未知。將這個檔案中的學生資訊讀到結構體鏈表中,并且輸出均分最高的學生的姓名和學號。要求:
a. 用結構體型別表示學生的學號,姓名,及數學、政治和英語三門功課的成績及均分。
b. 若干個學生以鏈表形式表示,讀到一個學生資訊,就動態申請一個空間,然后鏈接到鏈表中。
求代碼啊啊啊啊!!!!
uj5u.com熱心網友回復:
先自己寫寫吧,不然怎么提高和進步~uj5u.com熱心網友回復:
說是這么說,是真寫不出啊
uj5u.com熱心網友回復:
小學妹不要慌#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stu
{
char no[20];
char name[10];
float math;
float political;
float English;
float average;
struct Stu *next;
};
int main(void)
{
FILE *fp;
fp = fopen("student.txt", "r");
if (!fp)
{
printf("檔案打開失敗");
exit(0);
}
struct Stu* head = (struct Stu*)malloc(sizeof(struct Stu));
struct Stu* tmp;
head->next = NULL;
char no[20];
char name[10];
float math;
float political;
float English;
float average;
while (fscanf(fp, "%s\t %s\t %f\t %f\t %f\t %f", no, name, &math, &political, &English, &average) != EOF)
{
tmp = (struct Stu*)malloc(sizeof(struct Stu));
tmp->next = NULL;
if (tmp)
{
strcpy(tmp->no, no);
strcpy(tmp->name, name);
tmp->math = math;
tmp->political = political;
tmp->English = English;
tmp->average = average;
tmp->next = head->next;
head->next = tmp;
}
}
struct Stu* max = (struct Stu*)malloc(sizeof(struct Stu));
max->average = 0.0;
struct Stu* p = head->next;
while (p)
{
if (p->average > max->average)
{
max = p;
}
p = p->next;
}
printf("均分最高的學生的姓名是%s,學號是%s\n", max->name, max->no);
return 0;
}
uj5u.com熱心網友回復:
{
if (p->average > max->average)
{
max = p;
}
p = p->next;
}這里記憶體泄露了,你的max動態申請了空間,然后這里又max = p;那max指向的記憶體不就找不到了嗎?
uj5u.com熱心網友回復:

哥哥們好厲害(? ??_??)?
uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stu
{
char no[20];
char name[10];
float math;
float political;
float English;
float average;
struct Stu *next;
};
int main(void)
{
FILE *fp;
fp = fopen("student.txt", "r");
if (!fp)
{
printf("檔案打開失敗");
exit(0);
}
struct Stu* head = (struct Stu*)malloc(sizeof(struct Stu));
struct Stu* tmp;
//head->next = NULL;
char no[20];
char name[10];
float math;
float political;
float English;
float average;
head->next = NULL;
//while (fscanf(fp, "%s\t %s\t %f\t %f\t %f\t %f", no, name, &math, &political, &English, &average) != EOF)
while (fscanf(fp, "%s %s %f %f %f %f\n", no, name, &math, &political, &English, &average) != EOF)
{
tmp = (struct Stu*)malloc(sizeof(struct Stu));
if (!tmp) {
fprintf(stderr, "malloc error!\n");
exit(0);
}
tmp->next = NULL;
//if (tmp)
//{
strcpy(tmp->no, no);
strcpy(tmp->name, name);
tmp->math = math;
tmp->political = political;
tmp->English = English;
tmp->average = average;
tmp->next = head->next;
head->next = tmp;
//}
}
/*
struct Stu* max = (struct Stu*)malloc(sizeof(struct Stu));
max->average = 0.0;
*/
struct Stu *max = head->next;
struct Stu* p = head->next;
while (p)
{
if (p->average > max->average)
{
max = p;
}
p = p->next;
}
printf("均分最高的學生的姓名是%s,學號是%s\n", max->name, max->no);
return 0;
}
供參考~
借助樓上的代碼,改了一下,可以試一下是否可以使用~
uj5u.com熱心網友回復:
注意在最后要加上fclose(fp);用于關閉檔案uj5u.com熱心網友回復:
可,謝謝哥哥們

uj5u.com熱心網友回復:
{
if (p->average > max->average)
{
max = p;
}
p = p->next;
}
這里記憶體泄露了,你的max動態申請了空間,然后這里又max = p;那max指向的記憶體不就找不到了嗎?
小學妹不要慌
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stu
{
char no[20];
char name[10];
float math;
float political;
float English;
float average;
struct Stu *next;
};
int main(void)
{
FILE *fp;
fp = fopen("student.txt", "r");
if (!fp)
{
printf("檔案打開失敗");
exit(0);
}
struct Stu* head = (struct Stu*)malloc(sizeof(struct Stu));
struct Stu* tmp;
head->next = NULL;
char no[20];
char name[10];
float math;
float political;
float English;
float average;
while (fscanf(fp, "%s\t %s\t %f\t %f\t %f\t %f", no, name, &math, &political, &English, &average) != EOF)
{
tmp = (struct Stu*)malloc(sizeof(struct Stu));
tmp->next = NULL;
if (tmp)
{
strcpy(tmp->no, no);
strcpy(tmp->name, name);
tmp->math = math;
tmp->political = political;
tmp->English = English;
tmp->average = average;
tmp->next = head->next;
head->next = tmp;
}
}
struct Stu* max = (struct Stu*)malloc(sizeof(struct Stu));
max->average = 0.0;
struct Stu* p = head->next;
while (p)
{
if (p->average > max->average)
{
max = p;
}
p = p->next;
}
printf("均分最高的學生的姓名是%s,學號是%s\n", max->name, max->no);
return 0;
}
多謝,之前沒注意,確實應該改一下
struct Stu* max;
average = 0.0;
struct Stu* p = head->next;
while (p)
{
if (p->average > average)
{
max = p;
average = p->average;
}
p = p->next;
}
uj5u.com熱心網友回復:
注意在最后要加上fclose(fp);用于關閉檔案
可,謝謝哥哥們
你是妹子呢,還是漢子呢,哈哈
uj5u.com熱心網友回復:
注意在最后要加上fclose(fp);用于關閉檔案
可,謝謝哥哥們
你是妹子呢,還是漢子呢,哈哈
我是你們心目中最完美的樣子
uj5u.com熱心網友回復:
看到學妹我就進來了,看見題目,準備開溜了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/41229.html
標籤:C語言
上一篇:求解一個題
下一篇:新手簡單問題,型別轉換的問題
