怎样在c++中读入txt文件
txt文件中共有1000个数,就是把6分成1000份,0.006,0.012,0.018。。。一直到6(数组A[i])。现在要读入后计算得到数组B[i],其中B[i]=p...
txt文件中共有1000个数,就是把6分成1000份,0.006,0.012,0.018。。。一直到6(数组A[i])。现在要读入后计算得到数组B[i],其中B[i]=pow(2.718,A[i]),也就是说B[i]=exp(A[i])。从txt读入A[i]1000个数据后计算完得到B[i]1000个数据输出,求达人帮忙编个程序。十分感谢。高分答谢(>100分)。急!!!
展开
展开全部
看一下这样如何。
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
void main()
{
/*
ofstream out("data.txt");
double num = 0.006;
for (int i = 1; i <= 1000; ++i)
{
out << num * i << endl;
}
out.close();
*/
ifstream in("data.txt");
double A[1000], B[1000], temp;
int j, i = 0;
if (!in)
{
cerr << "Can't open the file" << endl;
return;
}
while (1)
{
in >> temp;
if (in.eof()) break;
A[i++] = temp;
}
cout << i << endl;
for (j = 0; j < i; ++j)
{
B[j] = pow(2.718, A[j]);
cout << B[j] << ' ';
}
cout << endl;
in.close();
}
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
void main()
{
/*
ofstream out("data.txt");
double num = 0.006;
for (int i = 1; i <= 1000; ++i)
{
out << num * i << endl;
}
out.close();
*/
ifstream in("data.txt");
double A[1000], B[1000], temp;
int j, i = 0;
if (!in)
{
cerr << "Can't open the file" << endl;
return;
}
while (1)
{
in >> temp;
if (in.eof()) break;
A[i++] = temp;
}
cout << i << endl;
for (j = 0; j < i; ++j)
{
B[j] = pow(2.718, A[j]);
cout << B[j] << ' ';
}
cout << endl;
in.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询