CHAPER 13 Arrey-Based Lists 2 4 5

第二题

在这里插入图片描述
翻译:
在这里插入图片描述
这里要用到文件的读取、打印的格式、输出换行、函数、循环。还有很多细节。

第四题

在这里插入图片描述
翻译:
在这里插入图片描述

文件1"List.h" :

//This file gives the specification of a list abstracter data type.
//*******************************************************************************************************************************

const int MAX_LENGTH=1000;
typedef char String20[21];  //定义char的别名为21个这种类型的字符变量,之后在用的时候不用写中括号
class List
{
public:
	bool IsEmpty() const;
	//Postcondition:   判断是否为空,加const是为了不改变类的对象
	//      Return value ==true,if list is empty
	//                   ==false,otherwise
	
	bool IsFull() const;
	//Postcondition:
	//      Return value==true,if list is empty  这里标错了,应该是是否满了
	//                  ==false,otherwise
	
	int Length() const;
	//Postcondition:   求长度
	//      Return value==length of list
	
	void Insert(/*in*/ char item[],int number);
	//Precondition:  插入一个数
	//   NOT IsFull()
	//Postcondition;
	//   item is in list && Length()==Length()@entry+1
	
	bool IsPresent(/*in*/ char item[],int number) const;
	//Postcondition:  判断是否在列表里
	//    Return value==true,if item is in list
	//                ==false,otherwise
	
	void Reset();
	//Postconditon:  初始化列表
	//    Iteration is initialized
	
    void  GetNextItem(/* out */String20 & );
	//Preconditon:   找现在控制器指向的列表的位置
	//     Iteration has been initialized by call to Reset;
	//Postcondition:
	//  Return value is the item at the current position in the list on entry;
	//  If last item has been returned,the next call will return the first item.
	
	List();  
	//Constuctor   创建列表
	//Postcondition:
	// Empty list is created 
	
	void write() const;
	//ouput list elements  输出列表成员
	 
private:
	int length;
	int currentPos;
	String20 address[MAX_LENGTH];
}; // 注意此分号!

文件2:

//This flie implements the List class member functions
//*****************************************************************************************************************************************
#include<iostream>
#include"List.h"
#include<cstring>
#include<string>
#include<fstream>
using namespace std;
//Private members of class:
//     int length;
//     int currentPos;
//     String20 address[MAX_LENGTH];
List::List()
//Constructor
//Postcondition:
//     lenggth==0;
{
	length=0;
}

//******************************************************************************************************************************************
bool List::IsEmpty() const
//Reports whether list is empty
//Postcondition:
//       Return value==true,if length==0
//                  ==false,otherwise
{
	return (length==0);

}

//******************************************************************************************************************************************
bool List::IsFull() const
//Reports whether list is full
//Postcondition:
//       Return value==true,if length==MAX_LENGTH
//                  ==false,otherwise
{
	return (length==MAX_LENGTH);

}

//*****************************************************************************************************************************************
int List::Length() const
//Returns current length of list
//Postcondition:
//      Return value==length
{
	return length; //所以length能动态变化?
}

//********************************************************************************************************************************************
void List::Insert(/*in*/ char item[],int number)
//Insert item into the list
//Precondition:
//     length<MAX_LENGTH
//Postcondition:
//     address[length@enty]==item
//     && length==length@entry+1
{
	if(length<MAX_LENGTH)
	{	strcpy(address[length],item);
	     length++;
	}
	else
	   cout<< "The list is full! Terminated !"<<endl;
}

//*******************************************************************************
bool List::IsPresent(/*in*/ String20 item, int number) const
//Searches the list for item,reporting whether it was found  是否在列表里
//Postcondition:
//     Return value ==true,if item is in data[0...length-1]
//                  ==false,otherwise
{
	int index=0;
	while(index<length )//&& (!strcmp(address[index],item)))
	   if(strcmp(address[index],item)==0)	break;
	   else index++;
	return (index<length);
}

//*******************************************************************************
void List::Reset()
//Postcondition:
//      currentPos has been initialized.
{
	currentPos=0;  //当前的指向归零
}

//*******************************************************************************
void List::GetNextItem(/* out */String20 & item)
//Precondition:
//      No transformer has been executed since last call
//postcondition:
//     Return value is current@entry
//     && current position has been updated
//     returns the fisrt item
{
	//String20 item; //local varible
	strcpy(item,address[currentPos]);
	if(currentPos==length-1)
		currentPos=0;
	else
		currentPos++;
	//return item;
}

void List::write() const 
	//ouput list elements
{
 int index=0;
 cout<<"Elements in the list are: "<<endl;
 for (index=0;index<length;index++) 
	 cout<<address[index]<<endl;
}

文件3:

//*******************************************************************************
//Address Program,  Programming Problem 4 of Chapter 13.
//This program reads e-mail address from file called "rawlist.dat" and discards any that already 
//have been input.
// By Shang Haiyan, June 21, 2010
//*******************************************************************************
#include<cstring>
#include"List.h"
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
   List AddrList; 
   String20 onemail,mail;
   int limit,count;
   ifstream inFile;
   ofstream outFile;
   inFile.open("rawlist.dat"); //先创建一个
   outFile.open("cleanlist.dat");
   if(!inFile&&!outFile)
   {
	   cout << "Can't open the file!" << endl;
	   return 1;
   }
   inFile.get(mail,21);
   inFile.ignore(30,'\n');

   cout<<"Current mail is :"<<mail<<endl;
   while(inFile)
   {
       if(!AddrList.IsPresent(mail,21))
	   {
		   AddrList.Insert(mail,21);
	       inFile.get(mail,21); cout<<"Current mail is :"<<mail<<endl;
           inFile.ignore(30,'\n');
	   }
	   else
		   cout << "This e_mail has been inputed." << endl;
   }
   AddrList.Reset();
   AddrList.write();//output the elements in the list
   limit=AddrList.Length();
   cout<< limit<<endl;
   for(count=0;count<limit;count++)
   {
	   AddrList.GetNextItem(mail);
	   cout<<"Current mail  get out  is :"<<mail<<endl;
	   outFile<<mail<<endl; //output an address to outfile
   }
   inFile.close();
   outFile.close();
   return 0;
}

输入文件“rawlist.dat”
在这里插入图片描述

输出文件“cleanlist.dat”

在这里插入图片描述

某次运行结果屏幕截图:

在这里插入图片描述

第五题

在这里插入图片描述
在这里插入图片描述
翻译:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒回顾,半缘君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值