两个模块的代码 求解释逻辑和错误出在哪些地方,谢谢。
题目为英文,单词不复杂,不过描述比较长。重点用红色字体标出, 我写的代码在后面, 谢谢您的耐心。Design and code two separate structures:
Line - defines a line item that consists of a line number and a descriptive string
List - defines a list of up to MAX_LINES Line items
Store each structure and its implementation in its own separate module.
Line Module
A Line represents a single line item in a list. An instance of a Line holds a line number and a C-style null-terminated string of up to MAX_CHARS characters excluding the null byte. Both data element are private.
A Line structure also incudes two public methods:
bool Line::set(int n, const char* str) - receives a line number and the address of a string from the calling function. If the line number received is positive, your method accepts the number and stores the string of characters, but never stores more than MAX_CHARS characters from the received address, and returns true. If the line number received is zero or negative-valued, your method does not store the line number or the string and returns false.
void Line::display() const - receives nothing and displays the line number and the string on standard output, separated by a single blank character.
Store the definition of your Line type in a header file named Line.h and the definitions of your methods in an implementation file named Line.cpp.
List Module
A List represents a list of Line instances. A List structure holds the number of Line instances that are filled and an array of MAX_LINES Line instances. All of the data in this structure is private.
A List structure also includes two public methods:
void List::set(int no) - receives the number of line items to be obtained from the user and entered into the list. This method accepts the input data from the user for each line item by calling the Line::set() method on a temporary Line instance. If the Line::set() method returns true, your List::set() method copies the temporary instance into the next empty Line item in the list and displays the string "Accepted" on standard output. Otherwise, your List::set() method does not copy the temporary instance into the list and displays the string "Rejected". Your method keeps accepting input from the user until the user has entered no valid items. If no exceeds MAX_LINES, your method only accepts MAX_LINES items.
void display() const - receives nothing and displays each line item in the list on a separate line of output. Your method calls the Line::display() method to access and display the information for each line.
Store the definition of your List type in a header file named List.h and your method definitions in an implementation file named List.cpp. Include the header file for the Line module at the start of your List.h file. (You need this to inform the compiler of the structure of the array of Line instances.)
Main Module
The main module that uses an instance of your List structure is as follows:
// w3.cpp
#include <iostream>
using namespace std;
#include "List.h"
int main() {
int no;
List list;
cout << "List Processor\n==============" << endl;
cout << "Enter number of items : ";
cin >> no;
list.set(no);
list.display();
}
The output from this application looks something like: 运行类似的结果如下:
List Processor
==============
Enter number of items : 3
Enter line number : 1
Enter line string : IPC144
Accepted
Enter line number : 2
Enter line string : BTP200
Accepted
Enter line number : -3
Enter line string : xxx123
Rejected
Enter line number : 3
Enter line string : OOP344
Accepted
1 IPC144
2 BTP200
3 OOP344
//Line.h
#define MAX_CHARS 10
struct Line
{
private:
int no;
char linestr[MAX_CHARS + 1];
public:
bool Line::set(int n, const char* str);
void Line::display() const;
};
//Line.cpp
#include <iostream>
using namespace std;
#include "Line.h"
bool Line::set(int n, const char* str)
{
bool valid = n > 0;
if (valid)
{
int i;
no=n;
for (i = 0; str[i] != '\0' && i < MAX_CHARS; i++)
linestr[i]=str[i];
linestr[i]='\0';
return
valid;
}
else
{
no=0;
linestr[0]='\0';
return
false;
}
}
void Line::display() const
{
cout << no << ' ' << linestr;
}
//List.h
#define MAX_LINES 10
struct List
{
private:
int noLines;
Line line[MAX_LINES];
public:
void List::set(int no);
void display() const;
};
//Line.cpp
#include <iostream>
using namespace std;
#include "List.h"
void List::set(int no)
{
int lines;
char line[MAX_LINES];
List temp;
noLines = 0;
while (noLines < no)
{
cout << "Enter line number : "<<endl;
cin >>lines ;
cout << "Enter line string : "<<endl;
cin >> line;
bool valid=Line::set(int n, const char* str);
if (valid)
{
List[noLines++] = temp;
cout << "Accepted"<<endl;
}
else
cout << "Rejected"<<endl;
}
}
void display() const
{
for (int i = 0; i < lineno; i++)
{
line[i].display();
cout << endl;
}
}