首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用atof(optarg)时的奇怪输出

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

Stack Overflow用户
提问于 2014-10-28 17:06:11
回答 2查看 412关注 0票数 0

编辑::解决-这是由于对getOpt函数的使用产生误解。我引用了这里的人员、堆栈溢出(http://linux.die.net/man/3/getopt)和GNU网站上的getOpt文档中的材料:感谢Bill和Remyabel引用了前面提到的源材料。

当我使用-f变量运行这个程序来运行“足球”命令时,除了使用-c之外,似乎还存在一个问题,不过,我主要关注的是现在只运行一个命令。

在投入中:

代码语言:javascript
复制
-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值有关,尽管我几乎完全不知道。

代码语言:javascript
复制
#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);

代码语言:javascript
复制
                }
            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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-28 17:08:30

我认真地建议正确地缩进代码。如果你看到了,你就会看到:

代码语言:javascript
复制
if(c == 'f'){
    if (c == 'p')
    ...
}

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

票数 6
EN

Stack Overflow用户

发布于 2014-10-28 17:08:49

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26614279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档