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
|
//the file prog1.txt must to have format according to
//Each line in the data file should contain a student’s name in
#include <fstream> /* for file i/o */
#include <iostream> /* interactive i/o*/
#include <string> // substr
using namespace std;
//here is defined the class
class Cstudent
{
private:
/*data of the class*/
char NAME[20], STANDING[10], GPA[5]; // first is the type, second the name
public:
//functions of the class Cstudent
void name(char a[20]) //asign the value of A to name
{
for (int i = 0; i<20; i++)
{
NAME[i] = a[i];
}
cout << NAME;
cout << "\n";
}
void standing(char b[10]) //asign the value of b to standing
{
for (int i = 0; i<10; i++)
{
STANDING[i] = b[i];
}
}
void gpa(char c[5]) //asign the value of c to GPA
{
for (int i = 0; i<5; i++)
{
GPA[i] = c[i];
}
}
char give_standing()
{
return STANDING[0];
}
char give_name()
{
return NAME[0];
}
};
//the function swap interchanges the values of string of students
void swap(Cstudent stdt1, Cstudent stdt2)
{
Cstudent temp = stdt1;
stdt1 = stdt2; // Swap two students’ records
stdt2 = temp;
}
int main() {
Cstudent students[25], fresh[25], soph[25], jun[25], sen[25]; /* the arrays of students (objects) */
char s[35], name_aux[20], standing_aux[10], gpa_aux[5];
ifstream infile("students.txt"); // read student data from the file
for (int i = 0; i<24; i++) // read line by line; 25 lines
{
infile.getline(s, 40); // put each line in the "s" variable
// cout<< s; //this prints are from tests
// cout<<"\n";
for (int j = 0; j<20; j++) //in this cycles descompose the variable s
{
name_aux[j] = s[j]; // the firsts 20 values goes to name
}
int k = 0;
for (int j = 20; j<30; j++) //between 20 and 30 goes to standing
{
k = k + 1; // k is an auxiliar variable to initialize the count
standing_aux[k] = s[j];
}
k = 0;
for (int j = 30; j<35; j++) // and the gpa
{
k = k + 1;
gpa_aux[k] = s[j];
}
students[i].name(name_aux); //call the function to asign the value taken of each line
students[i].standing(standing_aux);
students[i].gpa(gpa_aux);
// cout<<name_aux; //this print is for test
}
/* Use Selection sort to sort the array */
int i, j, k = 0;
char temp; //variable referential to sort
for (i = 0; i<25; i++)
{
temp = students[i].give_name();
j = 1;
for (j = i + j; j<26; j++)
{
// cout<<(temp >= students [j].give_name());
if (temp >= students[j].give_name())
{
temp = students[j].give_name();
k = j;
swap(students[i], students[k]);
}
}
}
/* copying freshman, sophomore, junior and senior
Fresh =freshman list
Soph = sophomore list
Jun=junior list
Sen=senior list */
/*to select the standing */
int fr = 0, so = 0, ju = 0, se = 0; /*fr=freshman index, so=sophomore index, ju=junior index, se=senior index*/
for (i = 0; i<25; i++)
{
switch (students[i].give_standing())
{
case 'r': //freshman llist/
fresh[fr++] = students[i];
break;
case 'o': // sophomore list
soph[so++] = students[i];
break;
case 'u': //junior list
jun[ju++] = students[i];
break;
case 'e': // senior list
sen[se++] = students[i];
break;
}
// cout<<students[i].give_name();
}
system("break");
return 0;
}
/* the results are not the better, i think is better to try whit string, not with char,
it is nescesary to adjust the sort algoritm */
|