MOOC高级语言程序设计第八章课后作业

MOOC高级语言程序设计第八章课后作业

日期类

题目描述

设计一个日期类(Date),用来实现日期的操作。包括一个空构造函数,三个成员函数,其余所需自行决定。
用成员函数setDate()用来给Date类设置日期。
用成员函数isLeapYear()用来判断是否是闰年。
用成员函数getSkip(Date o)用来计算两个日期之间相差的天数。

输入

输入两个日期,输入格式参考样例。
输入样例:
2012 10 20
2017 11 07

输出

判断两个日期是否是闰年,以及两个日期的间隔天数,输出格式参考样例。
输出样例:
2012 is leap year.
2017 is not leap year.
The skip of two date is 1844.

代码

#include<iostream>
#include<cmath>
#include<string.h>
using namespace std;
class Date{
    private:
    int year,month,day;
    public:
    void setDate(int y,int m,int d){
        year=y;
        month=m;
        day=d;
    };
    void isLeapYear(){
        if((year%4==0&&year%100!=0)||year%400==0){
            cout<<year<<" is leap year."<<endl;
        }else{
            cout<<year<<" is not leap year."<<endl;
        }
    }
    int getSkip(){
        int m[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if((year%4==0&&year%100!=0)||year%400==0)
		m[2]++;
	int result = 0;
	for(int i = 1;i < year;i++)
	{
		result += 365;
		if((i%4==0&&i%100!=0)||i%400==0)
			result ++;
	}
	for(int i = 1;i < month;i++)
	{
		result += m[i];
	}
	result += day;
	
	return result;
    }
};
int main(){
    int x1,x2,x3,x4,x5,x6;
    cin>>x1>>x2>>x3>>x4>>x5>>x6;
    Date d1,d2;
    d1.setDate(x1,x2,x3);
    d2.setDate(x4,x5,x6);
    d1.isLeapYear();
    d2.isLeapYear();
    cout<<"The skip of two date is "<<abs(d1.getSkip()-d2.getSkip())<<'.';
    system("pause");
}

三角形类

题目描述

先定义一个能描述平面上一条线段的类Beeline,包含私有数据成员为线段两个端点的坐标(X1,Y1,X2,Y2),在类中定义形参默认值为0的构造函数,计算线段长度的公有成员函数Length(),显示线段两个端点坐标的公有成员函数show()。然后再定义一个能描述平面上三角形的类Triangle,其数据成员为用Beeline定义的对象line1,line2,line3。在类中定义的构造函数要能对对象成员进行初始化。再定义计算三角形面积的函数Area()及显示三条边端点坐标及面积的函数Print(),Print()函数中可调用show()函数显示三条边两端点坐标。

输入

输入三角形三个顶点的坐标(x1,y1)、(x2,y2)、(x3,y3)。
其中 -100 <= x1,x2,x3,y1,y2,y3 <= 100,且为整数。
在主函数中创建类对象tri(x1,y1,x2,y2,x3,y3),对应line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1)。
输入样例:
0 0
0 4
3 0

输出

调用Print()函数,将三角形三条边的端点坐标及面积。面积保留两位小数。
输出样例:
Three edges’ points are listed as follows:
(0, 0),(0, 4)
(0, 4),(3, 0)
(3, 0),(0, 0)
The area of this triangle is: 6.00.

代码

#include<iostream>
#include<cmath>
#include<iomanip>
#include<string.h>
using namespace std;
class Beeline{
    private:
    int x1,x2,y1,y2;
    public:
    Beeline(int a1,int a2,int a3,int a4){
        x1=a1;
        y1=a2;
        x2=a3;
        y2=a4;
    }
    double Length(){
        return (double)sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    }
    void Show(){
        cout<<'('<<x1<<", "<<y1<<"),("<<x2<<", "<<y2<<')'<<endl;
    }
};
class Triangle{
    private:
    Beeline line1;
    Beeline line2;
    Beeline line3;
    public:
    Triangle(int x1,int y1,int x2,int y2,int x3,int y3):line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1){};
    double Area(){
        double p = (line1.Length()+line2.Length()+line3.Length())/2;
        double s;
        s = sqrt(p*(p-line1.Length())*(p-line2.Length())*(p-line3.Length()));
        return s;
    }
    void Print(){
        line1.Show();
        line2.Show();
        line3.Show();
        cout<<"The area of this triangle is: "<<fixed<<setprecision(2)<<Area()<<'.'<<endl;
    }
};
int main(){
    int x1,y1,x2,y2,x3,y3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    Triangle tri(x1,y1,x2,y2,x3,y3);
    cout<<"Three edges' points are listed as follows:"<<endl;
    tri.Print();
    system("pause");
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值