📄 pex4_5.cpp
字号:
#include <iostream.h>
#include <string.h>
#pragma hdrstop
struct Test
{
char name[30];
int testNumber;
};
typedef Test DataType;
// used by Find, Delete in SeqList class
int operator== (const Test& t1, const Test& t2)
{
return strcmp(t1.name, t2.name) == 0;
}
#include "aseqlist.h"
// traverse list L and print each element
void PrintList(SeqList L)
{
Test t;
// access each element by its position using GetData
for(int i=0;i < L.ListSize();i++)
{
// obtain current test record and print it
t = L.GetData(i);
cout << t.name << " " << t.testNumber << endl;
}
}
// clear all white space through end of line
void ClearEOL(void)
{
char c;
do
cin.get(c);
while (c != '\n');
}
void main(void)
{
SeqList submittedTests, borrowedTests;
int action;
Test t;
cout << "Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): ";
cin >> action;
while(action != 4)
{
// clear white space after action
ClearEOL();
switch(action)
{
case 1:
// read student data and insert into submittedTests
cout << "Enter name: ";
cin.getline(t.name,30,'\n');
cout << "Test number: ";
cin >> t.testNumber;
submittedTests.Insert(t);
break;
case 2:
cout << "Enter name: ";
cin.getline(t.name,30,'\n');
// find test record with the entered name. we assume
// such a record exists. Find returns all fields of t
submittedTests.Find(t);
// delete t from submittedTests
submittedTests.Delete(t);
// and insert into borrowedTests
borrowedTests.Insert(t);
break;
case 3:
cout << "Enter name: ";
cin.getline(t.name,30,'\n');
// find the record in borrowedTests
borrowedTests.Find(t);
// delete the record
borrowedTests.Delete(t);
// and insert into submittedTests
submittedTests.Insert(t);
break;
}
cout << endl;
// if submittedTests not empty, print it
if (!submittedTests.ListEmpty())
{
cout << "Submitted tests:" << endl;
PrintList(submittedTests);
cout << endl;
}
// if borrowedTests not empty, print it
if (!borrowedTests.ListEmpty())
{
cout << "Borrowed tests:" << endl;
PrintList(borrowedTests);
cout << endl;
}
cout << "Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): ";
cin >> action;
}
// return all borrowed tests to the teacher
while (!borrowedTests.ListEmpty())
submittedTests.Insert(borrowedTests.DeleteFront());
cout << endl << "Final list:" << endl;
PrintList(submittedTests);
}
/*
<Run>
Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): 1
Enter name: Bill
Test number: 1
Submitted tests:
Bill 1
Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): 1
Enter name: Dick
Test number: 2
Submitted tests:
Bill 1
Dick 2
Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): 2
Enter name: Dick
Submitted tests:
Bill 1
Borrowed tests:
Dick 2
Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): 2
Enter name: Bill
Borrowed tests:
Dick 2
Bill 1
Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): 3
Enter name: Bill
Submitted tests:
Bill 1
Borrowed tests:
Dick 2
Enter 1 (submit test) 2(look at test) 3(return test) 4(exit): 4
Final list:
Bill 1
Dick 2
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -