更改if范围内变量的类型

时间:2017-12-12 20:54:31

标签: c++

#include <iostream>
int main(){
    for (int i = 1; i <=5; i++){
    if (i == 3)
        char i[3] = "abc";
    std::cout << i << "\n";
    }
}

输出:

1
2
3
4
5

所以问题是char i[3]仅限于if内部,如果我在if语句中尝试cout它将按预期打印"abc"但在if句子之外不起作用,我可以称之为不粘。那么我如何声明它以便它在if句子之外工作而我不想在if句子中添加cout并且我不想更改cout在for循环中,因为在我的实际程序中,我正在打印一个表。

更新:这是我的实际代码:

#include <iostream>
#include <iomanip>
#include <cmath>
#define pi 3.14159265359

double d2r(int deg) {
    double rad = (pi * deg) / 180;
    return rad;
}

int main() {
    int sw = 2;
    cout << "Deg" << "\t" 
        << "Sin" << "\t"
        << "Cos" << "\t"
        << "Tan" << "\t"
        << "\n";
    for (int deg = 2; deg <= 90; deg += 2) {
        double A = sin(d2r(deg));
        double B = cos(d2r(deg));
        double C = tan(d2r(deg));
        if (abs(B) < 0.00009)
            B = 0;
        if (abs(C) > 100)
            char C[4] = "Inf";
        cout << setprecision(3) << fixed
            << deg << setw(sw) << "\t"
            << A << setw(sw) << "\t"
            << B << setw(sw) << "\t"
            << C << setw(0) << "\n";
    }
}

2 个答案:

答案 0 :(得分:5)

您无法像尝试一样更改C的类型。对于不同的数据类型,您需要单独的cout <<语句,例如:

cout << setprecision(3) << fixed
     << deg << setw(sw) << "\t"
     << A << setw(sw) << "\t"
     << B << setw(sw) << "\t";

if (abs(C) > 100)
    cout << "Inf";
else
    cout << C;

/* alternatively:
(abs(C) > 100) ? (cout << "Inf") : (cout << C);
*/

cout << setw(0) << "\n";

或者,根据您需要的标准格式化std::string,然后输出该字符串,例如:

std::ostringstream oss;

if (abs(C) > 100)
    oss << "Inf";
else
    oss << setprecision(3) << fixed << C;

/* alternatively:
(abs(C) > 100) ? (oss << "Inf") : (oss << setprecision(3) << fixed << C);
*/

cout << setprecision(3) << fixed
     << deg << setw(sw) << "\t"
     << A << setw(sw) << "\t"
     << B << setw(sw) << "\t"
     << oss.str() << setw(0) << "\n";

答案 1 :(得分:2)

您可以编写自己的I / O操纵器,例如:

#include <iostream>
#include <iomanip>
#include <cmath>
#define pi 3.14159265359

double d2r(int deg) {
    double rad = (pi * deg) / 180;
    return rad;
}

struct inf_if_greater_magnitude
{
    inf_if_greater_magnitude(double val, double limit) : val(val), limit(limit) {}

    friend std::ostream& operator<<(std::ostream&os, inf_if_greater_magnitude const& iig)
    {
        if (std::isinf(iig.val) or std::isnan(iig.val) or (std::abs(iig.val) > iig.limit))
            return os << "Inf";
        else
            return os << iig.val;
    }

    double val;
    double limit;
};

int main() {
    using namespace std;
    int sw = 2;
    cout << "Deg" << "\t" 
        << "Sin" << "\t"
        << "Cos" << "\t"
        << "Tan" << "\t"
        << "\n";
    for (int deg = 2; deg <= 90; deg += 2) {
        double A = sin(d2r(deg));
        double B = cos(d2r(deg));
        double C = tan(d2r(deg));
        if (abs(B) < 0.00009)
            B = 0;
        cout << setprecision(3) << fixed
            << deg << setw(sw) << "\t"
            << A << setw(sw) << "\t"
            << B << setw(sw) << "\t"
            << inf_if_greater_magnitude(C, 100) << "\n";
    }
}

预期结果:

...
84  0.995   0.105   9.514
86  0.998   0.070   14.301
88  0.999   0.035   28.636
90  1.000   0.000   Inf

http://coliru.stacked-crooked.com/a/0da21c8ef5579f95

进一步的重构可能会给你这样的东西:

#include <iostream>
#include <iomanip>
#include <cmath>
#define pi 3.14159265359

double d2r(int deg) {
    double rad = (pi * deg) / 180;
    return rad;
}

struct output_tan
{
    output_tan(double val) : val(val) {}

    friend std::ostream& operator<<(std::ostream&os, output_tan const& iig)
    {
        if (std::isinf(iig.val) 
         or std::isnan(iig.val) 
         or (std::abs(iig.val) > 100))
            return os << "Inf";
        else
            return os << iig.val;
    }

    double val;
};

struct output_cos
{
    output_cos(double val) : val(val) {}

    friend std::ostream& operator<<(std::ostream&os, output_cos const& iig)
    {
        auto v = iig.val;
        if (v < 0.00009) v = 0;
        return os << v;
    }

    double val;
};

int main() {
    using namespace std;
    int sw = 2;
    cout << "Deg" << "\t" 
        << "Sin" << "\t"
        << "Cos" << "\t"
        << "Tan" << "\t"
        << "\n";
    for (int deg = 2; deg <= 90; deg += 2) {
        cout << setprecision(3) << fixed
            << deg << setw(sw) << "\t"
            << sin(d2r(deg)) << setw(sw) << "\t"
            << output_cos(cos(d2r(deg))) << setw(sw) << "\t"
            << output_tan(tan(d2r(deg))) << "\n";
    }
}