24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 1908  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 0856材料化工调剂 总分330 +6 zhubinhao 2026-03-27 6/300 2026-03-27 10:23 by 尽舜尧1
[考研] 304求调剂 +3 曼殊2266 2026-03-27 3/150 2026-03-27 10:17 by guoweigw
[考研] 359求调剂 +4 王了个楠 2026-03-25 4/200 2026-03-27 08:43 by 不吃魚的貓
[考研] 284求调剂 +11 junqihahaha 2026-03-26 12/600 2026-03-27 04:37 by wxiongid
[考研] 304材料求调剂 +4 钟llll 2026-03-26 4/200 2026-03-27 03:42 by wxiongid
[考研] 化学308分求调剂 +5 你好明天你好 2026-03-23 5/250 2026-03-26 23:43 by 催化大白
[考研] 071000生物学求调剂,初试成绩343 +6 小小甜面团 2026-03-25 6/300 2026-03-26 23:01 by 不吃魚的貓
[考研] 求调剂 +6 白QF 2026-03-21 6/300 2026-03-26 20:37 by fmesaito
[考研] 286求调剂 +13 Faune 2026-03-21 13/650 2026-03-26 19:52 by peike
[考研] 07化学303求调剂 +5 睿08 2026-03-25 5/250 2026-03-25 22:46 by 418490947
[考研] 求b区院校调剂 +4 周56 2026-03-24 5/250 2026-03-25 17:12 by yishunmin
[考研] 0854人工智能方向招收调剂 +4 章小鱼567 2026-03-24 4/200 2026-03-25 13:29 by 2177681040
[考研] 285求调剂 +3 AZMK 2026-03-24 3/150 2026-03-25 12:23 by userper
[考研] 考研化学308分求调剂 +10 你好明天你好 2026-03-23 11/550 2026-03-25 10:23 by userper
[考研] 0703化学求调剂 +6 奶油草莓. 2026-03-22 7/350 2026-03-25 10:00 by shangxh
[考研] 材料学求调剂 +6 Stella_Yao 2026-03-20 6/300 2026-03-25 00:37 by baoball
[考研] 一志愿华东理工大学081700,初试分数271 +5 kotoko_ik 2026-03-23 6/300 2026-03-24 10:29 by 学术搬砖er
[考研] 341求调剂(一志愿湖南大学070300) +5 番茄头--- 2026-03-22 6/300 2026-03-23 23:45 by Txy@872106
[考研] 328求调剂 +4 LHHL66 2026-03-23 4/200 2026-03-23 14:55 by lbsjt
[考研] 276求调剂 +3 YNRYG 2026-03-21 4/200 2026-03-23 08:31 by 醉在风里
信息提示
请填处理意见