C++---练习

第一篇:

代码:

#include<iostream>
using namespace std;
class DataList
{
    int len;
    double *d;
public:
    DataList (int len,double data[]=NULL);
    DataList(){delete[]d;}
    int length()const{return len;}
    double getElement (int i)const{return d[i];}
    void sort()const;
};
void writeToFile(char*,const DataList&);
//main.cpp
#include"DataList.h"

DataList::DataList(int len,double data[]):len(len)
{
    d = new double[len];
    for(int i = 0;i<len;i++)
    d[i]=(data==NULL?0.0:data[i]);
}
void DataList::sort()
{
    for(int i=0;i < len;++i)
    for(int j =i;j< len; ++j)
    if(d[i]>d[j])
    {
        int temp =d[i];
        d[i] =d[j];
        d[j] = temp;
    }
}
void DataList::sort()const
{
    for(int i =0;i<len -1;i++)cout<<d[i]<<",";
    cout<<d[len-1]<<endl;
}
int main()
{
    double s[] = {7,1,3,11,6,9,12,10,8,4,5,2};
    DataList list (12,s);
    cout<<"排序前:";
    list.show();
    list.sort();
    cout<<endl<<"排序后:";
    list.show();
    writeToFile("",list);
    return 0;
}

命令:

第二篇:

代码: 

#include<iostream>
using namespace std;
class Door
{
    int num;
    bool closed;
    bool locked;
public:
    Door(int num)
    {
        this->num=num;
        closed = locked=true;
    }
    bool isClosed()const{return closed;}
    bool isOpened()const{return! closed;}
    bool isLocked()const{return locked;}
    bool isUnlocked()const{retuen !locked;}
    void open()
    {
        cout<<endl<<"打开"<<num<<"号门...";
        if(!=closed)
        cout<<"门是开着的,无须在开门。";
        else if(locked)
        count<<"门是锁着的,打不开。";
        else
        {
            closed = false;
            cout << "门打开了。";
        }
    }
    void close()
    {
        cout<< endl<<"关上"<<num<<"号门...";
        if(closed)
        cout<<"门是关着的,无须在关门。";
        else
        {
            closed = true;
            cout <<"门关上了。";
        }
    }
    void lock()
    cout<<endl<<"锁上"<<num<<"号门...";
    if(locked)
{
    cout<<"门是锁着的,无须再锁门。";
    else
    {
        if(!closed)
        {
            cout<<"先关门...";
            closed=true;
        }
        locked = true;
        cout<<"门锁上了。";
    }
}
void unlock()
{
    cout<<endl<<"开"<<num<<"号门的锁...";
    if(!locked)
    cout << "门没有上锁,无须再开锁。";
    else
    {
        locked = false;
        cout<<"锁开了。";
    }
}
};
int main()
{
    Door door(503);
    door.open();
    door.unlock();
    door.open();
    door.open();
    door.lock();
    return 0;
}

命令:

第三篇:

代码:

#include<iostream>
using namespace std;
class CharShape
{
public:
    CharShape(char ch):_ch(ch){};
    virtual void Show() = 0;
protected:
    char _ch;
};
class Triangle:public CharShape
{
public:
    Thiangle(char ch,int r):CharShape(ch),_rows(r){}
    void Show();
private:
    int_rows;
};
class Rectangle: public CharShape
{
public:
    Rectangle(char ch, int r,int c):CharShape(ch),_rows(r),_cols(c){}
    void Show();
private:
    int_rows,_cols;
void Triangle::Show()
    {
    for(int i = 1;j<=i*2-1;i++)
        count << _ch;
        cout << endl;
    }
}
void Rectangle::Show()
{
    for(int i =1;i<=_rows;i++)
    {
        for(int i =1;j<=_cols;J++)
        cout << _ch
        cout << endl;
    }
}
void fun(char Shape& cs){cs.Show();}
int main()
{
    Triangle tri('*',4);
    Rectangle rect('#',3,8);
    fun(tri);
    fun(rect);
    return 0;
}

命令:

 第四篇:

代码:

#include<iostream>
using namespace std;
class Book
{
    char * title;
    int num_pages;
    char *writer;
public:
    Book(const char * the_title,int pages, const char * the_writer):num pages(pages)
    {
    title = new char[strlen(the_title)+1];
    strcpy(title, the_title);
    writer = new char[strlen(the_writher)+1];
    strcpy(writer,the_writer);
    }
    Book()
    {delete[]title writher;}
    int numOfPages()const{return num_pages;}
    const char* theTitle()const{return title;}
    const char * theWriter()
    const{return writer;} 
};
class TeachingMaterial: public Book
{
    char * course;
public:
    TeachingMaterial(const char * the_title,int pages,const char *the_writer,const char*the_course):Book(the_title,pages.the_writer)
    course = new char[srtlen (the_course)+1];
    strcpy(course,the_course);
}
{
    TeachingMaterial(){delete[]course;}
    const char * theCourse() const{return course;}
};
int main()
{
    TeachingMaterial a book("C++语言程序设计",299,"张三","面向对象的程序设计");
    cout << "教材名:" << a_book.theTitle() <<endl;<<"页数:"<< a_book.numOf Pages() << endl << "相关课程:" <<a_book.theCOursc()<<endl;
    cout << endl;
    return 0 ;
}



命令:

 第五篇:

代码:

#include<iostream>
using namespace std;

class MyClass
{
public:
    MyClass(int i = 10)
    {
        value = i;cout << "Constructor called." <<endl;
    }
    MyClass(const MyClass&p)
    {
        value = p.value;
        cout << "Copy constructor called." << endl;
    }
    void Print()
    {
        cout << "The value is" << value << endl;
    }
    MyClass()
    {
        cout << "Destructor called." << endl;
    }
    private:
    int value;

};
int main()
{
    MyClass obj1;
    obj1.Print();
    MyClass obj2(obj1);
    obj2.Print();
    return 0;
}


命令:

第六篇:

代码:



#include<iostream>
using namespace std;
enum DOGCOLOR {BLACK, WHITE,YELLOW,BROWN,PIEBALD,OTHER};
class Dog
{
    DOGCOLOR color;
    char name[20];
    static int count;
public:
    Dog (char name[],DOGCOLOR color)
    {
        strcpy(this->name,name);
        this->color,color;
    }
    DOGCOLOR getColor() const{return color;}
    const char getName()const{return*name;}
    const char*getColorString()const
    {
        switch(color)
        {
            case BLACK: return"black";
            case WHITE: return"white";
            case YELLOW: return"brown";
            case PIEBALD: return"piebald";
        }
        return"motley";
    }
    void show()const
    {
        cout << "There is a " << getColorString() << "dog named"<< name <<'.'<<endl;
    }
};
int main()
{
    Dog dog1("Hoho",WHITE),dog2("Haha",BLACK),dog3("Hihi",OTHER);
    dog1.show();
    dog2.show();
    dog3.show();
    return 0;
}

命令:

 第七篇:

代码:

#include<iostream>
using namespace std;
//shape.h
class Shape
{
public:
    virtual double perimeter()const(return 0;)
    virtual double area()const{return 0;}
    virtual const char * name()const {return"抽象图形";}
};
class Point
{
    double x;
    double y;
public:
    Point(double x0,double y0):x(x0),y(y0){}
    double getX()const {return x;}
    double getY()const {return y;}
};
class Triangle:public Shape
{
    Point point,point2,point3;
public:
    Triangle(Point p1,Point p2,Point p3):point1(p1),point2(p2),point3(p3){}
    double perimeter()const;
    double area()const;
    const char * name()const{return "三角形";}
};
//shape.cpp
#include "shape.h"
#include<cmath>
double length (Point p1,Point p2)
{
    return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));
}
double Triangle::perimeter()const
{
    return length(point1,point2)+length(point1,point3)+length(point2,point3)
}
double Triangle::area()const
{
    double s= perimeter()/2.0;
    return sqrt(s*(s-length(point1,point2))*(s- length (point2,point3))*(s- length(point3,point1)));
}
//proj2.cpp
#include"shape.h"
#include<iostream>
using namespace std;
void show(shape&shape)
{
    count<<"此图形是一个" <<shape.name()<<",周长 ="<< shape.perimeter()<<",面积 = "<<shape.area() <<endl;
}

int main()
{
    Shape s;
    Triangle tri(Point(0,2),Point(2,0),Point(0,0));
    show(s);
    show(tri);
    return 0;
}



命令:

 

 第八篇:

代码:

#include<iostream>
using namespace std;
class Date
{
    int year,month,day;
public:
    Date(int year,int month,int day):year(year),month(month),day(day){}
    int getYear()const{return year;}
    int getMonth()const{return month;}
    int getDay()const{return day;}
};
class Person
{
    char name[14];
    bool is_male;
    Date birth_date;
public:
    Person(char*name,bool is_male,Date birth_date):is_male(is_male),birth_date(birth_date)
    {
        strcpy(this->name,name);
    }
    const char*getName()const{return name;}
    bool isMale()const{return is_male;}
    Date getBirthdate()const{return birth_date;}
    int compareName(const Person&p)const
    {
        return strcmp(name,p.getName());
        void show()
        {
            cout<<endl;
            cout<<name<<"<<(is_male?"男":"女")<<"<<"出生日期:"<<birth_date.getYear()<<"年"
            <<birth_date.getmonth()<<"月"<<birth_date.getDay()<<"日";
        }
    };
    void sortByName(Person ps[],int size)
    {
        for(int i=0;i<size-1;i++)
        {
            int m=i;
            for(int j=i+1;j9<size;j++)
            int m=i;
            for(int j = i+1;j<size;j++)
            if(ps[j].compareName(ps[m])<0)
            m = j;
            if(m>i)
            {
                Person p = ps[m];
                ps[m] =ps[i];
                ps[i] = p;
            }
        }
    }
    int main()
    {
        Person staff[]=
        {
            Person("张三",true,Date(1978,4,20)),
            Person("王五",false,Date(1965,8,3)),
            Person("杨六",false,Date(1965,9,5)),
            Person("李四",true,Date(1973,5,30))
        };
        const int size = sizeof(staff)/sizeof(staff[0]);
        int i;
        cout<<endl<<"按姓名排序";
        cout<<endl<<"排序前:";
        for(i = 0; i<size;i++)
        staff[i].show();
        sortByName(staff,size);
        cout<<endl<<endl<<"排序后:";
        for(i=0;i<size;i++)
        staff[i].show();
        cout<<endl;
        return 0;
    }
}

命令:

第九篇

代码:

#ifndef INTEGERSET
#define INTEGERSET
#include<iostream>
using namespace std;
const int MAXELEMENTS = 100;
class IntegerSet
{
    int elem[MAXELEMENTS];
    int counter;
public:
    IntegerSet():counter(0){}
    IntegerSet(int data[],int size);
    void remove(int element);
    void add(int element);
    void remove(int element);
    int getCount()const{return counter;}
    int getElement (int i)const{return elem[i];}
    void show()const;
};
void WriteToFile(char*);
#endif
//main.cpp
#include"IntegerSet.h"
#include<iomanip>
IntegerSet::IntegerSet(int data[],intsize):counter(0)
{
    for(int i=0;i<size;i++)
    add(data[i]);
    void IntegerSet::add(int element)
    {
        int j;
        for(j = counter;j>0;j--)
        if(element>=elem[j-1])
        break;
        if(j>0)
        if(element==elem[j-1])
        return;
        for(int k=counter;k>j;k--)
        elem[k] = elem[k-1];
        elem[j] = element;
        counter++;
    }
    void IntegerSet::remove(int element)
    {
        for(int i =0;i<counter;i++)
        if(element==elem[i])
        {
            for(int j = i;j<counter-1;j++)
            elem[j] = elem[j+1];
            counter--;
            return;
        }
    }
    void IntegerSet::show()const
    {
        for(int i=0;i<getCount();i++)
        count<<setw(4)<<getElement(i);
        cout<<endl;
    }
    int main()
    {
        int d[] = {5,28,2,4,5,3,2,75,27,66,31};
        IntegerSet s(d,11);
        s.show();
        s.add(6);
        s.show();
        s.add(19);         s.show();
        s.remove(2);       s.show();
        s.add(4);          s.show();
        WriteToFile("");
        return 0;
    }
}

命令:

第十篇:

代码:

#include<iostream>
using namespace std;
class TVSet
{
    const int size;
    int channel;
    int volume;
    bool on;
public:
    TVSet(int size):size(size)
    {
        this->size(size);
        channel = 0;
        volume = 15;
        on=false;
    }
    int getSize()const{return size;}
    bool isOn()const{return on;}
    int getVolume()const{return isOn()?volume:-1;}
    int getChannel()const{return isOn()?channel:-1;}
    void turnOnOff()
    {on=!on;}
    void setChannelTo(int chan)
    {
        if(isOn()&&chan>=0&&chan<=99)
        channel = chan;
    }
    void setVolumeTo(int vol)
    {
        if(isOn()&&vol>=0&&vol<=30)
        volume = vol;
    }
    void show_state()
    {
        cout<<"规格:"<<getSize()<<"英寸"<<",电源:"<<(isOn()?"开":"关")<<",频道:"<<getChannel()
        <<",音量:"<<getVolume()<<endl;
    }
};
int main()
{
    TVSet tv(29);
    tv.turnOnOff();
    tv.setChannelTo(5);
    tv.setVolumeTo(18);
    tv.show_state();
    tv.turnOnOff();
    tv.show_state();
    return 0;
}

命令:

第十一篇

代码:

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Root
{
public:
    const double x1;
    const double x2;
    const int num_of_roots;
    Root():x1(0.0),x2(0.0),num_of_roots(0){}
    Root (double root):x1(root),x2(root),num_of_roots(1){}
    Root(double root1,double root2):x1(root1),x2(root2),num_of_roots(2){}
    void show()const
    {
        cout<<"\t\t";
        switch(num_of_roots)
        {
            case 0;
            cout<<"无实根";break;
            case 1:
            cout<<"有两个相同的实根:"<<x1<<"和"<<x2;break;
            default:
            cout<<"有两个不同的实根:"<<x1<<"和"<<x2;break;
        }

    }
};
class Quadratic
{
public:
    const double a,b,c;
    Quadratic (double a, double b,double c):a(a),b(b),c(c)
}
Quadratic(Quadratic& x):a(x.a),b(x.b),c(x.c){}
Quadratic add (Quadratic x)count
{
    return Quadratic(a+x.a,b+x.b,c+x.c);
}
Quadratic sub(Quadratic x)cout
{
    return Quadratic(a-x.a,b-x.b,c-x.c);
}
double value(double x)const
{
    return a*x*x+b*x+c;
}
Root root()const
{
    double delta = b*b-4*a*c;
    if(delta<0.0)return Root();
    if(delta==0.0)
    return Root(-b/(2*a));
    double sq = sqrt(delta);
    return Root((-b+sq)/(2*a),(-b-sq)/(2*a));
}
void show()const
{
    cout<< endl<<a<<"X^2"<<showpos<<b<<"xX"<<c<<noshowpos;
}
void showFunnction()
{
    show();
    cout<<"=0.0";
}
int main()
{
    Quadratic q1(3.0,4.0,5.0),q2(4.5,6.0,2.0),q3(q2.sub(q1));
    q1.showFunction();
    q1.root().show();
    q2.showFunction();
    q2.root().show();
    q3.showFunction();
    q3.root().show();
    cout <<endl;
    return 0;
}

命令:

 第十二篇

代码:

#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
    Point(double x=0.0,double y=0.0):x_(x),y_(y){}
    double getX()const{return x_;}
    double getY()const{return y_;}
    void setX(double x){x_=x;}
    void setY(double y){y_=y;}
private:
    double x_;
    double y_;
};
class Rectangle
{
public:
    Rectangle(Point p,int w,int h):point(p),width(w),height(h){}
    double area()const
    {
        return width*height;
    }
    Point topLeft()const
    {
        return point;
    }
    Point bottomRight()const
    {
        return Point(point.getX()+width.point.getY()+height);
    }
private:
    Point point;
    double width;
    double height;
};
class Circle
{
public:
    Circle(Point p, double r):center(p),radius(r){}
    Rectangle boundingBox()const;
    double area()const
    {
        return PI*radius*radius
    public:
        static const double PI;
    private:
        Point center;
        double radius;
    };
    const double Circle::PI = 3.14159;
    Rectangle Circle::boundingBox()const
    {
        Point pt(center.getX()-radius,center.getY()-radius);
        int w,h;
        w = h = 2* radius;
        return Rectangle(pt,w,h);
    }
    int main()
    {
        Point p(3,2);
        Circle c(p,1);
        cout << "--圆形-------------\n";
        cout << "圆心 = ("<<p.getX()<<','<<p.getY()<<")\n";
        cout << "半径 ="<< 1 <<endl;
        cout << "面积 ="<<c.area()<<endl <<endl;
        Rectangle bb = c.boundingBox();
        Point t1 = bb.topLeft();
        Point br = bb.bottomRight();
        cout <<"--外切矩形--------------\n";
        cout <<"左上角 = ("<<t1.getX()<<','<<t1.getY()<<")\n";
        cout <<"右下角 = (" << br.getX() << ',' <<br.getY() << ")\n";
        cout <<"面积 ="<<bb.area()<<endl;
        return 0;
    }
}

命令:

第十三篇

代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值