使用atof时的输出奇怪(optarg)

时间:2014-10-28 17:06:12

标签: c++ math ascii atof

编辑:: 已解决 - 这是由于误解了getOpt函数的使用。 我在这里引用了man中的材料,堆栈溢出(http://linux.die.net/man/3/getopt)和GNU网站上的getOpt文档:gnu.org/software/libc/manual/html_node/Example-of-Getopt.html感谢Bill Lynch和Remyabel参考前面提到的源材料。

当我使用-f变量运行此程序以运行“足球”命令以及使用-c时,似乎存在问题。但是,我主要关心的是现在只需要一个工作。

放入输入栏:

-f -p 16 -a 25 -y 267 -t 1 -i 2

     Gives out::
     pC = 0
     pC = 32738
     pY = -1052776240
     T = 32738
     I = 1

现在,这些变量应该只是吐出我放入的内容,因为我正在使用的唯一转换(如下所示)是X = atof(optarg); 我怀疑这可能与ASCII值有关,尽管我几乎完全无能为力。

#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
#include <cmath>

using namespace std;

int main(int argc, char *argv[])
{
    srand(time(NULL));
    double r =  (6 + ( std::rand() % ( 8 - 6 + 1 ) )) / 10;
    int c;
    int pA;
    int pY;
    int T;
    int I;
    int pC;

    double mass;
    double bMass;
    double dist;
    double velo;
    double Cr = .001;
    double k = .18;
    double g = 9.8;
    double CFdraft;
    double Pair;
    double Proll;
    double Psec;
    double timeTravel = 0.0;
    double Et;
    double E;
    double Eavg = 0;
    int x = 0;
    double cT;
    double cC;
    double cY;
    double cI;
    double PasserRating;

    while ((c = getopt (argc, argv, "c:m:b:v:d:f:p:a:y:t:i:")) != -1)
    {
        if (c == 'f') // There seems to be some kind of misunderstanding with what this is doing

// c =='f'行用于指定要运行的计算集,因此,需要在程序开头检查//最重要的变量。                 {                 if(c =='p')                    {                         pC = atof(optarg);

                }
            if (c == 'a')
                {
                    pA = atof(optarg);

                }
            if (c == 'y')
                {
                    pY = atof(optarg);

                }
            if (c == 't')
                {
                    T = atof(optarg);

                }
            if (c == 'i')
                {
                    I = atof(optarg);

                }
            cout << "pC " << pC << endl;
            cout << "pC " << pA << endl;
            cout << "pY " << pY << endl;
            cout << "T " << T << endl;
            cout << "I " << I << endl;
            //Calculations
            cC = ((pC / pA) - 0 / 30) * 5;
            cY = ((pY / pA) - 3) * 0.25;
            cT = ((T / pA) * 20);
            cI = ((2.375) - (I / pA) * 25);

            if (cC <= 0)
                {
                    cC = 0;
                }
            if (cC >= 2.375)
                {
                    cC = 2.375;
                }
            if (cY <= 0)
                {
                    cY = 0;
                }
            if (cY >= 2.375)
                {
                    cY = 2.375;
                }
            if (cT <= 0)
                {
                    cT = 0;
                }
            if (cT >= 2.375)
                {
                    cT = 2.375;
                }
            if (cI <= 0)
                {
                    cI = 0;
                }
            if (cI >= 2.375)
                {
                    cI = 2.375;
                }
            PasserRating = (((cC + cY + cT + cI) / 6) * 100);
            string strPR = "Poor";

            if (PasserRating <= 85)
            {
                strPR = "poor";
            }
            if (PasserRating > 85)
            {
                strPR = "mediocre";
            }
            if (PasserRating > 90)
            {
                strPR = "good ";
            }
            if (PasserRating > 95)
            {
                strPR = "great ";
            }
            cout << strPR << " " << PasserRating << endl;
        }
        if (c == 'c')
        {
            if (c == 'm')
            {
                mass = atof(optarg);

            }
            if (c == 'b')
            {
                bMass = atof(optarg);

            }
            if (c == 'd')
            {
                dist = atof(optarg);

            }
            if (c == 'v')
            {
                velo = atof(optarg);
            }
            timeTravel = (dist * 1000) / velo;
            cout << "time:" << timeTravel << endl;
            cout << "mass " << mass << endl;
            cout << "bMass " << bMass << endl;
            cout << "dist " << dist << endl;
            cout << "velo " << velo << endl;

            for (x = 0; x < (10); x ++)
            {
                CFdraft = r;
                Pair = k * CFdraft * (pow(velo, 3));
                Proll = Cr * g * (mass + bMass) * velo;
                Psec = Pair + Proll;
                Et = (Psec * timeTravel);
                E = E + Et;
                Eavg = E / timeTravel;
            }

            cout << Eavg << " KJ" << endl;
        }

    }
    return 0;
}

2 个答案:

答案 0 :(得分:6)

我认真推荐正确缩进您的代码。如果你这样做,你会看到:

if(c == 'f'){
    if (c == 'p')
    ...
}

显然,c不会同时等同于'f''p'

答案 1 :(得分:3)

从不执行你的解析代码 - 一切都在if(c == 'f')条件内,这显然只是在你第一次运行循环时...所以你只需从中得到随机值记忆。