0
我正在爲我的編程類工作:按課程中討論的實現平均等級程序。成績信息將顯示在一個輸入文件中,每個學生的信息分爲兩行:第一行是名字中間名姓氏格式的學生姓名,第二行是學生的成績,即整數。 (學生可能沒有中間名。)最多有20名學生,每個學生將有10個成績。程序應該向用戶請求輸入文件的名稱。對於每個學生,計算他們的整體平均值(假設每個任務值得相同的點數)。將信息輸出到屏幕上,每個學生一行,每行是他們的'姓名'(姓氏,名字中間初始,10個等級和平均值,按姓氏排序。平均值應該用兩位小數顯示(即3.1會爲3.10輸出),你應該使用三個數組這個問題,討論並通過一套合理的功能。這是我到目前爲止所。使用swap按字母順序排列字符串數組
#include <iostream>
#include <string>
#include <fstream>
#include <climits>
#include <iomanip>
using namespace std;
const int NGrades= 10;
const int maxStudents=20;
string reformat(string& s);
int main(){
int num_of_students = 0;
string fullName;
double sum=0;
string names[maxStudents];
int grades[maxStudents][NGrades];
double average[maxStudents];
string fileName;
ifstream inputFile;
cout<< "Please type the file name including extension(such as .txt)."<<endl;
cout<< "If your file is in a different directory please specify the path:"; //asking user for file name. seperated into two cout statments for readibility
getline(cin,fileName);
inputFile.open(fileName.c_str());
if (!inputFile){ //produce an error if the file name is invalid
cout<<"Cannot open "<<fileName<<"."<<endl;
return 1;
}
while(getline(inputFile, fullName)){
names[num_of_students]=reformat(fullName);
cout << setw(20)<< names[num_of_students]<<" "<< setw(20);
for (int i = 0; i < NGrades; ++i){
inputFile >> grades[num_of_students][i];
cout <<setw(4)<<grades[num_of_students][i];
sum = sum + grades[num_of_students][i];
}
average[num_of_students]= sum/NGrades;
sum=0;
cout <<setw(15);
cout<< fixed << showpoint;
cout << setprecision(2);
cout <<average[num_of_students]<< endl;
inputFile.ignore(INT_MAX, '\n');
++num_of_students;
}
inputFile.close();
return 0;
}
string reformat(string& s){
int pos, posTwo;
string first_Middle;
string lastname;
string finished;
pos = s.find_first_of(' ');
first_Middle=s.substr(0,pos+2);
posTwo=s.find_first_of(' ', pos+1);
lastname=s.substr(posTwo+1);
finished=lastname+ ", "+first_Middle;
return finished;
}
我需要做什麼現在是按字母順序排列姓氏使用交換。我不允許使用結構或類似的東西。
使用swap實際上是部分任務嗎?你確定你不是指std :: sort嗎? – Cogwheel 2013-02-14 03:50:28