1. 友元函数设计
1.1 程序描述
模拟电力公司统计用户用电量。请设计一个类FamliyElecPower描述每户人家一年的用电情况,一年的12个月的每月电量都要记录。设计函数CalTotalEPower,计算每户一年的总电量;函数CalAveEPower计算每户的每月平均用电量。函数CalTotalEPower和函数CalAveEPower是类FamliyElecPower的友元函数。主程序中创建1户人家,输入这户人家1年12个月的用电量,然后调用函数CalTotalEPower和函数CalAveEPower分别计算并输出这家的总用电量和平均每月用电量。
1.2 提示
class FamliyElecPower
{ int m_nID; //用户编号,是从1开始的正整数,由程序自动生成
string m_strFamliyName; //家庭名称,是一个string型字符串;
//需要#include <string>
int m_array[12]; //12个月的用电量
static int m_nCount; //用来生成自动编号的计数器
public:
FamliyElecPower(){}
FamliyElecPower(string name, int* array); // int* array 表示 整数型
//数组array[12]
void Show(); //输出这户人家的所有信息
…… //其他方法请自行设计
};
……. //其他函数等请自行设计
-
- 输入输出样例:
请输入用户名称:Wang //带下划线的部分表示用户的输入
请分别输入12个月的电量:89 70 56 102 112 92 90 88 75 89 86 123
用户用电信息是:
编号:1 名称:Wang
12个月的用电量:89 70 56 102 112 92 90 88 75 89 86 123
年总电量:1072 月均电量::89
1.4 上机思考题部分
从文件输入100个家庭的信息,统计每户年总电量、月均电量,结果可以输出到另一个文件中
选作部分的输入参见in.txt
//famliyelecpower.h
#pragma once
#ifndef FAMLIYELECPOWER_H
#define FAMLIYELECPOWER_H
#include<string>
using namespace std;
class FamliyElecPower {
public:
FamliyElecPower();
FamliyElecPower(string& name, int array[]);
void Show();
void SetFEP(string& name, int array[]);
friend int CalTotalEPower(FamliyElecPower& x);
friend int CalAveEPower(FamliyElecPower& x);
private:
int m_nID;
string m_strFamliyName;
int m_array[12];
static int m_nCount;
};
#endif
//famliyelecpower.cpp
#include"famliyelecpower.h"
#include<stdlib.h>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int CalTotalEPower(FamliyElecPower& x)
{
int t = 0;
//cout << x.m_strFamliyName << "!" << endl;
for(int i = 0; i < 12; i++) {
t += x.m_array[i];
//cout << x.m_array[i] << endl;
}
return t;
}
FamliyElecPower::FamliyElecPower(string& name, int array[]) {
m_nID = ++m_nCount;
m_strFamliyName = name;
//int* m_array = new int[12];
for (int i = 0; i < 12; i++) {
m_array[i] = array[i];
//cout << m_array[i] << endl;
}
}
void FamliyElecPower::Show()
{
cout << "编号:" << setw(3) << m_nID << " " << "名称:" << setw(5) << m_strFamliyName << endl;
cout << "12个月的用电量:" << ends;
for (int i = 0; i < 12; i++) {
cout << " " << m_array[i];
}
cout << endl;
cout << "年总电量:" << CalTotalEPower(*this) << ends;
cout << " " << "月均电量:" << setw(4) << CalAveEPower(*this) << endl;
}
int CalAveEPower(FamliyElecPower& x)
{
int a = 0;
for (int i = 0; i < 12; i++) {
a += x.m_array[i];
}
a /= 12;
return a;
}
void FamliyElecPower::SetFEP(string& name, int array[])
{
m_strFamliyName = name;
for (int i = 0; i < 12; i++) {
m_array[i] = array[i];
}
}
FamliyElecPower::FamliyElecPower()
{
m_nID = ++m_nCount;
m_strFamliyName = "";
for (int i = 0; i < 12; i++) {
m_array[i] = 0;
}
}
// mian.cpp
#include"famliyelecpower.h"
#include<iostream>
#include<string>
using namespace std;
int FamliyElecPower::m_nCount = 0;
int main() {
string s;
cout << "请输入用户名称:" << ends;
cin >> s;
int a[12];
cout << "请分别输入12个月的电量:" << ends;
for (int i = 0; i < 12; i++) {
cin >> a[i];
}
FamliyElecPower f(s, a);
cout << "用户用电信息:" << endl;
f.Show();
return 0;
}
// sikao.cpp
#include"famliyelecpower.h"
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
//#pragma warning(disable:4996)
using namespace std;
int FamliyElecPower::m_nCount = 0;
int main() {
FILE* file, *file1;
freopen_s(&file, "in.txt", "r", stdin);
freopen_s(&file1, "out.txt", "w", stdout);
string s;
int a[12];
FamliyElecPower f[101];
for (int i = 0; i < 100; i++) {
cin >> s;
for (int j = 0; j < 12; j++) {
cin >> a[j];
}
f[i].SetFEP(s, a);
f[i].Show();
}
fclose(stdin);
fclose(stdout);
return 0;
}
2. 友元类设计
2.1 基本要求:
教师作为学生的友元类,操作学生的学分和总成绩。
2.2 功能设计:
(1)设计学生类,包含学生姓名、学分和总成绩;
(2)设计教师类,包含教师姓名,教师有权限修改每个学生的学分和总成绩;
(3)在以上类中增加合适的成员函数以完成程序的功能;
(4)教师类设计成学生类的友元;
2.3 主程序中建立1名教师,3名学生,实现教师分别修改3名学生的学分和总成绩的操作。
2.4 上机思考题部分:
主程序和界面要求:
2.4.1 主程序要求:
(1)至少有5名学生,3名教师;
(2)用户随意选择一位教师、一名学生,让该教师给该学生增加学分、
增加总成绩;
(3)程序结束前输出每位学生的姓名、学分、总成绩
2.4.2 界面要求(输出样例):
您需要建立几位学生(M)和几位教师(N):5 3
请输入5位学生的姓名:张朋 李元 刘文 贺明 鲁笑
请输入3位教师的姓名:严华 耿燕 吴晓乐
请选择一位教师: 吴晓乐
请选择一位学生:贺明
请输入要增加的学分:3
教师吴晓乐给学生贺明增加学分,操作成功。
请输入要增加的总成绩:88
教师吴晓乐给学生贺明增加总成绩,操作成功。
还要继续吗?(Y/N):
如果用户选择N,则输出每位学生的姓名、学分、总成绩,然后程序结束;
如果用户选择Y, 则转向步骤:选择教师,程序继续运行。
//student.h
#pragma once
#include<string>
#include"teacher.h"
using namespace std;
class Student {
friend class Teacher;
public:
Student();
void setStudentName(string& s);
void show();
bool check(string);
private:
string name;
double credit;
double score;
};
//teacher.h
#pragma once
#include<string>
#include"student.h"
using namespace std;
class Teacher {
public:
void modifyCredit(class Student& x, double c);
void modifySorce(class Student& x, double s);
void setName(string);
bool check(string);
private:
string name;
};
//teacher.cpp
#include"teacher.h"
#include"student.h"
#include<iostream>
using namespace std;
void Teacher::modifyCredit(Student& x, double c)
{
x.credit += c;
}
void Teacher::modifySorce(Student& x, double s)
{
x.score += s;
}
void Teacher::setName(string s)
{
name = s;
}
bool Teacher::check(string s)
{
return name == s;
}
// student.cpp
#include"student.h"
#include"teacher.h"
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;
Student::Student()
{
credit = 0;
score = 0;
}
void Student::setStudentName(string& s)
{
name = s;
}
void Student::show()
{
cout << "姓名:" << name << endl;
cout << "学分:" << credit << endl;
cout << "总成绩" << score << endl;
}
bool Student::check(string s)
{
return name == s;
}
//main.cpp
#include<iostream>
#include<string>
#include"student.h"
#include"teacher.h"
using namespace std;
int main() {
int m, n;
cout << "您需要输入的学生人数(M)和教师人数(N):" << ends;
cin >> m >> n;
Teacher* t = new Teacher[n];
Student* s = new Student[m];
string name;
cout << "请输入" << m << "位学生姓名:";
for (int i = 0; i < m; i++) {
cin >> name;
s[i].setStudentName(name);
}
cout << "请输入" << n << "位教师姓名:";
for (int i = 0; i < n; i++) {
cin >> name;
t[i].setName(name);
}
string st, te;
char op;
do {
cout << "请选择一位教师:" << ends;
cin >> te;
int c = 0;
for (int i = 0; i < n; i++) {
if (t[i].check(te)) {
c = i + 1;
break;
}
}
if (c) {
cout << "请输入一位学生:";
cin >> st;
int cc = 0;
for (int i = 0; i < m; i++) {
if (s[i].check(st)) {
cc = i + 1;
break;
}
}
if (cc) {
cout << "请输入要增加的学分:" << ends;
double cd;
cin >> cd;
t[c - 1].modifyCredit(s[cc - 1], cd);
cout << "教师" << te << "给学生" << st << "增加学分,操作成功。" << endl;
cout << "请输入要增加的总成绩:" << ends;
cin >> cd;
t[c - 1].modifySorce(s[cc - 1], cd);
cout << "教师" << te << "给学生" << st << "增加总成绩,操作成功。" << endl;
}
else {
cout << "您选择的不是学生" << endl;
}
}
else {
cout << "您不是教师,无法修改" << endl;
}
cout << "还要继续吗(Y/N)" << ends;
cin >> op;
} while (op == 'Y' || op == 'y');
for (int i = 0; i < m; i++) {
s[i].show();
}
return 0;
}