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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Cstudent
{
private:
char name[20];
char Class[10];
float GPA[5];
public:
void setName(char x[20])
{
for (int i = 0; i < 20; i++)
name[i] = x[i];
}
void setClass(char x[10])
{
for (int i = 0; i < 10; i++)
Class[i] = x[i];
}
void setGPA(float x[])
{
for (int i = 0; i < 5;i++)
GPA[i] = x[i];
}
char getName()
{
return name[0];
}
char getClass()
{
return Class[0];
}
float getGPA()
{
return GPA[0];
}
};
void swap(Cstudent stdt1, Cstudent stdt2)
{
Cstudent temp = stdt1;
stdt1 = stdt2; /* Swap two students’ records */
stdt2 = temp;
}
int main()
{
const int x = 25;
Cstudent students[x], fresh[25], soph[25], jun[25], sen[25];
char s[80];
Cstudent name[20];
Cstudent Class[10];
Cstudent GPA;
string students1[25];
ifstream infile("Students.txt"); /* read student data from the file*/
for (int i = 0; i < 25; i++)
{
infile.getline(s, 40);
cout << s;
(s, 0, 20,students[i].setName);
(s, 21, 30, students[i].setClass);
(s, 31, 34, students[i].setGPA);
for (int i = 0; i < 25; i++)
{
char temp = students[i].getName();
for (int j = i + 1; j< 25; j++)
{
if (temp > students[j].getName())
{
temp = students[j].getName();
int 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 */
int fr = 0, so = 0, ju = 0, se = 0; //(*fr = freshman index, so = sophomore index, ju = junior index, se = senior index*)
for (int i = 0; i < 25; i++)
{
switch (students[i].getClass())
{
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;
}
}
/* for (int i = 0; i < 25; i++)
{
cout << students[i].getName() << students[i].getClass() << students[i].getGPA() << endl;
} */
system("pause");
return 0;
}
}
|