⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex4_2.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

struct Person
{
	char name[20];
	int age;
	char gender;
};

#include <string.h>
#pragma hdrstop

// needed for List class method Find
int operator== (Person x, Person y)
{
	return strcmp(x.name, y.name) == 0;
}

typedef Person DataType;
#include "aseqlist.h"
	
// print each record of L whose gender field matches sex
void PrintByGender(const SeqList& L, char sex)
{
	Person p;
	
	// traverse list
	for(int i=0;i < L.ListSize();i++)
	{
		// record in p the data record at position i
		p = L.GetData(i);
		// if gender field matches sex, print name and age
		if (p.gender == sex)
			cout << p.name << "  " << p.age << endl;
	}
}

// determine if there is a record in L having name
// field nm. if so, assign the record to p. return 1
// if such a record is found and 0 if not
int InList(const SeqList& L, char *nm, Person& p)
{
	Person tmp;
	int retval;
	
	// create a record with name field nm
	strcpy(tmp.name,nm);
	
	// look for a record with name field nm in L
	if (L.Find(tmp))
	{
		// success. tmp is the record as it occurs in L.
		// assign tmp to p and record success in retval
		p = tmp;
		retval = 1;
	}
	else
		// failure. record this in retval
		retval = 0;
		
	return retval;
}

void main(void)
{
	SeqList L;
	ifstream fin;
	Person p;
	char name[20], blank;
	
	// open text file "person.dat"
	fin.open("person.dat", ios::in | ios::nocreate);
	if (!fin)
	{
		cerr << "Could not open 'person.dat'" << endl;
		exit(1);
	}
	
	// each line of "person.dat" has the format
	//			gender<blank>age<blank>name
	// where gender = 'M' or 'F'
	// read through the file, inserting each record in L
	while(!fin.eof())
	{
		// read gender field
		fin >> p.gender;
		// if we are at end of file, break the loop
		if (fin.eof())
			break;
		// read age field
		fin>> p.age;
		// clear the blank after the age
		fin.get(blank);
		// and read the name
		fin.getline(p.name,20,'\n');
		// insert record into the list (database)
		L.Insert(p);
	}
	
	// print all the males in the list and all the females
	// using PrintByGender
	cout << "Males in the database:" << endl;
	PrintByGender(L, 'M');
	cout << endl;
	
	cout << "Females in the database:" << endl;
	PrintByGender(L, 'F');
	cout << endl;
	
	cout << "Enter a name or <EOF>: ";
	while(cin.getline(name,20,'\n'))
	{
		// if a record with name exists, print it
		if (InList(L, name, p))
		{
			cout << p.name << "  " << p.age << "  ";
			if (p.gender == 'F')
				cout << "female" << endl;
			else
				cout << "male" << endl;
		}
		else
			cout << name << " is not in the database" << endl;
		cout << endl << "Enter a name or <EOF>: ";
	}
}

/*
<Run>

Males in the database:
Gronwall, Harry  35
Holter, William  95

Females in the database:
Mendez, Mary  23
Minden, Sylvia  18
Carlson, Donna  45

Enter a name or <EOF>: Mendez, Mary
Mendez, Mary  23  female

Enter a name or <EOF>: Holter, William
Holter, William  95  male

Enter a name or <EOF>: Barnes, Fred
Barnes, Fred is not in the database

Enter a name or <EOF>: <EOF>
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -