C+实验2

A:

/*
    title:C++2A
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
class String
{
public :
    String(const char *str=NULL);
    String(String &r);
    ~String();
private:
    char *mydata;
};
String::String(const char *str)
{
    //cout<<str<<endl;
    char s[1000];
    mydata=s;
    if(str)
    {
         strcpy(mydata,str);
         cout<<"gouzao"<<' '<<mydata<<endl;;
    }
   else
   {
        mydata=NULL;
        cout<<"gouzao"<<endl;
   }
}
String::String(String &p)
{

    char s[1000];
    mydata=s;
    *mydata=*p.mydata;
    cout<<"kaobei gouzao";
    if(mydata!=NULL)
        cout<<' '<<mydata<<endl;
}
String::~String()
{
    cout<<"xigou";
    if(mydata!=NULL)
        cout<<' '<<mydata<<endl;
    else
        cout<<endl;
}
int main()
{
    String s1,s2("hello");
    String s3(s2);
    return 0;
}

B:

/*
    title:
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
class Point
{
public:
  Point(int xx,int yy);
  Point(Point &r);
  int GetX();
  int GetY();
  ~Point();
private:
  int x,y;
};
class Distance
{
private:
  Point p1,p2;
  double dist;
public:
  Distance(Point a,Point b);
  double GetDis();
  ~Distance();
};
Point::Point(int xx,int yy)
{
    x=xx;
    y=yy;
    cout<<"Point's constructor was called"<<endl;
}
Point::Point(Point &r)
{
    x=r.x;
    y=r.y;
    cout<<"Point's copyConstructor was called"<<endl;
}
int Point::GetX()
{
    return x;
}
int Point::GetY()
{
    return y;
}
Point::~Point()
{
    cout<<"Point's destructor was called"<<endl;
}
Distance::Distance(Point a,Point b):p1(a),p2(b)
{
    cout<<"Distance's constructor was called"<<endl;
    cout<<endl;
}
double Distance::GetDis()
{
    double X=fabs(p1.GetX()-p2.GetX());
    double Y=fabs(p1.GetY()-p2.GetY());
    dist=sqrt(pow(X,2)+pow(Y,2));
    return dist;
}
Distance::~Distance()
{
    cout<<"Distance's destructor was called"<<endl;
}
int main()
{
  Point myp1(1,1),myp2(4,5);
  Distance myd(myp1,myp2);
  cout<<endl;
  cout<<"the distance is:"<<myd.GetDis()<<endl;
  cout<<endl;
}

C:

/*
    title:C++2C
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
struct FootballPlayer
{
  char name[20];
  int height;
  int age;
  char club[20];
  FootballPlayer *next;
};
void CreateFromHead(FootballPlayer *&f)
{
    FootballPlayer *s=new FootballPlayer();
    cin>>s->name>>s->height>>s->age>>s->club;
    s->next=f;
    f=s;
    return ;
}
void CreateFromTail(FootballPlayer *&f)
{
    FootballPlayer *s=new FootballPlayer();
    FootballPlayer *r=new FootballPlayer();
    cin>>s->name>>s->height>>s->age>>s->club;
    s->next=NULL;
    if(f==NULL)
    {
        f=s;
        return ;
    }
    r=f;
    while(r->next)
    {
        r=r->next;
    }
    r->next=s;
    return ;
}
void Show(FootballPlayer *f)
{
    while(f)
    {
        cout<<f->name<<' '<<f->height<<' '<<f->age<<' '<<f->club<<endl;
        f=f->next;
    }
    return ;
}
int main()
{
 int n,i;
 FootballPlayer *head;
 head=NULL;
 n=6;
 for(i=0;i<n;i++)
    CreateFromHead(head);
 Show(head);
 cout<<endl;
 head=NULL;
 for(i=0;i<n;i++)
    CreateFromTail(head);
Show(head);
 return 0;
}

D:

/*
    title:
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
class Student
{
public :
    Student(char *s,int sc)
    {
        strcpy(name,s);
        score=sc;
        total+=score;
    }
    int hetscore()
    {
        return score;
    }
    static void showtotal()
    {
        cout<<total<<endl;
    }
    static void Average(int sum);
private :
    static int total;
    int score;
    char name[10];
};
int Student::total=0;
void Student::Average(int sum)
{
    cout<<total/sum<<endl;
}
int main()
{
    int n=0;
    int score;
    char s[10];
    while(~scanf("%s%d",s,&score))
    {
        Student(s,score);
        n++;
    }
    Student::showtotal();
    Student::Average(n);
    return 0;
}

E:

/*
    title:
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
class Student
{
public :
    Student(char *s,int sc)
    {
        strcpy(name,s);
        score=sc;
        total+=score;
    }
    static void showtotal()
    {
        cout<<total<<endl;
    }
    static void Average(int sum);
    friend char Compare(const Student s1,const Student s2);
private :
    static int total;
    int score;
    char name[10];
};
int Student::total=0;
void Student::Average(int sum)
{
    cout<<total/sum<<endl;
}
char Compare(const Student s1,const Student s2)
{
    if(s1.score>s2.score)
        return '>';
    if(s1.score==s2.score)
        return '=';
    return '<';
}
int main()
{
    int score;
    char s[10];
    cin>>s>>score;
    Student s1(s,score);
    cin>>s>>score;
    Student s2(s,score);
    cout<<Compare(s1,s2)<<endl;
    return 0;
}

E:

/*
    title:
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
class Student
{
public :
    Student(char *s,int sc)
    {
        strcpy(name,s);
        score=sc;
        total+=score;
    }
    static void showtotal()
    {
        cout<<total<<endl;
    }
    static void Average(int sum);
    friend char Compare(const Student s1,const Student s2);
private :
    static int total;
    int score;
    char name[10];
};
int Student::total=0;
void Student::Average(int sum)
{
    cout<<total/sum<<endl;
}
char Compare(const Student s1,const Student s2)
{
    if(s1.score>s2.score)
        return '>';
    if(s1.score==s2.score)
        return '=';
    return '<';
}
int main()
{
    int score;
    char s[10];
    cin>>s>>score;
    Student s1(s,score);
    cin>>s>>score;
    Student s2(s,score);
    cout<<Compare(s1,s2)<<endl;
    return 0;
}

F:

/*
    title:
    description:
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
class Time
{
public:
    int SecCalc()
    {
        return(hour*60+minute)*60+second;
    }
    Time(int h,int m, int s=0);
    Time(int s);
    Time();
    void SetTime(int h=0,int m=0, int s=0);
    void print_12();
    void print_24();
    Time Add(Time &);
    Time Sub(Time &);
private:
    int hour,minute,second;
};
Time::Time()
{
}
Time::Time(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}
Time::Time(int s)
{
    hour=s/3600;
    minute=(s%3600)/60;
    second=(s%3600)%60;
}
void Time::print_12()
{
    if(hour>12)
    {
        cout<<(hour%12<10?"0":"")<<hour%12<<':'<<(minute<10?"0":"")<<minute<<':'<<(second<10?"0":"")<<second<<' '<<"PM";
    }
    else
        cout<<(hour<10?"0":"")<<hour<<':'<<(minute<10?"0":"")<<minute<<':'<<(second<10?"0":"")<<second<<' '<<"AM";
    return ;
}
void Time::print_24()
{
    cout<<(hour<10?"0":"")<<hour<<':'<<(minute<10?"0":"")<<minute<<':'<<(second<10?"0":"")<<second;
    return ;
}
Time Time::Add(Time &T)
{
    int p=SecCalc()+T.SecCalc();
    Time time=Time(p);
    return time;
}
Time Time::Sub(Time &T)
{
    int s=fabs(SecCalc()-T.SecCalc());
    Time time=Time(s);
    return time;
}
void Time::SetTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
    return ;
}
int main()
{
    Time t1(2,34),t2,t3(3723);
    t2.SetTime(13,23,34);
    cout<<"t2:";
    t2.print_12();
    cout<<endl<<"t2:";
    t2.print_24();
    cout<<"\nt1+t2:";
    t1.Add(t2).print_24();
    cout<<"\nt1-t2:";
    t1.Sub(t2).print_24();
    cout<<endl<<"t3:";
    t3.print_24();
    return 0;
}

G:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
class date
{
public :
    date(int X=0,int Y=0,int Z=0)
    {
        y=X;
        m=Y;
        d=Z;
    }
    int gety()
    {
        return y;
    }
    int getm()
    {
        return m;
    }
    int getd()
    {
        return d;
    }
private:
    int y;
    int m;
    int d;
};
class People
{
public :
    People(People &p)
    {
        number=p.number;
        sex=p.sex;
        birthday=p.birthday;
        strcpy(id,p.id);
        total++;
    }
     People();
    ~People()
    {
        total--;
        cout<<id<<" is deleted!"<<endl;
    }
    People(int nnum,int d1,int d2,int d3,char ID[],char SEX);
    //People(People &p);
    void Input();
    void Show();
    static void TotalNum()
    {
        cout<<"There have "<<total<<' '<<"people!"<<endl;
    }
    void SetID(char n[])
    {
        strcpy(id,n);
    }
private:
    int number;
    char sex;
    date birthday;
    char id[10];
    static int total;
};
int People::total=0;
People::People(int nnum,int d1,int d2,int d3,char ID[],char SEX):birthday(d1,d2,d3)
{
    number=nnum;
    sex=SEX;
    strcpy(id,ID);
    total++;
}
People::People():birthday()
{
    number=0;
    sex='f';
    strcpy(id,"0000000");
    total++;
}
void People::Input()
{
    int a,b,c;
    cin>>number;
    cin>>sex;
    cin>>a>>b>>c;
    birthday=date(a,b,c);
    cin>>id;
}
void People::Show()
{
    cout<<number<<' '<<sex<<' '<<birthday.gety()<<' '<<birthday.getm()<<' '<<birthday.getd()<<' '<<id<<endl;
}
int main()
{
People p1,p2;
    p1.Input();
    p2.Input();
    p1.Show();
    p2.Show();
    People p3(p2);
    p3.SetID("4201009");
    p3.Show();
    People *p4=new People(3,1999,2,2,"4201001",'m');
    p4->Show();
    People::TotalNum();
    delete p4;
    People::TotalNum();
    return 0;
}

H:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
class date
{
public :
    date(int X=0,int Y=0,int Z=0)
    {
        y=X;
        m=Y;
        d=Z;
    }
    int gety()
    {
        return y;
    }
    int getm()
    {
        return m;
    }
    int getd()
    {
        return d;
    }
private:
    int y;
    int m;
    int d;
};
class People
{
public :
    People(People &p)
    {
        number=p.number;
        sex=p.sex;
        birthday=p.birthday;
        strcpy(id,p.id);
        total++;
    }
     People();
    ~People()
    {
        total--;
        cout<<id<<" is deleted!"<<endl;
    }
    People(int nnum,int d1,int d2,int d3,char ID[],char SEX);
    //People(People &p);
    void Input();
    void Show();
    static void TotalNum()
    {
        cout<<"There have "<<total<<' '<<"people!"<<endl;
    }
    void SetID(char n[])
    {
        strcpy(id,n);
    }
private:
    int number;
    char sex;
    date birthday;
    char id[10];
    static int total;
};
int People::total=0;
People::People(int nnum,int d1,int d2,int d3,char ID[],char SEX):birthday(d1,d2,d3)
{
    number=nnum;
    sex=SEX;
    strcpy(id,ID);
    total++;
}
People::People():birthday()
{
    number=0;
    sex='f';
    strcpy(id,"0000000");
    total++;
}
void People::Input()
{
    int a,b,c;
    cin>>number;
    cin>>sex;
    cin>>a>>b>>c;
    birthday=date(a,b,c);
    cin>>id;
}
void People::Show()
{
    cout<<number<<' '<<sex<<' '<<birthday.gety()<<' '<<birthday.getm()<<' '<<birthday.getd()<<' '<<id<<endl;
}
int main()
{
People p1,p2;
    p1.Input();
    p2.Input();
    p1.Show();
    p2.Show();
    People p3(p2);
    p3.SetID("4201009");
    p3.Show();
    People *p4=new People(3,1999,2,2,"4201001",'m');
    p4->Show();
    People::TotalNum();
    delete p4;
    People::TotalNum();
    return 0;
}

I:

/*
    title:hdu1213
    description:并查集入门题
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
int father[100010];
//int mp[100010];
void  init(int n)
{
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
    }
    return;
}
int getfather(int i)
{
    while(father[i]!=i)
    {
        father[i]=getfather(father[i]);//路径压缩
    }
    return father[i];
}
void unio(int x,int y)
{
    int m=getfather(x);
    int n=getfather(y);
    if(m!=n)
    {
        father[m]=n;
    }
    return ;
}
bool issame(int x,int y)
{
    return getfather(x)==getfather(y);
}

int main()
{
    int T,n,m,a,b,ans;
    while(~scanf("%d",&T))
    {
        while(T--)
        {
            ans=0;
            scanf("%d%d",&n,&m);
            init(n);
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                unio(a,b);
            }
            for(int i=1;i<=n;i++)
            {
                if(father[i]==i)
                    ans++;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

J:

/*
    title:hdu1272
    description:并查集入门题,
    author: averyboy
    time:
    version:
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<cctype>
#include<ctime>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
int father[100010];
int mark[100010];
int llink=0,num;
bool circle;
void init()
{
    num=0;
    for(int i=0;i<100010;i++)
    {
        father[i]=i;
    }
    return ;
}
int getfather(int i)
{
    if(father[i]!=i)
    {
        father[i]=getfather(father[i]);
    }
    return father[i];
}
void unio(int x,int y)
{
    int xfather=getfather(x);
    int yfather=getfather(y);
    //cout<<xfather<<' '<<yfather<<endl;
    if(xfather!=yfather)
    {
       // cout<<"yes"<<endl;
        father[xfather]=yfather;
        llink++;
    }
    else
    {
        circle=true;
    }
    return ;
}
int main()
{
    int m,n;
    memset(mark,0,sizeof(mark));
    init();
    while(true)
    {
        scanf("%d%d",&m,&n);
        if(m==0&&n==0)
        {
            init();
            for(int i=1;i<=100010;i++)
            {
                if(mark[i]==1)
                    num++;
            }
           // cout<<num<<' '<<link<<' '<<circle<<endl;
            if(num==0)
            {
                cout<<"Yes"<<endl;
            }
            else if(llink==num-1&&circle==false)
            {
                cout<<"Yes"<<endl;
            }
            else
                cout<<"No"<<endl;
            llink=0;
            memset(mark,0,sizeof(mark));
                circle=false;
        }
        else if(m==-1&&n==-1)
            break;
        else
        {
            mark[m]=1;
            mark[n]=1;
            unio(m,n);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值