本博客内容为中国大学生MOOC国家精品课程《计算机程序设计C++》作业记录,仅供参考,观者忌照搬照抄,欢迎交流批评指正!
##第九周编程作业
本周作业内容类的设计
- 设计Person类
#include "iostream"
#include "cstring"
using namespace std;
class Person
{
private:
char name[20];
int age;
char sex;
public:
void Register(char *a,int b,char c);
void ShowMe();
};
void Person::Register(char *a, int b, char c)
{
strcpy(name, a);
age = b;
sex = c;
}
void Person::ShowMe()
{
cout << name << " " << age << " " << sex << endl;
}
int main()
{
Person person1, person2;
char name[20], sex;
int age;
cin >> name >> age >> sex;
person1.Register(name, age, sex);
person2.Register("Zhang3", 19, 'm');
person1.ShowMe();
person2.ShowMe();
return 0;
}
- 设计Dog类
#include "iostream"
#include "cstring"
using namespace std;
class Dog
{
private:
char name[20],sex;
int age;
double weight;
public:
void setdata(char *a, int b, char c, double d);
char *GetName(){ return name;}
int GetAge(){ return age; }
char GetSex(){ return sex; }
double GetWeight(){ return weight; }
void ShowInformation();
};
void Dog::setdata(char *a, int b, char c, double d)
{
strcpy(name, a);
age = b;
sex = c;
weight = d;
}
void Dog::ShowInformation()
{
cout << "It is my dog." << endl;
cout << "Its name is " << name << "." << endl;
cout << "It is " << age << " years old." << endl;
if (sex == 'm') cout << "It is male." << endl;
else cout << "It is female." << endl;
cout << "It is "<<weight<<" kg." << endl;
}
int main()
{
Dog dog;
char name[20], sex;
int age;
double weight;
cin >> name >> age >> sex >> weight;
dog.setdata(name, age, sex, weight);
dog.ShowInformation();
return 0;
}
- 设计并测试Trapezium类
#include "iostream"
using namespace std;
class Trapezium
{
private:
int x1, y1, x2, y2, x3, y3, x4, y4;
public:
void initial(int, int, int, int, int, int, int, int);
void GetPosition(int&, int&, int&, int&, int&, int&, int&, int&);
double Area();
};
void Trapezium::initial(int a, int b, int c, int d, int e, int f, int g, int h)
{
if (b == d&&f == h)
{
x1 = a; y1 = b; x2 = c; y2 = d; x3 = e; y3 = f; x4 = g; y4 = h;
}
else
{
cout << "输入有误" << endl;
}
}
void Trapezium::GetPosition(int &a, int &b, int &c, int &d, int &e, int &f, int &g, int &h)
{
a = x1; b = y1; c = x2; d = y2; e = x3; f = y3; g = x4; h = y4;
}
double Trapezium::Area()
{
return ((x2 - x1) + (x4 - x3))*(y1 - y3) / 2.0;
}
int main()
{
Trapezium t;
int a, b, c, d, e, f, g, h;
cin >> a >> b >> c >> d >> e >> f >> g >> h;
t.initial(a, b, c, d, e, f, g, h);
cout << t.Area() << endl;
return 0;
}
- 设计MyTime类
#include "iostream"
using namespace std;
class MyTime
{
private:
int hour, minute, second;
public:
void SetTime(int, int, int);
void print_12();
void print_24();
};
void MyTime::SetTime(int a,int b,int c)
{
hour = (a >= 0 && a < 24) ? a : 0;
minute = (b >= 0 && b < 60) ? b : 0;
second = (c >= 0 && c < 60) ? c : 0;
}
void MyTime::print_12()
{
if (hour<12)
printf("%02d:%02d:%02d AM\n",hour,minute,second);
else
printf("%02d:%02d:%02d PM\n", hour-12, minute, second);
}
void MyTime::print_24()
{
printf("%02d:%02d:%02d\n", hour, minute, second);
}
int main()
{
MyTime t;
int a, b, c;
cin >> a >> b >> c;
t.SetTime(a, b, c);
t.print_12();
t.print_24();
return 0;
}
- 设计Weekday类
#include "iostream"
using namespace std;
class Weekday
{
private:
int daynumber;
public:
void SetDay(int);
void IncDay();
void NowDay();
};
void Weekday::SetDay(int n)
{
daynumber = (n >= 0 && n < 7) ? n:0;
}
void Weekday::IncDay()
{
daynumber = (daynumber + 1 < 7) ? daynumber + 1 : daynumber + 1 - 7;
}
void Weekday::NowDay()
{
switch (daynumber)
{
case 0:
cout << "星期日" << endl; return;
case 1:
cout << "星期一" << endl; return;
case 2:
cout << "星期二" << endl; return;
case 3:
cout << "星期三" << endl; return;
case 4:
cout << "星期四" << endl; return;
case 5:
cout << "星期五" << endl; return;
case 6:
cout << "星期六" << endl; return;
}
}
int main()
{
Weekday w;
int n;
cin >> n;
w.SetDay(n);
w.NowDay();
w.IncDay();
w.NowDay();
w.IncDay();
w.NowDay();
return 0;
}
以上为第9周编程作业。