這是那個程式,是要從一個txt檔案中讀取學生資訊保存到鏈表里,然后輸入學號,輸出姓名和成績。txt檔案里有好多行,每行格式是:學號 姓名 成績。這個程式沒有報錯,但是也沒有任何反應,就是黑屏。
#include<iostream>
#include<fstream>
#include<cstring>
#include<string.h>
#include<stdlib.h>
#include<sstream>
using namespace std;
struct student{
long id;
string name;
int score;
student *next;
student *head;
};
student *generatelist(const char *filename)
{
ifstream file("Score.txt");
int idx=0;
string line,part;
student *head;
student *p;
head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
p=head->next=(struct student *)malloc(sizeof(struct student));
while(getline(file,line))
{
stringstream ss(line);
getline(ss,part,' ');
while(idx<=2)
{
if(idx==0)
p->id=stol(part);
else if(idx==1)
p->name=part;
else if(idx==2)
p->score=stoi(part);
idx++;
}
p->next=(struct student *)malloc(sizeof(struct student));
p=p->next;
}
p->next=NULL;
return head;
}
void returnname(student *stulist,long id)
{
student *p=stulist;
while(p->id!=id && p!=NULL)
{
p=p->next;
}
if(p!=NULL)
cout<<p->name<<'\t';
else
cout<<"沒有找到"<<endl;
}
void returnscore(student *stulist,long id)
{
student *p=stulist;
while(p->id!=id && p!=NULL)
{
p=p->next;
}
if(p!=NULL)
cout<<p->score;
}
int main()
{
student *stulist=generatelist("Score.txt");
long xuehao;
cout<<"請輸入一個學號:";
cin>>xuehao;
returnname(stulist,xuehao);
returnscore(stulist,xuehao);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/188319.html
標籤:C++ 語言
上一篇:控制元件GDI資源泄漏
