复数类

【问题描述】

  自定义一个复数类型Complex,其中含有若干成员函数,使用该类可以完成复数的加法以及对复数的输出。请完成类定义,并编制主函数,说明Complex类对象,对定义的各成员函数进行调用。

1
2
3
4
5
6
7
8
9
10
class Complex 
{
double real; //复数实部
double imag; //复数虚部
public:
Complex (); //无参构造函数,将复数对象的实部和虚部均置为0
Complex (double r, double i); //有参构造函数,设置对象的实部和虚部
Complex AddCom(Complex c2); //调用者对象与对象c2相加,返回Complex类对象
void OutCom () ; //输出调用者对象的有关数据(各分量)
};

具体 要求如下:

1、实现有参构造函数 Complex (double r, double i);

2、实现 Complex AddCom(Complex c2); 调用者对象与对象c2相加,返回Complex类对象

3、实现void OutCom () ;实现输出调用者对象的有关数据分量(一定要输出虚部的符号i),如果该数为纯虚数时,不需要输出实部,当虚部为0时,不需要输出实部。

4.编制主函数main,作用有参函数说明类对象cx,cy,使用 Complex 调用AddCom实现复数加法,并将相加的结果调用 OutCom方法以复数的形式输出。
【输入形式】

输入包括a,b,c,d四个整数,第一个复数为a+bi,第二个复数为c+di

【输出形式】
【样例输入】

1 2 3 4

【样例输出】

4+6i

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex()
{
real = 0;
imag = 0;
}
Complex(double,double);
Complex AddCom(Complex);
void OutCom();
};
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex Complex::AddCom(Complex c2)
{
(*this).imag += c2.imag;
(*this).real += c2.real;
return *this;
}
void Complex::OutCom()
{
if (real == 0)
cout << imag << "i";
else if (imag == 0)
cout << real;
else
cout << real << "+" << imag << "i";
}
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
Complex cx(a, b);
Complex cy(c, d);
cx.AddCom(cy);
cx.OutCom();
}

学生类

【问题描述】

  设计一个学生类(CStudent),其私有数据成员:注册号、姓名、数学、外语、计算机课程的成绩。公有成员函数是:求三门课总成绩的函数 Sum;求三门课平均成绩的函数 Average;显示学生数据信息的函数 Display;设置学生数据信息的函数 SetData。

1.可按如下样式设计 CStudent类的各数据成员以及成员函数
class CStudent { //学生类 CStudent
unsigned long reg_num; //数据成员:注册号
char name[30]; //数据成员:姓名
float math, eng, comp; //数据成员:数学、英语、计算机成绩
public: //公有成员函数
float Sum(); //求三门课总成绩的函数 Sum
float Average(); //求三门课平均成绩的函数 Average
Display(); //显示学生数据信息的函数 Display
SetData (unsigned long r, char* n, float m, float e, float c) ;
//设置学生数据信息的函数 SetData
};

在主函数,通过使用“CStudent stu[150];”的语句,来说明一个CStudent类对象的数组stu,而后通过各对象stu[i]来处理并求取每一学生的总成绩、平均成绩等。

(1)输入本次欲处理的学生人数 TOTAL(小于等于 150 的正整数);
(2)输入全班 TOTAL 个学生的有关信息,依次放入对象数组的各元素 stu[i]中(通过使用“stu[i].SetData(…);”形式的语句来实现);
(3)对全班TOTAL个学生,依次通过对象stu[i]来求出其总成绩、平均成绩等(其中要使用形如“stu[i].Sum()”以及“stu[i].Average()”式样的对成员函数进行调用的语句),并同时求出全班学生总成绩最高者处于 stu 数组的下标位置idx_max,而后通过使用“stu[idx_max].Display();”来输出该学生有关的全部数据信息。

3.程序执行后的输入输出界面样式可设计为:
TOTAL=3
CStudent 1 : 100001 ma 78 86 90(注意空格)
CStudent 2 : 100002 li 85 91 88
CStudent 3 : 100003 hu 82 89 88
CStudent1.Sum=254,CStudent1.average=84.6667
CStudent2.Sum=264,CStudent2.average=88
CStudent3.Sum=259,CStudent3.average=86.3333
class_Sum_max=264
The infomation of the CStudent with class_Sum_max : 100002 li 85 91 88

【输入形式】

TOTAL=3
CStudent 1 : 100001 ma 78 86 90(注意空格)
CStudent 2 : 100002 li 85 91 88
CStudent 3 : 100003 hu 82 89 88

【输出形式】

CStudent1.Sum=254,CStudent1.average=84.6667
CStudent2.Sum=264,CStudent2.average=88
CStudent3.Sum=259,CStudent3.average=86.3333
class_Sum_max=264
The infomation of the CStudent with class_Sum_max : 100002 li 85 91 88

【样例输入】

3
100001 ma 78 86 90
100002 li 85 91 88
100003 hu 82 89 88

【样例输出】

TOTAL=3
CStudent 1 : 100001 ma 78 86 90(注意空格)
CStudent 2 : 100002 li 85 91 88
CStudent 3 : 100003 hu 82 89 88
CStudent1.Sum=254,CStudent1.average=84.6667
CStudent2.Sum=264,CStudent2.average=88
CStudent3.Sum=259,CStudent3.average=86.3333
class_Sum_max=264
The infomation of the CStudent with class_Sum_max : 100002 li 85 91 88

【题解】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<string.h>
using namespace std;
class Cstudent
{
unsigned reg_num;
char name[30];
float math, eng, comp;
public:
float Sum();
float Average();
void Display();
void setData(unsigned long r, char* n, float m, float e, float c);
};
void Cstudent::Display()
{
cout << reg_num << " " << name << " " << math << " " << eng << " " << comp << endl;
}
float Cstudent::Sum()
{
return(math + eng + comp);
}
float Cstudent::Average()
{
return (*this).Sum() / 3;
}
void Cstudent::setData(unsigned long r, char* n, float m, float e, float c)
{

reg_num = r;
strcpy(name, n);
math = m;
eng = e;
comp = c;
}

int main()
{
int n;
unsigned long num;
char name[30];
float math, eng, comp;
float max = 0;
int max_stu = 0;
cin >> n;
Cstudent* cp = new Cstudent[n];

int j;
for (j = 0; j < n; j++)
{
cin >> num >> name >> math >> eng >> comp;
cp[j].setData(num, name, math, eng, comp);
}
for (j = 0; j < n; j++)
{
if (max < cp[j].Sum())
{
max_stu = j;
max = cp[j].Sum();
}

}
cout << "TOTAL=" << n << endl;
for (int i = 0; i < n; i++)
{
cout << "CStudent " << i + 1 << " : ";
cp[i].Display();
}
for (int i = 0; i < n; i++)
{
cout << "CStudent" << i + 1 << ".Sum=" << cp[i].Sum();
cout << ",CStudent" << i + 1 << ".average=" << cp[i].Average() << endl;
}
cout << "class_Sum_max=" << max << endl;
cout << "The infomation of the CStudent with class_Sum_max : ";
cp[max_stu].Display();
delete[]cp;
}