一、阅读、运行程序后,按要求增加类的功能(此题不用写实验报告)
(1)add_a_sec() //增加1秒钟
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void set_add_a_sec();
void show_add_a_sec();
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::set_add_a_sec()
{
sec=sec+1;
if(sec>0)
{
r=sec/60
minute=minute+r;
sec=sec%60;
}
if(minute>60)
{
t=minute/60;
hour=hour+t;
minute=minute%60;
}
}
void Time::show_add_a_sec()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
int main( )
{
Time t1;
t1.set_time( );
t1.show_time( );
t1.set_add_a_sec();
t1.show_add_a_sec();
return 0;
}
(2)add_a_minute() //增加1分钟
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void set_add_a_minute();
void show_add_a_minute();
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::set_add_a_minute()
{
minute=minute+1;
if(minute>60)
{
t=minute/60;
hour=hour+t;
minute=minute%60;
}
}
void Time::show_add_a_minute()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
int main( )
{
Time t1; t1.set_time( );
t1.show_time( );
t1.set_add_a_minute();
t1.show_add_a_minute();
return 0;
}
(3)add_an_hour() //增加1小时
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void set_add_a_hour();
void show_add_a_hour();
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::set_add_a_hour()
{
hour++;
}
void Time::show_add_a_hour()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
int main( )
{
Time t1;
t1.set_time( );
t1.show_time( );
t1.set_add_a_hour();
t1.show_add_a_hour();
return 0;
}
(4)add_seconds(int) //增加n秒钟
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void set_add_a_sec(int n);
void show_add_a_sec();
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::set_add_a_sec(int n)
{
int r,t;
sec=sec+n;
if(sec>60)
{
r=sec/60;
minute=minute+r;
sec=sec%60;
}
if(minute>60)
{
t=minute/60;
hour=hour+t;
minute=minute%60;
}
}
void Time::show_add_a_sec()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
int main( )
{
Time t1;
t1.set_time( );
t1.show_time( );
t1.set_add_a_sec(79);
t1.show_add_a_sec();
return 0;
}
(5)add_minutes(int) //增加n分钟
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void set_add_a_minute(int n);
void show_add_a_minute();
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::set_add_a_minute(int n)
{
int t;
minute=minute+n;
if(minute>60)
{
t=minute/60;
hour=hour+t;
minute=minute%60;
}
}
void Time::show_add_a_minute()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
int main( )
{
Time t1;
t1.set_time( );
t1.show_time( );
t1.set_add_a_minute(80);
t1.show_add_a_minute();
return 0;
}
(6)add_hours(int) //增加n小时
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void set_add_a_hour(int n);
void show_add_a_hour();
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::set_add_a_hour(int n)
{
hour=hour+n;
}
void Time::show_add_a_hour()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{
if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
int main( )
{
Time t1;
t1.set_time( );
t1.show_time( );
t1.set_add_a_hour(5);
t1.show_add_a_hour();
return 0;
}
二、项目名称:正整数类#include<iostream>
#include<math.h>
using namespace std;
class NaturalNumber
{
private:
int n;
public:
void setValue (int x);//置数据成员n的值,要求判断是否是正整数
int getValue(); //返回私有数据成员n的值
bool isPrime(); //判断数据成员n是否为素数,是返回true,否则返回false
void printFactor(); //输出数据成员n的所有因子,包括1和n自身
bool isPerfect(); //判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。
bool isReverse(int x);//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。
bool isDaffodil(int x); //判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
void printDaffodils(); //显示所有大于1,且小于数据成员n的水仙花数;
};
void NaturalNumber::setValue(int x)
{
if(double(x)==x&&x>0)
n=x;
else
cout<<"这不是整数,拒绝"<<endl;
}
int NaturalNumber::getValue()
{
return n;
}
bool NaturalNumber::isPrime()
{
if(n<2) return 0;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0)
return 1;
return 0;
}
void NaturalNumber::printFactor()
{
for(int j=1;j<=n;j++)
{ if(n%j==0)
cout<<j<<" ";
else cout<<"";
}
cout<<endl;
}
bool NaturalNumber::isPerfect()
{
int sum=0;
for(int i=1;i<n;i++)
{ if(n%i==0)
sum=sum+i;
else sum=sum+0;
}
if(sum==n)
return 1;
else return 0;
}
bool NaturalNumber::isReverse(int x)
{
int sum=0,d=n;
while(d!=0)
{
sum=sum*10+d%10;
d=d/10;
}
if(sum!=x)
return 0;
else return 1;
}
bool NaturalNumber::isDaffodil(int x)
{
int d=x,sum=0;
while(d)
{
sum+=(d%10)*(d%10)*(d%10);
d/=10;
}
if(sum==x)
return 1;
else return 0;
}
void NaturalNumber::printDaffodils()
{
for(int i=2;i<n;i++)
{
int d=i,sum=0;
while(d!=0)
{
sum+=(d%10)*(d%10)*(d%10);
d/=10;
}
if(sum==i)
cout<<i<<" ";
else cout<<"";
}
cout<<endl;
}
void main(void)
{
NaturalNumber nn; //定义类的一个实例(对象)
nn.setValue (6);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (37);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (84);
cout<<nn.getValue()<<"的因子有:";
nn.printFactor();
//随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况……
nn.setValue (6);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
nn.setValue(123);
int x=321;
cout<<"x等于"<<x<<(nn.isReverse(321)?"是":"不是")<<nn.getValue()<<"的逆向数"<<endl;
int x1=153;
cout<<"x1等于" <<x1<<(nn.isDaffodil(x1)?"是":"不是")<<"水仙花数"<<endl;
nn.setValue(1000);
cout<<"小于"<<nn.getValue()<<"的水仙花数有:" ;
nn.printDaffodils();
}
三、BOOK类
#include <iostream>
#include<cstring>
using namespace std;
class Book
{
private:
char name[50];
char writer[50];
char public_name[50];
float price;
int number;
int NO;
public:
void setBook(char *n,char *w,char *pu,float pr,int nu,int N);
void borrow();
void restore();
void print();
void set_NO(int N);
int get_NO();
};
void Book::setBook(char *n,char *w,char *pu,float pr,int nu,int N)
{
strcpy(name,n);
strcpy(writer,w);
strcpy(public_name,pu);
price=pr;
number=nu;
NO=N;
}
void Book::borrow()
{
cout<<number--;
}
void Book::restore()
{
cout<<number++;
}
void Book::print()
{
cout<<"书名:"<<name<<endl;
cout<<"作者:"<<writer<<endl;
cout<<"出版社:"<<public_name<<endl;
cout<<"价格:"<<price<<endl;
cout<<"数量:"<<number<<endl;
cout<<"书号:"<< NO<<endl;
cout<<endl;
}
void Book::set_NO(int N )
{
NO=N;
}
int Book::get_NO()
{
cout<<"新书号:";
return NO;
}
int main()
{
Book b;
b.setBook("jiewo","yuqiuyu","weifang",25,5,2014);
b.print();
b.borrow();
b.print();
b.restore();
b.print();
b.set_NO(2015);
b.print();
cout<<b.get_NO();
return 0;
}
五、分数类