将文本文件输出组织到列中

时间:2015-11-20 21:04:56

标签: c++ format output

一切都像它应该的那样工作,除非我输出到文本文件,我无法弄清楚如何保持所有输出排队。我的老师不会帮助我,我实际上已经用“fixed,setprecision,left,setw()等”进行了几个小时的试错。

//Project #5
//Start Date: November 17th
//Due Date: November 23rd

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
double f = 3.14159;
double upperBound = 0;
double lowerBound = 0;
double increment = 0;
double tempVal = 0;
double powVal = 0;
double expVal = 0;
double fact = 0;
double sinVal = 0;
double x = 0.0;
char userCont = 'Y';
int n = 0;
int i = 0;

//Factorial function using recursion...
double factorial(const int n) {
if (n <= 1) return 1;
fact = n * factorial(n - 1);
return fact;
}

//Power function using recursion...
double power(const double x, const int n) {
if (n <= 0)
    return 1;
powVal = x * power(x, n - 1);
return powVal;
}

//my_sin function using power and factorial functions
double my_sin(const double x) {
sinVal = 0;
for (int k = 0; k < 50; k++) {
    sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}

//my_exp(x) Function
double my_exp(const double x) {
expVal = 0;
for (int k = 0; k < 50; k++) {
    expVal += power(x, k) / factorial(k);
}
return expVal;
}


int main() {
ofstream fout("output.text");
while (userCont == 'y' || userCont == 'Y') {
    cout << "Enter lower and upper bounds: ";
    cin >> upperBound >> lowerBound;
    cout << "Enter Increment: ";
    cin >> increment;

    //Checking if upper and lower bounds are in the right order...
    if (upperBound < lowerBound) {
        tempVal = upperBound;
        upperBound = lowerBound;
        lowerBound = tempVal;
    }
    fout << "x        sin(x)             my_sin(x)          e(x)                      " <<
     "my_el(x)           my_exp(x)" << endl;

    //Loop to display and increase x by the incrememnt
    for (x = lowerBound; x <= upperBound; x = x + increment) {
        fout.precision(7);
        fout << setw(8) << left << x << "  ";
        fout << setw(8) << my_sin(x) << setw(8) << "  ";
        fout << setw(8) << sin(x) << setw(8) << "  ";
        fout << setw(8) << exp(x) << setw(8) << "  ";
        fout << setw(8) << exp(x) << setw(8) << "  ";
        fout << my_exp(x) << endl;
    }
    cout << "Another (y/n)? ";
    cin >> userCont;
}
return 0;
}

这是它应该如何看待 This is how it's supposed to look

1 个答案:

答案 0 :(得分:2)

一些清理工作以及我能够模仿格式化的最佳方法,希望它能带来一些灵感/帮助。

  

重要请注意我如何移除所有全局变量。他们不好:(

<强> Live On Coliru

// Project #5
// Start Date: November 17th
// Due Date: November 23rd

#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

double factorial(const int n) {
    if (n <= 1)
        return 1;
    return n * factorial(n - 1);
}

double power(const double x, const int n) {
    if (n <= 0)
        return 1;
    return x * power(x, n - 1);
}

// my_sin function using power and factorial functions
double my_sin(const double x) {
    double sinVal = 0;
    for (int k = 0; k < 50; k++) {
        sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
    }
    return sinVal;
}

// my_exp(x) Function
double my_exp(const double x) {
    double expVal = 0;
    for (int k = 0; k < 50; k++) {
        expVal += power(x, k) / factorial(k);
    }
    return expVal;
}

int main() {
    ofstream fout("output.text");
    char userCont = 'Y';

    while (userCont == 'y' || userCont == 'Y') {
        cout << "Enter lower and upper bounds: ";
        double upperBound, lowerBound;
        cin >> upperBound >> lowerBound;
        cout << "Enter Increment: ";

        double increment = 0;
        cin >> increment;

        // Checking if upper and lower bounds are in the right order...
        if (upperBound < lowerBound) {
            std::swap(upperBound, lowerBound);
        }

        fout << "   x             sin(x)          my_sin(x)       e(x)            my_el(x)     my_exp(x)" << endl;

        // Loop to display and increase x by the incrememnt
        for (double x = lowerBound; x <= upperBound; x = x + increment) {
            fout.precision(7);
            fout << right << std::fixed << std::setprecision(7) << setw(12) << x         << "  ";
            fout << right << std::fixed << std::setprecision(7) << setw(12) << my_sin(x) << setw(4) << "  ";
            fout << right << std::fixed << std::setprecision(7) << setw(12) << sin(x)    << setw(4) << "  ";
            fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x)    << setw(4) << "  ";
            fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x)    << setw(4) << "  ";
            fout << my_exp(x) << endl;
        }
        cout << "Another (y/n)? ";
        cin >> userCont;
    }
}

输出:

x             sin(x)          my_sin(x)       e(x)            my_el(x)     my_exp(x)
0.0000000     0.0000000       0.0000000       1.0000000       1.0000000    1.0000000
0.1000000     0.0998334       0.0998334       1.1051709       1.1051709    1.1051709
0.2000000     0.1986693       0.1986693       1.2214028       1.2214028    1.2214028
0.3000000     0.2955202       0.2955202       1.3498588       1.3498588    1.3498588
0.4000000     0.3894183       0.3894183       1.4918247       1.4918247    1.4918247
0.5000000     0.4794255       0.4794255       1.6487213       1.6487213    1.6487213
0.6000000     0.5646425       0.5646425       1.8221188       1.8221188    1.8221188
0.7000000     0.6442177       0.6442177       2.0137527       2.0137527    2.0137527
0.8000000     0.7173561       0.7173561       2.2255409       2.2255409    2.2255409
0.9000000     0.7833269       0.7833269       2.4596031       2.4596031    2.4596031
1.0000000     0.8414710       0.8414710       2.7182818       2.7182818    2.7182818
1.1000000     0.8912074       0.8912074       3.0041660       3.0041660    3.0041660
1.2000000     0.9320391       0.9320391       3.3201169       3.3201169    3.3201169
1.3000000     0.9635582       0.9635582       3.6692967       3.6692967    3.6692967
1.4000000     0.9854497       0.9854497       4.0552000       4.0552000    4.0552000
1.5000000     0.9974950       0.9974950       4.4816891       4.4816891    4.4816891
1.6000000     0.9995736       0.9995736       4.9530324       4.9530324    4.9530324
1.7000000     0.9916648       0.9916648       5.4739474       5.4739474    5.4739474
1.8000000     0.9738476       0.9738476       6.0496475       6.0496475    6.0496475
1.9000000     0.9463001       0.9463001       6.6858944       6.6858944    6.6858944
2.0000000     0.9092974       0.9092974       7.3890561       7.3890561    7.3890561
2.1000000     0.8632094       0.8632094       8.1661699       8.1661699    8.1661699
2.2000000     0.8084964       0.8084964       9.0250135       9.0250135    9.0250135
2.3000000     0.7457052       0.7457052       9.9741825       9.9741825    9.9741825
2.4000000     0.6754632       0.6754632      11.0231764      11.0231764    11.0231764
2.5000000     0.5984721       0.5984721      12.1824940      12.1824940    12.1824940
2.6000000     0.5155014       0.5155014      13.4637380      13.4637380    13.4637380
2.7000000     0.4273799       0.4273799      14.8797317      14.8797317    14.8797317
2.8000000     0.3349882       0.3349882      16.4446468      16.4446468    16.4446468
2.9000000     0.2392493       0.2392493      18.1741454      18.1741454    18.1741454
3.0000000     0.1411200       0.1411200      20.0855369      20.0855369    20.0855369
3.1000000     0.0415807       0.0415807      22.1979513      22.1979513    22.1979513
3.2000000    -0.0583741      -0.0583741      24.5325302      24.5325302    24.5325302
3.3000000    -0.1577457      -0.1577457      27.1126389      27.1126389    27.1126389
3.4000000    -0.2555411      -0.2555411      29.9641000      29.9641000    29.9641000
3.5000000    -0.3507832      -0.3507832      33.1154520      33.1154520    33.1154520
3.6000000    -0.4425204      -0.4425204      36.5982344      36.5982344    36.5982344
3.7000000    -0.5298361      -0.5298361      40.4473044      40.4473044    40.4473044
3.8000000    -0.6118579      -0.6118579      44.7011845      44.7011845    44.7011845
3.9000000    -0.6877662      -0.6877662      49.4024491      49.4024491    49.4024491
4.0000000    -0.7568025      -0.7568025      54.5981500      54.5981500    54.5981500
4.1000000    -0.8182771      -0.8182771      60.3402876      60.3402876    60.3402876
4.2000000    -0.8715758      -0.8715758      66.6863310      66.6863310    66.6863310
4.3000000    -0.9161659      -0.9161659      73.6997937      73.6997937    73.6997937
4.4000000    -0.9516021      -0.9516021      81.4508687      81.4508687    81.4508687
4.5000000    -0.9775301      -0.9775301      90.0171313      90.0171313    90.0171313
4.6000000    -0.9936910      -0.9936910      99.4843156      99.4843156    99.4843156
4.7000000    -0.9999233      -0.9999233     109.9471725     109.9471725    109.9471725
4.8000000    -0.9961646      -0.9961646     121.5104175     121.5104175    121.5104175
4.9000000    -0.9824526      -0.9824526     134.2897797     134.2897797    134.2897797
5.0000000    -0.9589243      -0.9589243     148.4131591     148.4131591    148.4131591
5.1000000    -0.9258147      -0.9258147     164.0219073     164.0219073    164.0219073
5.2000000    -0.8834547      -0.8834547     181.2722419     181.2722419    181.2722419
5.3000000    -0.8322674      -0.8322674     200.3368100     200.3368100    200.3368100
5.4000000    -0.7727645      -0.7727645     221.4064162     221.4064162    221.4064162
5.5000000    -0.7055403      -0.7055403     244.6919323     244.6919323    244.6919323
5.6000000    -0.6312666      -0.6312666     270.4264074     270.4264074    270.4264074
5.7000000    -0.5506855      -0.5506855     298.8674010     298.8674010    298.8674010
5.8000000    -0.4646022      -0.4646022     330.2995599     330.2995599    330.2995599
5.9000000    -0.3738767      -0.3738767     365.0374679     365.0374679    365.0374679
6.0000000    -0.2794155      -0.2794155     403.4287935     403.4287935    403.4287935
6.1000000    -0.1821625      -0.1821625     445.8577701     445.8577701    445.8577701
6.2000000    -0.0830894      -0.0830894     492.7490411     492.7490411    492.7490411
6.3000000     0.0168139       0.0168139     544.5719101     544.5719101    544.5719101
6.4000000     0.1165492       0.1165492     601.8450379     601.8450379    601.8450379
6.5000000     0.2151200       0.2151200     665.1416330     665.1416330    665.1416330
6.6000000     0.3115414       0.3115414     735.0951892     735.0951892    735.0951892
6.7000000     0.4048499       0.4048499     812.4058252     812.4058252    812.4058252
6.8000000     0.4941134       0.4941134     897.8472917     897.8472917    897.8472917
6.9000000     0.5784398       0.5784398     992.2747156     992.2747156    992.2747156
7.0000000     0.6569866       0.6569866    1096.6331584    1096.6331584    1096.6331584
7.1000000     0.7289690       0.7289690    1211.9670745    1211.9670745    1211.9670745
7.2000000     0.7936679       0.7936679    1339.4307644    1339.4307644    1339.4307644
7.3000000     0.8504366       0.8504366    1480.2999276    1480.2999276    1480.2999276
7.4000000     0.8987081       0.8987081    1635.9844300    1635.9844300    1635.9844300
7.5000000     0.9380000       0.9380000    1808.0424145    1808.0424145    1808.0424145
7.6000000     0.9679197       0.9679197    1998.1958951    1998.1958951    1998.1958951
7.7000000     0.9881682       0.9881682    2208.3479919    2208.3479919    2208.3479919
7.8000000     0.9985433       0.9985433    2440.6019776    2440.6019776    2440.6019776
7.9000000     0.9989413       0.9989413    2697.2823283    2697.2823283    2697.2823283
8.0000000     0.9893582       0.9893582    2980.9579870    2980.9579870    2980.9579870
8.1000000     0.9698898       0.9698898    3294.4680753    3294.4680753    3294.4680753
8.2000000     0.9407306       0.9407306    3640.9503073    3640.9503073    3640.9503073
8.3000000     0.9021718       0.9021718    4023.8723938    4023.8723938    4023.8723938
8.4000000     0.8545989       0.8545989    4447.0667477    4447.0667477    4447.0667477
8.5000000     0.7984871       0.7984871    4914.7688403    4914.7688403    4914.7688403
8.6000000     0.7343971       0.7343971    5431.6595914    5431.6595914    5431.6595914
8.7000000     0.6629692       0.6629692    6002.9122173    6002.9122173    6002.9122173
8.8000000     0.5849172       0.5849172    6634.2440063    6634.2440063    6634.2440063
8.9000000     0.5010209       0.5010209    7331.9735392    7331.9735392    7331.9735392
9.0000000     0.4121185       0.4121185    8103.0839276    8103.0839276    8103.0839276
9.1000000     0.3190984       0.3190984    8955.2927035    8955.2927035    8955.2927035
9.2000000     0.2228899       0.2228899    9897.1290587    9897.1290587    9897.1290587
9.3000000     0.1244544       0.1244544    10938.0192082    10938.0192082    10938.0192082
9.4000000     0.0247754       0.0247754    12088.3807302    12088.3807302    12088.3807302
9.5000000    -0.0751511      -0.0751511    13359.7268297    13359.7268297    13359.7268297
9.6000000    -0.1743268      -0.1743268    14764.7815656    14764.7815656    14764.7815656
9.7000000    -0.2717606      -0.2717606    16317.6071980    16317.6071980    16317.6071980
9.8000000    -0.3664791      -0.3664791    18033.7449278    18033.7449278    18033.7449278
9.9000000    -0.4575359      -0.4575359    19930.3704382    19930.3704382    19930.3704382
10.0000000    -0.5440211      -0.5440211    22026.4657948    22026.4657948    22026.4657948