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
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
/*
* Definition of Patient structure
*/
struct patient
{
string date;
string name;
string ic;
string type;
string therapist;
int charge;
};
//so we don't have to write 'struct patient' every time, this way
//we can only write 'Patient'
typedef struct patient Patient;
/*
* Functions we'll use
*/
Patient* parse_file(string, int*);
Patient parse_line(string);
Patient* expand_array(Patient*, int*);
void print_patient(Patient&);
int main()
{
Patient* patients;
int numPatients = 0;
//read patients from file
patients = parse_file("input.txt", &numPatients);
for(int i = 0; i < numPatients; i++)
print_patient(patients[i]);
//free memory allocated for patients
delete[] patients;
system("pause");
return 0;
}
/*
* input: filename to be opened, pointer to integer
* output: array of Patients read from file
* summary: this function reads data from file and saves it into structure array
*/
Patient* parse_file(string filename, int* num)
{
//declare patients array
Patient* ret = new Patient[10];
string line;
*num = 0; //at first total number of patients is zero
ifstream input(filename.c_str(), ios::in);
if(!input.is_open())
{
cout << "Couldn't open file. Exiting..." << endl;
return NULL;
}
//waste first two lines
getline(input, line);
getline(input, line);
while(getline(input, line))
{
ret[*num] = parse_line(line);
*num += 1;
if(*num % 10 == 0)
{
ret = expand_array(ret, num);
}
line.clear();
}
input.close();
return ret;
}
Patient parse_line(string line)
{
Patient ret;
int phase = 0;
istringstream iss(line);
string token;
while (getline(iss, token, '\t'))
{
if(token[0] != 0)
{
//std::cout << token << std::endl;
switch(phase)
{
case 0:
ret.date = token;
break;
case 1:
ret.name = token;
break;
case 2:
ret.ic = token;
break;
case 3:
ret.type = token;
break;
case 4:
ret.therapist = token;
break;
case 5:
istringstream(token) >> ret.charge;
break;
}
phase++;
}
}
return ret;
}
Patient* expand_array(Patient* smaller, int* s)
{
Patient* ret = new Patient[*s + 10];
for(int i = 0; i < *s; i++)
ret[i] = smaller[i];
delete[] smaller;
return ret;
}
|