我嘗試檢查我的參數是否排序。我有代碼,但它不起作用...你能告訴我我該怎麼修復?我認爲問題出現在第一個循環中,因爲結果可能在參數列表的末尾爲0。我不知道應該如何改變它,我試圖做到這一點:當atof(argv[i]) <= atof(argv[i + 1])
不是真的,程序應該去下一個for循環...這是我第一個C++程序。我的參數是否已排序?
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
int result;
for (int i = 1; i < argc - 1; i++) {
if (atof(argv[i]) <= atof(argv[i + 1])) {
result = 0;
}
}
if (result == 0) {
cout << "Sorted!" << endl;
system("pause");
}
for (int i = 1; i < argc - 1; i++) {
if (atof(argv[i]) >= atof(argv[i + 1])) {
result = 2;
}
}
if (result == 2) {
cout << "Sorted!" << endl;
system("pause");
}
else {
cout << "Bad." << endl;
system("pause");
}
system("pause");
return 0;
}
「atof(argv [i])<= atof(argv [i + 1])'不成立時,程序應該轉到下一個循環」,並且當條件爲真時它也轉到下一個for循環。 – MikeCAT
請告訴我們所需的行爲。 – MikeCAT
首先假定它們是排序的。所以初始化你的* result *變量。然後,你所要做的就是檢查它們是否被排序。 –