C++作业2-类和对象1

来源:互联网 发布:网络十大帅哥排行榜 编辑:程序博客网 时间:2024/05/29 19:26

一、正整数类

#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<<" ";          }      }  }  void main(void)    {        NaturalNumber nn;       nn.setValue (7);        cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;            ;nn.setValue (32);         cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;            nn.setValue (79);         cout<<nn.getValue()<<"的因子有:";        nn.printFactor();       cout<<endl;      nn.setValue(666);      cout<<nn.getValue()<<"的水仙花数有:  ";      nn.printDaffodils();      cout<<endl;  }  
二、学生成绩

#include<iostream>  #include<string>    using namespace std;    class Stu    {  public:      void setStudent(string n,float c,float m);      void show();      void setName(string a);      string getName();      double average();  private:        string name;    //学生姓名        float chinese;  //语文成绩        float math;     //数学成绩  };    void Stu::setStudent(string n,float c,float m)  {      name=n;chinese=c;math=m;  }  void Stu::show()  {      cout<<"名字:  "<<name<<endl;      cout<<"得分  :"<<chinese<<"   "<<math<<endl;      cout<<"平均:   "<<(chinese+math)/2<<"   sum:  "<<(math+chinese)<<endl;  }  void Stu::setName(string a)  {      name=a;  }  string Stu::getName()  {      return name;  }  double Stu::average()  {      return((math+chinese)/2);  }  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 CFraction    {    private:        int nume;  // 分子        int deno;  // 分母    public:      CFraction(int nu=0,int de=1)      {          nume=nu;deno=de;      }      void set(int nu=0,int de=1)      {  nume=nu;deno=de;      }        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;    {          if(style==0)              cout<<"原样输出:"<<nume<<"/"<<deno<<endl;          else if(style==1)          {cout<<"输出化简后的分数: ";simplify();}          else if(style==2)          {              int a,b;              if(nume>deno)              {                  a=nume-deno;                  while(1)                  {                      if(a>deno)                          a=a-deno;                      else if(a<deno)                          break;                  }              }              b=(nume-a)/deno;              cout<<"输出真分数形式: "<<b<<"("<<a<<"/"<<deno<<")"<<endl;          }          else if(style==3)          {              double c;              c=double(nume)/double(deno);              cout<<"输出小数形式: "<<c<<endl;          }      }                           };  void CFraction::input()  {      char a;      cout<<"请输入一个分数(格式a/b): "<<endl;      while(1)      {          cin>>nume>>a>>deno;          if(a!='/')              cout<<"输入格式不正确,请重新输入"<<endl;          else              break;      }  }  void CFraction::simplify()  {      int a;      for(a=2;a<=nume;a++)      {          if(nume%a==0)          {              if(deno%a==0)              {nume=nume/a;deno=deno/a;}          }      }      cout<<nume<<"/"<<deno<<endl;  }  void CFraction::amplify(int n)  {      nume=nume*n;  }  void main()  {      CFraction a1;      a1.set(9,7);      a1.output();      a1.output(6);      a1.output(4);      a1.input();      a1.output(3);      a1.amplify(8);      cout<<"将分数增大10倍后,分数为: ";      a1.output();      a1.output(7);      a1.output(1);  }  
四、时间类

#include <iostream>    using namespace std;    class Time    {    public:        void set_time( );           void show_time( );      void add_a_sec()      {          sec=sec+1;      }      void add_a_minute()       {         minute=minute+1;      }      void add_an_hour()       {          hour=hour+1;      }      int add_seconds(int n);      int add_minutes(int n);      int add_hours(int n);      void tiao();  private:         bool is_time(int, int, int);   //这个成员函数设置为私有的,是合适的,请品味        int hour;        int minute;        int sec;    };  void Time::add_seconds(int n)  {  sec=sec+n;  }  void Time::add_minutes(int n)  {     minute=minute+n;  }  void Time::add_hours(int n)  {      hour=hour+n;  }  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::tiao()  {      if(sec>=60)          sec=0;      if(minute>=60)          minute=0;      if(hour>=60)          hour=0;  }




0 0
原创粉丝点击