24小时热门版块排行榜    

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

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的回帖
查看全部 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的回帖

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的回帖
信息提示
请填处理意见