#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
const int STUDENTS = 100;
const int SUBJECTS = 3;
const int LINE_LEN = 1024;
int main()
{
int grades[STUDENTS][SUBJECTS] = {0};
char line[LINE_LEN];
const char seps[] = " ,\t\n";
std::ifstream fin( "book1.csv");
fin.getline( line, LINE_LEN );
int studentIndex = 0;
while ( fin.good() && studentIndex < STUDENTS )
{
fin.getline( line, LINE_LEN );
if ( fin.eof() )
break;
int j = 0;
char* token = strtok( line, seps );
while( token != NULL && j < SUBJECTS )
{
grades[studentIndex][j++] = atoi( token );
token = strtok( NULL, seps );
}
++studentIndex;
}
for ( int i = 0; i < studentIndex; ++i )
{
std::cout << i+1 << ": ";
int sum = 0;
for ( int j = 0; j < SUBJECTS; ++j )
{
std::cout << std::setw(3) << grades[i][j] << ", ";
sum += grades[i][j];
}
std::cout << " sum=" << sum << " average=" << (double)sum / SUBJECTS << std::endl;
}
}
追问能留个QQ吗? [email protected]