/*
Name:Assignment 6
Author: muguangde
Date:2014-8-14: 12.0.0PM
Description: This is a simple program to match men and women in
two files from their last name and address, this program is to
practice to use class.
*/
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>
#include<iomanip>
using namespace std;
const int NUMCONS=25;
//Define the class of men
class CMen
{
public:
string first;
string last;
string address;
};
//Declare the class of women
class CWomen
{
public:
string first;
string last;
string address;
};
//Individual class for both men and women
class CIndividual
{
public:
CMen manName;
CWomen womanName;
};
void readMen(CIndividual[], int&);
void readWomen(CIndividual[], int&);
void sortMen(CIndividual[], int);
int match(CIndividual[],CIndividual[],int,int,int,int&);
int main()
{
int numMen=0;
int numWomen=0;
int indexMan=0;
ofstream outfile2;
//Declare objects
CIndividual men[NUMCONS];
CIndividual women[NUMCONS];
//To read the files of men and women from two files
readMen(men,numMen);
readWomen(women,numWomen);
//To sort the men from their last names
sortMen(men,numMen);
//To find the match women
outfile2.open("matchOrNot.txt");
for (int i=0; i<numWomen; i++) {
if(match(women,men,numWomen,numMen,i,indexMan)==1)
outfile2<<women[i].womanName.last<<" "
<<women[i].womanName.first<<" matched to "
<<men[indexMan].manName.first<<endl<<endl;
else
outfile2<<women[i].womanName.last<<" "
<<women[i].womanName.first<<", who's living in "
<<women[i].womanName.address
<<", doesn't match to anyone"<<endl
<<endl;
}
return 0;
}
//Function to read the data from men
void readMen(CIndividual men[], int &numMen)
{
ifstream infile;
string s;
infile.open("men.txt");
int num=0;
if(!infile.is_open()) {
//If the file can not be opened
cout<<"Error: can not open file"<<endl;
system("PAUSE");
exit(1);
}
//Determine the string length for first, last and address
while(!infile.eof() && getline(infile,s)) {
//Determine the first name by looking for first space
int i=s.find(" ");
men[num].manName.first=s.substr(0,i);
//From the input, all last name start from position 9
//determine the last character of last name
int j=s.find(" ",9)-9;
men[num].manName.last=s.substr(9,j);
//From the input, all address start from position 21
//determine the last character of address
int k=s.length()-1;
men[num].manName.address=s.substr(21,k);
num++;
}
numMen=num;
infile.close();
}
//Function to read the data from women
void readWomen(CIndividual women[], int &numWomen)
{
ifstream infile2;
string s;
infile2.open("women.txt");
int num=0;
if(!infile2.is_open()) {
//If the file can not be opened
cout<<"Error: can not open file"<<endl;
system("PAUSE");
exit(1);
}
//Determine the string length for first, last and address
while(!infile2.eof() && getline(infile2,s)) {
//Determine the first name by looking for first space
int i=s.find(" ");
women[num].womanName.first=s.substr(0,i);
//From the input, all last name start from position 9
//determine the last character of last name
int j=s.find(" ",9)-9;
women[num].womanName.last=s.substr(9,j);
//From the input, all address start from position 21
//determine the last character of address
int k=s.length()-1;
women[num].womanName.address=s.substr(21,k);
num++;
}
numWomen=num;
infile2.close();
}
//Function to sort the data from men and output
void sortMen(CIndividual men[],int numMen)
{
bool switched=true;
string tempLast,tempFirst,tempAddress;
int pass;
ofstream outfile;
outfile.open("sorted.txt");
if(!outfile.is_open()) {
//If the file can not be opened
cout<<"Error: can not open output file"<<endl;
system("PAUSE");
exit(1);
}
for(pass=0; pass<numMen-1 && switched==true; pass++ ) {
switched=false;
for(int i=0; i<numMen-pass-1; i++) {
if(men[i].manName.last>men[i+1].manName.last) {
switched=true;
tempLast=men[i].manName.last;
men[i].manName.last=men[i+1].manName.last;
men[i+1].manName.last=tempLast;
//Also swap the first name and address
tempAddress=men[i].manName.address;
men[i].manName.address=men[i+1].manName.address;
men[i+1].manName.address=tempAddress;
tempFirst=men[i].manName.first;
men[i].manName.first=men[i+1].manName.first;
men[i+1].manName.first=tempFirst;
}//end of if
}//end of inner for
}//end of outer for
for(int k=0; k<numMen; k++) {
// outfile.setf(ios::fixed);
outfile.width(8);
outfile<<men[k].manName.first;
outfile.width(12);
outfile<<men[k].manName.last;
outfile.width(28);
outfile<<men[k].manName.address<<endl;
}
}
//Function to determine the match of women
int match(CIndividual women[],CIndividual men[],
int numWomen,int numMen,int k,int &indexMan)
{
int matchOrNot=-1;
for(int j=0; j<numMen; j++) {
if(women[k].womanName.last.compare(men[j].manName.last)==0 &&
women[k].womanName.address.compare(men[j].manName.address)==0)
{
matchOrNot=1;
indexMan=j;//found the position of man
}
}//end of for
return matchOrNot;
}
回到主页
回到目录