c++ 英文题 求解决, 100分 谢谢!!!

h560802 2008-03-19 10:43:09
The Professors at CaLTech have really begun to appreciate the
object-oriented concepts of C++ and would like you to design and
code a class that will be used to encapsulate a collection of
"Segment" objects that you created in your previous assignment.

Appropriately, they would like you to write the code for a class
called a "Disk" which may contain any number of Segment objects, and a
string (maximum of 2 characters) indicating the
mode (i.e. "w" (write), or "a" (append)) that this Disk may be accessed with.

First, now that you know more of the C++ language, you must modify
the "Segment" class you wrote for assignment #2 to include the
following constructors, each calling their appropriate initialize( ) function
(from assignment #2):

Segment( )

Segment(const char [ ][2000], int)

As well, you must make the following functions:

int match(const char [ ]) const;
void get_word(char [ ], int) const;
char get_char(int, int) const;

which were not decalred as const functions previously, into constant
member functions, by adding the keyword const after their decalration
in both the "segment.h" and "segment.cpp" source files.


A "Disk" has the following publicly accessible MEMBER functions:

Disk(int num_of_Segments, const char *mode)
This constructor allocates enough memory for the Disk to store
a series of up to "num_of_Segments" Segments, and sets the
Disk's access mode to the constant string stored in "mode".
If the string in "mode" is anything other than "w", or "a",
the Disk's mode must be set to "w" (write).

Disk( )
This constructor allocates enough memory for the Disk to store
up to twenty (20) Segments, and initializes the Disk's access
mode to "w" (write).

const char* get_mode( ) const
This function returns the Disk's access mode as a constant
string.

Segment get_Segment(int pos) const
This function returns the Segment at position "pos"
(where a "pos" value of 0 means the first Segment).
The function returns a default Segment if "pos" does not reference
one of the Disk's properly initialized Segments.

int get_Segment_count( ) const
This is used to return an integer value representing the number of
Segment objects that have been properly initialized.

const Segment* get_all_Segments( ) const
This function returns the array containing all of the Disk's
data Segments.

int access(const char fname[ ])
This function will either write "w", or append "a" data
(depending on the class's access mode) to the file whose name is
stored in "fname", and return the number of charcters processed
(please see below for details about each access mode).

write:
For this operation, the function must write all of the data in each
of the class's Segments (one Segment per line) followed by a newline.
Also, each word in the Segment must be separated by a space ' ' when
written to the file
(spaces and newlines each count as a single (1) character when written).
NOTE: This operation overwrites any data which the file may have
contained.
(see example below).

Assume a "Disk" object consisting of the following 3 Segments:

words: 1 2 3 4 5 6 7 8 9 10
Segment1: "", "" "This", "is", "OOP244", "", "", "", "", ""
Segment2: "Isn't", "this", "", "", "easy?", "", "", "", "", ""
Segment3: "", "", "", "Yes!","", "", "", "", "", ""

Then the function would write the following to the file:

This is OOP244
Isn't this easy?
Yes!

and return a value of: 37
15 for (line 1) +
17 for (line 2) +
5 for (line 3)
for a total of 37 charcters written to the file.
BE CAREFUL HERE!
Pay careful attention to the number of spaces and newlines written
with respect to each Segment!

append:
Exactly the same as "write", except that data is appended to the
end of the file instead of being overwritten.

operator+=
You must also be able to add Segments to a Disk by overloading the
+= operator which adds Segments (on the right side of the operator)
to the Disk (on the left side of the operator), unless the Disk is
already full in which case nothing is done.
NOTE: This operator must return the newly modified "Disk" object by value.

NOTE: Because "proper copies" of "Disk" objects must be made, it will be
necessary for you to include a copy constructor as well an equal
operator overload to handle situations when "Disk" objects are
returned by value or when they are assigned to one another.
You must also code a destructor to remove any memory which may
have been dynamically allocated.
...全文
762 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mnky 2008-04-08
  • 打赏
  • 举报
回复
按9楼代码,只需修改一下 operator= 函数即可,就都pass了:

Disk Disk::operator=(const Disk& dk)
{
strcpy(m_mode,dk.m_mode);
sg_num_max=dk.sg_num_max;
sg_num=dk.sg_num;
if(sg)
delete []sg;
sg=new Segment[dk.sg_num_max];
memcpy(sg,dk.get_all_Segments(),sizeof(Segment)*sg_num);
for(int i=0;i<sg_num;i++)
{
for(int j=0;j<NUM_MAX;j++)
{
char temp[41];
dk.sg[i].get_word(temp,j);
sg[i].set_word(temp,j);
}
}
return *this;
}
hoomey 2008-04-08
  • 打赏
  • 举报
回复
mark 有时间慢慢看下
h560802 2008-04-06
  • 打赏
  • 举报
回复
#include <iostream.h>
using namespace std;

#include "disk.h"

int main( ) {
const char core_data[ ][1][2000] = {
{ "00100011" /* 35 = '#' */
"01101001" /* 105 = 'i' */
"01101110" /* 110 = 'n' */
"01100011" /* 99 = 'c' */
"01101100" /* 108 = 'l' */
"01110101" /* 117 = 'u' */
"01100100" /* 100 = 'd' */
"01100101" /* 101 = 'e' */
"00100000" /* 32 = ' ' */
"00111100" /* 60 = '<' */
"01110011" /* 115 = 's' */
"01110100" /* 116 = 't' */
"01100100" /* 100 = 'd' */
"01101001" /* 105 = 'i' */
"01101111" /* 111 = 'o' */
"00101110" /* 46 = '.' */
"01101000" /* 104 = 'h' */
"00111110" /* 62 = '>' */
"00000000" /* 0 = '\0' */
},

{ "01101001" /* 105 = 'i' */
"01101110" /* 110 = 'n' */
"01110100" /* 116 = 't' */
"00100000" /* 32 = ' ' */
"01101101" /* 109 = 'm' */
"01100001" /* 97 = 'a' */
"01101001" /* 105 = 'i' */
"01101110" /* 110 = 'n' */
"00101000" /* 40 = '(' */
"00100000" /* 32 = ' ' */
"00101001" /* 41 = ')' */
"00100000" /* 32 = ' ' */
"01111011" /* 123 = '{' */
"00000000" /* 0 = '\0' */
},

{ "00100000" /* 32 = ' ' */
"00100000" /* 32 = ' ' */
"00100000" /* 32 = ' ' */
"01110000" /* 112 = 'p' */
"01110010" /* 114 = 'r' */
"01101001" /* 105 = 'i' */
"01101110" /* 110 = 'n' */
"01110100" /* 116 = 't' */
"01100110" /* 102 = 'f' */
"00101000" /* 40 = '(' */
"00100010" /* 34 = '"' */
"01001000" /* 72 = 'H' */
"01100101" /* 101 = 'e' */
"01101100 RPT2 x" /* 108 = 'l' */
"01101111" /* 111 = 'o' */
"00100000" /* 32 = ' ' */
"01010111" /* 87 = 'W' */
"01101111" /* 111 = 'o' */
"01110010" /* 114 = 'r' */
"01101100" /* 108 = 'l' */
"01100100" /* 100 = 'd' */
"00100001" /* 33 = '!' */
"01011100" /* 92 = '\' */
"01101110" /* 110 = 'n' */
"00100010" /* 34 = '"' */
"00101001" /* 41 = ')' */
"00111011" /* 59 = ';' */
"00000000" /* 0 = '\0' */
},

{ "00100000" /* 32 = ' ' */
"00100000" /* 32 = ' ' */
"00100000" /* 32 = ' ' */
"01110010" /* 114 = 'r' */
"01100101" /* 101 = 'e' */
"01110100" /* 116 = 't' */
"01110101" /* 117 = 'u' */
"01110010" /* 114 = 'r' */
"01101110" /* 110 = 'n' */
"00100000" /* 32 = ' ' */
"00110000" /* 48 = '0' */
"00111011" /* 59 = ';' */
"00000000" /* 0 = '\0' */
},

{ "01111101" /* 125 = '}' */
"00000000" /* 0 = '\0' */
}
};

char correct[ ][41] = {
"#include <stdio.h>", "int main( ) {",
" printf(\"Hello World!\\n\");", " return 0;", "}"
};
char phrase[41];

int i, num, valid=1, pass=0, sc;
Disk d1(7, "w"), d2;
Segment sgs[5];

for(i=0; i<5 && valid; i++, pass++) {
sgs[i].initialize(core_data[i], 1);

d1 += sgs[i]; // calling operator+=( )
// which in turn calls the copy constructor
// because of the return by value

d1.get_Segment(i).get_word(phrase, 0);
sc = d1.get_Segment_count( );
if(strcmp(phrase, correct[i]) || sc != pass + 1) {
cout << "\nFailed while testing the operator+=( )...\n";
cout << "Failed on test... " << pass+1 << endl << endl;
valid = 0;
}
else {
cout << "\nPassed test... " << pass+1 << endl << endl;
}
cout << "Segment[" << i << "]'s string -------> '" << phrase << "'" << endl;
cout << "Actual string -------------> '" << correct[i] << "'" << endl;
cout << "Your Segment count --------> " << sc << endl;
cout << "Actual Segment count ------> " << pass + 1 << endl;
cout << "Press the ENTER key to continue..." << endl;
cin.get( );
}
if(valid) {
d2 = d1; // calling operator=( )

const Segment *p1 = d1.get_all_Segments( );
const Segment *p2 = d2.get_all_Segments( );
if(p1 == p2 ||
d1.get_Segment_count( ) != d2.get_Segment_count( )) {
// p1 and p2 should have unique memory addresses
// if the operator=( ) was coded correctly!
cout << "\nFailed while testing the operator=( )...\n";
cout << "Failed on test... " << pass + 1 << endl << endl;
valid = 0;
}
else {
cout << "\nPassed test... " << pass+1 << endl << endl;
cout << "First disk's Segment count ----> " <<
d1.get_Segment_count( ) << endl;
cout << "Second disk's Segment count ---> " <<
d2.get_Segment_count( ) << endl;
cout << "Press the ENTER key to continue..." << endl;
cin.get( );
pass++;
}
}
if(valid) {
num = d2.access("hello.c");
if(num != 77) {
cout << "\nFailed while testing the access( ) function...\n";
cout << "Failed on test... " << pass + 1 << endl << endl;
valid = 0;
}
else {
cout << "\nPassed test... " << pass+1 << endl << endl;
cout << "Number of characters written --> " << num << endl;
cout << "Actual number of characters ---> " << 77 << endl;
cout << "Press the ENTER key to continue..." << endl;
cin.get( );
pass++;
}
}
if(valid) {
cout << "\nCongratualtions!!! You passed all tests.\n";
cout << "Your program is ";
cout.setf(ios::fixed);
cout.precision(2);
cout << 100.0*(pass)/7.0 << "% complete...\n";
cout << "You may hand in your assignment.\n";
}
else {
cout << "\nYou passed " << pass-1 << "/7 tests...\n";
cout << "Your program is ";
cout << 100.0*(pass)/7.0 << "% complete...\n";
cout << "Your program still needs some work!\n";
cout << "Keep at it!\n\n";
}
return 0;
}
rabbii 2008-04-06
  • 打赏
  • 举报
回复
学习
ysuliu 2008-04-06
  • 打赏
  • 举报
回复
3楼强人,呵呵,都翻译好了。
jinwei1984 2008-04-06
  • 打赏
  • 举报
回复
JF
V若只如初见 2008-03-24
  • 打赏
  • 举报
回复
还是多点英文的交流,可以刺激我们去学E文,LOL
地下室小红叔 2008-03-20
  • 打赏
  • 举报
回复
放在C++的版块中 不算是难题的 我想是英文把多数人搞晕的
zhangliaoren 2008-03-20
  • 打赏
  • 举报
回复
没分,散点分吧
xnkjdx1998 2008-03-20
  • 打赏
  • 举报
回复
接点小分
hzzgra 2008-03-20
  • 打赏
  • 举报
回复
题目好长……
jeff_nie 2008-03-19
  • 打赏
  • 举报
回复
没看懂,MARK先.
wuhuzhangwei 2008-03-19
  • 打赏
  • 举报
回复
mark
Mnky 2008-03-19
  • 打赏
  • 举报
回复
有个bug:
operator+= 的 ++sg_num 这句,移到上面if的括号里。

另外,为什么要定义operator=啊??去掉好像也行。。。
V若只如初见 2008-03-19
  • 打赏
  • 举报
回复
//segment.cpp


#include<vector>



//segment.h;

class Segment
{
std::vector<char*> m_vecStrData;
int m_ivecSize;
public:
Segment(){};
Segment(const char strBuf[ ][2000], int iCount);
~Segment();
int match(const char strBuf[ ]) const;
void get_word(char strBuf[ ], int iIndex) const;
char get_char(int iStr, int iIndex) const;
}

//segment.cpp
Segment::Segment(const char strBuf[ ][2000], int iCount)
{
m_ivecSize = iCount;
for(int i = 0; i< iCount; i++)
{
char* pBuf = new char[2000];
if(NULL != pBuf)
{
strncpy( pBuf,&strBuf[i],2000);
m_vecStrData.push_back(pBuf);
}
}
}

int Segment::match(const char strBuf[ ]) const
{
int iRet = -1;
for(int i = 0; i< m_ivecSize; i++)
{
if (i > m_vecStrData.size())
{
return iRet;
}
char* pBuf = m_vecStrData[i];
if ( 0 == strcmp(pBuf,strBuf))
{
iRet = i;
return iRet;
}
}
return iRet;
}

void Segment::get_word(char strBuf[ ], int iIndex) const
{
if (iIndex > m_ivecSize)
{
return ;
}
char* pBuf = m_vecStrData[iIndex];
strncpy(strBuf,pBuf,2000);
}

char Segment::get_char(int iStr, int iIndex) const
{
if (iIndex > m_ivecSize)
{
return 0;
}
char* pBuf = m_vecStrData[iIndex];
return *(pBuf+iStr);
}

Segment::~Segment()
{

for(int i = 0; i< m_ivecSize; i++)
{
char* pBuf = m_vecStrData[i];
if (NULL != pBuf)
{
delete pBuf;
pBuf = NULL;
}
}
m_vecStrData.clear();
}
V若只如初见 2008-03-19
  • 打赏
  • 举报
回复
谢谢11楼,OK,我知道了

#include <vector>
class Segment
{
std::vector <char*> m_vecStrData;
int ivecSize;
public:
Segment(){};
Segment(const char strBuf[ ][2000], int iCount);
~Segment()
int match(const char strBuf[ ]) const;
void get_word(char strBuf[ ], int iIndex) const;
char get_char(int iStr, int iIndex) const;
}
V若只如初见 2008-03-19
  • 打赏
  • 举报
回复
CalTech加利福尼亚理工学院
MagiSu 2008-03-19
  • 打赏
  • 举报
回复
CalTech?名校啊!
0黄瓜0 2008-03-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 msn123321 的回复:]
could please tell me how to submit the context formated??
[/Quote]

代码放这儿
V若只如初见 2008-03-19
  • 打赏
  • 举报
回复
could please tell me how to submit the context formated??
加载更多回复(9)

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧