一、时间类
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
void add_a_sec();
void add_a_minute();
void add_an_hour();
void add_seconds(int);
void add_minutes(int);
void add_hours(int);
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;
}
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;
}
void Time::add_a_sec()
{
if(sec>=59)
{
minute+=1;
sec-=59;
cout<<hour<<":"<<minute<<":"<<'0'<<sec<<endl;
}
else
{
sec+=1;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
}
void Time::add_a_minute()
{
if(minute>=59)
{
hour+=1;
minute-=59;
cout<<hour<<":"<<'0'<<minute<<":"<<sec<<endl;
}
else
{
minute+=1;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
}
void Time::add_an_hour()
{
if(hour>=23)
{
hour-=1;
cout<<'0'<<hour<<":"<<minute<<":"<<sec<<endl;
}
else
{
hour+=1;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
}
void Time::add_seconds(int)
{
int x;
x=sec;
sec+=x;
if(sec>59)
{
minute+=1;
sec-=60;
cout<<hour<<":"<<minute<<":"<<'0'<<sec<<endl;
}
else cout<<hour<<":"<<minute<<":"<<sec<<endl;
if(minute>59)
{
hour+=1;
minute-=60;
cout<<hour<<":"<<'0'<<minute<<":"<<sec<<endl;
}
else cout<<hour<<":"<<minute<<":"<<sec<<endl;
if(hour>23)
{
hour-=24;
cout<<'0'<<hour<<":"<<minute<<":"<<sec<<endl;
}
else cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::add_minutes(int)
{
int x;
x=minute;
minute+=x;
if(minute>59)
{
hour+=1;
minute-=60;
cout<<hour<<":"<<'0'<<minute<<":"<<sec<<endl;
}
else cout<<hour<<":"<<minute<<":"<<sec<<endl;
if(hour>23)
{
hour-=24;
cout<<'0'<<hour<<":"<<minute<<":"<<sec<<endl;
}
else cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
void Time::add_hours(int)
{
int x;
x=hour;
hour+=x;
if(hour>23)
{
hour-=24;
cout<<'0'<<hour<<":"<<minute<<":"<<sec<<endl;
}
else cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
Time t1,t2;
t1.set_time( );
t1.show_time( );
t1.add_a_sec();
t1.add_a_minute();
t1.add_an_hour();
t2.set_time();
t2.add_seconds(34);
t2.add_minutes(23);
t2.add_hours(6);
return 0;
}
二、正整数类
#include<iostream>
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)
{
n=x;
}
int NaturalNumber::getValue()
{
return n;
}
bool NaturalNumber::isPrime()
{
int i,j;
for(i=2; i<n; i++)
{
if(n%i==0)
j++;
}
if(j>0)
return true;
if(j=0)
return false;
}
void NaturalNumber::printFactor()
{
int x;
for(x=1; x<=n; x++)
{
if(n%x==0)
cout<<x<<" ";
}
}
bool NaturalNumber::isPerfect()
{
int x,y=0;
for(x=1; x<n; x++)
{
if(n%x==0)
y+=x;
}
if(y==n)
return true;
else return false;
}
bool NaturalNumber::isReverse(int x)
{
cout<<x;
int a,b,c,a1,b1,c1;
a=n/100;
b=n%100/10;
c=n%10;
a1=x/100;
b1=x%100/10;
c1=x%10;
if(a==c1&&b==b1&&c==a1)
return true;
else return false;
}
bool NaturalNumber::isDaffodil(int x)
{
int a,b,c;
a=x/100;
b=x%100/10;
c=x%10;
if(a*a*a+b*b*b+c*c*c==x)
return true;
else return false;
}
void NaturalNumber::printDaffodils()
{
int x;
for(x=1;x<n;x++)
{
if((x/100)*(x/100)*(x/100)+(x%100/10)*(x%100/10)*(x%100/10)+(x%10)*(x%10)*(x%10)==x)
cout<<x<<" ";
}
}
int main()
{
NaturalNumber nn; //定义类的一个实例(对象)
nn.setValue (34);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (37);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (84);
cout<<nn.getValue()<<"的因子有:";
nn.printFactor();
cout<<endl;
nn.setValue(56);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
nn.setValue(6);
cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
int y;
nn.setValue(154);
y=nn.getValue();
cout<<(nn.isReverse(451)?"是":"不是")<<y<<"的逆向数"<<endl;
int z;
nn.setValue(445);
z=nn.getValue();
cout<<(nn.isReverse(451)?"是":"不是")<<z<<"的逆向数"<<endl;
nn.setValue(153);
cout<<nn.getValue()<<(nn.isDaffodil(153)?"是":"不是")<<"水仙花数"<<endl;
nn.setValue(34);
cout<<nn.getValue()<<(nn.isDaffodil(34)?"是":"不是")<<"水仙花数"<<endl;
nn.setValue(348);
cout<<nn.getValue()<<"内的水仙花数有: ";
nn.printDaffodils();
}
三、Book类
#include<iostream>
#include<string>
using namespace std;
class Book
{
private:
string name;
string writer;
string publicer;
float price;
int number;
int NO;
public:
void print();
void setBook(string a,string w,string p,float q,int n,int N);
void borrow();
void restore();
int set_NO(int x);
int get_NO();
};
void Book::setBook(string a,string w,string p,float q,int n,int N)
{
name=a;
writer=w;
publicer=p;
price=q;
number=n;
NO=N;
}
void Book::print()
{
cout<<"书名: "<<name<<endl;
cout<<"作者: "<<writer<<endl;
cout<<"出版社: "<<publicer<<endl;
cout<<"价格: "<<price<<endl;
cout<<"数量: "<<number<<endl;
cout<<"书号: "<<NO<<endl;
}
void Book::borrow()
{
number+=1;
cout<<"此时的数量: "<<number<<endl;
}
void Book::restore()
{
number-=1;
cout<<"此时的数量: "<<number<<endl;
}
int Book::set_NO(int x)
{
NO=x;
}
int Book::get_NO()
{
cout<<"此时的书号:";
cout<<NO<<endl;
}
main()
{
Book b;
b.setBook("西游记","吴承恩","江西出版社",34.5,45,2);
b.print();
b.borrow();
b.setBook("西游记","吴承恩","江西出版社",34.5,45,2);
b.restore();
b.set_NO(6);
b.get_NO();
return 0;
}
四、分数类