using namespace std; int *new_array(int n) {  int* p;  p = new int[n];  return p;}
void init_array(int *p, int n, int c) {  for (int i = 0; i < n; ++i)   p[i] = c; }
int main() {  int n, c;  cin >> n >> c;  int *arr = new_array(n);  init_array(arr, n, c);  for (int i = 0; i < n; ++i)  {   if (i == n - 1)    cout << *arr;   else    cout << *arr << ' ';   arr++;  }  return 0; }
5、矩阵对角线元素之和(20分) 题目内容:
编写函数,求n阶方阵的对角线元素之和。编写主程序,用户输入矩阵的阶数n,动态申请n*n的存储空间,再输入n行、n列的元素,调用函数求矩阵的对角元素之和,在主函数中输出这个和。设元素均为整数。n>=1。
函数格式:int sumDiagonal(int *a,int n);
#include  //不使用题目的函数做法 using namespace std; int main() {  int n;  int sum = 0;  int* *arr;  cin >> n;   arr = new int*[n];  for (int i = 0; i < n; ++i)  {   arr[i] = new int[n];   for (int j = 0; j < n; ++j)    cin >> arr[i][j];  }  for (int i = 0; i < n; ++i)   sum += arr[i][i];  cout << sum;for (int i = 0; i < n; ++i) //删除与答题无关,只为养成良好的习惯   delete[] arr[i];  delete[] arr;  return 0; } 6、(本题只记3分)十进制点分IP转换为32位二进制IP(3分) 题目内容:
编写程序,将十进制点分的IP转换为32位二进制IP地址。程序要能验证输入的十进制点分IP地址的合法性。用户输入的IP不和法时,输出\"data error\"。
请使用模块化程序设计的思想,将功能模块编写成函数。通过指针传递参数,操作数据,返回结果。在主函数中输入IP地址,调用函数进行合法性验证和转换,在主函数中输出32位二进制IP。 提示:十进制转换为二进制。对整数部分,除2取余,直到商为0。例如 13/2=6.....1(低位) 6/2=3......0 3/2=1......1 1/2=0......1
转换后的二进制数位1101
#include  //递归法转换IP为二进制 using namespace std; void dec2bin(int n, int k) {  if (k > 1)   dec2bin(n >> 1, k - 1);  cout << n % 2; }int main() {  int a, b, c, d;  scanf(\"%d.%d.%d.%d\ if ((a > 255 || a < 0) || (b > 255 || b < 0) || (c > 255 || c < 0) || (d > 255 || d < 0))   cout << \"data error\";  else  {   dec2bin(a, 8);   dec2bin(b, 8);   dec2bin(c, 8);   dec2bin(d, 8);  }  return 0; }
九周目
1、设计Person类(20分) 题目内容:
设计一个Person类,包含name、age、sex属性以及对这些属性操作的方法。实现并测试这个类。 根据类的封装性要求,把name、age、sex声明为私有的数据成员,声明公有的成员函数Register()、ShowMe()
来访问这些属性,在Register()函数中对数据成员进行初始化。person1通过cin来得到信息,person2通过Register(\"Zhang3\来得到信息。 #include  #include  using namespace std; class Preson {public:  void Register(string m_name, int m_age, char m_sex);  void ShowMe(); private:  string name;  int age;  char sex; };
int main() {  Preson preson1, preson2;  string name;  int age;  char sex;  cin >> name >> age >> sex;  preson1.Register(name, age, sex);  preson2.Register(\"Zhang3\ preson1.ShowMe();  preson2.ShowMe();  return 0; }
void Preson::Register(string m_name, int m_age, char m_sex) {  name = m_name;  age = m_age;  sex = m_sex; }
void Preson::ShowMe() {  cout << name << ' ' << age << ' ' << sex << endl; }
2、设计Dog类(20分) 题目内容:
设计一个Dog类,包含name、age、sex和weight等属性以及对这些属性操作的方法。实现并测试这个类。
根据类的封装性要求,把name、age、sex和weight声明为私有的数据成员,编写公有成员函数setdata()对数据进行初始化,GetName()、GetAge()、GetSex()和GetWeight()获取相应属性。初始化数据由用户输入。 #include  #include  using namespace std;class Dog {
public:  void setdata(string Dname, int Dage, string Dsex, double Dweigth);  void GetName();  void GetAge();  void GetSex();  void GetWeight(); private:  string name;  int age;  string sex;  double weight; };
int main() {  Dog dog;  string name;  int age;  string sex;  double weight;  cin >> name >> age >> sex >> weight;  dog.setdata(name, age, sex, weight);  cout << \"It is my dog.\" << endl;  dog.GetName();  dog.GetAge();  dog.GetSex();  dog.GetWeight();  return 0; }
void Dog::setdata(string Dname, int Dage, string Dsex, double Dweigth) {  name = Dname;  age = Dage;  sex = Dsex;  weight = Dweigth; }
void Dog::GetName() {  cout << \"Its name is \" << name << \".\" << endl; }
void Dog::GetAge() {  cout << \"It is \" << age << \" years old.\" << endl; }
void Dog::GetSex() {
if (sex == \"m\")   cout << \"It is male.\" << endl;  else   cout << \"It is female.\" << endl; }
void Dog::GetWeight() {  cout << \"It is \" << weight << \" kg.\" << endl; }
3、设计并测试Trapezium类(20分) 题目内容:
设计并测试一个名为Trapezium的梯形类,其属性为梯形的四个顶点的坐标。该梯形上边和下边均和x轴平行。
根据类的封装性要求,在类的声明中用8个私有的整型变量表示4个点的坐标值,声明成员函数initial(int,int,int,int,int,int,int,int)初始化数据成员,函数GetPosition(int&,int&,int&,int&,int&,int&,int&,int&)读取坐标值,函数Area()计算面积。梯形的面积,依次为左上(x1,y1)、右上(x2,y2)、左下(x3,y3)和右下(x4,y4)角的顶点。
#include  using namespace std; class Trapezium {public:  void InitTra(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);  void GetPosition(int &x1, int &y1, int &x2, int &y2, int &x3, int &y3, int &x4, int &y4);  void Area(); private:  int xur, yur;  int xul, yul;  int xdl, ydl;  int xdr, ydr; };
int main() {  int a1, a2, b1, b2, c1, c2, d1, d2;  cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2 >> d1 >> d2;  Trapezium Tt;  Tt.InitTra(a1, a2, b1, b2, c1, c2, d1, d2);  Tt.Area();  return 0; }
void Trapezium::InitTra(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {  xur = x2;  yur = y2;  xul = x1;  yul = y1;  xdl = x3;
ydl = y3;  xdr = x4;  ydr = y4; }
void Trapezium::GetPosition(int & x1, int & y1, int & x2, int & y2, int & x3, int & y3, int & x4, int & y4) {  cout << x1 << \" \" << y1 << \" \" << x2 << \" \" << y2 << \" \" << x3 << \" \" << y3 << \" \" << x4 << \" \" << y4 << endl; }
void Trapezium::Area() {  double s = (xur - xul + xdr - xdl)*(yur - ydr) / 2;  cout << s << endl; }
4、设计MyTime类(20分) 题目内容:
设计一个MyTime类,成员函数SetTime()设置时间,print_12()以12(0-11)小时制显示时间(AM上午,PM下午),print_24()以24(0-23)小时制显示时间。
按照12小时制和24小时制依次显示时间,注意时间格式中的冒号是英文冒号,时分秒都是两位,AM,PM前有一个空格,晚上12:00是00:00:00 AM,中午十二点是00:00:00 PM。 #include  using namespace std; class MyTime {public:  void SetTime(int h, int m, int s);  void print12();  void print24(); private:  int hh, mm, ss; };
int main() {  int h, m, s;  cin >> h >> m >> s;  MyTime tt;  tt.SetTime(h, m, s);  tt.print12();  tt.print24();  return 0; }
void MyTime::SetTime(int h, int m, int s) {  hh = h;  mm = m;  ss = s; }
void MyTime::print12() {  if ((hh - 12) == 0)   cout << \"00\";  else if ((hh - 12) > 0)  {   if ((hh - 12) > 10)    cout << hh - 12;   else    cout << 0 << hh - 12;  }  else  {   if (hh > 9)    cout << hh;   else    cout << 0 << hh;  }  cout << \":\";  if (mm < 10)   cout << 0 << mm;  else   cout << mm;  cout << \":\";  if (ss < 10)   cout << 0 << ss;  else   cout << ss;  if ((hh - 12) > 0 || (hh == 12 && (mm != 0 || ss != 0)))   cout << \" PM\" << endl;  else if (((hh - 12) == 0) && mm == 0 && ss == 0)   cout << \" PM\" << endl;  else   cout << \" AM\" << endl; }
void MyTime::print24() {  if (hh < 10)   cout << 0 << hh;  else   cout << hh;  cout << \":\";  if (mm < 10)   cout << 0 << mm;  else   cout << mm;  cout << \":\";
if (ss < 10)   cout << 0 << ss << endl;  else   cout << ss << endl; }
5、设计Weekday类(20分) 题目内容:
设计一个Weekday类,成员函数SetDay()设置星期几,IncDay()前进一天,NowDay()打印当前是星期几。 #include  using namespace std; class Weekday {public:  void SetDay(int day);  void IncDay();  void NowDay(); private:  int today; };
int main() {  int n;  cin >> n;  Weekday wday;  wday.SetDay(n);  wday.NowDay();  wday.IncDay();  wday.NowDay();  wday.IncDay();  wday.NowDay();  return 0; }
void Weekday::SetDay(int day) {  today = day; }
void Weekday::IncDay() {  today = (today + 1) % 7; }
void Weekday::NowDay() {  switch (today)  {
}
case 0:  cout << \"星期日\" << endl;  break; case 1:  cout << \"星期一\" << endl;  break; case 2:  cout << \"星期二\" << endl;  break; case 3:  cout << \"星期三\" << endl;  break; case 4:  cout << \"星期四\" << endl;  break; case 5:  cout << \"星期五\" << endl;  break; case 6:  cout << \"星期六\" << endl;  break; default:  cout << \"Try again\";  break; }
十周目
1、定义一个带重载构造函数的日期类(20分) 题目内容:
定义一个带重载构造函数的日期类Date,数据成员有年、月、日;成员函数包括:一个带参数的构造函数Date(int,int,int),一个不带参数的构造函数(设置日期为1900年1月1日),一个按“年-月-日”格式显示日期的函数,一个对数据成员赋值的函数void init(int,int,int)。 主函数中对类的测试要求:
1. 分别使用两个不同的重载构造函数创建两个日期类对象(必须为d1,d2,d2初始值为2100-12-12); 2. 按“年-月-日”格式分别显示两个对象的值; 3. 输入数据,用init函数为d1赋值; 4. 按“年-月-日”格式显示对象d1的值;。 #include  using namespace std; class Date {public:  Date() :_year(1900), _month(1), _day(1) {};  Date(int, int, int) :_year(2100), _month(12), _day(12) {};  ~Date() {};
void Init(int y, int m, int d);  void Show(); private:  int _year;  int _month;  int _day; };
int main() {  Date *d1 = new Date();  Date *d2 = new Date(2100, 12, 12);  int y, m, d;  cin >> y >> m >> d;  d1->Show();  d2->Show();  d1->Init(y, m, d);  d1->Show();  delete d1, d2;  return 0; }
void Date::Init(int y, int m, int d) {  this->_year = y;  this->_month = m;  this->_day = d; }
void Date::Show() {  cout << _year << \"-\" << _month << \"-\" << _day << endl; }
2、动态生成Person类的对象(20分) 题目内容:
编写Person类,数据成员为姓名(20字符长度)、年龄(int)和性别(char)。 编写无参数的构造函数,其中姓名赋值为“XXX”,年龄0,性别'm';
编写析构函数,在其中输出字符串“Now destroying the instance of Person”; 编写Register成员函数,为数据成员赋值;
编写showme成员函数,显示姓名、年龄和性别。 编写主函数:
用Person类创建2个指针,p1和 p2;
用new创建两个Person对象,分别将指针赋值给p1,p2; 用showme成员函数显示p1,p2所指对象的值;
再输入一组“姓名、年龄和性别”值,用成员函数Register为p1的成员赋值; 将p1所指对象的值赋值给p2所指对象; 用showme显示p1、p2所指对象的值。 删除动态对象。 #include  #include using namespace std; class Person {
public:  Person()  {   strcpy(name, \"XXX\");   age = 0;   sex = 'm';  }  ~Person() { cout << \"Now destroying the instance of Person\" << endl; }  void Register(char *n,int a, char s)  {   strcpy(name, n);   age = a;   sex = s;  }  void showme()  {   cout << name << \" \" << age << \" \" << sex << endl;  } private:  char name[20];  int age;  char sex; };
int main() {  char name[20];  int age;  char sex;  Person *p1 = new Person();  Person *p2 = new Person();  cin >> name >> age >> sex;  cout << \"person1:\";  p1->showme();  cout << \"person2:\";  p2->showme();  p1->Register(name, age, sex);  cout << \"person1:\";  p1->showme();  *p2 = *p1;  cout << \"person2:\";  p2->showme();  delete p1;  delete p2;  return 0;
}
3、设计带构造函数的Dog类(20分) 题目内容:
设计一个Dog类,包含name、age、sex和weight等属性,在有参数的构造函数中对数据成员进行初始化。
公有成员函数有:GetName()、GetAge()、GetSex()和GetWeight()可获取名字、年龄、性别和体重。编写成员函数speak()
显示狗的叫声。编写主函数,输入狗的名字、年龄、性别和体重;声明Dog对象并用输入的数据通过构造函数初始化对象,通过成员函数获取狗的属性并显示出来。 #include  #include  using namespace std; class Ellipse {public:  Ellipse() :x(0), y(0), a(0), b(0) {}  Ellipse(int, int, double, double);  inline double Area() { return 3.14 * a * b; } private:  int x, y;  double a, b; };
int main() {  int x, y;  double a, b, s;  cin >> x >> y >> a >> b;  Ellipse *e = new Ellipse(x,y,a,b);  s = e->Area();  cout << s << endl;  delete e;  return 0; }
Ellipse::Ellipse(int _x, int _y, double _a, double _b) {  x = _x;  y = _y;  a = _a;  b = _b; }
4、设计并测试一个椭圆类(20分) 题目内容:
设计并测试一个名为Ellipse的椭圆类,其属性为圆心坐标及长半轴和短半轴的长度。设计一个构造函数(Ellipse(int,int,double,double))对这些属性进行初始化,并通过成员函数计算出椭圆的面积(double Area())。
S(椭圆面积)=PI(圆周率)×a(长半轴)×b(短半轴)其中PI取3.14 #include #include  using namespace std; class Ellipse {public:  Ellipse() :x(0), y(0), a(0), b(0) {}  Ellipse(int, int, double, double);  inline double Area() { return 3.14 * a * b; } private:  int x, y;  double a, b; };
int main() {  int x, y;  double a, b, s;  cin >> x >> y >> a >> b;  Ellipse *e = new Ellipse(x,y,a,b);  s = e->Area();  cout << s << endl;  delete e;  return 0; }
Ellipse::Ellipse(int _x, int _y, double _a, double _b) {  x = _x;  y = _y;  a = _a;  b = _b; }
5、设计一个多功能的MyTime类(20分) 题目内容:
设计一个多功能的MyTime类,设计多个重载的构造函数,可以设置时间,进行时间的加减运算,按各种可能的格式(24小时制、12小时制)输出时间。 注意:
(1)请考虑设置的时间的合理性(时0-23; 分0-59;秒0-59)。 (2)12小时制中,12:00:00前为AM, 12:00:00及以后为PM
(3)加减运算的加数、减数是一个时间的长度,单位为“时、分、秒”
(4)构造函数:没参数时,设置时间为0时 0分 0秒;有参数时,设置成给定的时、分、秒。  在主函数中
(1)声明两个对象t1,t2,并通过构造函数初始化它们(t2初始化为为8:10:30) (2)显示按12、14小时制显示t1、t2的时间。 (3)再设置t1的时间,数据由用户输入。 (4)再输入待加减的时间。
(5)t1加输入的时间,并按12小时和24小时制显示。 (6)t2减输入的时间,并按12小时和24小时制显示。 #include #include  using namespace std; class MyTime {public:  MyTime() :hour(0), minute(0), second(0) {}  MyTime(int, int, int);  inline void SetTime(int h, int m, int s) { hour = h; minute = m; second = s; }  void AddTime(int h, int m, int s);  void SubTime(int h, int m, int s);  void Show12();  void Show24(); private:  int hour, minute, second; };
int main() {  MyTime *t1 = new MyTime;  MyTime *t2 = new MyTime(8, 10, 30);  int h1, h2, m1, m2, s1, s2;  cin >> h1 >> m1 >> s1;  cin >> h2 >> m2 >> s2;  t1->Show12();  t1->Show24();  t2->Show12();  t2->Show24();  t1->SetTime(h1, m1, s1);  t1->AddTime(h2, m2, s2);  t2->SubTime(h2, m2, s2);  t1->Show12();  t1->Show24();  t2->Show12();  t2->Show24();  delete t1;  delete t2;  return 0; }
MyTime::MyTime(int h, int m, int s) {  hour = h;  minute = m;  second = s; }
void MyTime::AddTime(int h, int m, int s) {
hour += h;  minute += m;  second += s;  if (second >= 60)  {   second -= 60;   minute += 1;  }  if (minute >= 60)  {   minute -= 60;   hour += 1;  }  if (hour >= 24)   hour -= 24; }
void MyTime::SubTime(int h, int m, int s) {  if (second < s)  {   second += 60;   minute--;  }  if (minute < m)  {   minute += 60;   hour--;  }  if (hour < h)   hour += 24;  hour -= h;  minute -= m;  second -= s; }
void MyTime::Show12() {  int tmp;  hour >= 12 ? tmp = (hour - 12) : tmp = hour;  if (tmp < 10)   cout << \"0\";  cout << tmp << \":\";  if (minute < 10)   cout << \"0\";  cout << minute << \":\";  if (second < 10)
cout << '0';  cout << second;  if (hour > 12 || ((hour == 12) && ((minute > 0) || (second > 0))))   cout << \" PM\" << endl;  else   cout << \" AM\" << endl; }
void MyTime::Show24() {  if (hour < 10)   cout << '0';  cout << hour << \":\";  if (minute < 10)   cout << '0';  cout << minute << \":\";  if (second < 10)   cout << '0';  cout << second << endl; }
十一周目
1、公有继承中派生类Student对基类Person成员的访问(20分) 题目内容:
已知基类Person的定义如下: class Person { char Name[20]; char Sex; int Age; public:
void Register(char *name, int age, char sex) ; void ShowMe(); };
请通过继承的方法建立一个派生类Student,其中 1.新增的数据成员有: int Number;
char ClassName[10]; 2.新增的成员函数有:
void RegisterStu(char *classname, int number, char *name, int age, char sex)  //对数据成员赋值,并使用基类的Register
void ShowStu() //显示数据成员信息,并使用基类的ShowMe
在主程序中建立一个派生类对象,利用已有的成员函数分别显示派生类对象和基类对象的数据成员。 #include  #include  using namespace std; class Person{
public:  void Register(char* name, int age, char sex);  inline void ShowMe() { cout << Name << ' ' << Age << ' ' << Sex << endl; } private:  char Name[20];  char Sex;  int Age; };
class Student :public Person {
public:  void RegisterStu(char* classname, int number, char* name, int age, char sex);  inline void ShowStu()  {   cout << Number << ' ' << ClassName << ' ';   Person::ShowMe();  } private:  int Number;  char ClassName[10]; };
int main() {  char name[20], classname[10];  char sex;  int age, number;  Student* stu1 = new Student;  cin >> classname >> number >> name >> age >> sex;  stu1->RegisterStu(classname, number, name, age, sex);  stu1->ShowStu();  stu1->ShowMe();  delete stu1;  return 0; }
void Person::Register(char* name, int age, char sex) {  strcpy(Name, name);  Age = age;  Sex = sex; }
void Student::RegisterStu(char* classname, int number, char* name, int age, char sex) {  Person::Register(name, age, sex);  strcpy(ClassName, classname);  Number = number; }
2、一个基类Person的多个派生类(20分) 题目内容:
已知基类Person的定义如下: class Person {
protected:
char Name[10]; char Sex; int Age; public:
void Register(char *name,int age,char sex); void ShowMe(); };
请通过继承的方法建立两个派生类,其中 派生类Teacher:
1.新增的数据成员有: char Dept[20]; int Salary;
2.新增的成员函数有:
构造函数,并使用基类的Register 3.重写的成员函数有:
void ShowMe() //显示数据成员信息,并使用基类的ShowMe 派生类Student:
1.新增的数据成员有: char ID[12]; char Class[12];
2.新增的成员函数有:
Student(char *name,int age,char sex, char *id,char *classid); 3.重写的成员函数有:
void ShowMe() //显示数据成员信息,并使用基类的ShowMe
在主程序中分别建立两个派生类对象,利用已有的成员函数分别显示两个派生类对象的数据成员。 #include  #include  using namespace std; class Person {public:  inline void Register(char* name, int age, char sex)  {   strcpy(Name, name);   Age = age;   Sex = sex;  }  inline void ShowMe()  {   cout << \"姓名 \" << Name << endl;   cout << \"性别 \";
if (Sex == 'm')    cout << \"男\" << endl;   else    cout << \"女\" << endl;   cout << \"年龄 \" << Age << endl;  } private:  char Name[10];  char Sex;  int Age; };
class Teacher :public Person {
public:  Teacher(char* name, int age, char sex, char* dept, int salary)  {   Person::Register(name, age, sex);   strcpy(Dept, dept);   Salary = salary;  }  void ShowMe()  {   Person::ShowMe();   cout << \"工作单位 \" << Dept << endl;   cout << \"月薪 \" << Salary << endl;  } private:  char Dept[20];  int  Salary; };
class Student :public Person {
public:  Student(char* name, int age, char sex, char* id, char* classid)  {   Person::Register(name, age, sex);   strcpy(ID, id);   strcpy(Class, classid);  }  void ShowMe()  {   cout << \"学号 \" << ID << endl;   Person::ShowMe();   cout << \"班级 \" << Class << endl;  } private:  char ID[12];
char Class[12]; };
int main() {  char name1[10], name2[10], dept[20], id[12], classid[12];  char sex1, sex2;  int age1, age2, salay;  cin >> name1 >> age1 >> sex1 >> dept >> salay;  cin >> name2 >> age2 >> sex2 >> id >> classid;  Teacher* tea = new Teacher(name1, age1, sex1, dept, salay);  tea->ShowMe();  Student* stu = new Student(name2, age2, sex2, id, classid);  stu->ShowMe();  delete tea;  delete stu;  return 0; }
3、派生类Student的构造函数和析构函数(20分) 题目内容:
已知基类Person的定义如下: class Person
{ char Name[10]; //姓名 int Age; //年龄 public:
Person(char* name,int age) { strcpy(Name, name); Age = age;
cout<<\"constructor of person \"<{ cout<<\"deconstructor of person \"<Student(char *name, int age, char *classname, char *name1, int age1) //name1和age1是班长的信息 ~Student()在主程序中建立一个派生类对象。 #include  #include  using namespace std; class Person {public:  Person(char* name, int age)  {   strcpy(Name, name);
Age = age;   cout << \"constructor of person \" << Name << endl;  }  ~Person() { cout << \"deconstructor of person \" << Name << endl; } private:  char Name[10];  int Age; };
class Student :public Person {
public:  Student(char* name, int age, char* classname, char* name1, int age1) :   Person(name, age), Monitor(name1, age1)  {   strcpy(ClassName, classname);   cout << \"constructor of Student\" << endl;  }  ~Student() { cout << \"deconstructor of Student\" << endl; } private:  char ClassName[10];  Person Monitor; };
int main() {  char name[10], name1[10], classname[10];  int age, age1;  cin >> name >> age >> classname >> name1 >> age1;  Student* stu = new Student(name, age, classname, name1, age1);  delete stu;  return 0; }
4、从Point类继承的Circle类(20分) 题目内容:
已知基类Point的定义如下:  class Point {
int x, y; //点的x和y坐标
public: Point( int = 0, int = 0 ); // 构造函数  void SetPoint( int, int ); // 设置坐标  int GetX() { return x; } // 取x坐标  int GetY() { return y; } // 取y坐标  void Print(); //输出点的坐标 };
Point( int a, int b ) { SetPoint( a, b ); }
void SetPoint( int a, int b ) { x = a; y = b; }
void Print() { cout << \"[\" << x << \ }
请通过继承的方法建立一个派生类Circle,其中
1.新增的数据成员有: double radius;  2.新增的成员函数有:
Circle(int x = 0, int y = 0 , double r = 0.0); //对数据成员赋值,并使用SetRadius和基类的Point  void SetRadius( double ); //设置半径  double GetRadius(); //取半径  double Area(); //计算面积
void Print(); //输出圆心坐标和半径,并使用基类的Print
在主程序中分别建立基类对象和派生类对象,使用用户输入的初值分别对基类对象和派生类对象的数据成员赋值后,利用已有的成员函数分别显示基类对象和派生类对象的数据成员信息。 圆周率取3.14。  #include  using namespace std; const double PI = 3.14; class Point {public:  Point(int a, int b) { SetPoint(a, b); }  void SetPoint(int a, int b) { x = a; y = b; }  int GetX() { return x; }  int GetY() { return y; }  void Print() { cout << \"[\" << x << \private:  int x, y; };
class Circle :public Point {
public:  Circle(int x = 0, int y = 0, double r = 0.0);  void SetRadius(double r) { radius = r; }  double GetRadius() { return radius; }  double Area();  void Print(); private:  double radius; };
int main() {  int x1, x2, y1, y2;  double r;  cin >> x1 >> y1;  cin >> x2 >> y2 >> r;  Point myPoint(x1, y1);  Circle myCircle(x2, y2, r);  cout << \"Point p \";  myPoint.Print();  cout << endl;  myCircle.Print();
myCircle.Area();  return 0; }
Circle::Circle(int x, int y, double r) :Point(x, y) {  SetRadius(r); }
double Circle::Area() {  cout << \"The center of circle c \";  //注意:输入样例的center单词是写错的  Point::Print();  cout << \"\\nThe area of circle c \";  cout << PI*radius*radius << endl;  return PI*radius*radius; }
void Circle::Print() {  cout << \"Circle c Center=\";  Point::Print();  cout << \"\\nRadius=\" << radius << endl; }
5、从Student类和Teacher类多重派生Graduate类(20分) 题目内容:
已知基类Person定义如下: class Person {
char Name[10]; char Sex[10]; int Age; public:
void Register(char *name,int age,char *sex); void ShowMe(); };
请通过继承的方法建立两个派生类,其中 派生类Teacher:
1.新增的数据成员有: char Dept[20]; int Salary;
2.新增的成员函数有:
Teacher(char *name,int age,char *sex,char *dept,int salary); void Show() //显示新增数据成员 派生类Student:
1.新增的数据成员有: char ID[12]; char Class[12];
2.新增的成员函数有:
Student(char *name,int age,char *sex,char *ID,char *Class);
void Show()//显示新增数据成员
请通过继承的方法从Teacher和Student中建立派生类Graduate,其中 1.新增的成员函数有:
Graduate(char *name,int age,char *sex,char *dept,int salary,char *id,char *classid); 2.重写的成员函数有:
void ShowMe()//显示数据成员,要求调用基类的Show和ShowMe
在主程序中建立一个派生类Graduate的对象,利用成员函数显示对象的数据成员。 #include  #include  using namespace std; class Person {public:  inline void Register(char* name, int age, char* sex)  {   strcpy(Name, name);   Age = age;   strcpy(Sex, sex);  }  inline void ShowMe()  {    cout << \"姓名 \" << Name;   if (Sex[0] == 'f')    cout << \"\\n性别 女\";   else    cout << \"\\n性别 男\";   cout << \"\\n年龄 \" << Age << endl;  } private:  char Name[10];  char Sex[10];  int Age; };
class Teacher :public Person {
public:  Teacher(char *name, int age, char *sex, char* dept, int salary)  {   Person::Register(name, age, sex);   strcpy(Dept, dept);   Salary = salary;  }  inline void Show() { cout << \"工作单位 \" << Dept << \"\\n月薪 \" << Salary << endl; } private:  char Dept[20];  int Salary; };
class Student :public Person {
public:  Student(char* name, int age, char* sex, char* id, char* classid)  {   Person::Register(name, age, sex);   strcpy(ID, id);   strcpy(Class, classid);  }  void Show()  {   cout << \"班级 \" << Class << \"\\n学号 \" << ID << endl;  } private:  char ID[12];  char Class[12]; };
class Graduate :public Teacher, public Student {
public:  Graduate(char* name, int age, char* sex, char* dept, int salary, char* id, char* classid) :   Student(name, age, sex, id, classid), Teacher(name, age, sex, dept, salary) {}  void ShowMe()  {   Student::Show();   Student::ShowMe();   Teacher::Show();  } };
int main() {  char name[10], sex[10], dept[20], id[12], classid[12];  int age, salary;  cin >> name >> age >> sex >> dept >> salary >> id >> classid;  Graduate *gra = new Graduate(name, age, sex, dept, salary, id, classid);  gra->ShowMe();  delete gra;  return 0; }
十二周目
1、虚函数实现多态性(20分) 题目内容:
定义宠物类Pet,包含虚函数Speak,显示如下信息“How does a pet speak?”;定义公有派生类Cat和Dog,其Speak成员函数分别显示:“miao! miao!”和“wang! wang!”。主函数中定义Pet,Cat和Dog对象,再定义Pet指针变量,分别指向Pet,Cat和Dog对象,并通过指针调用Speak函数,观察并分析输出结果。
#include  using namespace std; class Pet {public:  Pet() {}  ~Pet() {}  virtual void Speak() { cout << \"How does a pet speak?\" << endl; } };
class Cat :public Pet {
public:  Cat() {}  ~Cat() {}  virtual void Speak() { cout << \"miao!miao!\" << endl; } };
class Dog :public Pet {
public:  Dog() {}  ~Dog() {}  virtual void Speak() { cout << \"wang!wang!\" << endl; } };
int main() {  Pet pet, *pt;  Cat cat;  Dog dog;  pt = &pet;  pt->Speak();  pt = &cat;  pt->Speak();  pt = &dog;  pt->Speak();  return 0; }
2、抽象宠物类的实现(20分) 题目内容:
定义抽象宠物类Pet,其中数据成员包括:名字,年龄和颜色;成员函数包括:构造函数;获取成员数据值的函数;纯虚函数Speak和纯虚函数GetInfo;
定义Pet的派生类Cat和Dog,其中Speak函数分别显示猫和狗的叫声,而GetInfo函数分别输出Cat和Dog的属性。主函数中定义Pet指针变量,分别指向动态生成的Cat和Dog对象,并通过指针分别调用GetInfo函数和Speak函数,观察并分析输出结果。 #include  #include  using namespace std; class Pet{
public:  Pet(char* name, int age, char* color)   {   strcpy(Name, name);   Age = age;   strcpy(Color, color);  }  ~Pet() {}  virtual void Speak() = 0;  virtual void GetInfo() = 0; protected:  char Name[10];  int Age;  char Color[10]; };
class Cat :public Pet {
public:  Cat(char* name, int age, char* color) :Pet(name, age, color) {}  ~Cat() {}  virtual void Speak() { cout << \"猫的叫声:miao!miao!\" << endl; }  virtual void GetInfo()  {   cout << \"猫的名字:\" << Name << endl;   cout << \"猫的年龄:\" << Age << endl;   cout << \"猫的颜色:\" << Color << endl;  } };
class Dog :public Pet {
public:  Dog(char* name, int age, char* color) :Pet(name, age, color) {}  ~Dog() {}  virtual void Speak() { cout << \"狗的叫声:wang!wang!\" << endl; }  virtual void GetInfo()  {   cout << \"狗的名字:\" << Name << endl;   cout << \"狗的年龄:\" << Age << endl;   cout << \"狗的颜色:\" << Color << endl;  } };
int main() {  char name[10], color[10];  int age;  cin >> name >> age >> color;
Cat* cat = new Cat(name, age, color);  cin >> name >> age >> color;  Dog* dog = new Dog(name, age, color);  cat->GetInfo();  cat->Speak();  dog->GetInfo();  dog->Speak();  return 0; }
3、重载加法运算符的复数运算(20分) 题目内容:
定义一个复数类,并重载加法运算符(+)和赋值运算符(=)以适用对复数运算的要求。 #include  using namespace std; class Complex {public:  Complex(double Real = 0, double Img = 0) { this->Real = Real; this->Img = Img; }  ~Complex() {}  Complex operator +(Complex& x)  {   Complex* temp = new Complex;   temp->Real = Real + x.Real;   temp->Img = Img + x.Img;   return *temp;  }  void Show()  {   cout << Real;   if (Img >= 0)    cout << \"+j\" << Img << endl;   else    cout << \"-j\" << -Img << endl;  } private:  double Real;  double Img; };
int main() {  double a, b, c, d;  cin >> a >> b >> c >> d;  Complex test1(a, b);  Complex test2(c, d);  test1.Show();  test2.Show();  Complex* c1 = new Complex(test1 + test2);
c1->Show();  delete c1;  return 0; }
4、重载矩阵加法运算(20分) 题目内容:
编写一个矩阵类,重载矩阵加法运算。设A,B,C均为m行,n列的矩阵,要求程序能实现C=A+B的操作。
提示:由于涉及深浅拷贝的问题,不建议使用动态数组。 #include  using namespace std; class Matrix {public:  Matrix() :Row(0), Column(0), Index(0) {}  Matrix(int row, int column)  {   Row = row;   Column = column;   Data = new double[row*column];  }  void InitData()  {   for (int i = 0; i < Row*Column; ++i)    cin >> Data[i];  }  void Show()  {   Index = 0;   for (int i = 0; i < Row; ++i)   {    for (int j = 0; j < Column; ++j)    {     if (j != Column - 1)      cout << Data[Index] << ' ';     else      cout << Data[Index] << endl;     Index++;    }   }  }  Matrix operator +(const Matrix& mat)  {   static Matrix temp(Row, Column);   for (int i = 0; i < Row*Column; ++i)    temp.Data[i] = Data[i] + mat.Data[i];   return temp;
}  ~Matrix() {} private:  int Row, Column, Index;  double* Data; };
int main() {  int row, col;  cin >> row >> col;  Matrix mat1(row, col);  Matrix mat2(row, col);  mat1.InitData();  mat2.InitData();  Matrix* mat3 = new Matrix(mat1 + mat2);  mat3->Show();  delete mat3;  return 0; }
5、纯虚函数与基类指针数组的应用(20分) 题目内容:
定义抽象基类Shape,
其中纯虚函数printName()输出几何图形的名称和相应的成员数据、纯虚函数printArea()计算几何图形的面积。并由Shape类派生出5个派生类:Circle(圆形),数据成员为半径、Square(正方形)
,数据成员为边长、Rectangle(长方形) ,数据成员为长和宽、Trapezoid(梯形) ,数据成员为上底、下底和高、Triangle(三角形)
,数据成员为底和高。测试过程,定义一个指向基类的指针数组,使其每个元素指向一个动态产生的派生类对象,分别调用相应的成员函数显示各个几何图形的属性及面积,最终输出总面积值。 #include  using namespace std;const double PI = 3.14159; class Shape {
public:  Shape() {}  ~Shape() {}  virtual void printName() = 0;  virtual void printArea() = 0;  virtual double area() = 0; };
class Circle :public Shape {
public:  Circle(double radius) { m_radius = radius; }  virtual void printName() { cout << \"圆:半径=\" << m_radius << \ virtual void printArea()  {
double area;   area = m_radius*m_radius*PI;   cout << \"面积:\" << area << endl;  }  double area() { return m_radius*m_radius*PI; }  ~Circle() {} private:  double m_radius; };
class Square :public Shape {
public:  Square(double side) { m_side = side; }  virtual void printName() { cout << \"正方形:边长=\" << m_side << \ virtual void printArea()  {   double area = m_side*m_side;   cout << \"面积:\" << area << endl;  }  double area() { return m_side*m_side; }  ~Square() {} private:  double m_side; };
class Rectangle :public Shape {
public:  Rectangle(double width, double height)  {   m_width = width;   m_height = height;  }  virtual void printName() { cout << \"长方形:长=\" << m_height << \宽=\" << m_width << \ virtual void printArea()  {   double area = m_width*m_height;   cout << \"面积:\" << area << endl;  }  double area() { return m_height*m_width; }  ~Rectangle() {} private:  double m_width, m_height; };
class Trapezoid :public Shape {
public:  Trapezoid(double upside, double downside, double height)
{   m_upside = upside;   m_downside = downside;   m_height = height;  }  virtual void printName() { cout << \"梯形:上底=\" << m_upside << \下底=\" << m_downside << \高=\" << m_height << \ virtual void printArea()  {   double area = (m_upside + m_downside) / 2 * m_height;   cout << \"面积:\" << area << endl;  }  double area() { return (m_upside + m_downside) / 2 * m_height; }  ~Trapezoid() {} private:  double m_upside, m_downside, m_height; };
class Triangle :public Shape {
public:  Triangle(double downside, double height)  {   m_downside = downside;   m_height = height;  }  virtual void printName() { cout << \"三角形:底边=\" << m_downside << \高=\" << m_height << \ virtual void printArea()  {   double area = (m_downside*m_height) / 2;   cout << \"面积:\" << area << endl;  }  double area() { return (m_downside*m_height) / 2; }  ~Triangle() {} private:  double m_downside, m_height; };
int main() {  double Area = 0;  double cirRadius, squSide, rectHeight, rectWidth, traUpside, traDownside, traHeight, triHeight, triDownside;
cin >> cirRadius >> squSide >> rectHeight >> rectWidth >> traUpside >> traDownside >> traHeight >> triHeight >> triDownside;  Shape* arr[5];  arr[0] = new Circle(cirRadius);  arr[1] = new Square(squSide);  arr[2] = new Rectangle(rectWidth, rectHeight);
}
arr[3] = new Trapezoid(traUpside, traDownside, traHeight); arr[4] = new Triangle(triHeight, triDownside); for (int i = 0; i < 5; ++i) {  arr[i]->printName();  arr[i]->printArea();  Area += arr[i]->area(); }
cout << \"总面积:\" << Area << endl; return 0;
十三周目
1、计算某个正整数平方根,并按要求输出(20分) 题目内容:
输入一个正整数。计算其平方根(用sqrt函数),并将结果按取1~6位小数分六行显示出来。 #include  #include  int main() {  int number;  std::cin >> number;  double resualt = sqrt(number);  std::cout.setf(std::ios::showpoint | std::ios::fixed);  for (int i = 1; i < 7; ++i)  {   std::cout.precision(i);   std::cout << resualt << std::endl;  }  return 0; }2、读取文件,添加行号显示(20分) 题目内容:
输入5行信息,生成文件A.txt。然后再次打开该文件,为每一行前面加一个行号后显示在屏幕上,行号占据4个字符宽,行号左对齐显示。 #include  #include  #include  using namespace std; int main() {  int n = 5;  char fix = ' ';  char str[5][80];  for (int i = 0; i < 5; ++i)   cin.getline(str[i], 80);cout.unsetf(ios::left);  for (int i = 0; i < n; ++i)   cout << i + 1  << setw(3) << cout.fill(fix) << str[i] << endl;   //ifstream in(\"A.txt\ //if (!in)  //{  // cerr << \"read failed.\" << endl;  // return EXIT_FAILURE;  //}  //for (int i = 0; i < 5; ++i)  // in.getline(str[i], 50);  //in.close();  //int j = 1;  //ofstream out(\"A1.txt\");  //if (!out)  //{  // cerr << \"write failed.\" << endl;  // return EXIT_FAILURE;  //}  //for (int i = 0; i < 5; ++i)  //{  // out << j << \"   \" << str[i] << endl;  // j++;  //}  //out.close();  return 0; }
3、读写文件并转换字符(20分) 题目内容: 编写一个程序,从键盘输入一行字符(可包含各种字符,遇回车符结束),写入到文件a1.txt中;再从a1.txt中读出文件内容,将其中的小写字母转换成大写字母后显示在屏幕上。
提示:输入带空格的字符串,用cin.getline()。注:提交时,注释文件操作语句。 #include  #include  #include  #include  using namespace std; int main() {  string str;  //fstream outFile;  getline(cin, str);  //outFile.open(\"a1.txt\"); //fstream的对象如果找不到目标文件不会像ofstream对象那样创建一个  //if (outFile.fail())  //{  // system(\"@echo File not found.creating file.\");  // system(\"@echo > a1.txt\");// system(\"@echo File has been created.\");  // system(\"@echo off\");  // cerr << \"File has been created, but can't write. Please run again.\";  // system(\"pause\");  // return EXIT_FAILURE;  //}  transform(str.begin(), str.end(), str.begin(), ::toupper);  //outFile << str << endl;  cout << str << endl;  //outFile.close();  return 0; }
4、读文件中的数字,算平均值(20分) 题目内容:
输入n个数字(实数),将他们写入文件out1.txt,数字之间用空格分开。然后再次打开该文件,读出全部数字,计算他们平均值并输出在屏幕上。 #include  #include  using namespace std; int main() {  int num;  double sum = 0.0, sum1 = 0.0;  cin >> num;  double *p = new double[num];  for (int i = 0; i < num; ++i)  {   cin >> p[i];   sum += p[i];  }  //ofstream out(\"out1.txt\"); //将输入的信息保存到out1.txt文件中  //for (int i = 0; i < num; ++i)  // out << p[i] << \" \";  //out.close();  //ifstream in(\"out1.txt\//以只读的方式从out1.txt中读取数据  //for (int i = 0; i < num; ++i)  //{  // in >> p[i];  // sum1 += p[i];  //}  //in.close();  cout << \"Avg=\" << sum / num << endl;  delete[] p;  return 0; }5、读文件中的字符并排序输出(20分) 题目内容:
输入n个字符写入文本文件A.txt,字符间用空格分开。在打开该文件,读取所有字符并排序后,按从小到大顺序写入B.txt(字符间用空格分开),同时将文件B的内容显示在屏幕上。 #include  #include  using namespace std; int main() {  //ofstream out(\"A.txt\");  int num;  cin >> num;  char *p = new char[num];  for (int i = 0; i < num; ++i)  {   cin >> p[i];   //out << p[i];  }  //out.close();  for (int i = 0; i < num - 1; ++i)  {   int nFlag = i;   for (int j = i + 1; j < num; ++j)   {    if (p[j] < p[nFlag])     nFlag = j;   }   swap(p[i], p[nFlag]);  }  //ofstream out2(\"B.txt\");  for (int i = 0; i < num; ++i)  {   if (i == num - 1)   {    cout << p[i] << endl;    //out2 << p[i];   }   else   {    cout << p[i] << \" \";    //out2 << p[i] << \" \";   }  }  //out2.close();  return 0; }