第十五周上机

第一题

这一题主要是想强调一下纯虚函数的用法,纯虚函数的作用就是确保子类一定会实现某个方法,从而防止派生类缺少了部分性质,另外就是让大家使用一下常量成员函数,这种函数的特征就是不会修改成员的状态,这两个都涉及到关键字const的使用。用法老师也说过了,就不再赘述,剩下的就简单看看类图就好.另外注意下pi的用法就行
一般在C++里面我们通常使用这样的语法结构去描述pi的值

const double pi = acos(-1.0);

在这里插入图片描述
//因为show_position这块比较固定,其实可以直接写在shape
.cpp里面
shape.cpp

#include "Shape.h"
#include <iostream>
using namespace std;
/* write your code here */

void Shape::show_position() const
{
   cout << "(" << x << "," << y << ")";
}

/* write your code here */

circle.cpp

#include "Circle.h"
#include <cmath>

const double pi = acos(-1.0);//以后碰到pi的计算就用这个表达式就可以

double Circle::area() const
{
    return pi * r * r;
}

double Circle::circum() const
{
    return 2 * pi * r;
}

square.cpp

#ifndef SQUARE_H
#define SQUARE_H

#include "Rectangle.h"

class Square : public Rectangle
{
/* write your code here */
    public:
    Square(double x,double y,double w): Rectangle(x,y,w,w){}
    double area() const { return width * height; }
    double circum() const { return 2 * (width + height); }



/* write your code here */    
};


#endif

第二题

这一题主要涉及到一些比较细节一点的东西,比如数据的存储方式等,在之前的几次作业里面有提高过需要同学们实现BigInt类,这次其实就是上次那一个题目的升级版,在实现了这个longlongInt类之后,这次我们采用顺序的方法实现两个大整数的计算。剩下的就是简单的复数计算了,类图大概是下面这样

在这里插入图片描述

这个代码来自于班上的一位同学,可以参考一下,写得还是很不错的。
3.cpp

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#include "LLIComplex.h"
#include "LongLongInt.h"
ostream & operator <<(ostream &os,longlongintcomplex& n)
{
    
    if(n.a.value!="0"){
        cout<<n.a.value;
    }
    if(n.a.value!="0"&&n.b.value!="0")
    {
        cout<<'+';
    }
    if(n.b.value!="0"){
        cout<<n.b.value<<'i';
    }
    if(n.a.value=="0"&&n.b.value=="0")
    {
        cout<<'0';
    }
    return os;
}
int main()
{
    string r1_re, r1_im, r2_re, r2_im;
    
    cin>>r1_re>>r1_im>>r2_re>>r2_im;
    longlongintcomplex r1(r1_re,r1_im),r2(r2_re,r2_im),r3;
    cout<<"r1="<<r1<<endl;
    cout<<"r2="<<r2<<endl;
    r3=r1+r2;
    cout<<"r3=r1+r2="<<r3<<endl;
    
    return 0;
}

LLIcomplex.cpp

 #include "LLIComplex.h"
#include <iostream>
using namespace std;
longlongintcomplex::longlongintcomplex(string ma,string mb)
{
    int la=ma.length();
    int lb=mb.length();
    a.value=ma;
    a.l=la;
    b.value=mb;
    b.l=lb;
}
longlongintcomplex longlongintcomplex::operator +(longlongintcomplex&n2)
{
    string ma=a.add(n2.a).value;
    string mb=b.add(n2.b).value;
    longlongintcomplex n3(ma,mb);
    return n3;

}

LLIcomplex.h

 #ifndef LLICOMPLEX_H
#define LLICOMPLEX_H
#include "LongLongInt.h"
class longlongintcomplex
{
public:
    longlongint a;
    longlongint b;
    longlongintcomplex(string ma="0",string mb="0");
    longlongintcomplex operator +(longlongintcomplex&n2);
};
#endif

LongLongInt.cpp

 #include <bits/stdc++.h>
using namespace std;
#include "LongLongInt.h"
longlongint::longlongint(string s,int length)
{
    value=s;
    l=length;
}
longlongint longlongint::add(longlongint& n)
{
    longlongint n1=*this,n2=n;
    if(l<n.l)
    {
        n1=n;
        n2=*this;
    }
    for(int i=0;i<n1.l-n2.l+1;i++)
    {
        n2.value='0'+n2.value;
    }
    n1.value='0'+n1.value;
    for(int i=n1.l;i>0;i--)
    {
        n1.value[i]+=n2.value[i]-'0';
        if(n1.value[i]>'9')
        {
            n1.value[i]-=10;
            n1.value[i-1]++;
        }
    }
    string s="";
    if(n1.value[0]=='0')
    {
        for(int i=1;i<=n1.l;i++)
            s+=n1.value[i];
        n1.value=s;
    }
    return n1;
        

};

LongLongInt.h

 #ifndef LongLongInt_H
#define LongLongInt_H
#include <bits/stdc++.h>
using namespace std;
class longlongint
{
public:    
    string value;
    int l;
    longlongint(string s="0",int length=0);
    longlongint add(longlongint& n);
};
#endif
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值