24小时热门版块排行榜    

CyRhmU.jpeg
南方科技大学公共卫生及应急管理学院2026级博士研究生招生报考通知(长期有效)
查看: 1843  |  回复: 6

sciencejoy

新虫 (著名写手)

[交流] C++从命令行读取参数已有6人参与

在科学计算中,用户经常需要在程序运行的时候设置一些参数,用户可以在命令行达到此目的。

比如要用有限差分法计算一条棒的温度分布,用户需要设置棒的热导率、差分网格的节点数目。
CODE:
#include <iostream>
#include <cstdlib>

int main(int argc, char *argv[])
{
     std::cout << "Number of command line arguments = "
                   << argc << "\n";

     for (int i=0; i<argc; i++)
     {
         std::cout  << "Argument " << i << " = " << argv[i];
          std::cout << "\n";
      }

      std::string program_name = argv[0];
      int number_of_nodes = atoi(argv[1]);
      double conductivity = atof(argv[2]);
       std::cout << "Program name = " << program_name << "\n";
       std::cout << "Number of nodes = " << number_of_nodes;
       std::cout << "\n";
       std::cout << "Conductivity = " << conductivity << "\n";

        return 0;
}

如果计算需要100个节点,热导率为5.0,程序可执行文件名为CommandLineCode,在命令行输入
CODE:
./CommandLineCode 100 5.0

argc存储命令行输入的量的数目,在这个例子里为3,分别为./CommandLineCode,整数 100,浮点数 5.0,分别存储在argv[0],argv[1],argv[2]。函数 atoi(argv[1])把存储在argv[1]的字符转换成整型变量,atof(argv[2])把argv[2]转换成浮点变量。函数atoi和atof需要头文件cstdlib。

[ Last edited by sciencejoy on 2014-2-22 at 20:57 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

yaozhq

金虫 (小有名气)

★ ★
小木虫: 金币+0.5, 给个红包,谢谢回帖
jjdg: 编辑内容 2014-02-23 00:44
jjdg: 金币+1, 感谢参与 2014-02-23 00:45:04
如果考虑linux下计算的话 gnu的getopt更好用一些
CODE:
#include <ctype.h>
     #include <stdio.h>
     #include <stdlib.h>
     #include <unistd.h>
     
     int
     main (int argc, char **argv)
     {
       int aflag = 0;
       int bflag = 0;
       char *cvalue = NULL;
       int index;
       int c;
     
       opterr = 0;
     
       while ((c = getopt (argc, argv, "abc:")) != -1)
         switch (c)
           {
           case 'a':
             aflag = 1;
             break;
           case 'b':
             bflag = 1;
             break;
           case 'c':
             cvalue = optarg;
             break;
           case '?':
             if (optopt == 'c')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (isprint (optopt))
               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
             else
               fprintf (stderr,
                        "Unknown option character `\\x%x'.\n",
                        optopt);
             return 1;
           default:
             abort ();
           }
     
       printf ("aflag = %d, bflag = %d, cvalue = %s\n",
               aflag, bflag, cvalue);
     
       for (index = optind; index < argc; index++)
         printf ("Non-option argument %s\n", argv[index]);
       return 0;
     }

Here are some examples showing what this program prints with different combinations of arguments:
     % testopt
     aflag = 0, bflag = 0, cvalue = (null)
     
     % testopt -a -b
     aflag = 1, bflag = 1, cvalue = (null)
     
     % testopt -ab
     aflag = 1, bflag = 1, cvalue = (null)
     
     % testopt -c foo
     aflag = 0, bflag = 0, cvalue = foo
     
     % testopt -cfoo
     aflag = 0, bflag = 0, cvalue = foo
     
     % testopt arg1
     aflag = 0, bflag = 0, cvalue = (null)
     Non-option argument arg1
     
     % testopt -a arg1
     aflag = 1, bflag = 0, cvalue = (null)
     Non-option argument arg1
     
     % testopt -c foo arg1
     aflag = 0, bflag = 0, cvalue = foo
     Non-option argument arg1
     
     % testopt -a -- -b
     aflag = 1, bflag = 0, cvalue = (null)
     Non-option argument -b
     
     % testopt -a -
     aflag = 1, bflag = 0, cvalue = (null)
     Non-option argument -

考虑跨平台的话可以用boost

[ Last edited by jjdg on 2014-2-23 at 00:44 ]
2楼2014-02-22 23:52:01
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

旋转的月

金虫 (正式写手)

不明觉厉。先收藏。
3楼2014-02-23 00:57:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
4楼2014-02-23 16:06:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

匿名

用户注销 (文坛精英)


小木虫: 金币+0.5, 给个红包,谢谢回帖
本帖仅楼主可见
5楼2014-02-23 21:10:57
已阅   申请程序强帖   回复此楼   编辑   查看我的主页

quick25

新虫 (初入文坛)

牛人 支持
6楼2014-02-23 22:24:33
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

酒红色果糖

新虫 (初入文坛)

感觉老师讲的怎么一点意思都没有

[ 发自小木虫客户端 ]
7楼2014-02-26 07:56:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 sciencejoy 的主题更新
普通表情 高级回复(可上传附件)
信息提示
请填处理意见