24小时热门版块排行榜    

查看: 1337  |  回复: 4
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

liupeiping

新虫 (初入文坛)

[求助] 麻烦哪位大神帮忙修改一下这个程序,不胜感激! 已有2人参与

麻烦哪位大神帮忙看一下下面这个程序,编译的时候会提示While(true)处有错,应该怎么改,请告知一下,不胜感激!
// stat.cpp : Defines the entry point for the console application.
  //

  #include <stdio.h>
  #include <stdlib.h>
  #include <math.h>
  #include <getopt.h>
  #include <string.h>

  // Show a simple usage information.
  void usage();

  int main(int argc, char* argv[])
  {
       char opt;                              // Options charactors
       int a=30, b=1, c=7;
       double r_critical = 3.3943e-2;
                                              // Critical radius for judging inner or outer
       double alpha = 1.0e-6;
                                              // Error critical for test double equal
       char ofname[255] = "out.txt";  // Output file name
       FILE *fin, *fout;
       unsigned int i = 0;                // Treatment index
       double time = 0.0L;                // Total sampling time
       double time_bak = 0.0L;            // time backup
       unsigned int steps = 0;            // Sampling times
       double y = 0.0L;                   // axis y pos
       double z = 0.0L;                   // axis z pos
       double r = 0.0L;                   // Radius
       double iq = 0.0L;                    // Irradiation quantity
       unsigned int inner = 0;            // Inner sampling counter
       unsigned int outer = 0;            // Outer sampling counter

       while ((opt = getopt(argc, argv, "a:b:c:e:r:h") != -1) {
              switch (opt) {
                      case 'a':
                              a = atoi(optarg);
                              break;
                      case 'b':
                              b = atoi(optarg);
                              break;
                      case 'c':
                              c = atoi(optarg);
                              break;
                      case 'e':
                              alpha = atof(optarg);
                              break;
                      case 'o':
                              strcpy(ofname, optarg);
                              break;
                      case 'r':
                              r_critical = atof(optarg);
                              break;
                      case ':':
                              // Go through
                      case '?':
                              // Go through
                      case 'h':
                              // Go through
                      default:
                             usage();
                             exit(0);
                          }// switch
           }// while
       if (optind != argc-1) {
            fprintf(stderr, "Need a data file.\n";
            fprintf(stderr, "Use option -h to get some helpful message.\n";
            exit(1);
           }
       else
            // Open input & output files
            if ((fin = fopen(argv[optind], "r")==NULL)
                        {
                   printf("data file: %s\n", argv[optind]);
                   printf("Open input file %s failed!\n", argv[optind]);
                   exit(1);
                        }

           if ((fout = fopen(ofname, "w+") == NULL)
           {
             printf("Open/Create output file %s failed!\n", ofname);
             exit(1);
           }
       // Output table header
       fprintf(fout, "\tIndex\t\tTime\t\tSteps\t\tInner\t\tOuter\t\tRate\t\tIrradiation\n";
       fprintf(fout, "\t-------\t\t------\t\t------\t\t------\t\t------\t\t------\t\t------\n";

       // Read a line datas
       do
           {
            // Read a line, but just store useful data
            fscanf(fin, "%le%*le%*le%le%le%*le%*le%*le%*le%*le%*le%*le", &time, &y, &z);

            if (feof(fin)) // end of file
                        {
                  fprintf(fout, "\t%d\t\t%le\t%d\t\t%d\t\t%d\t\t%f\t\t%f\n", i, time_bak, steps, inner,
       outer, ((double)inner)/(inner + outer), iq);
                  printf("\t%d\t\t%le\t%d\t\t%d\t\t%d\t\t%f\t\t%f\n", i, time_bak, steps, inner, outer,
       ((double)inner)/(inner + outer), iq);
                  break;                   // out of repeat
                        }


            if (time < alpha) // time == 0 means a new treatment
                        {
                  // Store treatment data
                  if (i > 0)
                                  {
                        fprintf(fout, "\t%d\t\t%le\t%d\t\t%d\t\t%d\t\t%f\t\t%f\n", i, time_bak, steps,
       inner, outer, ((double)inner)/(inner + outer), iq);
                        printf("\t%d\t\t%le\t%d\t\t%d\t\t%d\t\t%f\t\t%f\n", i, time_bak, steps, inner,
       outer, ((double)inner)/(inner + outer), iq);
                                  }

                  // Clear variable relative to treatment
                  steps = 0;
                  inner = 0;
                  outer = 0;
                  iq = 0;
                  time_bak = 0;
                  i++;     // increment index
                        }

            // Increment step counters
            steps++;
            // Calculate radius
            r = sqrt(y*y + z*z);
            if (r-r_critical > alpha)
                        {
                  outer++;
                        }
            else
                        {
                  inner++;
                        }

                // Calculate irradiation quantity
            //printf("r=%lf\n", r);
            iq += a*b*pow(10, -1*c*r)*fabs(time-time_bak);

                time_bak = time;
           } while (true);

       // Close files
       fclose(fin);
       fclose(fout);

       //printf("Calculate finished, press ENTER to exit.";
       //printf("pow(10, 1) = %f", pow(10, 1));
       //getchar();
       return 0;
   }

   void usage()
   {
         printf("[Usage]: stat datafile.\n";
         printf("Options: \n";
         printf("\t-a, -b, -c: set coefficiency relatively.\n";
         printf("\t-o: set output file name.\n";
         printf("\t-e: set alpha used to compare two float number.\n";
         printf("\t-r: set referrence diameter.\n";
         printf("\nDeveloped by Sam <yeahspyme@sina.com>, published under GPL licence.\n";
   }
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

sxu2009

至尊木虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
liupeiping: 金币+20, ★★★很有帮助, 问题已解决 2015-01-16 09:24:15
引用回帖:
3楼: Originally posted by liupeiping at 2015-01-15 09:31:32
谢谢关注,提示的是 while (true)处有问题: error C2065: 'true' : undeclared identifier。应该怎么修改呢?...

声明一个宏试试,加:
#define true 1
4楼2015-01-15 10:57:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 5 个回答

rbs

木虫 (小有名气)

【答案】应助回帖

感谢参与,应助指数 +1
错误提示是什么?
2楼2015-01-14 18:21:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

liupeiping

新虫 (初入文坛)

引用回帖:
2楼: Originally posted by rbs at 2015-01-14 18:21:16
错误提示是什么?

谢谢关注,提示的是 while (true)处有问题: error C2065: 'true' : undeclared identifier。应该怎么修改呢?
3楼2015-01-15 09:31:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

liupeiping

新虫 (初入文坛)

引用回帖:
4楼: Originally posted by sxu2009 at 2015-01-15 10:57:51
声明一个宏试试,加:
#define true 1...

好的,我试试,谢谢!
5楼2015-01-16 09:23:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 今天系统多次维护,明天很可能放榜! +6 zju2000 2026-08-16 6/300 2026-08-17 10:01 by xter9665
[基金申请] 哪位老哥知道今年的国自然具体哪一天放榜? +10 Ldrop2023 2026-08-13 10/500 2026-08-17 09:59 by 小豌豆_发芽
[论文投稿] 岩土工程学报什么时候才能终审完呐 25+3 yeager111 2026-08-10 4/200 2026-08-16 20:42 by tfang
[基金申请] 时间戳又变了8-15 +13 archvillain 2026-08-15 25/1250 2026-08-16 20:13 by zhaosm1982
[基金申请] 2027广东省杰青 +3 奶牛小黑 2026-08-15 6/300 2026-08-16 20:07 by 奶牛小黑
[基金申请] 咱们一起用铁证分析2026国家社科基金中标与否 +7 启萌科技 2026-08-12 26/1300 2026-08-16 12:35 by 启萌科技
[基金申请] 我的国基提前知道中了,可是同事的操作让我实在接受不了,怎么会有这样的人 +11 家与远方 2026-08-10 16/800 2026-08-16 10:28 by ray43
[基金申请] filecode=后面第一个是大写字母 +6 wangze12014 2026-08-14 7/350 2026-08-15 20:12 by gltch
[基金申请] 小木虫上这么多卖论文的,真有人买论文么?感觉没必要啊 +12 Tide man 2026-08-10 13/650 2026-08-15 16:34 by 氺木
[基金申请] 关于Filecode分析方法 +10 majunge000 2026-08-10 13/650 2026-08-15 12:26 by hanpeng972
[基金申请] 是这周出结果还是下周出结果? +4 yuleib84 2026-08-11 4/200 2026-08-14 23:05 by lfy8008
[硕博家园] 读博的好处 +4 lnee 2026-08-11 4/200 2026-08-14 10:20 by ahsoarli
[基金申请] 重要来源:本周末出结果 +10 瞬息宇宙 2026-08-12 10/500 2026-08-13 15:46 by likettle
[基金申请] 不应该看fileCode +7 且听虎啸 2026-08-12 9/450 2026-08-13 14:27 by flydreamws
[基金申请] 分享一下我之前已中青C的计划书的filecode +4 布布和一二 2026-08-11 5/250 2026-08-13 12:56 by cratir
[基金申请] 结合人工智能,周易传统文化,filecode打分制来了,3分以上希望很大。 +3 Tide man 2026-08-12 4/200 2026-08-13 08:35 by ZJTJZ
[基金申请] 2019年青年基金涵评意见,大家看看几个A,几个B? +11 Tide man 2026-08-11 11/550 2026-08-13 07:35 by 撸猫猫
[基金申请] 为什么网上很多人说本周 12号出结果 +6 瞬息宇宙 2026-08-10 7/350 2026-08-11 19:25 by Tide man
[基金申请] 什么时候出结果,有咨询渠道??? +3 Tide man 2026-08-11 3/150 2026-08-11 17:54 by kudofaye
[基金申请] 国自然结果 +4 Vierhys 2026-08-10 8/400 2026-08-10 15:06 by Vierhys
信息提示
请填处理意见