我已经编写了一个程序来计算给出的数据的x平方值。但是,我需要编写它,这样我的程序就可以解释不知道它正在读取的文本文件中有多少行。如果我将我的结构点定义为点thePoints1000 (即大于文件中的行数),那么计算就不起作用,最后得到'nan‘值。这是我的节目:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
//define my own data structure
struct point {
double x;
double y;
double s;
double w;
double wx;
double wy;
double wxy;
double wxx;
double wmxcy;
};
//main program
int main() {
//array of point
point thePoints[11];
int i = 0;
//open a file to read in
ifstream myFile ("xys.data.txt");
//check if it opened successfully
if (myFile.is_open() ) {
//file is open
//read it in
while (!myFile.eof() ) {
//whilst we are not at the end of the file
myFile >> thePoints[i].x >> thePoints[i].y >> thePoints[i].s;
//increment i
i++;
}
//close the file
myFile.close();
}
// something went wrong
else {
cout << "Error opening file!\n";
exit(1);
}
// data is now in an array of point structures
//set the summation variables to zero - sets an initial value for the rest of the appropriate array to then be added onto
double Sw = 0;
double Swxx = 0;
double Swxy = 0;
double Swx = 0;
double Swy = 0;
double xsq = 0;
//create an array for w
for (int j = 0; j <= 10; j++){
thePoints[j].w = 1/(pow((thePoints[j].s),2));
//sum over all w i.e. create an array for Sw
Sw += thePoints[j].w;
//create an array for wx
thePoints[j].wx = (thePoints[j].w) * (thePoints[j].x);
//sum over all wx i.e. create an array for Swx
Swx += thePoints[j].wx;
//create an array for wy
thePoints[j].wy = (thePoints[j].w) * (thePoints[j].y);
//sum over all wy i.e. create an array for Swy
Swy += thePoints[j].wy;
//create an array for wxy
thePoints[j].wxy = (thePoints[j].w) * (thePoints[j].x) * (thePoints[j].y);
//sum over all wxy i.e. create an array for Swxy
Swxy += thePoints[j].wxy;
//create an array for wxx
thePoints[j].wxx = (thePoints[j].w) * (thePoints[j].x) * (thePoints[j].x);
//sum over all wxx i.e. create an array for Swxx
Swxx += thePoints[j].wxx;
}
printf("%6.2f, %6.2f, %6.2f, %6.2f, %6.2f\n", Sw, Swx, Swy, Swxy, Swxx);
//caluculate a value for D
double D = ((Sw * Swxx) - (Swx * Swx));
//calculate a value for m
double m = ((Sw * Swxy) - (Swx * Swy))/D;
//calculate a value for dm
double dm = sqrt(Sw/D);
//calculate a value for c
double c = ((Swy * Swxx) - (Swx * Swxy))/D;
//calculate a value for dc
double dc = sqrt(Swxx/D);
//calculate chi-squared value, xsq = Sw(((m * x) + c - y)^2)
for (int j = 0; j < i; j++){
thePoints[j].wmxcy = (thePoints[j].w * (pow(((m * thePoints[j].x) + c - thePoints[j].y),2)));
//sum over all chi-squared
xsq += thePoints[j].wmxcy;
}
//prints all of the results of the data
printf("The equation of the line for the data is y = %6.2f x + %6.2f.\n", m, c);
printf("The gradient, m, has an associated error of %6.2f.\n", dm);
printf("The y intercept, c, has an associated error of %6.2f.\n", dc);
printf("The data has a chi-squared value of %6.2f.\n", xsq);
return 0;
}我已经附加了输入文件.txt文件
如有任何意见,将不胜感激。
发布于 2017-11-24 01:55:23
如果您不知道编译时的点数,那么应该使用动态分配的内存。您可以通过两次读取文件来计数点数,然后使用new一次性分配内存并在第二次传递时填充这些点,或者使用数据结构存储在读取文件时可以根据需要增长的点。我建议您看一下std::载体,从STL作为起点。下面是一个使用std::vector的简单示例。
//vector of point
std::vector<point> thePoints;
//open a file to read in
ifstream myFile ("xys.data.txt");
//check if it opened successfully
if (myFile.is_open() ) {
//file is open
//read it in
while (!myFile.eof() ) {
point aPoint;
//whilst we are not at the end of the file
myFile >> aPoint.x >> aPoint.y >> aPoint.s;
//add a point to the vector of points
thePoints.push_back(aPoint);
}
//close the file
myFile.close();
}您可以通过thePoints.size()获得点数。确保更新所有for循环以删除硬编码的10。
https://stackoverflow.com/questions/47463045
复制相似问题