C++第二次作业

一:
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time( );
    void show_time( );
private:
    bool is_time(int h, int m, int s);   //这个成员函数设置为私有的,是合适的,请品味
    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;
}
int main( )
{
    Time t1;
    t1.set_time( );
    T1.show_time( );
    return 0;
}
二:<pre name="code" class="cpp">#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 print Daffodils(); //显示所有大于1,且小于数据成员n的水仙花数;
};

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()<<”的因子有:”;
    printFactor();

    //随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况……
}
//请在下面定义类中的各个成员函数
三;
<pre name="code" class="cpp">#include<iostream>
#include<string>
using namespace std;
class Stu
{
private:
    string name;    //学生姓名
    float chinese;  //语文成绩
    float math;     //数学成绩
    //接下去写
};



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;
}
四:
<pre name="code" class="cpp">class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1);   //构造函数,初始化用
    void set(int nu=0,int de=1);    //置值,改变值时用
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入
    void simplify();            //化简(使分子分母没有公因子)
    void amplify(int n);        //放大n倍,如2/3放大5倍为10/3
    void output(int style=0);   //输出:以8/6为例,style为0时,原样输出8/6;
                            //style为1时,输出化简后形式4/3;
                        //style为2时,输出1(1/3)形式,表示一又三分之一;
                            //style为3时,用小数形式输出,如1.3333;
                            //默认方式0
};


 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值