原文書上的練習題(猜數字)

Home Home
引用 | 編輯 dfgkopeeed
2011-10-20 23:01
樓主
推文 x0
我在原文書上看到這題猜數字的題目(我知道猜數字這種題型很老舊,但是原文書所規定的條件,我不會用他的條件做),請各為高手幫幫我一下~條件如下:
1.Define (input)(a) the number of integers (N) to play with. For example, input5 means you like to play with 5 numbers whichis 1 t ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 dfgkopeeed
2011-10-22 17:23
1樓
  
有人能教如何用argument list作嗎?  如果是從基本的9個數字4個出來這種我會做, 但是題目今天用將這兩個數字都設定成未知數, 有誰能教教我?  

獻花 x0
引用 | 編輯 ebolaman
2011-10-28 22:38
2樓
  
C++的 Arguments list 可以在 int main 的參數取得

複製程式
int main(int ctArg, char* cArg[], char* cEnv[])
{
   ......
}

▲ 以上是我個人喜愛的用法,當然變數名稱可以自己改
我取 ct 為 count 的意思,Arg 為 Argument,c 為 char,Env 為 Environment

ctArg  是參數(Argument) 的個數,一般要大於等於 1
cArg  就是參數的字元/字串了,其中 cArg[0] 是自己路徑的名稱,因此沒有參數時,ctArg 會是 1,大於 1 時,就是有額外的參數
cEnv  一般來說用不到這個...這是環境參數 (有興趣可以用 for 全部列出來看看)





我這邊做了一個小範例,可以點 Run.bat 來執行 Temp.exe
會看到 用C++做的程式是如何取得 Argument list 的

例如我呼叫 "Temp.exe" 1 2 3 4

程式會列出


Count of arguments = 5
Argument[0] = Temp.exe
Argument[1] = 1
Argument[2] = 2
Argument[3] = 3
Argument[4] = 4







Temp.exe 的 C 程式碼 :

複製程式
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;


int main(int ctArg, char* cArg[], char* cEnv[])
{   /// @Main

    int i;

    cout << "Count of arguments = " << ctArg << endl;

    for (i = 0; i < ctArg; i++)
        cout << "Argument[" << i << "] = " << cArg[i] << endl;


    cout << endl << endl;


    ///========== Suspend or terminate =========

    cout << "Press <ENTER> to exit..." << endl;
    cin.sync(); cin.get(); ///< Suspend

    return 0;
}





Run.bat :

複製程式
@echo off

echo Execute "Temp.exe" with arguments 1 2 3 4...
echo.

"Temp.exe" 1 2 3 4




本帖包含附件
檔名: zip ArgList_Example.rar   (2022-06-09 14:19 / 117 KB)  
Example | 點 Run.bat 來執行
下載次數:0


獻花 x1