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?
不,那不是。首先,它不会编译,因为没有“重复”的定义;其次,您缺少循环机制。for
, while
, do/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);
}
}
}