原文书上的练习题(猜数字)

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