c++第二次实验 作业

book类

一.问题及代码

/*  
* 文件名称:class book.cpp  
* 作    者  曾魏伟
* 完成日期:2016 年 4月 8日  
* 版 本 号:v1.0  
* 编写一个Book类 
* 输入描述:无  
* 问题描述:c++编写类与对象; 
* 程序输出: 
* 问题分析:无 
* 算法设计:类与函数; 
*/    
#include<iostream>  
#include<string>  
using namespace std;  
class Stu  
{  
public:  
    void setStudent(string n,float x,float y);  
    void show();  
    void setName(string m);  
    string getName();  
    float average();  
private:  
    string name;    //学生姓名  
    float chinese;  //语文成绩  
    float math;     //数学成绩  
    //接下去写  
};  
  
void Stu::setStudent(string n,float x,float y)  
{  
    name=n;  
    chinese=x;  
    math=y;  
}  
void Stu::show()  
{  
    cout<<"Name:  "<<name<<endl;  
    cout<<"Score:  "<<chinese<<"     "<<math<<endl;  
    cout<<"average:  "<<(chinese+math)/2<<"    Sum:"<<chinese+math<<endl;  
}  
void Stu::setName(string m)  
{  
    name=m;  
}  
string Stu::getName()  
{  
    return name;  
}  
float Stu::average()  
{  
    float a;  
    a=(chinese+math)/2;  
    return a;  
}  
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;  
}  

二.运行结果


 正整数类

一.问题及代码

#include<iostream>      
using namespace std;      
class NaturalNumber      
{private:      
    int n;       
public:      
    void setValue (int x);    
    int getValue();    
    bool isPrime();      
    void printFactor();     
    bool isPerfect();    
    bool isDaffodil(int x);    
    void printDaffodils();    
};    
void NaturalNumber::setValue (int x)    
{    
    if(x>0&&(int)x==x)    
        cout<<x<<"是正整数"<<endl;    
    n=x;    
}    
int NaturalNumber::getValue()    
{    
return n;    
}    
bool NaturalNumber::isPrime()    
{    
    int a;    
    for(a=2;a<=n;a++)    
    {    
        if(n%a==0)    
            break;    
    }    
    if(n==a)    
        return true;    
    else    
        return false;    
}    
void NaturalNumber::printFactor()    
{    
    int a;    
    for(a=1;a<=n;a++)    
    {    
        if(n%a==0)    
            cout<<a<<" ";    
    }    
}    
bool NaturalNumber::isPerfect()    
{    
    int a,sum=0;    
    for(a=1;a<n;a++)    
    {    
        if(n%a==0)    
            sum=sum+a;    
    }    
    if(sum==n)    
    return true;    
    else    
        return false;    
}    
bool NaturalNumber::isDaffodil(int x)    
{    
    if(x==1)    
        return true;    
    else if(x<10)    
        return false;    
    else if(x<100)    
    {int a,b;    
    a=x/10;b=x-a*10;    
    if((a*a*a+b*b*b)==x)    
        return true;    
    else    
        return false;    
    }    
    else if(x<1000)    
    {    
        int a,b,c;    
        a=x/100;b=(x-a*100)/10;c=x-a*100-b*10;    
        if(x==(a*a*a+b*b*b+c*c*c))    
            return true;    
        else    
            return false;    
    }    
    
}    
void NaturalNumber::printDaffodils()    
{    
    int x;    
    for(x=2;x<n;x++)    
    {    
        if(x<100&&x>10)    
        {int a,b;    
        a=x/10;b=x-a*10;    
        if((a*a*a+b*b*b)==x)    
            cout<<x<<" ";    
        }    
        else if(x<1000&&x>100)    
        {    
            int a,b,c;    
            a=x/100;b=(x-a*100)/10;c=x-a*100-b*10;    
            if(x==(a*a*a+b*b*b+c*c*c))    
                cout<<x<<" ";    
        }    
    }    
}    
      
int 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();     
    cout<<endl;    
    nn.setValue(888);    
    cout<<nn.getValue()<<"的水仙花数有:  ";    
    nn.printDaffodils();    
    cout<<endl;    
}    
二.运行结果
<img src="https://img-blog.csdn.net/20160412092934346?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值