
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cstdlib>
#include<sstream>
const int MaxEntries = 120// max length of characters in each (actually 49 since last byte is null '\0'
int grades[500][20] = {0}
int grades_columns[500] = {0}// 记录每一行的实际个数
using namespace std
string int2string( int value )
{
stringstream strStream
strStream <<value
return strStream.str()
}
int main()
{
cout<<"These are the students quizz averages:"<<endl
cout<<"------------------------------------------------------"<<endl
char s[MaxEntries]// declare a char array to hold the string of characters read
ofstream outFile("c:\\avgGrades.csv")
ifstream inFile ("c:\\grades2.csv")// open the file specified by the path
int QuizzEntry = 0// count how many student records read
int columns// the scope is now greater than including in the while loop
while(!inFile.eof() )
{
inFile.getline(s, MaxEntries)
columns = 0
char* p = strtok(s, ",")// separate into substrings by comma delimiter and get the first token
if ( p )
{
while(p!=NULL)
{
grades[QuizzEntry][columns++] = atoi(p)
p=strtok(NULL, ",")// get the next token
}
grades_columns[QuizzEntry] = columns
++QuizzEntry// increment the rows
}
}
for(int rows=0rows <QuizzEntryrows++)
{
string str=""
cout<<rows+1<<" )"
int sum=0
double average
for(int c=0c <grades_columns[rows]c++)
{
cout<<setw(6)<<grades[rows][c]
sum+= grades[rows][c]
str+= int2string(grades[rows][c]) +","
}
average=(double)sum/grades_columns[rows]
str+=int2string(average)
outFile<<str<<endl
cout<<"\t average="<<average<<endl
}
inFile.close()
return 0
}
不要用csv库, 直接用pandaspandas读取完csv文件以后,哪厅直接加一列,或者直接修改列的内容
至于怎么迅者补全字符串,可以去搜“python字符串补齐亩缓薯”
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)