CPP


有序插入函数

#include <iostream>
#include <fstream>
using namespace std;


class IntArray
{
    int a[11];
    int n;

public:
    IntArray(char fname[])
    {
        ifstream fin(fname);
        fin >> n;
    
        for (int i = 0; i < n; i++)
            fin >> a[i];
    
    }
    IntArray(IntArray& A) {
        n = A.n;
        for (int i = 0; i < n; i++)
            a[i] = A.a[i];

    }

    void Output()
    {
        for (int i = 0; i < n; i++)
            cout << a[i] << " ";
        cout << endl;
    }
    int Search( int x)
    {
        for (int i = 0; i < n; i++)
            if (a[i] == x)
                return i;
        return -1;
    }
    // a[]: a[0] a[1]  ...a[k]   a[k+1] .....a[n-1]
    void RemoveAt( int k)
    {
        for (int i = k + 1; i < n; i++)
            a[i - 1] = a[i];
        n--;
    }
    void Insert(int x)
    {
        if (n >=10) return;
        for (int i = n; i >= 0; i--)
            a[i] = a[i - 1];
        a[0] = x;
        n++;
    }
    void InsertAt(int k,int x)
    {
        if (n >= 10) return;
    
        for (int i =n; i >= k; i--)
            a[i] = a[i -1];
        a[k] = x;
        n++;
    }
    void InsertSort(int x)
    {
        int y = 0;
        // 1 从后向前,循环比较,大数后移
        for (int i = n - 1; i >= 0 && a[i] > x; i--) {
            a[i + 1] = a[i];
            y = i;
        }

        // 2 x 到位
        a[y + 1] = x;
        n++;
    }

};

int main()
{
    // 观察数组的地址
    // 观察数组中的单元
    IntArray obj("data.txt");
    

    obj.Output();
     int k = obj.Search(2);
    cout << "查找的位置在" << k << endl;

    IntArray obj1(obj);


    obj.InsertSort(4); obj.Output();

    return 0;
}

定点插入函数

#include <iostream>
#include <fstream>
using namespace std;


class IntArray
{
    int a[11];
    int n;

public:
    IntArray(char fname[])
    {
        ifstream fin(fname);
        fin >> n;
    
        for (int i = 0; i < n; i++)
            fin >> a[i];
    
    }
    IntArray(IntArray& A) {
        n = A.n;
        for (int i = 0; i < n; i++)
            a[i] = A.a[i];

    }

    void Output()
    {
        for (int i = 0; i < n; i++)
            cout << a[i] << " ";
        cout << endl;
    }
    int Search( int x)
    {
        for (int i = 0; i < n; i++)
            if (a[i] == x)
                return i;
        return -1;
    }
    // a[]: a[0] a[1]  ...a[k]   a[k+1] .....a[n-1]
    void RemoveAt( int k)
    {
        for (int i = k + 1; i < n; i++)
            a[i - 1] = a[i];
        n--;
    }
    void Insert(int x)
    {
        if (n >=10) return;
        for (int i = n; i >= 0; i--)
            a[i] = a[i - 1];
        a[0] = x;
        n++;
    }
    void InsertAt(int k,int x)
    {
        if (n >= 10) return;
    
        for (int i =n; i >= k; i--)
            a[i] = a[i -1];
        a[k] = x;
        n++;
    }

};

int main()
{
    // 观察数组的地址
    // 观察数组中的单元
    IntArray obj("data.txt");
    

    obj.Output();
     int k = obj.Search(2);
    cout << "查找的位置在" << k << endl;

    IntArray obj1(obj);


    obj.InsertAt(3,15); obj.Output();

    return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
/*2.编写一个基类 Shape,有计算面积显示面积的area(兩数,利用派生类正方形Square、
十酒
圆形Circle以及圆柱体Cylinder类分别计算面积,均含有area(0兩数,要求有带参数的
小茧,
构造函数,编写主函数并测试(使用虑函数area0和基类Shape 指针实现多态,计算并
显示对应图形的面积)。e
 */
//Shape.h 基类
class Shape {
public:
    virtual double area();
};
//Shape.app
double Shape::area()
{
    return 0.0;
}
//Square.h
class Square:public Shape{
public:
    Square(int sideLength);
    virtual double area();
private:
    int sideLength;
};
//Square.cpp
Square::Square(int sideLength):Shape()
{
    this->sideLength = sideLength;
}
double Square::area()
{
    double area=pow(double(sideLength),2);
    cout<<"Square area ="<<area<<endl;
    return area;
}
//Circle.h
class Circle:public Shape{
public :
    Circle(int r);
    virtual double area();

private:
    int r;
    float Pi =3.14;
};
//Circle.cpp
Circle::Circle(int r): Shape(){
    this->r=r;
};
double Circle::area() {
    double area=Pi* pow(r,2);
    cout<<"Circle area ="<<area<<endl;
    return area;
}

//Cylinder.h
class Cylinder:public Shape{
public:
    Cylinder(int r,int height);
    virtual double area();

private:
    int r;
    int height;
    float Pi=3.14;

};
//Cylinder.cpp
Cylinder::Cylinder(int r, int height) {
    this->r=r;
    this->height=height;
}
double Cylinder::area() {
    double area =2*Pi*r*height+2*Pi*(pow(r,2));
    cout<<"Cylinder area ="<<area<<endl;
    return area;
}

int main( )
{
    Shape *shape;
    Square square(10);
    Circle circle(10);
    Cylinder cylinder(10,10);
//正方形
    shape=&square;
    shape->area();
//圆形
    shape=&circle;
    shape->area();
//圆柱体
    shape=&cylinder;
    shape->area();
    return 0;
}

artist

#include <string>
#include <iostream>
using namespace std;
/*
四、编程题(第1题20分,第二题12分,共32分) 4
1. 创建一个顶层基类Artist, 其中有name和age两个属性以及show(兩数用来显示
各项属性值;按着创建两个派生类Actor 和Singer,并增加额外的fansAmount和income
属性,并且也具有show0函数:最后使用Actor和Singer类派生出一个底层派生类Star,
并额外增加一个属性forbesRank, 并且包含show0函数;请使用虚基类来避免多级及
Anc
多重继承后出现的二义性,在main函数中分别创建Artist类对象、Aclor类对象、Singer
类对象以及一个Star类对象,并分别使川show0函数显示其各项数据。4
*/
//Artist.h
class Artist {
public :
    Artist( string  name,int age);
    void show();
    string name;
    int age;
};
//Artist.cpp
Artist::Artist( string  name,int age){
    this->name = name;
    this->age = age;
}
void Artist::show() {
    cout<<"Artist name="<<name<<"age="<<age<<endl;
}
//Actor.h
class Actor:virtual public Artist{
public:
    Actor(string  name, int age,long fansAmount, double income);
    void show();
    long fansAmount;
    double income;
};
//Actor.cpp
Actor::Actor(string  name, int age, long fansAmount, double income) : Artist(name,age) {
    this->fansAmount=fansAmount;
    this->income=income;
}
void Actor::show() {
    cout<<"Actor name="<<name<<"age="<<age<<"fansAmount="<<fansAmount<<"income="<<income<<endl;
}

//Singer.h
class Singer:virtual public Artist{
public:
    Singer( string  name,  int age, long fansAmount, double income);
    void show();

    long fansAmount;
    double income;
};
//Singer.cpp
Singer::Singer(string  name,  int age, long fansAmount, double income) : Artist(name,age) {
    this->fansAmount=fansAmount;
    this->income=income;
}
void Singer::show() {
    cout<<"Actor name="<<name<<"age="<<age<<"fansAmount="<<fansAmount<<"income="<<income<<endl;
}
//Star.h
class Star:virtual public Actor,virtual public Singer{
public:
    Star(string  name,int age, long fansAmount, double income,int rank);
    void show();
private:
    int ForbesRank;
};
//Star.cpp
Star::Star(string  name,int age, long fansAmount, double income,int rank)
        :Artist(name,age),
         Actor(name,age,fansAmount,income),
         Singer(name,age,fansAmount,income){
    ForbesRank = rank;
}
void Star::show(){
    cout<<"Star name="<<name<<"age="<<age<<"fansAmount="<<Actor::fansAmount<<"income="<<Singer::income<<"rank="<<ForbesRank<<endl;
}
int main() {
    Artist artist("测试",132);
    artist.show();
    Actor actor("刘德华",57,600,0.7f);
    actor.show();
    Singer singer("刘德华",57,100,0.58f);
    singer.show();
    Star star("刘德华", 57, 1000, 1.28f, 27);
    star.show();
    return 0;
}

声明:sma's blog|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - CPP


做一个温柔而又有力量的人