6.3w+
社区成员
//源数据在3.26.txt里面;
//处理后的数据在3.26__.txt里面;
#include <fstream>
#include <string>
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
const int Row=100;
const int Coll=0;
double dDest[Row];
ifstream inFile("3-26.txt");
ofstream outFile("3-26__.txt");
assert(inFile!=NULL);
assert(outFile!=NULL);
string line;
int i=0, count=0;
double sum=0.0;
while ( !inFile.eof() )
{
getline(inFile, line, '\n');
sum+=atof( line.c_str() );
dDest[count++]=sum;
}
inFile.close();
for (int i=0; i<Row; i++)
{
outFile<<dDest[i];
if( (i+1)%10==0 )
outFile<<endl;
else
outFile<<" ";
}
outFile.close();
return 0;
}
//输出数据:
1.21989 2.43985 3.65957 4.87952 6.09953 7.31924 8.5392 9.75911 10.9791 12.1988
1.21997 2.43988 3.65976 4.87972 6.09954 7.31935 8.53927 9.75924 10.9791 12.1991
0.611675 1.2235 1.83535 2.44713 3.05904 3.67091 4.28265 4.89452 5.50634 6.1181
0.611831 1.22373 1.83549 2.44723 3.05919 3.67084 4.28271 4.89446 5.5063 6.11818
0.611871 1.22365 1.83549 2.44723 3.05913 3.67089 4.28277 4.89452 5.50639 6.11819
0.611722 1.22362 1.83539 2.44728 3.05913 3.67096 4.28283 4.89449 5.50644 6.11822
0.611949 1.22361 1.83549 2.44732 3.05918 3.67108 4.28285 4.89476 5.50649 6.1183
0.611882 1.22365 1.83554 2.44731 3.05923 3.67098 4.28284 4.89463 5.50652 6.11841
0.611852 1.22362 1.83551 2.44717 3.05914 3.6709 4.28267 4.89458 5.50642 6.11819
0.611833 1.2237 1.83545 2.44733 3.05924 3.67102 4.28288 4.8947 5.50657 -9.25596e+061
#include <fstream>
#include <string>
#include <iostream>
#include <assert.h>
using namespace std;
int main()
{
const int Row=10;
const int Coll=10;
double dDest[Row][Coll];
ifstream inFile("3-25.txt");
assert(inFile!=NULL);
string line;
int i=0, j=0, count=0;
double sum=0.0;
while ( !inFile.eof() )
{
j = count%Coll;
if(j == 0)
sum=0.0;
i = count/Row;
getline(inFile, line, '\n');
sum+=atof( line.c_str() );
dDest[i][j]=sum;
count++;
}
inFile.close();
for (int i=0; i<Row; i++)
for(int j=0; j<Coll; j++)
{
cout<<dDest[i][j];
if( (j+1)%Coll==0 )
cout<<endl;
else
cout<<" ";
}
return 0;
}
二维数组,lz你的第二维有几个元素了?
你没有说清楚啊,
lz是不是搞三维的?呵呵
可以:
float sum=0;
ifstream inFile("input.txt");
assert(inFile!=NULL);
string line;
int i=0;
while( !inFile.eof() )
{
getline(inFile, line, '\n');
sum+=atof(line.c_str());
array[i++]=sum;
}
atof将字符串转化为float函数,累加就可以的;