实验三 类和对象(二)

1、设计一个4*4的魔方程式。要求:

魔方各行值之和=魔方的各列值之和=两条对角线值的和。例如:

31

3

5

25

9

21

19

15

17

13

11

23

7

27

29

1

  【提示】:求4*4的魔方的一般步骤如下:

设置初始魔方的起始值和相邻元素之间的差值。例如上述魔方的初始魔方的起始值(first)和相邻元素之间的差值(step)分别为:first=1; step=2;

设置初始魔方元素的值,例如上述魔方的初始魔方为:

1   3   5   7

9   11  13  15

17  19  21  23

25  27  29  31

生成最终魔方。方法如下:

求最大元素值与最小元素值的和sum,该实例的sum是:1+31=32

用32减去初始魔方所有对角线上元素的值,然后将结果放在原来的位置,这样就可以求得最终魔方。

本题的魔法类magic的参考框架如下:

class magic

{public:

        void getdata();

        void getfirstmagic();

    void generatemagic();

    void printmagic();

private:

       int m[4][4];

       int step;

       int first;

       int sum;

}

#include<iostream>
using namespace std;
class magic
{
public:
	magic(int f,int s);
	void generatemagic();
	void printmagic();
private:
	int first;
	int step;
	int m[4][4];
};
magic::magic(int f,int s)
{
	first = f;
	step = s;
}

void magic::generatemagic()
{
	int a = first;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			m[i][j] = a;
			a = a + step;
		}
	}
	int sum = m[0][0] + m[3][3];
	
	m[0][0] = sum - m[0][0];
	m[3][3] = sum - m[3][3];
}
void magic::printmagic()
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cout << m[i][j] << "\t";
		}
		cout << endl;
	}
}
int main()
{
	int f, s;
	cin >> f >> s;
	magic m1(f, s);
	m1.generatemagic();
	m1.printmagic();
	return 0;
	
}

 2、设计类Time(用来处理时、分、秒)和类Data(用来处理年、月、日),然后分别在两个类中声明display函数为其友员函数。在主调函数中调用display函数,display函数分别引用两个类的对象的私有数据,输出年、月、日、时、分、秒。(请注意形参的合理定义)

#include<iostream>
using namespace std;
class Time;
class Date
{
public:
	Date(int y, int mo, int d) :year(y), month(mo), day(d)
	{
	}
	friend void display(const Date& d, const Time& t);
private:
	int year;
	int month;
	int day;
};
class Time
{
public:
	Time(int h, int m, int s) :hour(h), minute(m), sec(s)
	{
	}
	friend void display(const Date& d, const Time& t);
private:
	int hour;
	int minute;
	int sec;
};
void display(const Date& d, const Time& t)
{
	cout << d.year << ":" << d.month << ":" << d.day << ":";
	cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
int main()
{
	Date d(2024,3,20);
	Time t(21,13,14);
	display(d, t);
	return 0;
}

3、商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购买10件以上者,还可以享受2%的优惠,现在已知3个销售员的销售情况如下表:

销售员号(num)

销售件数(quantity)

销货单价(price)

101

5

23.5

102

12

24.56

103

100

21.5

请编程计算当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。

提示:将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,再定义静态成员函数average(求平均售价)和display(输出结果)。

#include<iostream>
#include<string>
using namespace std;
class Sail
{
public:
	Sail(int nu, int q, double p)
	{
		num = nu;
		quantity = q;
		price = p;
		sum = sum + quantity * price * (1 - discount);
		n = n + quantity;
	}
	static int average()
	{
		return sum / n;
	}
	static void display()
	{
		cout << "总售价:" << sum << endl;
		cout << "平均售价:" << average() << endl;
	}
private:
	int num;
	int quantity;
	char price;
	static double discount;
	static double sum;
	static double n;
};
double Sail::discount = 0.05;
double Sail::n = 0;
double Sail::sum = 0;
int main()
{
	Sail s1(101, 5,   23.5);               
	Sail s2(102, 12, 24.56);
	Sail s3(103, 100, 21.5);
	Sail::display();
	return 0;
}

 4、声明一个Student类,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全部成绩的平均值。在main函数中,输入某班同学的成绩,并调用上述函数求出全班同学的成绩之和和平均分。

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	void account(int s);
	static void sum();
	static void average();
private:
	float score;
	static int count;
	static float totalscore;
	static float ave;
};
void Student::account(int s)
{
	score = s;
	totalscore = totalscore + score;
	count++;
}
void Student::sum()
{
	cout << "成绩总和:" <<totalscore<< endl;
}
void Student::average()
{
	ave = totalscore / count;
	cout << "平均分:" << ave << endl;

}
int Student::count = 0;
float Student::totalscore = 0.0;
float Student::ave = 0.0;
int main()
{
	int n;
	cin >> n;
	Student* stu = new Student[n];
	int score;
	for (int i = 0;i < n;i++)
	{
		cin >> score;
		stu[i].account(score);
	}
	stu->sum();
	stu->average();
	return 0;
}

  • 27
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值