Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
using namespace std;
void loadData(string firstname[],string lastname[],int score1[],int score2[],int score3[])
{
int i=0;
ifstream in;
in.open("F:\\test 4.d\\New Folder (4)\\Data.dat");
if(in.fail())
{
in.close();
cout<<"Data.txt not found,program ending!"<<endl;
system("Pause");
exit(1);
}
for(int i=0;i<25;i++)
{
in >> firstname[i] >> lastname[i] >> score1[i] >> score2[i] >> score3[i];
}
}
void ChangeLastName(string lastname[25])
{
for(int i=0; i<25; i++)
{
string tmp = lastname[i];
char temp[tmp.length()];
strcpy(temp, tmp.c_str());
for(int j=0; j<tmp.length(); j++)
{
temp[j] = toupper(temp[j]);
}
lastname[i] = temp;
}
}
void swapValues(string& v1,string& v2)
{
string temp;
temp=v1;
v1=v2;
v2=temp;
}
void swapValues(int& v1,int& v2)
{
int temp;
temp=v1;
v1=v2;
v2=temp;
}
void sorting(string lastname[],string firstname[],int score1[],int score2[],int score3[])
{
for(int i=0;i<25;i++)
{
for(int j=0; j<25; j++)
{
if(lastname[i] < lastname[j])
{ swapValues(lastname[i],lastname[j]);
swapValues(firstname[i],firstname[j]);
swapValues(score1[i],score1[j]);
swapValues(score2[i],score2[j]);
swapValues(score3[i],score3[j]); }
}
}
}
void grade(int score1[],int score2[],int score3[],double ave[],double aveScore[],double sum[],string rank[])
{
sum[0]=0,sum[1]=0,sum[2]=0;
for(int i=0;i<25;i++)
{
ave[i]=(score1[i]+score2[i]+score3[i])/3.;
if(ave[i]>=90)
rank[i]="A";
else if(ave[i]<90&&ave[i]>=80)
rank[i]="B";
else if(ave[i]<80&&ave[i]>=70)
rank[i]="C";
else if(ave[i]<70&&ave[i]>=60)
rank[i]="D";
sum[0]+=score1[i];
sum[1]+=score2[i];
sum[2]+=score3[i];
}
aveScore[0]=sum[0]/25.;
aveScore[1]=sum[1]/25.;
aveScore[2]=sum[2]/25.;
}
void WriteToFile(string lastname[],string firstname[],int score1[],int score2[],int score3[],double ave[],double aveScore[],string rank[])
{
int i=0,j=0;
ofstream out;
out.open("F:\\test 4.d\\New Folder (4)\\Data2.dat");
if(out.fail())
{
out.close();
cout<<"Data2.txt not found,program ending!"<<endl;
exit(1);
}
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);
out<<"LASTNAME"<<setw(20)<<"FIRSTNAME"<<setw(10)<<"SCORE1"<<setw(10)<<"SCORE2"<<setw(10)<<"SCORE3"<<setw(10)<<"RANK"<<endl;
out<<"*************************************************************************************************************************************"<<endl;
for(int i=0;i<25;i++)
{ out<<lastname[i]<<","<<setw(20)<<firstname[i]<<":"<<score1[i]<<setw(10)<<score2[i]<<score3[i]<<ave[i]<<rank[i]<<endl;
}
out<<"*************************************************************************************************************************************"<<endl;
out<<"TEST AVERAGE"<<setw(30)<<aveScore[0]<<setw(10)<<aveScore[1]<<setw(10)<<aveScore[2]<<endl;
}
int main(int argc, char *argv[])
{
string firstname[25], lastname[25],rank[25];
int score1[25], score2[25], score3[25],score[3];
double aveScore[3],sum[3],ave[25];
loadData(firstname,lastname,score1,score2,score3);
ChangeLastName(lastname);
ChangeLastName(firstname);
sorting(lastname,firstname,score1,score2,score3);
grade(score1,score2,score3,ave,aveScore,sum,rank);
WriteToFile(lastname,firstname,score1,score2,score3,ave,aveScore,rank);
system("pause");
return 0;
}
this is my output!