6379:统计学生信息(使用动态链表完成)
·
#老师写的:两个struct分的很清晰
#include<iostream>
#include<list>
using namespace std;
struct L{
string number;
string name;
char sex;
int age;
float score;
string address;
};
struct Node{
L data;
Node*next;
};
int main()
{
//list <L> a;
L b;
Node * first=NULL, *p;
while(true)
{
cin>>b.number;
if(b.number=="end")
break;
cin>>b.name>>b.sex>>b.age>>b.score>>b.address;
p=new Node;
p->data=b;
p->next=first;
first=p;
}
p=first;
while (p)
{
cout<<p->data.number<<" "<<p->data.name<<" "<<p->data.sex<<" "<<p->data.age<<" "<<p->data.score<<" "<<p->data.address<<"\n";
p=p->next;
}
return 0;
}
#额超内存看了老师写的,就换思路用一个来解决……
#include<iostream>
using namespace std;
struct info
{
string id;
string name;
char sex;
int age;
float score;
string address;
void showinfo()
{
cout<<id<<" "<<name<<" "<<sex<<" "<<age<<" "<<score<<" "<<address<<endl;
}
info *next;
};
int main()
{
info i;
info *first= NULL, *p;
while(1)
{
cin>>i.id;
if(i.id == "end")break;
cin>>i.name>>i.sex>>i.age>>i.score>>i.address;
// p = new info;
// *p = i;
// p -> next = first;
// first = p;
i.next = first;
first = new info;
*first = i;
}
p = first;
while(p != NULL)
{
p->showinfo();
p = p->next;
}
return 0;
}
#一开始写的第二改,一改太长也没保存
#include<iostream>
#include<string>
using namespace std;
class info
{
public:
string id;
string name;
char sex;
int age;
float score;
string address;
info(){}
info(string i, string n, char s, int a, float sc, string ad):id(i),name(n),sex(s),age(a),score(sc),address(ad){}
~info(){}
void showinfo()
{
cout<<id<<" "<<name<<" "<<sex<<" "<<age<<" "<<score<<" "<<address<<endl;
}
info *next;
};
class link
{
public:
link()
{
first = new info;
first->next = NULL;
}
~link(){}
void get(info &in)
{
info *s = NULL;
s = new info;
s -> id = in.id;
s -> name = in.name;
s -> sex = in.sex;
s -> age = in.age;
s -> score = in.score;
s -> address = in.address;
s -> next = first->next;
first -> next = s;
}
void print()
{
info *p = first->next;
while(p != NULL)
{
p -> showinfo();
p = p->next;
}
}
info *first;
};
int main()
{
string id,name,address;
char sex;
int age;
float score;
int n=0;
link l;
while(1)
{
cin>>id;
if(id == "end")break;
cin>>name>>sex>>age>>score>>address;
info in(id,name,sex,age,score,address);
l.get(in);
}
l.print();
return 0;
}
修改方法:将分数的数据类型由int 改为 float ,就没了……
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)