第二题
翻译:
这里要用到文件的读取、打印的格式、输出换行、函数、循环。还有很多细节。
第四题
翻译:
文件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”
某次运行结果屏幕截图:
第五题
翻译: