【问题标题】:Calling a function with an if statement in C [closed]在 C 中使用 if 语句调用函数 [关闭]
【发布时间】:2013-01-14 12:58:17
【问题描述】:

我有一个程序可以计算出分子的减少质量,并想用 if 函数调用它。这是我目前拥有的代码:

int program ();
int main()
{
    printf("Press 1 to calculate the reduced mass of a molecule or press any other button to exit:");
    scanf ("%lg",&repeat);

    if(repeat==1)
    {
    program();
    }
    else
    {
    return(0);
    }
}

int program ()
//etc...

我对 C 不太熟悉,所以解释一下可能会有用。这是否也可以让您可以根据需要多次重复该功能?

【问题讨论】:

  • 在 C.whilefordo-while 中查找 条件循环
  • repeat 定义在哪里?
  • 这里不适合作为问题(如果有的话)。请事先对 SO 或整个网络进行最少的研究。
  • int main() 是多年前的标准做法,并在 1989 年正式过时。使用 int main(void)int main(int argc, char **argv)
  • if() is not function is flow-control structure in C.你的程序检查repeat的值是否等于1然后调用一个函数program();,该函数在某些地方定义你的代码。如果repeat 不等于1,则程序终止。

标签: c function if-statement


【解决方案1】:

如果你从 C 开始,你可以从使用程序(你编译的 C)参数开始。这样,您可以向程序提供 N 次要调用的 program() 函数。

例如

   // Includes that have some library functions declarations
   #include <stdio.h>
   #include <stdlib.h>

   // argc and argc can be provided to the main function.
   // argv is an array of pointers to the arguments strings (starting from 0)
   int main(int argc, char *argv[]) {
      if (argc < 2) return 1; // argc number of parameters including the program name itself
      int repeat = atoi(argv[1]); // atoi convert a string to integer

      // repeat-- decrements repeat after its value was tested against 0 in the while
      while (repeat-- > 0) {
         program();
      }

      return 0;
   } 

argc 针对 2 进行测试,由于程序名称本身是第一个参数,因此您至少需要 2,其中 N。例如

   ./myprog 5

将运行 program() 5 次。

【讨论】:

    【解决方案2】:

    I'm not too experienced with C, so an explanation might be useful.
    不确定您究竟需要解释什么:

    int program (); <-- function prototype for "program" defined here so you can call it in
    int main()      <--  main. 
    {
        // Display a message
        printf("Press 1 to calculate the reduced mass of a molecule or press any other button to exit:");
        // store the value typed by the user as a double, also it will accept scientific 
        // notation (that’s he g part). So you could enter 3.964e+2 if you wanted... an 
        // int or even a char would have been fine here
        scanf ("%lg",&repeat);
    

    Also would this make it so that you can repeat the function as many times as you like? 不,那不是。首先,它不会编译,因为没有“重复”的定义;其次,您缺少循环机制。 forwhiledo/while,递归......不管它可能。您的代码可以很容易地通过无限循环完成:

    int main()
    {
        double repeat = 0;
        for(;;) {
            printf("Press 1 to calculate the reduced mass of a molecule or press any other button to exit:");
            scanf ("%lg",&repeat);
    
            if(repeat==1)
            {
                program();
            }
            else
            {
                return(0);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多