2017-11-23 2 views
0

주어진 데이터를 사용하여 카이 제곱 값을 계산하는 프로그램을 작성했습니다. 그러나 필자는 필자가 작성한 프로그램에서 텍스트 파일에 몇 줄이 있는지 알지 못하는 이유를 설명 할 수 있도록 필기체를 써야합니다. 내 구조 점을 점 [1000] (즉, 파일의 행 수보다 큼)으로 정의하면 계산이 작동하지 않고 결국 '나노'값이됩니다. 내가 입력 파일을 첨부읽는 데이터 텍스트 파일에 몇 줄이 포함되어 있는지 미리 알지 못하기 때문에 계정에 C++로 프로그램을 작성하는 방법은 무엇입니까?


#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 file

의견을 보내 주시면 감사하겠습니다.

+2

읽기 시작할 수있는 곳 : http://en.cppreference.com/w/cpp/numeric/math/nan, https : //en.m .wikipedia.org/wiki/NaN –

+2

[while (myFile.eof())'가 틀린 이유] (https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition) -considered-wrong) – Barmar

+0

문제를 판별하기 어려울 수 있습니다. –

답변

0

컴파일 타임에 포인트 수를 모르는 경우 동적으로 할당 된 메모리를 사용해야합니다. 포인트 수를 계산하기 위해 두 번에 걸쳐 파일을 읽은 다음 new을 사용하여 메모리를 한꺼번에 할당하고 두 번째 패스에서 포인트를 채우거나 데이터 구조를 사용하여 필요할 때 증가 할 수있는 포인트를 저장할 수 있습니다 당신이 파일을 읽는 동안. 시작 장소로 STL의 std :: vector를 살펴 보시기 바랍니다. 다음은 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 루프를 업데이트하여 하드 코드 된 코드를 제거하십시오.

관련 문제