南大C++:《程序设计教程 用C++语言编写》答案(八)
这些是我自己写的代码,全部通过编译。
6.8.15 定义一个日期类Date,它能表示年、月、日。为其设计一个成员函数increment,它能把某个日期增加一天。
/*
P202 6.8.15
定义一个日期类Date,它能表示年、月、日。为其设计一个成员函数increment,它能把某个日期增加一天。
*/
#include <iostream>
using namespace std;
class Date
{
int y, m, d;
public:
Date(int ty,int tm,int td)
{
y = ty;
m = tm;
d = td;
}
void increment()
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d == 31)
{
d = 1;
if (m == 12)
{
y++;
m = 1;
}
else
m++;
}
else
d++;
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d == 30)
{
d = 1;
m++;
}
else
d++;
}
else
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
{
if (d == 29)
{
d = 1;
m++;
}
else
d++;
}
else
{
if (d == 28)
{
d = 1;
m++;
}
else
d++;
}
}
}
void display()
{
cout << y << "年" << m << "月" << d << "日" << endl;
}
};
int main()
{
int ty, tm, td;
cout << "输入一个日期(年 月 日):";
cin >> ty >> tm >> td;
Date d(ty, tm, td);
for (int i = 1; i <= 10000; i++)
{
cout << "增加一天:";
d.increment();
d.display();
}
}
6.8.17 定义一个元素类型为int、元素个数不受限制的集合类IntSet,该类具有下面的接口:
class IntSet{
...
public:
IntSet();
IntSet(const IntSet& s);
~IntSet();
bool is empty() const;//判断是否为空集
int size() const;//获取元素个数
bool is_element(int e) const;//判断e是否包含于集合
bool is_subset(const IntSet& s) const;//判断s是否包含于集合
bool is_equal(const IntSet& s) const;//判断集合是否相等
void display() const;//显示集合中的所有元素
IntSet& insert(int e);//将e加入到集合中
IntSet& remove(int e);//将e从集合中删除
IntSet Union2(const IntSet& s) const;//计算集合的并集
IntSet intersection2(const IntSet& s) const;//计算集合的交集
IntSet difference2(const IntSet& s) const;//计算集合的差
/*
P202 6.8.17
定义一个元素类型为int、元素个数不受限制的集合类IntSet,该类具有下面的接口:
class IntSet{
...
public:
IntSet();
IntSet(const IntSet& s);
~IntSet();
bool is empty() const;//判断是否为空集
int size() const;//获取元素个数
bool is_element(int e) const;//判断e是否包含于集合
bool is_subset(const IntSet& s) const;//判断s是否包含于集合
bool is_equal(const IntSet& s) const;//判断集合是否相等
void display() const;//显示集合中的所有元素
IntSet& insert(int e);//将e加入到集合中
IntSet& remove(int e);//将e从集合中删除
IntSet Union2(const IntSet& s) const;//计算集合的并集
IntSet intersection2(const IntSet& s) const;//计算集合的交集
IntSet difference2(const IntSet& s) const;//计算集合的差
*/
#include <iostream>
using namespace std;
struct Node
{
int val;
Node *next;
};
class IntSet
{
Node *head;
public:
IntSet()
{
head = NULL;
}
IntSet(const IntSet &s)
{
Node *p = s.head;
head = NULL;
while (p!= NULL)
{
insert(p->val);
p = p->next;
}
}
~IntSet()
{
Node *p;
while (head != NULL)
{
p = head;
head = head->next;
delete p;
}
}
bool is_empty() const
{
if (head == NULL)
return true;
return false;
}
int size() const
{
Node *p = head;
int c = 0;
while (p != NULL)
{
c++;
p = p->next;
}
return c;
}
bool is_element(int e) const
{
Node *p = head;
while (p != NULL)
{
if (p->val == e)
return true;
p = p->next;
}
return false;
}
bool is_subset(const IntSet &s) const
{
Node *p = s.head;
while (p != NULL)
{
if (!is_element(p->val))
return false;
p = p->next;
}
return true;
}
bool is_equal(const IntSet &s) const
{
if (is_subset(s) && s.is_subset(*this))
return true;
return false;
}
void display() const
{
Node *p = head;
while (p != NULL)
{
cout << p->val << ' ';
p = p->next;
}
cout << endl;
}
IntSet &insert(int e)
{
if (!is_element(e))
{
Node *p = new Node;
p->val = e;
p->next = head;
head = p;
}
return *this;
}
IntSet &remove(int e)
{
Node *p = head;
if (head->val == e)
{
p = head;
head = head->next;
delete p;
}
else
{
while (p->next!= NULL)
{
if (p->next->val == e)
{
Node *q = p->next;
p->next = q->next;
delete q;
break;
}
}
}
return *this;
}
IntSet union2(const IntSet &s) const
{
IntSet a(s);
Node *p = head;
while (p != NULL)
{
if (!a.is_element(p->val))
a.insert(p->val);
p = p->next;
}
return a;
}
IntSet intersection2(const IntSet &s) const
{
IntSet a;
Node *p = head;
while (p != NULL)
{
if (s.is_element(p->val))
a.insert(p->val);
p = p->next;
}
return a;
}
IntSet difference2(const IntSet &s) const
{
IntSet a;
Node *p = head;
while (p != NULL)
{
if (!s.is_element(p->val))
a.insert(p->val);
p = p->next;
}
return a;
}
};
int main()
{
int t;
IntSet a;
cout << "输入集合1元素(以-1结尾):";
while (1)
{
cin >> t;
if (t != -1)
a.insert(t);
else
break;
}
IntSet b;
cout << "输入集合2元素(以-1结尾):";
while (1)
{
cin >> t;
if (t != -1)
b.insert(t);
else
break;
}
a.display();
b.display();
cout << a.size() << endl;
cout << b.size() << endl;
}
6.8.18 定义一个由int型元素所构成的线性表类LinearList,它有下面的成员函数:
- bool insert(int x,int pos);//在位置pos之后插入一个元素x,pos为0时。在第一个元素之前插入
操作成功返回true,否则返回false - bool remove(int &x,int pos);//删除位置pos处的元素,操作成功时返回true,否则返回false
- int element(int pos) const;//返回位置pos处的元素
- int search(int x) const;//查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0
- int length() const;//返回元素个数
/*
P202 6.8.18
定义一个由int型元素所构成的线性表类LinearList,它有下面的成员函数:
bool insert(int x,int pos);//在位置pos之后插入一个元素x,pos为0时。在第一个元素之前插入 操作成功返回true,否则返回false
bool remove(int &x,int pos);//删除位置pos处的元素,操作成功时返回true,否则返回false
int element(int pos) const;//返回位置pos处的元素
int search(int x) const;//查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0
int length() const;//返回元素个数
*/
#include <iostream>
using namespace std;
struct Node
{
int val;
Node *next;
};
class LinearList
{
Node *head;
public:
LinearList()
{
head = NULL;
}
bool insert(int x, int pos)
{
Node *p = head;
if (pos == 0)
{
Node *q = new Node;
q->val = x;
q->next = head;
head = q;
return true;
}
else
{
while (pos - 1 && p)
{
p = p->next;
pos--;
}
if (pos == 1&&p)
{
Node *q = new Node;
q->val = x;
q->next = p->next;
p->next = q;
return true;
}
else
{
return false;
}
}
}
bool remove1(int &x, int pos)
{
Node *p = head;
if (p)
{
if (pos == 1)
{
head = head->next;
delete p;
return true;
}
else
{
while (pos-2&&p)
{
p = p->next;
pos--;
}
if (pos == 2 && p&&p->next)
{
x = p->next->val;
Node *q = p->next;
p->next = q->next;
delete q;
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
int element(int pos) const
{
Node *p = head;
if (pos == 1)
return p->val;
while (pos-1&&p)
{
p = p->next;
pos--;
}
if (pos == 1 && p)
return p->val;
return -1;
}
int search(int x) const
{
Node *p = head;
int c = 1;
while (p)
{
if (p->val == x)
return c;
c++;
p = p->next;
}
return 0;
}
int length() const
{
Node *p = head;
int c = 0;
while (p)
{
p = p->next;
c++;
}
return c;
}
void display()
{
Node *p = head;
while (p)
{
cout << p->val << ' ';
p = p->next;
}
cout << endl;
}
};
int main()
{
LinearList l;
cout << "输入元素和插入位置(以-1 -1结尾):";
int t;
int p;
while (1)
{
cin >> t >> p;
if (t == -1)
break;
l.insert(t, p);
}
l.display();
cout << "要移除的位置:";
cin >> p;
l.remove1(t, p);
l.display();
cout << "输入位置:";
cin >> p;
cout << "位置" << p << "上的元素值为:" << l.element(p) << endl;
cout << "输入元素值:";
cin >> t;
cout << "元素" << t << "的位置为:" << l.search(t) << endl;
cout << "线性表长度为:" << l.length() << endl;
}