2017.11.24李锦浩【第45天】
今天又做了一个关于类与链表的程序,对于一些类的知识又有了进一步的了解。看来对于程序的理解必须在编写程序的过程中实现。
附:
#include<iostream>
using namespace std;
struct score
{
char name[32];
int scores;
score *next;
};
class student
{
public:
friend class student;
void scoretotalcount(score *&head);//输入成绩;
void avaerage(score *head);//求平均值;
void compare(student &A,double &p);
static void input();
private:
static int total, count;
double average;
char clas[20];
};
int student::total = 0;
int student::count = 0;
void student::input()
{
int y;
cout << "count=";
cin >> y;
count = y;
}
void student::scoretotalcount(score *&head)
{
cout << "请输入成绩";
score *s, *p;
p = new score;
for (int i = 0; i < count; i++)
{
s = new score;
cout << "请输入姓名";
cin >> s->name;
cout << "请输入成绩";
cin >> s->scores;
if (head == NULL)
{
head = s;
p->next = s;
p = p->next;
}
else
{
p->next = s;
p = p->next;
}
}//使输入的成绩成为一个链表;
p->next = NULL;
s = NULL;
p = NULL;
delete s;
delete p;
}
void student::avaerage(score *head)
{
int y=0;
while (head->next)
{
y += head->scores;
head = head->next;
}
average = y / count;
}
void student::compare(student&A, double &p)
{
if (A.average > p)
cout << "class1 is good!";
else
cout << "class2 is good!";
}
int main()
{
student c1, c2;
score *head,*head2=NULL;
head = NULL;
double j;
c1.input();
c1.scoretotalcount(head);
c1.avaerage(head);
c2.input();
c2.scoretotalcount(head2);
c2.avaerage(head2);
c1.compare(c2, j);
system("pause");
return 0;
}
明日任务:再做一个程序,复习重载运算符。