21天学通C++之对象作业实战


因为作者平时不使用C++进行编程,但是课程刷分无奈又学习一遍,这样其实只要跟随老师的脚步一步一步进行学习和实验就可以了(ps:第一次没好好学,只拿了73分)。所以初期关于C++的学习大多OJ编程作业上进行,所以内容比较关键但却又比较浅显,有需要的读者凑合着看即可。


第三次C++作业代码和解析


第一题作业:

//
//  main.cpp
//  CppCP3_1
//
//  Created by Chenjun Xiong on 2017/3/16.
//  Copyright © 2017年 Chenjun Xiong. All rights reserved.
//
//题目描述
//(Circle类)一个圆形的旱冰场地,场地内抹水泥,造价为每平方米20元,围栏用木条围成,每米造价35元。设计一个Circle类,可以求出圆的面积和边长,进而支持求出旱冰场的等价。请在下面提示的基础上完成程序,其中需要做的工作包括:(1)在类声明中声明相关的公共成员函数;(2)在类声明和main()函数之间,定义声明的成员函数。PI采用3.1415926
//
//输入描述
//请输入旱冰场的半径
//
//输出描述
//旱冰场的造价,小数点后保留2位
//
//输入样例
//5.5
//
//输出样例
//3110.18

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

double PI=3.1415926;

class Circle{
private:
    double radio;

public:
    Circle(double r=0);
    void SetRadio(double r=0);
    double GetPerimeter();
    double GetArea();
    double GetPrice();
};

Circle::Circle(double r){
    radio=r;
}

void Circle::SetRadio(double r){
    radio=r;
};

double Circle::GetArea(){
    return PI*radio*radio;
}

double Circle::GetPerimeter(){
    return PI*radio*2;
}

double Circle::GetPrice(){
    return 20*GetArea()+35*GetPerimeter();
}

int main(int argc, const char * argv[]) {
    double radio;
    Circle circle;
    while(cin>>radio){
        circle.SetRadio(radio);
        cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << circle.GetPrice() << endl;
    }
    return 0;
}

解析:

  • 本次作业主要使用了类,类成员函数的类外声明,构造函数的重写,小数输出保留小数点后两位等技术。
  • 小数点后两位主要使用了头文件和cout输出时的函数
#include <iomanip>
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << circle.GetPrice() << endl;

第二题作业:

//
//  main.cpp
//  CppCP3_2
//
//  Created by Chenjun Xiong on 2017/3/16.
//  Copyright © 2017年 Chenjun Xiong. All rights reserved.
//
//输入描述
//分别构造两个空栈,再读入若干对整数v、x; 1<=v<=2; 将元素x入第v个栈 。
//
//输出描述
//最后将两个栈中元素出栈,并输出;每个栈中元素占一行,元素间以空格分隔。
//
//输入样例
//1 100
//2 200
//1 300
//2 400
//1 50
//1 60
//2 80
//
//输出样例
//60 50 300 100
//80 400 200

#include <iostream>

#include <iostream>
using namespace std;

typedef struct linknode{
    int num;
    linknode *next;
}linknode;

class stack{
private:
    struct linknode *top;
    int size;

public:
    stack(){
        top=new struct linknode;
        top->num=-1;
        top->next=NULL;
        size=0;
    };

    //stack(const stack& s);

    void Push(int data){
        linknode *newnode=new struct linknode;
        newnode->num=data;
        newnode->next=top;
        size++;
        top=newnode;
    };

    void Pop(){
        if(size==0){
            return;
        }
        linknode *prenode=top->next;
        delete top;
        top=prenode;
        size--;
    };

    int GetTop(){
        return top->num;
    };

    bool IsEmpty(){
        if(top->next==NULL){
            return 1;
        }
        return 0;
    }
};

int main(int argc, const char * argv[]) {
    int v,x;
    stack s1,s2;
    //int n;
    //cin>>n;
    //for(int i=0;i<n;i++){
    while(cin>>v>>x){

        if(v==1){
            s1.Push(x);
        }else{
            s2.Push(x);
        }
    }
    while(!s1.IsEmpty())
    {
        cout<<s1.GetTop()<<" ";
        s1.Pop();
    }
    cout<<endl;
    while(!s2.IsEmpty())
    {
        cout<<s2.GetTop()<<" ";
        s2.Pop();
    }
    cout<<endl;
    return 0;
}

解析:

  • 很简单的自己实现的整数栈,因为是链栈,所以也用到了c++里的动态空间建立和释放
linknode *newnode=new struct linknode;
delete top;

第三题作业:

//
//  main.cpp
//  CppCP3_3
//
//  Created by Chenjun Xiong on 2017/3/16.
//  Copyright © 2017年 Chenjun Xiong. All rights reserved.
//
//题目描述
//请设计实现集合类,元素类型为整形。该集合类支持集合元素增加、删除、查询;并支持集合并、交、差运算;利用你设计的集合类,实现本题要求。程序应体现面向对象程序设计思想,结构合理。为保证结果唯一,集合元素递增排列。假设集合元素个数不超100个
//
//输入描述
//开始为两个正整数m,n;后续m个整数构成集合A,再后续n个整数构成集合B
//
//输出描述
//集合A、B和他们的并、交、差集;每个集合元素间以,分隔;不同集合显示在不同行
//
//输入样例
//3 5
//1 2 3
//3 5 8 2 1
//
//输出样例
//{1,2,3}
//{1,2,3,5,8}
//{1,2,3,5,8}
//{1,2,3}
//{}

#include <iostream>
using namespace std;

class Vector{
private:
    int *num;
    int length;

public:
    Vector();
    void Insert(int ele);
    void Delete(int ele);
    int GetElement(int pos);
    bool ExistElement(int ele);
    void Print();
    Vector operator+(Vector &b);
    Vector operator-(Vector &b);
    Vector operator*(Vector &b);
};

Vector::Vector(){
    num=new int[100];
    length=0;
}

void Vector::Insert(int ele){
    if(length>=100)
        return;
    else if(length==0){
        num[0]=ele;
        length++;
    }else if(ele>=num[length-1]){
        num[length]=ele;
        length++;
    }else{
        for(int i=0;i<length;i++){
            if(num[i]>=ele){
                for(int j=length;j>i;j--){
                    num[j]=num[j-1];
                }
                num[i]=ele;
                length++;
                return;
            }
        }
    }
}

bool Vector::ExistElement(int ele){
    for(int i=1;i<=length;i++){
        if(ele==GetElement(i))
            return true;
    }
    return false;
}

void Vector::Print(){
    cout<<'{';
    for(int i=1;i<=length;i++){
        if(i==1){
            cout<<GetElement(i);
        }else{
            cout<<','<<GetElement(i);
        }
    }
    cout<<'}'<<endl;
}

void Vector::Delete(int ele){
    if(length==0){
        return;
    }else{
        for(int i=0;i<length;i++){
            if(num[i]==ele){
                for(int j=i;j<=length-2;j++){
                    num[j]=num[j+1];
                }
                length--;
            }
        }
    }
}

int Vector::GetElement(int pos){
    if(pos>length||pos<1)
        return NULL;
    else
        return num[pos-1];
}

Vector Vector::operator+(Vector &b){
    Vector c;
    int n=1,m=1;
    while(n<=length&&m<=b.length){
        if(GetElement(n)<b.GetElement(m)){
            c.Insert(GetElement(n));
            n++;
        }else if(GetElement(n)>b.GetElement(m)){
            c.Insert(b.GetElement(m));
            m++;
        }else{
            c.Insert(GetElement(n));
            n++;m++;
        }
    }
    while(n<=length){
        c.Insert(GetElement(n));
        n++;
    }
    while(m<=b.length){
        c.Insert(b.GetElement(m));
        m++;
    }
    return c;
}

Vector Vector::operator-(Vector &b){
    Vector c;
    for(int i=1;i<=length;i++){
        if(!b.ExistElement(GetElement(i)))
            c.Insert(GetElement(i));
    }
    return c;
}

Vector Vector::operator*(Vector &b){
    Vector c;
    for(int i=1;i<=length;i++){
        if(b.ExistElement(GetElement(i)))
            c.Insert(GetElement(i));
    }
    return c;
}

int main(int argc, const char * argv[]) {
    int n,m,k;
    Vector c,a,b;
    while(cin>>n>>m){
        while(n--){
            cin>>k;
            a.Insert(k);
        }
        while(m--){
            cin>>k;
            b.Insert(k);
        }
        a.Print();
        b.Print();
        c=a+b;
        c.Print();
        c=a*b;
        c.Print();
        c=a-b;
        c.Print();
    }
}

解析:

  • 这题作业就有趣一些了,因为需要自己实现int类型的vector类,所以需要用到运算符重写,这也是一个很好用的功能,实现自定义的对象之间对运算。
  • 首先是类内对运算符重载函数的声明和类外定义,最后在运用时直接像普通的数学运算一样使用即可
//类内声明
class Vector{
private:
    int *num;
    int length;

public:
    Vector();
    void Insert(int ele);
    void Delete(int ele);
    int GetElement(int pos);
    bool ExistElement(int ele);
    void Print();
    Vector operator+(Vector &b);
    Vector operator-(Vector &b);
    Vector operator*(Vector &b);

//定义重载函数
Vector Vector::operator+(Vector &b){
    Vector c;
    int n=1,m=1;
    while(n<=length&&m<=b.length){
        if(GetElement(n)<b.GetElement(m)){
            c.Insert(GetElement(n));
            n++;
        }else if(GetElement(n)>b.GetElement(m)){
            c.Insert(b.GetElement(m));
            m++;
        }else{
            c.Insert(GetElement(n));
            n++;m++;
        }
    }
    while(n<=length){
        c.Insert(GetElement(n));
        n++;
    }
    while(m<=b.length){
        c.Insert(b.GetElement(m));
        m++;
    }
    return c;
}

Vector Vector::operator-(Vector &b){
    Vector c;
    for(int i=1;i<=length;i++){
        if(!b.ExistElement(GetElement(i)))
            c.Insert(GetElement(i));
    }
    return c;
}

Vector Vector::operator*(Vector &b){
    Vector c;
    for(int i=1;i<=length;i++){
        if(b.ExistElement(GetElement(i)))
            c.Insert(GetElement(i));
    }
    return c;
}
};
//运算符重载后的使用
        b.Print();
        c=a+b;
        c.Print();
        c=a*b;
        c.Print();
        c=a-b;
        c.Print();
以上就是C++第三次作业的三道题的内容,很简单是不是,用这些功能就可以基本实现一个小项目了,比如图书管理系统这样的c语言期末大作业了。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值