שאלה 1 (52 נקודות):
להלן קוד המטפל בליין-מסיבות, ובהמשך פלט ל- main המצורף.
עליכם להשלים את חלקי הקוד החסרים.
#include iostream
Using namespace std;
const int MAX_NAME_LEN = 20;
typedef struct
{
char nickName[MAX_NAME_LEN];
char favoriteSong[MAX_NAME_LEN];
} DJ_t;
typedef struct
{
char location[MAX_NAME_LEN];
DJ_t* theDJ; // the DJ that is playing in the party,
// NULL if it is not set yet
} party_t;
typedef struct
{
char name[MAX_NAME_LEN];
int year;
party_t* parties[12][31]; // pointer to party in each day of the year,
// NULL if there is no party
} line_t;
void readDJ(DJ_t* theDJ);
void readParty(party_t* theParty);
void printDJ(const DJ_t* theDJ);
void printParty(const party_t* theParty);
void printLine(const line_t* theClub);
void main()
{
line_t theLine = {"80s", 2013};
bool fContinue = true;
char answer;
int i, j;
while (fContinue)
{
printf("\nAdd another party? ");
flushall();
scanf("%c", &answer);
if (answer == 'n' || answer == 'N')
fContinue = false;
else
{
int month, day;
cout < "Enter month and day --> ";
cin > month > day;
if (theLine.parties[month-1][day-1] != NULL)
{
cout < "Sorry, there is already a party in this date..\n";
}
else
{
theLine.parties[month-1][day-1] = new party_t;
readParty(theLine.parties[month-1][day-1]);
}
}
}
printLine(&theLine);
// free all memory
for (i=0 ; i < 12 ; i++)
{
for (j=0 ; j < 31 ; j++)
{
if (theLine.parties[i][j] != NULL)
{
if (theLine.parties[i][j]->theDJ != NULL)
delete theLine.parties[i][j]->theDJ;
delete theLine.parties[i][j];
}
}
}
}
void readDJ(DJ_t* theDJ)
{
cout < "Enter the DJ's name: ";
flushall();
cin.getline(theDJ->nickName, MAX_NAME_LEN);
cout < "Enter the DJ's favorite song: ";
flushall();
cin.getline(theDJ->favoriteSong, MAX_NAME_LEN);
}
void readParty(party_t* theParty)
{
char answer;
cout < "Enter the location: ";
flushall();
cin.getline(theParty->location, MAX_NAME_LEN);
cout < "Does this party have a DJ (Y/N)? ";
cin > answer;
if (answer == 'Y' || answer == 'y')
{
theParty->theDJ = new DJ_t;
readDJ(theParty->theDJ);
}
else
theParty->theDJ = NULL;
}
void printDJ(const tDJ* theDJ)
{
cout < "nickName: " < theDJ->nickName < ", Song: "
< theDJ->favoriteSong < endl;
}
void printParty(const party_t* theParty)
{
cout < "location: " < theParty->location;
cout < " DJ: ";
if (theParty->theDJ != NULL)
printDJ(theParty->theDJ);
else
cout < "NONE";
cout < "\n";
}
void printLine(const line_t* theClub)
{
cout < "\nThese are the parties in " theClub->year
" in the line \" < theClub->name "\"\n",
for (int i=0 ; i < 12 ; i++)
{
cout < "Parties in month #" < i+1 < ":\n";
for (int j=0 ; j < 31 ; j++)
{
if (theClub->parties[i][j] != NULL)
{
cout < "\t";
printParty(theClub->parties[i][j]);
}
}
}
}
/*
Add another party? y
Enter month and day --> 1 19
Enter the location: The Caliph
Does this party have a DJ (Y/N)? y
Enter the DJ's name: Eran Barnea
Enter the DJ's favorite song: Vogue
Add another party? y
Enter month and day --> 3 17
Enter the location: The Riff
Does this party have a DJ (Y/N)? n
Add another party? y
Enter month and day --> 1 19
Sorry, there is already a party in this date..
Add another party? y
Enter month and day --> 2 13
Enter the location: TLV
Does this party have a DJ (Y/N)? n
Add another party? n
These are the parties in 2013 in the line "80s":
Parties in month #1:
location: The Caliph DJ: nickName: Eran Barnea, Song: Vogue
Parties in month #2:
location: TLV DJ: NONE
Parties in month #3:
location: The Riff DJ: NONE
Parties in month #4:
Parties in month #5:
Parties in month #6:
Parties in month #7:
Parties in month #8:
Parties in month #9:
Parties in month #10:
Parties in month #11:
Parties in month #12:
Press any key to continue . . .
*/
שאלה 2 (48 נקודות):
להלן הגדרת משתנים בתוכנית. מהו פלט התוכנית?
void main()
{
char* arr[] = {"abc", "defh", "ijklm", "no"};
cout < **arr < endl;
cout < arr[2] < endl;
cout < &arr[2][1]+2 < endl;
cout < *(&arr[2][1]+2) < endl;
}
סעיף א' (20 נקודות):
צייר את זכרון התוכנית (גם את ה- stack וגם את ה- static storage).
הנח כי המשתנים שעל ה- stack מתחילים בכתובת 1000 והם נמצאים ברצף כתובות עולה עפ"י סדר הגדרתם. בציור יש לכלול את שם המשתנה, ערכו וכתובתו.
פלט התוכנית:
a
ijklm
lm
l
6