#C++PrimerPlus# Chapter10_Exersice8_v2.0

完善例题8中的程序。构思如下。

用一个结构替代v1.0版本中的列表项bool类型。

结构: 列表项 用某类型的别名Item。

    bool 标识列表项是否为空。

      bool 标识列表项是否可写。

将这个结构加入到此前定义的类中,并对公用方法做一些修改。

私有部分:结构

       一个计数器,int变量,记录多少个列表项为满。

公有部分:初始化函数,初始空列表。(全标识为空,可写。)

     添加/重设某项,参数为项编号,标识是否可写(默认可写)。

     删除某项,参数为项编号。

       显示项,参数为项编号。

     提取项,返回对应项编号的值。

     判定项是否为只读。

     检验列表是否为空或满的函数。

     显示整个列表的函数。

     重置整个列表的函数。

程序清单如下:


 // list20.h
#ifndef LIST20_H_
#define LIST20_H_

#include <string>

typedef std::string Item;

const int SIZE = 10;

struct termInf
{
Item term;
bool termDefined;
bool termReadOnly;
};

class List
{
private:
termInf lists[SIZE];
int count;
public:
List();
void addTerm(int num, Item str, bool readonly = 0);
void delTerm(int num);
void showTerm(int num) const;
const Item& pickTerm(int num) const;
bool ifReadOnly(int num) const;
bool ifListEmpty() const;
bool ifListFull() const;
void showList() const;
void resetList();
};

#endif


 // list20.cpp
#include <iostream>
#include "list20.h"

List::List()
{
for (int i = 0; i < SIZE; i++)
{
lists[i].term = "无";
lists[i].termDefined = false;
lists[i].termReadOnly = false;
}
}

void List::addTerm(int num, Item m_item, bool readonly)
{
if (!lists[num-1].termReadOnly)
{
lists[num-1].term = m_item;
lists[num-1].termDefined = true;
lists[num-1].termReadOnly = readonly;
std::cout << "成功的添加了列表项" << num << "。\n";
}
else
std::cout << "操作无效,此列表项为只读。\n";
}

void List::delTerm(int num)
{
if (!lists[num-1].termReadOnly)
{
lists[num-1].term = "无";
lists[num-1].termDefined = false;
std::cout << "成功的删除了列表项" << num << "。\n";
}
else
std::cout << "操作无效,此列表项为只读。\n";
}

void List::showTerm(int num) const
{
std::cout << lists[num-1].term;
}

const Item& List::pickTerm(int num) const
{
return lists[num-1].term;
}

bool List::ifReadOnly(int num) const
{
return lists[num-1].termReadOnly;
}

bool List::ifListEmpty() const
{
bool result = true;
for (int i = 0; i < SIZE; i++)
{
if (lists[i].termDefined)
result = false;
}
return result;
}

bool List::ifListFull() const
{
bool result = true;
for (int i = 0; i < SIZE; i++)
{
if (!lists[i].termDefined)
result = false;
}
return result;
}

void List::showList() const
{
using std::cout;
cout << "\n编号\t只读\t内容\n";
for (int i = 0; i < SIZE; i++)
cout << i+1 << "\t" << lists[i].termReadOnly << "\t" << lists[i].term << "\n";
cout << "\n";
}

void List::resetList()
{
for (int i = 0; i < SIZE; i++)
{
lists[i].term = "无";
lists[i].termDefined = false;
lists[i].termReadOnly = false;
}
std::cout << "\a列表重置完成!\n\n";
}


// uselist20.cpp
#include <iostream>
#include "list20.h"

// 添加修改列表项模块
void add(List& m_list);
// 删除列表项模块
void del(List& m_list);

int main()
{
using std::cout;
using std::cin;

List newlist;

char input;
cout << "请选择您进行的操作 A:输入列表项 D:删除列表项 R:重置列表 其他任意键退出:";
while (cin >> input)
{
while (cin.get() != '\n')
continue;

if (input == 'A' || input == 'a')
{
add(newlist);
}
else if (input == 'D' || input == 'd')
{
del(newlist);
}
else if (input == 'R' || input == 'r')
{
newlist.resetList();
}
else
break;

cin.clear();
while (cin.get() != '\n')
continue;

cout << "请选择您进行的操作 A:输入列表项 D:删除列表项 R:重置列表 其他任意键退出:";
}

system("pause>nul");
return 0;
}

void add(List& m_list)
{
using std::cout;
using std::cin;

int termNum;
cout << "\n请输入您要修改的项编号(1-" << SIZE << "),其他任意值退出到选择界面:";
while (cin >> termNum)
{
while (cin.get() != '\n')
continue;

// 检验编号是否超界
if (termNum < 1 || termNum > SIZE)
{
cout << "编号无效,重新输入。\n\n";
cout << "请输入您要修改的项编号(1-" << SIZE << "),其他任意值退出到选择界面:";
continue;
}

if (!m_list.ifReadOnly(termNum))
{
// 输入项内容
Item tempItem;
cout << "请输入列表内容:";
while (!(cin >> tempItem))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "输入错误,重新输入。\n";
cout << "请输入列表内容:";
}
while (cin.get() != '\n')
continue;

// 选择项是否只读
bool tempDefined;
cout << "是否设置此项为只读?(1 = 是; 0 = 否):";
while (!(cin >> tempDefined))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "输入错误,重新输入。\n";
cout << "是否设置此项为只读?(1 = 是; 0 = 否): ";
}
while (cin.get() != '\n')
continue;

// 使用类方法
m_list.addTerm(termNum, tempItem, tempDefined);
m_list.showList();
if(m_list.ifListEmpty())
cout << "列表为空。\n";
if(m_list.ifListFull())
cout << "列表已满。\n";
}
else
cout << "操作无效,此列表项为只读。\n";

cout << "\n请输入您要修改的项编号(1-" << SIZE << "),其他任意值退出到选择界面:";
}
cout << "退出输入模块,返回选择界面。\n\n\n";
}

void del(List& m_list)
{
using std::cout;
using std::cin;
int termNum;
cout << "\n请输入您要删除的项编号(1-" << SIZE << "),其他任意值退出到选择界面:";
while (cin >> termNum)
{
while (cin.get() != '\n')
continue;

// 检验编号是否超界
if (termNum < 1 || termNum > SIZE)
{
cout << "编号无效,重新输入。\n\n";
cout << "请输入您要修改的项编号(1-" << SIZE << "),其他任意值退出到选择界面:";
continue;
}

// 类方法
m_list.delTerm(termNum);
m_list.showList();
if(m_list.ifListEmpty())
cout << "列表为空。\n";
if(m_list.ifListFull())
cout << "列表已满。\n";

cout << "\n请输入您要删除的项编号(1-" << SIZE << "),其他任意值退出到选择界面:";
}
cout << "退出输入模块,返回选择界面。\n\n\n";
}


结束。

原文链接: https://www.cnblogs.com/zhuangdong/archive/2013/04/28/3046821.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    #C++PrimerPlus# Chapter10_Exersice8_v2.0

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/86328

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午10:31
下一篇 2023年2月9日 下午10:31

相关推荐