流数据输出问题

发布于 2024-11-06 02:12:21 字数 5413 浏览 0 评论 0原文

在我的程序中,我的 dataout: void outfile 没有写入文件,有人能弄清楚为什么吗?

using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>

// Declaration of functions used
void writetable (double, double, double, double);
void tableout (double, double, double, double);
void secant(double, double, double, double, double, double, double, double, double, double&, int&);
void writedata (double, double, double);
void outfile(double, double, double&);
double fx( double, double, double, double, double, double, double);

const double tol=0.0001;    // Tolerance for convergence
const int max_iter=50;      // Maximum iterations allowed
// main program
int main()
{
    int iteration;          // Number of iterations

    double  kr, uc, q, b, radians;

    double x0, x1;          // Starting values for x
    double root;           // Root found by secant method
    const double PI = 4.0*atan(1.0);
    ifstream datain ("shuttle.txt");
    ofstream dataout ("results.txt");
    datain >> kr >> uc >> q >> b;
    x0= 1000;
    x1 = 200;
    writetable(kr, uc, q, b);
    tableout(kr, uc, q, b);
    for (double angle = 10; angle <= 70; angle += 15)
    {
        for (double velocity = 16000; velocity <= 17500; velocity += 500)
        {
            radians= angle * PI/180  ;
            //cout << velocity << endl;
            //  cout << radians << endl;
            //  cout << angle << endl;
            secant (radians, velocity, kr, uc, q, b, x0, x1, angle, root, iteration);
            writedata(angle, velocity, root);
        }
    }
    system("pause");
}

// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double radians, double velocity, double kr, double uc, double q, double b, double x0, double x1, double angle, double& root, int& iteration)
{
    double xnminus1, xnplus1, xn; // Local variables
    iteration=0;                  // Initialize iterations
    xnminus1=x0;
    xn=x1;
    do
    {
        ++iteration;
        xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/
                                  (fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
        //cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
        xnminus1 = xn;
        xn=xnplus1;
    }  
    while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
    root=xnplus1;  

    //cout<<"\nThe root is = "<<root<<endl;
    //cout<<"The number of iterations was = "<<iteration<<endl;
    //cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;

    outfile(angle, velocity, root);
}

// Defines "fx" 
double fx(double radians,double velocity, double kr, double uc, double q, double b, double ts)
{
    return kr * pow(ts,4.0) + uc * ts - q - pow((velocity / b), 2.0) * sin(radians);
}

void writetable(double kr, double uc, double q, double b)
{
    cout <<endl << "Input Parameters:" <<endl;
    cout<< "Kr(1/K^2)=" << kr << endl << "uc(1/K)=" << uc <<endl << "q(unitless)=" << q << endl << "b(mph)=" << b<< endl; 
    cout << "  angle..............velocity...........surface temp..............safe..........";
    cout << "  degs...............mph................Kelvin.....................?............";
    cout << "--------------------------------------------------------------------------------";      
}

void writedata (double angle, double velocity, double root)
{
    cout << left << "  " << angle << "                 "<<  velocity << "                 "<< fixed << setprecision(0) << setw(5) <<root<< "                 ";
    if(root <1000) 
        cout << "safe"<< endl; 
    else 
        cout << "unsafe" <<endl;
}

void tableout(double kr, double uc, double q, double b)
{
    ofstream dataout ("results.txt");
    dataout<<endl << "Input Parameters:" <<endl;
    dataout<< "Kr(1/K^2)=" << kr << endl << "uc(1/K)=" << uc <<endl << "q(unitless)=" << q << endl << "b(mph)=" << b<< endl; 
    dataout << "  angle..............velocity...........surface temp..............safe.........."<< endl;
    dataout << "  degs...............mph................Kelvin.....................?............"<< endl;
    dataout << "--------------------------------------------------------------------------------"<< endl;
}

void outfile (double angle, double velocity, double& root)                    
{
    ofstream dataout ("results.txt");
    dataout << left << "  " << angle << "                 "<<  velocity << "                 "<< fixed << setprecision(0) << setw(5) <<root<< "                 ";
    if(root <1000) 
        dataout << "safe"<< endl; 
    else 
        dataout << "unsafe" <<endl;
}

In my program, my dataout: void outfile is not writing to the file, can anyone figure out why?

using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>

// Declaration of functions used
void writetable (double, double, double, double);
void tableout (double, double, double, double);
void secant(double, double, double, double, double, double, double, double, double, double&, int&);
void writedata (double, double, double);
void outfile(double, double, double&);
double fx( double, double, double, double, double, double, double);

const double tol=0.0001;    // Tolerance for convergence
const int max_iter=50;      // Maximum iterations allowed
// main program
int main()
{
    int iteration;          // Number of iterations

    double  kr, uc, q, b, radians;

    double x0, x1;          // Starting values for x
    double root;           // Root found by secant method
    const double PI = 4.0*atan(1.0);
    ifstream datain ("shuttle.txt");
    ofstream dataout ("results.txt");
    datain >> kr >> uc >> q >> b;
    x0= 1000;
    x1 = 200;
    writetable(kr, uc, q, b);
    tableout(kr, uc, q, b);
    for (double angle = 10; angle <= 70; angle += 15)
    {
        for (double velocity = 16000; velocity <= 17500; velocity += 500)
        {
            radians= angle * PI/180  ;
            //cout << velocity << endl;
            //  cout << radians << endl;
            //  cout << angle << endl;
            secant (radians, velocity, kr, uc, q, b, x0, x1, angle, root, iteration);
            writedata(angle, velocity, root);
        }
    }
    system("pause");
}

// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double radians, double velocity, double kr, double uc, double q, double b, double x0, double x1, double angle, double& root, int& iteration)
{
    double xnminus1, xnplus1, xn; // Local variables
    iteration=0;                  // Initialize iterations
    xnminus1=x0;
    xn=x1;
    do
    {
        ++iteration;
        xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/
                                  (fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
        //cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
        xnminus1 = xn;
        xn=xnplus1;
    }  
    while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
    root=xnplus1;  

    //cout<<"\nThe root is = "<<root<<endl;
    //cout<<"The number of iterations was = "<<iteration<<endl;
    //cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;

    outfile(angle, velocity, root);
}

// Defines "fx" 
double fx(double radians,double velocity, double kr, double uc, double q, double b, double ts)
{
    return kr * pow(ts,4.0) + uc * ts - q - pow((velocity / b), 2.0) * sin(radians);
}

void writetable(double kr, double uc, double q, double b)
{
    cout <<endl << "Input Parameters:" <<endl;
    cout<< "Kr(1/K^2)=" << kr << endl << "uc(1/K)=" << uc <<endl << "q(unitless)=" << q << endl << "b(mph)=" << b<< endl; 
    cout << "  angle..............velocity...........surface temp..............safe..........";
    cout << "  degs...............mph................Kelvin.....................?............";
    cout << "--------------------------------------------------------------------------------";      
}

void writedata (double angle, double velocity, double root)
{
    cout << left << "  " << angle << "                 "<<  velocity << "                 "<< fixed << setprecision(0) << setw(5) <<root<< "                 ";
    if(root <1000) 
        cout << "safe"<< endl; 
    else 
        cout << "unsafe" <<endl;
}

void tableout(double kr, double uc, double q, double b)
{
    ofstream dataout ("results.txt");
    dataout<<endl << "Input Parameters:" <<endl;
    dataout<< "Kr(1/K^2)=" << kr << endl << "uc(1/K)=" << uc <<endl << "q(unitless)=" << q << endl << "b(mph)=" << b<< endl; 
    dataout << "  angle..............velocity...........surface temp..............safe.........."<< endl;
    dataout << "  degs...............mph................Kelvin.....................?............"<< endl;
    dataout << "--------------------------------------------------------------------------------"<< endl;
}

void outfile (double angle, double velocity, double& root)                    
{
    ofstream dataout ("results.txt");
    dataout << left << "  " << angle << "                 "<<  velocity << "                 "<< fixed << setprecision(0) << setw(5) <<root<< "                 ";
    if(root <1000) 
        dataout << "safe"<< endl; 
    else 
        dataout << "unsafe" <<endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2024-11-13 02:12:21

outfile 中的 open 是否成功。您已在 main 中打开文件进行输出;某些系统不允许同一文件打开两次,或打开两次进行输出。 (既然你不使用 main 中打开的文件,为什么要在那里打开它呢?)

Is the open in outfile succeeding. You've opened the file for output in main; some systems won't allow the same file to be opened twice, or opened twice for output. (Since you don't use the open file in main, why open it there?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文