一、阅读、运行程序后,按要求增加类的功能:
- #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
- {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 (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;
- }
三、BOOK类
- #include<iostream>
- #include<string>
- using namespace std;
- class Book
- {
- public:
- void setBook(string n,string p,string w,double pri,int num);
- int borrow();
- int restore();
- void printf();
- void setNo(int a);
- int getNo();
- private:
- string name,publicer,writer;
- double price;
- int No,number;
- };
- void Book::setBook(string n,string p,string w,double pri,int num)
- {
- name=n;publicer=p;writer=w;price=pri;number=num;
- }
- int Book::borrow()
- {
- return(number=number-1);
- }
- int Book::restore()
- {
- return(number=number+1);
- }
- void Book::printf()
- {
- cout<<"书名: "<<name<<endl;
- cout<<"出版社: "<<publicer<<endl;
- cout<<"作者: "<<writer<<endl;
- cout<<"价格: "<<price<<endl;
- cout<<"书号: "<<No<<endl;
- cout<<"数量: "<<number<<endl;
- }
- void Book::setNo(int a)
- {
- No=a;
- }
- int Book::getNo()
- {
- return No;
- }
- void main()
- {
- Book b1;
- b1.setBook("C++语言基础教程","北京邮电大学出版社","王更生",33,1);
- b1.setNo(12345678);
- b1.printf();
- }
四、分数类
- #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(10,4);
- a1.output();
- a1.output(1);
- a1.output(3);
- a1.input();
- a1.output(2);
- a1.amplify(10);
- cout<<"将分数增大10倍后,分数为: ";
- a1.output();
- a1.output(3);
- a1.output(2);
- }