1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Roster
{
string lname;
string fname;
char cheese;
};
const int MAXSIZE = 80;
char * givemename(char* fn);
int recordcount(const char * filename);
struct Roster* readarray(const int rcount, char *filename);
void readsurvey(const int rcount, const char * filename, struct Roster *);
int countp(const int rcount, struct Roster *MyRoster);
int countc(const int rcount, struct Roster *MyRoster);
void swap(struct Roster *MyRoster1,struct Roster * MyRoster2);
void BubbleSort (const int rcount, struct Roster*MyRoster);
void DrawRecords(const int rcount, struct Roster*MyRoster);
int drawsummary(const int rcount,const int pcount, const int ccount);
int main(void)
{
char * filename = new char [MAXSIZE] ;
struct Roster * MyRoster;
struct Roster *PassedRoster;
int rcount=0, pcount=0, ccount=0;
filename=givemename(filename);
rcount = recordcount(filename);
MyRoster = new struct Roster[rcount];
readsurvey(rcount, filename, MyRoster);
pcount = countp(rcount,MyRoster);
ccount = countc(rcount,MyRoster);
BubbleSort (rcount, MyRoster);
DrawRecords(rcount,MyRoster);
drawsummary(rcount,pcount,ccount);
return 0;
}
char * givemename(char * fn)
{
ifstream myfile;
cout << "Please enter a file name : ";
cin >> fn;
myfile.open(fn);
while(!myfile.is_open())
{
cout << "Invalid file name. Please re-enter: ";
cin >> fn;
myfile.open(fn);
}
return fn;
}
int recordcount ( const char* filename)
{
int count=0;
char inputline[MAXSIZE];
ifstream myfile;
myfile.open(filename);
do
{
count ++;
myfile.getline(inputline, MAXSIZE);
cout <<inputline<< endl;
}
while(!myfile.eof());
myfile.close();
return count;
}
void readsurvey(const int rcount, const char * fn, struct Roster *mr)
{
ifstream myfile;
myfile.open(fn);
for(int counter=0; counter<rcount ; counter++)
{
myfile >> mr[counter].lname >> mr[counter].fname >> mr[counter].cheese;
}
}
int countp(const int rcount, struct Roster* MyRoster)
{
int count = 0;
for (int i = 0; i<=rcount; i++)
{
if(MyRoster[i].cheese == 'P')
{
count ++ ;
}
}
return count;
}
int countc(const int rcount, struct Roster* MyRoster)
{
int count = 0;
for (int i = 0; i<=rcount; i++)
{
if(MyRoster[i].cheese == 'C')
{
count ++ ;
}
}
return count;
}
void swap(struct Roster* MyRoster1,struct Roster* MyRoster2)
{
struct Roster* temp = new struct Roster;
temp = MyRoster1;
MyRoster1 = MyRoster2;
MyRoster2 = temp;
}
void BubbleSort (const int rcount,struct Roster*MyRoster)
{
bool swapped=true;
int j = 0;
while (swapped)
{
swapped = false;
j++;
for (int i=0; i<=rcount; i++)
{
if ( MyRoster[i].lname > MyRoster[i+1].lname)
{
swap(&MyRoster[i], &MyRoster[i+1]);
swapped=true;
}
}
}
}
void DrawRecords(const int rcount, struct Roster*MyRoster)
{
cout <<" THE CHEESECAKE REPORT " << endl
<<"LAST NAME FIRST NAME CAKE OR PIE " << endl
<<"__________ __________ ___________ "<< endl;
for(int i=0; i <= rcount; i++)
{
cout << MyRoster[i].lname << MyRoster[i].fname;
if(MyRoster[i].cheese == 'C')
{ cout << "CAKE";}
else
cout << "PIE";
cout << endl;
}
}
int drawsummary(const int rcount,const int pcount,const int ccount )
{
cout << "Number of Records: " << rcount << endl << "Number of People"
"Who Believe Cheesecake is Pie: " << pcount << endl << "Number of "
"People Who Believe Cheesecake is Cake: " << ccount << endl;
return 0;
}
|