一时间增加
程序代码
- #include <iostream>
- using namespace std;
- class Time
- {
- public:
- void set_time( );
- void show_time( );
- void add_a_sec(); //增加1秒钟
- void add_a_minute(); //增加1分钟
- void add_an_hour(); //增加1小时
- void add_seconds(int n1); //增加n秒钟
- void add_minutes(int n2); //增加n分钟
- void add_hours(int n3); //增加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)"<<endl;
- 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::add_a_sec()
- {
- sec++;
- if(sec>=60)
- {
- sec=sec%60;
- minute++;
- if(minute>=60)
- {
- minute=minute%60;
- hour++;
- if(hour>=24)
- {
- hour=hour%24;
- }
- }
- }
- }
- void Time::add_a_minute()
- {
- minute++;
- if(minute>=60)
- {
- minute=minute%60;
- hour++;
- if(hour>=24)
- {
- hour=hour%24;
- }
- }
- }
- void Time::add_an_hour()
- {
- hour++;
- if(hour>=24)
- {
- hour=hour%24;
- }
- }
- void Time::add_seconds(int n1)
- {
- cout<<"增加的秒数:"<<endl;
- cin>>n1;
- sec+=n1;
- if(sec>=60)
- {
- minute+=(sec/60); sec=sec%60;
- if(minute>=60)
- {
- hour+=(minute/60);
- minute=minute%60;
- if(hour>=24)
- {
- hour=hour%24;
- }
- }
- }
- }
- void Time::add_minutes(int n2)
- {
- cout<<"增加的分数:"<<endl;
- cin>>n2;
- minute+=n2;
- if(minute>=60)
- {
- hour+=(minute/60);
- minute=minute%60;
- if(hour>=24)
- {
- hour=hour%24;
- }
- }
- }
- void Time::add_hours(int n3)
- {
- cout<<"增加的时数:"<<endl;
- cin>>n3;
- hour+=n3;
- if(hour>=24)
- {
- hour=hour%24;
- }
- }
- 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;
- int n1,n2,n3;
- t1.set_time( );
- t1.add_a_sec();
- t1.show_time( );
- t1.set_time( );
- t1.add_a_minute();
- t1.show_time( );
- t1.set_time( );
- t1.add_an_hour();
- t1.show_time( );
- t1.set_time( );
- t1.add_seconds(n1);
- t1.show_time( );
- t1.set_time( );
- t1.add_minutes(n2);
- t1.show_time( );
- t1.set_time( );
- t1.add_hours(n3);
- t1.show_time( );
- return 0;
- }
二 正整数类
程序代码
- #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 printDaffodils(); //显示所有大于1,且小于数据成员n的水仙花数;
- };
- void NaturalNumber::setValue(int x)//置数据成员n的值,要求判断是否是正整数
- {
- n=x;
- if(n>0&&n%1==0)
- cout<<"是正整数"<<endl;
- else cout<<"不是正整数"<<endl;
- }
- int NaturalNumber::getValue()//返回私有数据成员n的值
- {
- return n;
- }
- bool NaturalNumber::isPrime()//判断数据成员n是否为素数,是返回true,否则返回false
- {
- int i;
- for(i=2;i<n;i++)
- {
- if(n%i==0)
- return false;
- }
- return true;
- }
- void NaturalNumber::printFactor()//输出数据成员n的所有因子,包括1和n自身
- {
- int i;
- for(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 i,sum;
- sum=0;
- for(i=1;i<n;i++)
- {
- if(n%i==0)
- sum+=i;
- }
- if(sum==n)
- cout<<"是完全数"<<endl;
- else cout<<"不是完全数"<<endl;
- }
- bool NaturalNumber::isReverse(int x)//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)
- {
- int sum=0;
- while(x)
- {
- sum=sum*10+x%10;
- x/=10;
- }
- if(n==sum)
- cout<<"是逆向数"<<endl;
- else
- cout<<"不是逆向数"<<endl;
- }
- bool NaturalNumber::isDaffodil(int x)//判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3
- {
- int a,b,c;
- a=x/100;
- b=(x/10)%10;
- c=x%10;
- if(x==a*a*a+b*b*b+c*c*c)
- cout<<"是水仙花数"<<endl;
- else cout<<"不是水仙花数"<<endl;
- }
- void NaturalNumber::printDaffodils()//显示所有大于1,且小于数据成员n的水仙花数;
- {
- int i;
- int a,b,c;
- for(i=2;i<n;i++)
- {
- a=i/100;
- b=(i/10)%10;
- c=i%10;
- if(i==a*a*a+b*b*b+c*c*c)
- cout<<i<<" ";
- }
- }
- int main()
- {
- NaturalNumber nn; //定义类的一个实例(对象)
- nn.setValue (321);
- nn.getValue();
- cout<<"ok"<<endl;
- nn.isPrime();
- cout<<endl;
- nn.printFactor();
- cout<<endl;
- nn.isPerfect();
- cout<<endl;
- nn.isReverse(123);
- cout<<endl;
- nn.isDaffodil(200);
- cout<<endl;
- nn.printDaffodils();
- cout<<endl;
- return 0;
- }
三 Book类
程序代码
- #include<iostream>
- using namespace std;
- class Book
- {
- public:
- void setBook(string n,string w,string N,string pub,float pri,int num);
- int borrow();
- int restore();
- void print();
- string set_NO(string a);
- string get_NO();
- private:
- string name;
- string writer;
- string publicer;
- float price;
- int number;
- string NO;
- };
- void Book::setBook(string n,string w,string N,string pub,float pri,int num)
- {
- name=n;
- writer=w;
- NO=N;
- publicer=pub;
- price=pri;
- number=num;
- }
- int Book::borrow()
- {
- number--;
- return number;
- }
- int Book::restore()
- {
- number++;
- return number;
- }
- void Book::print()
- {
- cout<<name<<" "<<writer<<" "<<NO<<" "<<publicer<<" "<<price<<" "<<number<<endl;
- }
- string Book::set_NO(string a)
- {
- NO=a;
- return NO;
- }
- string Book::get_NO()
- {
- return NO;
- }
- int main()
- {
- Book b1;
- b1.setBook("C++","某某","12.34","某某出版社",30,100);
- b1.print();
- cout<<b1.borrow()<<endl;
- cout<<b1.restore()<<endl;
- cout<<b1.set_NO("12.35")<<endl;
- cout<<b1.get_NO()<<endl;
- return 0;
- }
- //编写一个Book类,包含name(书名)、writer(作者)、publicer(出版社)、price(价格)、number(数量)、NO(书号)等数据成员。
- //定义成员函数setBook,用来给书籍的数据成员赋值,定义成员函数borrow和restore,分别办理借书和还书(借、还书时,暂时只完成数量的增1减1)业务,
- //定义成员函数print用于输出—有关书的信息,定义set_NO与get_NO来实现修改类对象的书号与获得类对象的书号。
- //自己写main函数,用来测试你编写的类。
四 分数类
程序代码
- #include<iostream>
- using namespace std;
- class CFraction
- {
- 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
- private:
- int nume; // 分子
- int deno; // 分母
- };
- CFraction::CFraction(int nu,int de)
- {
- nume=nu;
- deno=de;
- }
- void CFraction::set(int nu,int de) //置值,改变值时用
- {
- nume=nu;
- deno=de;
- }
- void CFraction::input()//按照"nu/de"的格式,如"5/2"的形式输入
- {
- char c;
- cout<<"请输入值"<<endl;
- cin>>nume>>c>>deno;
- if(c!='/')
- {
- cout<<"输入错误"<<endl;
- }
- }
- void CFraction::simplify()//化简(使分子分母没有公因子)
- {
- int t,r;
- int n,d;
- n=nume;
- d=deno;
- if(n<d)
- {
- t=n;
- n=d;
- d=t;
- }
- while(d!=0)
- {
- r=n%d;
- n=d;
- d=r;
- }
- //cout<<nume/n<<" "<<deno/n<<endl;
- nume=nume/n;
- deno=deno/n;
- }
- void CFraction::amplify(int n)//放大n倍,如2/3放大5倍为10/3
- {
- cout<<nume*n<<"/"<<deno<<endl;
- }
- void CFraction::output(int style) //输出:以8/6为例,style为0时,原样输出8/6;
- //style为1时,输出化简后形式4/3;
- //style为2时,输出1(1/3)形式,表示一又三分之一;
- //style为3时,用小数形式输出,如1.3333;
- //默认方式0
- {
- if(style==0)
- {
- cout<<nume<<"/"<<deno<<endl;
- }
- if(style==1)
- {
- simplify();
- cout<<nume<<"/"<<deno<<endl;
- }
- if(style==2)
- {
- if(nume>deno)
- {
- int i,j;
- i=nume/deno;
- j=nume%deno;
- cout<<i<<"("<<j<<"/"<<deno<<")"<<endl;
- }
- else cout<<nume<<"/"<<deno<<endl;
- }
- if(style==3)
- {
- double p;
- p=nume/deno;
- cout<<p<<endl;
- }
- }
- int main()
- {
- int nu,de,style,n;
- CFraction c;
- c.set(nu=0,de=1); //置值,改变值时用
- c.input(); //按照"nu/de"的格式,如"5/2"的形式输入
- c.simplify();
- c.output(); //化简(使分子分母没有公因子)
- c.amplify(2); //放大n倍,如2/3放大5倍为10/3
- c.output(style=0);
- return 0;
- }