C++学习2

来源:互联网 发布:windows 制作mac u盘 编辑:程序博客网 时间:2024/05/01 05:12

课后习题编程题(C++语言程序设计 杨进才版)

第七章 类与对象

P241

//1.求三角形面积和周长#include <iostream>#include <cmath>using namespace std;class triangle{    private:        double a,b,c;    public:        triangle(double a=0,double b=0,double c=0)        {            this->a=a;            this->b=b;            this->c=c;        }        double getarea()        {            double p=(a+b+c)/2.0;            return sqrt(p*(p-a)*(p-b)*(p-c));        }        double getcircum()        {            return a+b+c;        }};int main(){    triangle t(1,1,1);    cout<<t.getarea()<<endl;    cout<<t.getcircum()<<endl;    return 0;}



//2.定义point求距离#include <iostream>#include <cmath>using namespace std;class point{    private:        double x,y;    public:        point(double x=0,double y=0)        {            this->x=x;            this->y=y;        }        double distance(point a)        {            return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));        }};int main(){    point p1;    point p2(1,1);    cout<<p1.distance(p2)<<endl;    return 0;}


//3.定义日期类,求下一天#include <iostream>using namespace std;bool is_leap(int n){    if(n%4==0)    {        if(n%100==0)        {            if(n%400==0)                return true;            return false;        }        return true;    }    return false;}class CData{    private:        int y,m,d;    public:        CData(int y=0,int m=0,int d=0)        {            this->y=y;            this->m=m;            this->d=d;        }        void Newday()        {            if(m==12&&d==31)            {                y++;                m=1;                d=1;                return ;            }            if(is_leap(y))            {                if(m==2)                {                    d++;                    if(d==29)                        d=1,m++;                    else if(d>=30)                        cout<<"input error!"<<endl;                    return ;                }            }            else            {                if(m==2)                {                    d++;                    if(d==30)                        d=1,m++;                    return;                }            }            if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)            {                if(d==31)                {                    d=1;                    m++;                }                else                    d++;            }            else if(m==4||m==6||m==9||m==11)            {                if(d==30)                {                    d=1;                    m++;                }                else                    d++;            }        }        void show()        {            cout<<y<<"."<<m<<"."<<d<<endl;        }};int main(){    CData d(2012,2,28);    d.Newday();    d.show();    return 0;}


//4.定义时钟类,模拟闹钟//与北京时间同步,求勿鄙视#include <iostream>#include <time.h>using namespace std;class T{    private:        int h,m,s,H,M,S;    public:        T(int h=0,int m=0,int s=0)        {            this->h=h;            this->s=s;            this->m=m;        }        T(long long tt)        {            s=tt%60;            tt/=60;            m=tt%60;            tt/=60;            h=tt%24;            h+=8;            h%=24;        }        void show()        {            cout<<h<<":"<<m<<":"<<s<<endl;        }        void set_alarm(int h=0,int m=0,int s=0)        {            this->H=h;            this->S=s;            this->M=m;        }        void fun()        {            long long r=time(0);            int ss=r%60;            r/=60;            int mm=r%60;            r/=60;            int hh=r%24;            hh+=8;            hh%=24;            if(hh!=h||ss!=s||mm!=m)            cout<<h<<":"<<m<<":"<<s<<endl;            h=hh;            m=mm;            s=ss;        }        bool  is_ok()        {            if(h==H&&m==M&&s==S)                return true;            return false;        }};int main(){    long long miao=time(0);    T c(miao);    c.set_alarm(10,32,00);    while(1)    {        if(c.is_ok())        {            c.show();            cout<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a"<<endl;            cout<<"pika~"<<endl;            break;        }        else            c.fun();    }    return 0;}

//5.设计学生类,计算平均成绩#include <iostream>#include <cstring>using namespace std;class student{    private:        long no;        char *name;        char *classname;        int grade;        static int num;        static int sum;    public:        student(long no=0,char *name=NULL,char *classname=NULL,int grade=0)        {            num++;            this->no=no;            this->name=new char [strlen(name)+1];            strcpy(this->name,name);            this->classname=new char [strlen(classname)+1];            strcpy(this->classname,classname);            this->grade=grade;            sum+=grade;        }        static double average()        {            return (sum*1.0/num);        }};int student::sum=0;int student::num=0;int main(){    student s1(1234,"nimei","data",89);    student s2(1234,"nimei","data",90);    student s3(1234,"nimei","data",91);    student s4(1234,"nimei","data",92);    cout<<student::average()<<endl;    return 0;}


//6.信息管理登陆系统#include <iostream>#include <cstring>using namespace std;class system{    private:        char *name;        long long password;    public:        system(char *name=NULL,long long password=0)        {            this->password=password;            this->name=new char [strlen(name)+1];            strcpy(this->name,name);        }        bool is_ok(system &p)        {            if(password==p.password&&strcmp(p.name,name)==0)                return true;            return false;        }};int main(){    system miao("nicai",1234);    int t=4;    while(t)    {        cout<<"please input the name :"<<endl;        char *name;        cin>>name;        cout<<"please input the password :"<<endl;        long long no;        cin>>no;        system wu(name,no);        if(wu.is_ok(miao))        {            cout<<"ok"<<endl;            break;        }        else        {            t--;            cout<<"wrong"<<endl<<endl;            if(t==0)            {                cout<<"sha bi !"<<endl;                cout<<"You are not allowed to loggin in"<<endl;                break;            }            cout<<"I will give you "<<t<<" more times"<<endl<<endl;        }    }    return 0;}


//7.定义一个String类//函数判断是否为子串,换成大写,转换成数#include <iostream>#include <cstring>#include <cstdlib>using namespace std;class String{    private:        char *str;        int len;    public:        String(const char *s)        {            str=new char[strlen(s)+1];            strcpy(str,s);            len=strlen(s);        }        int getlen()        {            return len;        }        bool issubstring(const String &sstr)        {            char *pstr=sstr.str;            int plen=sstr.len,j;            for(int i=0;i<len;i++)            {                int k=i;                for(j=0;j<plen;j++,k++)                    if(pstr[j]!=str[i]) break;                if(j==plen) return true;            }            return false;        }        bool issubstring(const char *sstr)        {            int j;            for(int i=0;i<len;i++)            {                int k=i;                for(j=0;sstr[j];j++,k++)                    if(sstr[j]!=str[k])                        break;                if(!str[j]) return true;            }            return false;        }        long long strnum()        {            long long a;            a=atoi(str);            return a;        }        void touppercase()        {            for(int i=0;i<len;i++)            if(str[i]<='z'&&str[i]>='a')                str[i]-=32;        }        void show()        {            cout<<str<<" "<<len<<endl;        }};int main(){    String s1("1234");    cout<<s1.strnum()<<endl;    String s2("abs");    s2.touppercase();    s2.show();    if(s2.issubstring("ABSFRE"))        cout<<"ok"<<endl;    else        cout<<"wrong"<<endl;    if(s2.issubstring(s1))        cout<<"ok"<<endl;    else        cout<<"wrong"<<endl;    return 0;}


//8.集合类Set及操作#include <iostream>using namespace std;class Set{    private:        int n;        int *a;    public:        Set()        {            a=NULL;            n=0;        }        bool is_empty()        {            if(n)                return false;            return true;        }        int size()        {            return n;        }        bool is_in_set(int num)        {            for(int i=1;i<=n;i++)                if(a[i]==num)                    return  true;            return false;        }        bool is_in_set(int num)const        {            for(int i=1;i<=n;i++)                if(a[i]==num)                    return true;                return false;        }        Set(Set &t)        {            this->n=t.n;            for(int i=1;i<=n;i++)                this->a[i]=t.a[i];        }        void insert(int num)        {            if(is_in_set(num))                return ;            int b[n];            for(int i=1;i<=n;i++)                b[i]=a[i];            delete []a;            n++;            a=new int [n+1];            for(int i=1;i<n;i++)                a[i]=b[i];            a[n]=num;        }        void show()        {            if(n==0)            {                cout<<"NULL"<<endl;                return;            }            cout<<"{ ";            for(int i=1;i<n;i++)                cout<<" "<<a[i]<<",";            cout<<" "<<a[n]<<" }"<<endl;            cout<<"size="<<n<<endl;        }        bool issubset(const Set &s) const        {             if(s.n>n) return false;             for(int i=1;i<=s.n;i++)                if(!is_in_set(s.a[i]))                    return false;             return true;        }        bool is_equal(const Set &s) const        {            if(n!=s.n)                return false;            if(issubset(s))                return true;            return false;        }        Set Union(Set &s)        {            for(int i=1;i<=s.n;i++)            {                if(is_in_set(s.a[i]))                    continue;                insert(s.a[i]);            }            return *this;        }        Set Intersection(Set &s)        {            Set A;            for(int i=1;i<=n;i++)                for(int j=1;j<=s.n;j++)                    if(a[i]==s.a[j])                        A.insert(a[i]);            return A;        }        Set difference(Set &s)        {            Set A;            for(int i=1;i<=n;i++)                    if(!s.is_in_set(a[i]))                        A.insert(a[i]);            return A;        }};int main(){    Set s;    for(int i=1;i<=9;i++)        s.insert(i);    s.insert(1000);    s.show();    cout<<"Set s's size : "<<s.size()<<endl;    Set m;    for(int i=4;i<=8;i++)        m.insert(i);    m.insert(100);    m.show();    if(m.is_empty())        cout<<"empty."<<endl;    else        cout<<"not empty."<<endl;    if(s.issubset(m))        cout<<"Set s contains Set m."<<endl;    else        cout<<"Set s doesn't contains Set m."<<endl;    if(s.is_equal(m))        cout<<"equal."<<endl;    else        cout<<"different."<<endl;    s.Union(m);    s.show();    m.Intersection(s);    m.show();    s=s.difference(m);    s.show();    return 0;}