标题:[求助]各位能不能帮我看一下这两个程序有什么问题
只看楼主
lichee8611
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-4-3
 问题点数:0 回复次数:4 
[求助]各位能不能帮我看一下这两个程序有什么问题

各位能不能帮我看一下这两个程序有什么问题,小弟才疏,确实没瞧出个究竟来,有劳了!
第一个
#include<iostream.h>
class Student
{
public:
void scoretotalcount();
static float sum();
static float average();
private:
float score;static float total=0;static int count=0;
};
void Student::scoretotalcount()
{
Student *P;
cout<<"Please input the score of students(end up with -1):\n";
cin>>p->score;
p->total+=p->score;
p->count++;
while(p->score!=-1)
{
cin>>p->score;
p->total+=p->score;
p->count++;
}
cout<<"Have set it.\n";
}
float Student::sum()
{
Student *p;
return p->total;
}
float Student::average()
{
Student *p;
return (p->total)/(p->count);
}
void main()
{
scoretotalcount();
cout<<"The total score is:"<<sum()<<endl;
cout<<"The average score is:"<<average()<<endl;
cout<<"Completed!"<<endl;
}


第二个
#include<iostream.h>
class book
{
public:
void display(char);
void borrow(char);
void restore(char);
book *next;
char bookname;int price,number; (我本来想把这三个定义为private,但出现了好多错误)
};
void main()
{
book *s,*head,*nect;
int i=0,j;char k;
cout<<"Please input some books(name,price)\n";
s=new book;
cin>>s->bookname>>s->price;
i++;
while(s->bookname!='#')
{
s=new book;
cin>>s->bookname>>s->price;
i++;}
cout<<"the number of books:\n"<<i<<endl;
cout<<"please input the name of the book you want:\n";
cin>>k;
cout<<"What do you want to do?(Input 1 to show is it availible;2 to borrow the book;3 to restore the book):\n";
cin>>j;
switch(j)
{
case 1:display(k);break; (编译时说这三个函数没定义)
case 2:borrow(k);break;
case 3:restore(k);break;
}
}
void book::display(char x)
{
book *s;
while(book *head)
{
if(s->bookname==x)cout<<"It's useble\n";
else cout<<"It's unuseble\n";
head=head->next;
}
}
void book::borrow(char x)
{
book *s;
int i;
while(book *head)
{
if(s->bookname==x)i--;
head=head->next;
}
cout<<"Now the number of books is:"<<i<<endl;
}
void book::restore(char x)
{
int i;book *s;
while(book *head)
{
if(s->bookname==x)i++;
head=head->next;
}
cout<<"Now the number of books is:"<<i<<endl;
}

搜索更多相关主题的帖子: 各位 什么 问题 
2007-04-05 18:02
余来
Rank: 6Rank: 6
等 级:贵宾
威 望:26
帖 子:956
专家分:18
注 册:2006-8-13
得分:0 
第一个静太成员不能那么初始化,还有非静态成员函数不能访问静态成员,这个应该很简单,楼住不会是拿书上的东东发到这里来要别人做吧,

2007-04-05 18:11
Arcticanimal
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:341
专家分:20
注 册:2007-3-17
得分:0 
以下是引用lichee8611在2007-4-5 18:02:51的发言:

各位能不能帮我看一下这两个程序有什么问题,小弟才疏,确实没瞧出个究竟来,有劳了!
第一个
#include<iostream.h>
class Student
{
public:
void scoretotalcount();
static float sum();
static float average();
private:
float score;static float total=0;static int count=0;
//不可以这样初始化,应该在定义外这样初始化 Student::total=0;
};
void Student::scoretotalcount()
{
Student *P;
cout<<"Please input the score of students(end up with -1):\n";
cin>>p->score;
p->total+=p->score;
p->count++;
while(p->score!=-1) //想一想直接输入-1会怎样?
{
cin>>p->score;
p->total+=p->score;
p->count++;
}
cout<<"Have set it.\n";
}
float Student::sum()
{
Student *p;
return p->total; //直接return total; 就行了
}
float Student::average()
{
Student *p;
return (p->total)/(p->count);
}
void main()
{
scoretotalcount();
cout<<"The total score is:"<<sum()<<endl;
cout<<"The average score is:"<<average()<<endl;
cout<<"Completed!"<<endl;
}



try new catch
2007-04-05 18:37
Arcticanimal
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:341
专家分:20
注 册:2007-3-17
得分:0 
以下是引用lichee8611在2007-4-5 18:02:51的发言:

各位能不能帮我看一下这两个程序有什么问题,小弟才疏,确实没瞧出个究竟来,有劳了!

第二个
#include<iostream.h>
class book
{
public:
void display(char);
void borrow(char);
void restore(char);
book *next;
char bookname;int price,number; (我本来想把这三个定义为private,但出现了好多错误)
};
void main()
{
book *s,*head,*nect;
int i=0,j;char k;
cout<<"Please input some books(name,price)\n";
s=new book; //一定要记得在new之后检查内存分配!
cin>>s->bookname>>s->price;
i++;
while(s->bookname!='#')
{
s=new book;
cin>>s->bookname>>s->price;
//不能够声明为private是因为你访问了私有变量
i++;}
cout<<"the number of books:\n"<<i<<endl;
cout<<"please input the name of the book you want:\n";
cin>>k;
cout<<"What do you want to do?(Input 1 to show is it availible;2 to borrow the book;3 to restore the book):\n";
cin>>j;
switch(j)
{
case 1:display(k);break; (编译时说这三个函数没定义)
case 2:borrow(k);break; //成员函数能够这样直接访问吗?
case 3:restore(k);break;
}
}
void book::display(char x)
{
book *s;
while(book *head)
{
if(s->bookname==x)cout<<"It's useble\n"; //这里?有问题!
else cout<<"It's unuseble\n";
head=head->next;
}
}
void book::borrow(char x)
{
book *s;
int i;
while(book *head)
{
if(s->bookname==x)i--;
head=head->next;
}
cout<<"Now the number of books is:"<<i<<endl;
}
void book::restore(char x)
{
int i;book *s;
while(book *head)
{
if(s->bookname==x)i++;
head=head->next;
}
cout<<"Now the number of books is:"<<i<<endl;
}


try new catch
2007-04-05 19:34
北斗七星
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-6-24
得分:0 
(编译时说这三个函数没定义)
case 2:borrow(k);break; //成员函数能够这样直接访问吗?
case 3:restore(k);break;
当然不能了,用实例去调用此函数
2007-06-24 22:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-129530-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.264186 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved