📄 acadeawards.cpp
字号:
/* This program maintains and displays a list of
Academy Awards Motion Pictures.
Written by: G & F
Date: 2/1998
Revisions: 4/1999 Converted to C++
Added delete logic (not in text)
Copyright(C) 2000 Brooks/Cole Publishing
Thomson Learning
All Rights Reserved
*/
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <cType.h>
#include "ADT.h"
const short STR_MAX = 41;
struct PICTURE
{
short key; // year
char picture [STR_MAX];
char director[STR_MAX];
}; // PICTURE
// Prototype Declarations
void instr (void);
void search (List <PICTURE, short>& list);
void printList (List <PICTURE, short>& list);
void process (List <PICTURE, short>& list);
char getChoice (void);
void buildList (List <PICTURE, short>& list);
void deletePic (List <PICTURE, short>& list); //Not in text
int main (void)
{
// Local Definitions
List<PICTURE, short> list;
// Statements
instr ();
buildList (list);
process (list);
cout << "End Best Pictures\n"
<< "Hope you found your favorite!\n";
return 0;
} // main
/* ==================== instr ====================
Print instructions to user.
Pre nothing
Post instructions printed
*/
void instr (void)
{
// Statements
cout << "This program will print the Academy Awards \n"
<< "Best Picture of the Year and its director. \n"
<< "Your job is to enter the year; we will do \n"
<< "the rest. Enjoy.\n\n";
return;
} // instr
/* ==================== buildList ====================
Reads a text data file and loads the linked list.
Pre file exists in format: yy;pic;dir\n
Post list contains data
-or- program aborted if problems
*/
void buildList (List <PICTURE, short>& list)
{
// Local Definitions
ifstream fsData;
int addResult;
PICTURE pic;
// Statements
fsData.open("pictures.dat");
if (!fsData)
{
cerr << "\aError opening input file\n";
exit (110);
} // if
while (fsData >> pic.key)
{
fsData.getline (pic.picture,
sizeof(pic.picture), ';');
fsData.getline (pic.director, sizeof(pic.director));
// Insert into list
addResult = list.addNode (pic);
if (addResult != 0)
if (addResult == -1)
{
cout << "Memory overflow adding movie\a\n";
exit (120);
} // Add overflow
else
cout << "Duplicate year: "
<< pic.key
<< " not added\n\a";
} // while
cout << endl;
return;
} // buildList
/* ==================== process ====================
Process user choices.
Pre list has been created
Post all of user's choices executed
*/
void process (List<PICTURE, short>& list)
{
// Local Definitions
char choice;
// Statements
do
{
choice = getChoice ();
switch (choice)
{
case 'P': printList (list);
break;
case 'D': deletePic(list);
break;
case 'S': search (list);
case 'Q': break;
} // switch
} while (choice != 'Q');
return;
} // process
/* ==================== getChoice ====================
Prints the menu of choices.
Pre nothing
Post menu printed and choice returned
*/
char getChoice (void)
{
// Local Definitions
char choice;
bool valid;
// Statements
cout << "======== MENU ======= \n"
<< "Here are your choices:\n"
<< " D: Delete a year: \n"
<< " S: Search for a year\n"
<< " P: Print all years \n"
<< " Q: Quit \n\n"
<< "Enter your choice: ";
do
{
cin >> choice;
choice = toupper(choice);
switch (choice)
{
case 'D':
case 'S':
case 'P':
case 'Q': valid = true;
break;
default: valid = false;
cout << "\aInvalid choice\n"
<< "Please try again: ";
break;
} // switch
} while (!valid);
return choice;
} // getChoice
/*==================== printList ====================
Prints the entire list.
Pre list has been created
Post list printed
*/
void printList (List <PICTURE, short>& list)
{
// Local Definitions
PICTURE pic;
// Statements
//Get first node
if (list.listCount () == 0)
cout <<"Sorry, nothing in the list\n\a";
else
{
cout << "\nBest Pictures List\n";
list.getNext (0, pic);
do
{
cout.setf (ios::left);
cout << pic.key << " "
<< setw (sizeof(pic.picture))
<< pic.picture << " "
<< setw (sizeof(pic.director))
<< pic.director << endl;
cout.unsetf (ios::left);
} while (list.getNext (1, pic));
cout << "End of Best Pictures List\n\n";
} // else
return;
} // printList
/* ==================== search ====================
Searches for year and prints year, picture, and
director.
Pre list has been created
user has selected search option
Post year printed or error message
*/
void search (List<PICTURE, short>& list)
{
// Local Definitions
short year;
bool found;
PICTURE pic;
// Statements
cout << "Enter a four digit year: ";
cin >> year;
found = list.retrieveNode (year, pic);
if (found)
cout << pic.key << " \""
<< pic.picture << "\" Directed by: "
<< pic.director << endl;
else
cout << "Sorry, but " << year
<< " is not available.\n";
return;
} // search
// ==================== deletePic ====================
/* Deletes picture from list and prints year, picture,
and director after delete.
Pre list has been created
user has selected delete option
Post year deleted and data printed or error message
*/
void deletePic (List<PICTURE, short>& list)
{
// Local Declarations
short year;
bool found;
PICTURE pic;
// Statements
cout << "Enter a four digit year to be deleted: ";
cin >> year;
found = list.removeNode (year, &pic);
if (found)
cout << pic.key << " \""
<< pic.picture << "\" Directed by: "
<< pic.director << " "
<< "deleted.\n\n";
else
cout << "\aSorry, but " << year << " is not in list.\n\n";
return;
} // search
/* Results
This program will print the Academy Awards
Best Picture of the Year and its director.
Your job is to enter the year; we will do
the rest. Enjoy.
Duplicate year: 1942 not added
======== MENU =======
Here are your choices:
S: Search for a year
P: Print all years
Q: Quit
Enter your choice: p
Best Pictures List
1932 Cavalcade Frank Lloyd
1934 It Happened One Night Frank Capra
1938 You Can't Take It With You Frank Capra
1939 Gone With the Wind Victor Fleming
1941 How Green Was My Valley John Ford
1942 Mrs. Miniver William Wyler
1943 Casablanca Michael Curtiz
1945 The Lost Weekend William Wyler
1946 The Best Years of Our Lives William Wyler
1947 Gentleman's Agreement Elia Kazan
1950 All About Eve Joseph L. Mankiewicz
1953 From Here To Eternity Fred Zinnemann
End of Best Pictures List
======== MENU =======
Here are your choices:
S: Search for a year
P: Print all years
Q: Quit
Enter your choice: s
Enter a four digit year: 1932
1932 "Cavalcade" Directed by: Frank Lloyd
======== MENU =======
Here are your choices:
S: Search for a year
P: Print all years
Q: Quit
Enter your choice: q
End Best Pictures
Hope you found your favorite!
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -