c++第二次实验 作业

一、阅读、运行程序后,按要求增加类的功能:

#include <iostream>
using namespace std;
class Time
{
public:
	void set_time( );   
	void show_time( );  
	void add_an_hour() 
	{
		hour++;
	}
	void add_a_minute()
	{
		minute++;
		if(minute>=60)
		{
			add_an_hour();
				minute=0;
		}
	}
	void add_a_sec()
	{
		sec++;
		if(sec>=60)
		{
			add_a_minute();
				sec=0;
		}
	}
	void add_seconds(int n); //增加n秒钟
	void add_minutes(int n); //增加n分钟
	void add_hours(int n);
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_hours(int n)
{
	hour+=n;
}
void Time::add_minutes(int n)
{
	minute+=n;
	if(minute>60)
	{
		hour+=minute/60;
		minute%=60;
	}
}
void Time::add_seconds(int n)
{
	sec+=n;
	if(sec>60)
	{
		add_minutes(sec/60);
			sec/=60;
	}
}
int main( )
{
	Time t1;  
	int n;
	t1.set_time( );   
	t1.show_time( );
	cout<<"\n增加一秒后:";
	t1.add_a_sec();
	t1.show_time( );
	cout<<endl;
	cout<<"再增加一分后:";
	t1.add_a_minute();
	t1.show_time( );
	cout<<endl;
	cout<<"\n增加一时后:";
	t1.add_an_hour();
	t1.show_time( );
	cout<<"\n请输入需要增加的秒数:";
	cin>>n;
	cout<<"\n增加"<<n<<"秒后:";
	t1.add_seconds(n);
	t1.show_time( );
	cout<<"\n请输入需要增加的分数:";
	cin>>n;
	cout<<"\n增加"<<n<<"分后:";
	t1.add_minutes(n);
	t1.show_time( );
	cout<<"\n请输入需要增加的时数:";
	cin>>n;
	cout<<"\n增加"<<n<<"时后:";
	t1.add_hours(n);
	t1.show_time( );
	return 0;
}


二、项目名称:正整数类



#include<iostream>
using namespace std;
class NaturalNumber
{
public:
	int 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 print_Daffodils(); //显示所有大于1,且小于数据成员n的水仙花数;
private:
    int n;
};
int NaturalNumber::setValue (int x)
{
	if(x>0) n=x;
	else {cout<<"x不是正整数\n"; return -1;}
}
int NaturalNumber::getValue()
{
	return n;
}
bool NaturalNumber::isPrime()
{
	int i=2;
	if(n==1) return 0;
	else
	{
		for(i=2;i*i<=n;i++)
		{
			if(n%i==0)
				return 0;
		}
	}
	return 1;
}
void NaturalNumber::printFactor()//输出数据成员n的所有因子,包括1和n自身
{
	for(int i=1;i<=n;i++)
		if(n%i==0) cout<<i<<" ";
		cout<<endl;
}
bool NaturalNumber::isPerfect()//判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。
{
    int s;
	for(int i=1;i<n;i++)
	{
		if(n%i==0) s+=i;
	}
	if(s==n) return 1;
	else return 0;
}
bool NaturalNumber::isReverse(int x)//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。
{
	int a[20],m=n,i;
	for(i=0;m;i++)
    {
        a[i]=m%10;
        m/=10;
    }
	while(i>0||x)
	{
		if(a[--i]!=x%10) return 0;
		x/=10;
	}
	return 1;
}
bool NaturalNumber::isDaffodil(int x)//判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
{
	int s=0,m=x;
	while(m)
	{
		s+=(m%10)*(m%10)*(m%10);
			m/=10;
	}
	if(x==s) return 1;
	else return 0;
}
void NaturalNumber::print_Daffodils()//显示所有大于1,且小于数据成员n的水仙花数;
{
	for(int i=1;i<n;i++)
	{
		if(isDaffodil(i)) cout<<i<<" ";
	}
	cout<<endl;
}
int main()
{
	int n;
	NaturalNumber nn;	//定义类的一个实例(对象)
	nn.setValue (6);
	cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数"<<endl;
	cout<<nn.getValue()<<( nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
	nn.setValue (37);
	cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
	cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
	cout<<nn.getValue()<<"你觉得的逆向数是:";
		cin>>n;
	while(!nn.isReverse(n))
    {
        cout<<"不是,请重新输入:";
        cin>>n;
    }
    cout<<"对了\n";
	nn.setValue (84);
	cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
	cout<<nn.getValue()<<(nn.isPerfect()?"是":"不是")<<"完全数"<<endl;
	cout<<nn.getValue()<<"你觉得的逆向数是:";
		cin>>n;
	while(!nn.isReverse(n))
    {
        cout<<"不是,请重新输入:";
        cin>>n;
    }
    cout<<"\n对了\n";
	cout<<nn.getValue()<<"的因子有:";
	nn.printFactor();
	nn.setValue (999);
	cout<<nn.getValue()<<"以下的水花仙数有:\n";
	nn.print_Daffodils();

	//随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况……
}
//请在下面定义类中的各个成员函数



三、项目名称:Book类

#include<iostream>
#include<string.h>
using namespace std;
class book
{
public:
    void setBook(string n,string w,string pu,int p,int m,int no);
    void borrow();
    void restore();
    void print();
    void setNo(int m);
    int getNo();
private:
    string name,wrt,pub;
    int price,num,No;
};
void book::setBook(string n ,string w,string pu,int p,int m,int no)
{
    name=n;wrt=w;pub=pu;
    price=p;num=m;No=no;
}
void book::borrow()
{
    num--;
}
void book::restore()
{
    num++;
}
void book::print()
{
    cout<<"name:"<<name<<endl;
    cout<<"writer:"<<wrt<<endl;
    cout<<"publicer:"<<pub<<endl;
    cout<<"price:"<<price<<endl;
    cout<<"No:"<<No<<endl;
    cout<<"number:"<<num<<endl;
    cout<<"\n";
}
void book::setNo(int m)
{
    No=m;
}
int  book::getNo()
{
    return No;
}
int main()
{
    book b[4];
    b[1].setBook("平凡的世界1","路遥","人民出版社",21,8,1);
    b[2].setBook("平凡的世界2","路遥","人民出版社",22,7,2);
    b[3].setBook("平凡的世界3","路遥","人民出版社",23,6,3);
    b[1].borrow();
    b[1].print();
    b[2].restore();
    b[2].print();
    b[3].setNo(4);
    b[3].print();
}


四、项目名称:学生成绩

#include<iostream>
#include<string>
using namespace std;
class Stu
{
public:
    void setStudent(string na,int c,int m);
    void show();
    void setName(string na);
    string getName();
    float average();
private:
    string name;	//学生姓名
    float chinese;	//语文成绩
    float math;		//数学成绩
    float ave;
    float sum;
};
void Stu::setName(string na)
{
    name=na;
}
void Stu::setStudent(string na,int c,int m)
{
    name=na;
    chinese=c;
    math=m;
    sum=math+chinese;
    ave=sum/2;
}
void Stu::show()
{
    cout<<"Name:  "<<name<<endl;
    cout<<"Score:  "<<chinese<<"     "<<math<<endl;
    cout<<"average:  "<<ave<<"     Sum:  "<<sum<<endl;
    cout<<endl;
}
string Stu::getName()
{
    return name;
}
float Stu::average()
{
    return ave;
}
int main()
{
    Stu s1,s2;
    s1.setStudent("Lin daiyu", 98, 96); //对象置初值
    s2.setStudent("Jia baoyu", 90, 88); //对象置初值
    s1.show();//打印信息
    s2.show();//打印信息
    s1.setName("xue baochai");//重新置p1对象的名字
    s1.show();
    cout<<"s1.Name: "<<s1.getName()<<endl;//打印对象的名字
    cout<<"s1.average: "<<s1.average()<<endl;//打印对象的成绩
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值