C++2回目の実験作業

9195 ワード

一、プログラムを読み、実行した後、要求に応じてクラスの機能を追加する.
#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<<"
:"; t1.add_a_sec(); t1.show_time( ); cout<<endl; cout<<" :"; t1.add_a_minute(); t1.show_time( ); cout<<endl; cout<<"
:"; t1.add_an_hour(); t1.show_time( ); cout<<"
:"; cin>>n; cout<<"
"<<n<<" :"; t1.add_seconds(n); t1.show_time( ); cout<<"
:"; cin>>n; cout<<"
"<<n<<" :"; t1.add_minutes(n); t1.show_time( ); cout<<"
:"; cin>>n; cout<<"
"<<n<<" :"; t1.add_hours(n); t1.show_time( ); return 0; }

二、正の整数類:
#include<iostream>  
using namespace std;  
class NaturalNumber  
{  
public:  
    int 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     ;  
private:  
    int n;  
};  
int NaturalNumber::setValue (int x)  
{  
    if(x>0) n=x;  
    else {cout<<"x     
"; return -1;} } int NaturalNumber::getValue() { return n; } bool NaturalNumber::isPrime() { int i=2; if(n==1) return 0; else { for(i=2;i*i<=n;i++) { if(n%i==0) return 0; } } return 1; } void NaturalNumber::printFactor()// n , 1 n { for(int 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 s; for(int i=1;i<n;i++) { if(n%i==0) s+=i; } if(s==n) return 1; else return 0; } bool NaturalNumber::isReverse(int x)// x n ( 321 123 )。 { int a[20],m=n,i; for(i=0;m;i++) { a[i]=m%10; m/=10; } while(i>0||x) { if(a[--i]!=x%10) return 0; x/=10; } return 1; } bool NaturalNumber::isDaffodil(int x)// x 。 , 153=1*1*1+5*5*5+3*3*3 { int s=0,m=x; while(m) { s+=(m%10)*(m%10)*(m%10); m/=10; } if(x==s) return 1; else return 0; } void NaturalNumber::print_Daffodils()// 1, n ; { for(int i=1;i<n;i++) { if(isDaffodil(i)) cout<<i<<" "; } cout<<endl; } int main() { int n; NaturalNumber nn; // ( ) nn.setValue (5); cout<<nn.getValue()<<(nn.isPrime()?" ":" ")<<" "<<endl; cout<<nn.getValue()<<( nn.isPerfect()?" ":" ")<<" "<<endl; nn.setValue (59); cout<<nn.getValue()<<(nn.isPrime()?" ":" ")<<" " <<endl; cout<<nn.getValue()<<(nn.isPerfect()?" ":" ")<<" "<<endl; cout<<nn.getValue()<<" :"; cin>>n; while(!nn.isReverse(n)) { cout<<" , :"; cin>>n; } cout<<"
"; nn.setValue (81); cout<<nn.getValue()<<(nn.isPrime()?" ":" ")<<" " <<endl; cout<<nn.getValue()<<(nn.isPerfect()?" ":" ")<<" "<<endl; cout<<nn.getValue()<<" :"; cin>>n; while(!nn.isReverse(n)) { cout<<" , :"; cin>>n; } cout<<"

"; cout<<nn.getValue()<<" :"; nn.printFactor(); nn.setValue (999); cout<<nn.getValue()<<" :
"; nn.print_Daffodils(); // , 。 …… }

三、Book類
#include<iostream>       
#include<string>       
using namespace std;    
class Book    
{    
public:    
    void setBook(string n,string w,string p,int pr,int nu,string N);    
    void borrow();    
    void restore();    
    void print();    
    void set_NO(string N);    
    string get_NO();    
private:    
    string name,writer,publicer,NO;    
    int price,number;    
};  
  
void Book::setBook(string n,string w,string p,int pr,int nu,string N)  
{  
    name=n;  
    writer=w;  
    publicer=p;  
    price=pr;  
    number=nu;  
    NO=N;  
}  
  
void Book::borrow()  
{  
    number--;  
}  
  
void Book::restore()  
{  
    number++;  
}  
  
void Book::print()  
{  
    cout<<"  :"<<name<<endl;  
    cout<<"  :"<<writer<<endl;  
    cout<<"   :"<<publicer<<endl;  
    cout<<"  :"<<NO<<endl;  
    cout<<"  :"<<price<<endl;  
    cout<<"  :"<<number<<endl;  
    cout<<endl;  
}  
  
void Book::set_NO(string N)  
{  
    NO=N;  
}  
  
string Book::get_NO()  
{  
    return NO;  
}  
  
int main()  
{  
    Book book1,book2;  
    book1.setBook("    ","     ","         ",45,56,"ISBN 978-7-5357-3230-9");  
    book2.setBook("      ","   ","       ",24,16,"ISBN 978-7-03-018442-9");  
    book1.borrow();  
    book1.print();  
    book2.restore();  
    book2.print();  
    book2.set_NO("ISBN 111-1-1111-1111-1");  
    book2.print();  
    return 0;  
}  

四、学生の成績
#include<iostream>  
#include<string>  
using namespace std;  
class Stu  
{  
public:  
    void setStudent(string na,int c,int m);  
    void show();  
    void setName(string na);  
    string getName();  
    float average();  
private:  
    string name;    //      
    float chinese;  //      
    float math;     //      
    float ave;  
    float sum;  
};  
void Stu::setName(string na)  
{  
    name=na;  
}  
void Stu::setStudent(string na,int c,int m)  
{  
    name=na;  
    chinese=c;  
    math=m;  
    sum=math+chinese;  
    ave=sum/2;  
}  
void Stu::show()  
{  
    cout<<"Name:  "<<name<<endl;  
    cout<<"Score:  "<<chinese<<"     "<<math<<endl;  
    cout<<"average:  "<<ave<<"     Sum:  "<<sum<<endl;  
    cout<<endl;  
}  
string Stu::getName()  
{  
    return name;  
}  
float Stu::average()  
{  
    return ave;  
}  
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;  
}