24小时热门版块排行榜    

查看: 1349  |  回复: 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的回帖

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的回帖

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的回帖

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的回帖
相关版块跳转 我要订阅楼主 liupeiping 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[博后之家] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 jCd0dEvKHShX 2026-08-29 4/200 2026-08-30 19:13 by hZiFeudZyoGR
[考研] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +5 G6APbkg8SA6w 2026-08-29 6/300 2026-08-30 19:02 by hZiFeudZyoGR
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 G6APbkg8SA6w 2026-08-29 4/200 2026-08-30 18:53 by hZiFeudZyoGR
[考研] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +3 G6APbkg8SA6w 2026-08-29 5/250 2026-08-30 18:40 by hZiFeudZyoGR
[考博] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +4 gy1nBQXYQJqL 2026-08-29 5/250 2026-08-30 17:56 by hZiFeudZyoGR
[考研] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +4 gy1nBQXYQJqL 2026-08-29 6/300 2026-08-30 17:47 by hZiFeudZyoGR
[硕博家园] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +4 gy1nBQXYQJqL 2026-08-29 7/350 2026-08-30 17:45 by hZiFeudZyoGR
[找工作] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 gy1nBQXYQJqL 2026-08-29 8/400 2026-08-30 12:03 by l0VvVHGBGRLv
[论文投稿] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +5 gy1nBQXYQJqL 2026-08-29 7/350 2026-08-30 11:51 by l0VvVHGBGRLv
[考博] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 ASdOkHsho7FD 2026-08-28 6/300 2026-08-30 11:10 by l0VvVHGBGRLv
[基金申请] 为什么到现在没收到通知? +4 tannykie 2026-08-29 4/200 2026-08-29 17:14 by lmz0216
[基金申请] 系统查不到 +11 董八千 2026-08-26 11/550 2026-08-28 18:06 by Leogzhya
[基金申请] 基金不中,共勉 +11 eulota 2026-08-26 11/550 2026-08-28 14:22 by 火星超人xi
[基金申请] 面上合作单位盖章 +5 ssyjh 2026-08-27 5/250 2026-08-27 20:50 by gdfollow
[文学芳草园] 梦想 +3 myrtle 2026-08-26 3/150 2026-08-27 10:01 by angelyueyi
[基金申请] 国合里面能看到了 +7 一怀馨秋 2026-08-26 7/350 2026-08-26 11:23 by zhaosm1982
[基金申请] 牛来!米来!面来! +8 beefly 2026-08-26 8/400 2026-08-26 08:37 by xuzhipiao
[基金申请] 在坚冰还盖着北海的时候,我看到了怒放的梅花。 (金币+10) +6 ziyangfang 2026-08-25 9/450 2026-08-25 20:26 by huagongfeihu
[基金申请] 某些机构,以效率低为荣,以效率低作为存在感 +9 yuleib84 2026-08-25 10/500 2026-08-25 17:14 by alexon
[基金申请] 没有任何消息-是不是就凉了 +9 图啦图啦 2026-08-24 10/500 2026-08-25 11:59 by 南海小哥
信息提示
请填处理意见