Homework4_ch4 类和对象(2)

  1. 定义、实现并测试Number类。

定义类的私有数据成员n1n2为两个数字。

定义缺省形参值(0)的有参构造函数,赋予n1n2初始值。

定义加addition、减subtration、乘multiplication、除division等公有成员函数分别对两个成员变量执行加、减、乘、除的运算。

在主程序中创建Number类的对象调用各个方法并显示计算结果。

//number.h
#pragma once
#ifndef NUMBER_H
#define NUMBER_H
class Number {
private:
	int n1, n2;
public:
	Number(int, int);
	int addition() {
		return n1 + n2;
	}
	int subtration() {
		return n1 - n2;
	}
	int multiplication() {
		return n1 * n2;
	}
	double division() {
		return n1 / (n2 * 1.0);
	}
};
#endif
#include "number.h"
#include <iostream>
#include <cstdio>

Number::Number(int a=0, int b=0) {
	n1 = a;
	n2 = b;
}
//number.cpp
//main.cpp
#include "number.h"
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
	int a, b;
	cin >> a >> b;
	Number x(a, b);
	cout << x.addition() << endl;
	cout << x.subtration() << endl;
	cout << x.multiplication() << endl;
	cout << x.division() << endl;
	return 0;
}

  1. 设计货币类CMoney,用来描述生活中的货币。数据成员(私有)包括元、角、分。 成员函数(公有)包括设置值函数SetValue、显示函数Show、获得元值GetYuan、获得角值GetJiao、获得分值GetFen等函数。和以下构造函数、拷贝构造函数和析构函数:

a)无参构造函数,该函数把元角分的初值都设成0,并输出“无参构造函数被调用”;(b)带有3个参数的构造函数,该函数把元角分的初值设成用户输入的初始值;请做进位操作(比如:“12233分”经过进位操作后,转化成353分);在函数结束前输出“有参构造函数被调用”

c)拷贝构造函数,完成拷贝构造的功能,并输出“拷贝构造函数被调用”

d)析构函数,并输出“析构函数被调用”

主程序中,请用以下3种方式创建对象:

1 普通变量的方式。(如:CMoney m;

  1. 指针的方式。(如:CMoney * pm = new CMoney;

3 数组的方式。(如:CMoney money[10];

//cmoney.h
#pragma once
#ifndef CMONEY_H
#define CMONEY_H
class CMoney {
private:
	int yuan, jiao, fen;
public:
	void SetValue(int a, int b, int c);
	void Show();
	int GetYuan();
	int GetJiao();
	int GetFen();
	CMoney();
	CMoney(int a, int b, int c);
	~CMoney();
};
#endif

 

//cmoney.cpp
#include"cmoney.h"
#include<iostream>
using namespace std;
void CMoney::SetValue(int a, int b, int c) {
	yuan = a;
	jiao = b;
	fen = c;
}
void CMoney::Show() {
	cout << yuan << "元" << jiao << "角" << fen << "分" << endl;
}
int CMoney::GetYuan() {
	return yuan;
}
int CMoney::GetJiao() {
	return jiao;
}
int CMoney::GetFen() {
	return fen;
}
CMoney::CMoney() {
	yuan = 0;
	jiao = 0;
	fen = 0;
	cout << "无参构造函数被调用" << endl;
}
CMoney::CMoney(int a, int b, int c) {
	b += c / 10;
	c %= 10;
	a += b / 10;
	b %= 10;
	yuan = a;
	jiao = b;
	fen = c;
	cout << "有参构造函数调用" << endl;
}
CMoney::~CMoney() {
	cout << "析构函数被调用" << endl;
}
//main.cpp
#include"cmoney.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
	int a, b, c;
	CMoney m;
	cout << "普通变量方式创建对象测试,输入元角分空格隔开" << endl;
	cin >> a >> b >> c;
	m.SetValue(a, b, c);
	m.Show();
	m.~CMoney();
	cout << "\n指针方式创建对象及进位函数测试,输入元角分" << endl;
	cin >> a >> b >> c;
	CMoney* pm = new CMoney(a, b, c);
	pm->Show();
	pm->~CMoney();
	cout << "\n数组方式创建对象测试,金额随机" << endl;
	CMoney money[3];
	time_t t = time(0);
	struct tm st;
	localtime_s(&st, &t);
	srand(st.tm_sec);
	for (int i = 0; i <= 2; i++) {
		a = rand();
		b = rand()%10;
		c = rand()%10;
		money[i].SetValue(a, b, c);
		money[i].Show();
	}
	return 0;
}

 

  1. 定义、实现并测试一个三角形类。

定义三角形类的私有数据成员abc三个数代表边长。

定义下列成员函数:

a)定义缺省形参值(111)的有参构造函数,赋予abc初始值。.

b)对成员变量赋值函数

c)判断三角形是否满足三角形定义(任意两边之和大于第三边);

d)判断三角形是等腰三角形;

e)判断三角形是否为等边三角形;

f)判断三角形是否为直角三角形;

g)判断三角形是否为等腰直角三角形;

h)判断三角形属于前面哪类三角形;

i)计算三角形的周长;

j)计算三角形的面积。

编写主程序:用户输入三角形三边长,输出三角形是否为正确的三角形,如果是接着输出三角形是哪种类型的三角形,接着输出三角形的周长和面积。

#pragma once
#ifndef TRIANGLE_H
#define TRIANGLE_H
class Triangle {
private:
	double a, b, c;
public:
	Triangle(double x = 1, double y = 1, double z = 1) {
		a = x;
		b = y;
		c = z;
	};
	void SetSide(double x, double y, double z);
	bool TriangleYN();
	bool Side_E();
	bool Sides_E();
	bool Right();
	friend bool Side_Right(Triangle &x);
	friend int Which(Triangle &x);
	double Perimeter();
	long double Aera();
};
#endif
#include "triangle.h"
#include<iostream>
#include<cmath>
using namespace std;

void Triangle::SetSide(double x, double y, double z) {
	a = x;
	b = y;
	c = z;
}
bool Triangle::TriangleYN() {
	//cout << (a + b <= c) << endl;
	if (a + b <= c) return false;
	if (b + c <= a) return false;
	return a + c > b;
}
bool Triangle::Side_E() {
	if (a == b || a == c || b == c) {
		return true;
	}
	return false;
}
bool Triangle::Sides_E() {
	if (a == b && b == c) return true;
	return false;
}
bool Triangle::Right() {
	if (a > b && a > c)
		return a * a == b * b + c * c;
	if (b > a && b > c)
		return b * b == a * a + c * c;
	if (c > a && c > b)
		return c * c == a * a + b * b;
	return false;
}
double Triangle::Perimeter() {
	return a + b + c;
}
long double Triangle::Aera() {
	double p = (a + b + c) / 2.0;
	return pow(p * (p - a) * (p - b) * (p - c), 0.5);
}
bool Side_Right(Triangle& x) {
	if (x.Right() && x.Side_E())
		return true;
	return false;
}
int Which(Triangle& x) {
	if (x.Sides_E()) {
		cout << "等边三角形" << endl;
		return 1;
	}
	if (Side_Right(x)) {
		cout << "等腰直角三角形" << endl;
		return 2;
	}
	if (x.Right()) {
		cout << "直角三角形" << endl;
		return 3;
	}
	if (x.Side_E()) {
		cout << "等腰三角形" << endl;
		return 4;
	}
	cout << "just a triangle" << endl;
	return 0;
}
#include"triangle.h"
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
int main() {
	Triangle t1;
	cout << "输入三边边长" << endl;
	double a, b, c;
	cin >> a >> b >> c;
	t1.SetSide(a, b, c);
	if (!t1.TriangleYN()) {
		cout << "无法构成三角形" << endl;
		return 0;
	}
	int k = Which(t1);
	cout << "面积:" << t1.Aera() << endl;
	cout << "周长:" << t1.Perimeter() << endl;
	return 0;
}

  1. 定义、实现并测试一个双向链表结点类DNode

链表结点类中包含私有数据成员为两个整数xy,以及左结点指针left及右结点指针right

包含的函数成员包括:

(a)构造函数DNode(int xx=0,int yy=0,DNode* templeft=NULL,DNode* tempright=NULL);       

(b)对结点的数据成员赋值setDNodeValues(int,int);

(c)链表左插入结点void insertLeft()                 

(d)链表右插入结点void insertRight();                  

(e)删除结点void deleteNode();                              

(f)获取左侧相邻节点地址DNode* nextNodeLeft();

(g)获取右侧相邻节点的地址DNode* nextNodeRight();

(h)结点数据输出printNode();

编写主程序:至少输入3组整数(x, y, 分别利用左侧和右侧插入结点函数组织数据为双向链表,测试输出链表中每个结点的左侧和右侧相邻节点内容,测试对指定结点的删除。

#pragma once
#ifndef DNODE_H
#define DNODE_H
#include<iostream>
using namespace std;
class DNode {
private:
	int x, y;
	DNode* left, * right;
public:
	DNode(int xx = 0, int yy = 0, DNode* templeft = NULL, DNode* tempright = NULL) {
		x = xx;
		y = yy;
		left = templeft;
		right = tempright;
		//cout << "11" << endl;
	};
	void setDNodeValues(int, int);
	void insertleft();
	void insertright();
	void deleteNode();
	DNode* nextNodeLeft();
	DNode* nextNodeRight();
	void printNode();
};
#endif

#include"dnode.h"
#include<iostream>
#include<cstdlib>
using namespace std;
void DNode::setDNodeValues(int xx, int yy)
{
	x = xx;
	y = yy;
}
void DNode::insertleft() {
	DNode* n;
	n = (DNode*)malloc(sizeof(DNode));
	if (this->left == NULL) {
		this->left = n;
		n->right = this;
		n->left = NULL;
	}
	else {
		n->right = this->right;
		this->right->left = n;
		this->left = n;
		n->right = this;
	}
}
void DNode::insertright()
{
	DNode* n;
	n = (DNode*)malloc(sizeof(DNode));
	if (this->right == NULL) {
		this->right = n;
		n->left = this;
		n->right = NULL;
	}
	else {
		n->left = this->left;
		this->left->right = n;
		this->right = n;
		n->left = this;
	}
}

DNode* DNode::nextNodeLeft()
{
	return left;
}

DNode* DNode::nextNodeRight()
{
	return right;
}
void DNode::deleteNode()
{
	DNode* a = this->nextNodeLeft();
	DNode* b = this->nextNodeRight();
	if (a != NULL&&b != NULL) {
		a->right = b;
		b->left = a;
		return;
	}
	if (b == NULL&&a == NULL) {
		return;
	}
	if (a == NULL) {
		b->left = NULL;
		return;
	}
	if (b == NULL) {
		a->right = NULL;
		return;
	}
}
void DNode::printNode()
{
	if (this == NULL)
	{
		cout << "此节点为空" << endl; return;
	}
	cout << x << " " << y << endl;
}
#include"dnode.h"
#include<iostream>
#include<cstdlib>
//using namespace std;
int main() {
	DNode d1;
	//head = (DNode*)malloc(sizeof(DNode));
	cout << "输入一对整数" << endl;
	int x, y;
	cin >> x >> y;
	d1.setDNodeValues(x, y);
	cout << "测试左右侧插入节点" << endl;
	d1.insertleft();
	d1.insertright();
	cout << "输入左节点数据" << endl;
	cin >> x >> y;
	DNode* L = d1.nextNodeLeft();
	L->setDNodeValues(x, y);
	cout << "输入右节点数据" << endl;
	cin >> x >> y;
	DNode* R = d1.nextNodeRight();
	R->setDNodeValues(x, y);
	cout << "原节点:" << endl;
	d1.printNode();
	cout << "左节点:" << endl;
	L->printNode();
	cout << "右节点" << endl;
	R->printNode();
	cout << "测试删除右节点" << endl;
	R->deleteNode();
	d1.nextNodeRight()->printNode();
	cout << "删除成功" << endl;
	cout << "恢复" << endl;
	d1.insertright();
	R = d1.nextNodeRight();
	R->setDNodeValues(x, y);
	cout << "删除中间节点" << endl;
	d1.deleteNode();
	L->nextNodeRight()->printNode();
	cout << "删除成功" << endl;
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值