編輯:: 已解決 - 這是由於誤解了getOpt函數的使用。 我在這裏引用了這個人的資料,堆棧溢出(http://linux.die.net/man/3/getopt)和GNU網站上的getOpt文檔:gnu.org/software/libc/manual/html_node/Example-of-Getopt.html感謝Bill Lynch和Remyabel用於引用前面提到的源材料。使用atof時出現奇怪的輸出(optarg)
當我使用-f變量運行「Football」命令並使用-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」線是有指定哪一組的計算來運行的,所以,需要是//最前面變量在的開頭進行檢查程序。 (c =='p') {p = 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;
}
您是否嘗試打印'optarg'的值? – 2014-10-28 17:08:11
請正確縮進您的代碼。很難看到每個塊在哪裏開始和結束。 – interjay 2014-10-28 17:08:14
另外:爲什麼你選擇使用'atof'?這總是一個不好的選擇。 – 2014-10-28 17:08:54