我是编程新手。我想从函数中调用(首先,第二,第三个是函数“enter”和函数“math”中的变量)。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void math (void);
void first (void);
void second (void);
void third (void);
void enter(void);
void scan(void);
int main()
{
first(); //function
second(); //function
third(); //function
enter(); //function
printf("\n\n Would you like to edit? (Y/N)\n\n");
scan(); //function
return(0);
}
这是用户输入:
void first (void){
float a;
printf("Enter First number: ");
scanf("%f",&a);
}
void second (void){
float b;
printf("Enter Second number: ");
scanf("%f",&b);
}
void third (void){
float c;
printf("Enter Third number: ");
scanf("%f", &c );
}
如何将用户输入的变量调用到下面的函数中?
void enter(){
float a,b,c;
printf("you have entered a: %f, b: %f, c:%f", a,b,c);
}
这是一个循环,以防用户想要编辑或继续初始输入的数字。
void scan(void){
int ch;
scanf("%d", &ch);
if (ch==1){
main();
}else{
math();
}
}
在这里,我还想从函数(第一、第二、第三)中调用输入变量,以执行一些数学计算。
void math (void){
float a,b,c;
float x,y,z;
x=a+b+c;
y= powf(x,2);
z=sqrt(y);
printf("Final Result of Addition is: %f \n Final Result of
Multiplication is: %f \n Final Result of squareroot is:%f\n
",x,y,z);
}
最佳答案
如果函数不依赖或修改它们外部的数据,那是最好的。这并不总是可能的,但如果可以在定义时考虑到该目标,那就更好了。
在您的例子中,函数 first
、second
和 third
本质上做同样的事情。最好只使用一个函数,并为其取一个更合适的名称,例如read_float
,并更改返回类型,以便将用户输入的值返回给调用函数。
声明为:
float read_float(char const* prompt);
然后,您可以将其用作:
float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");
等等
函数 enter
没有正确传达它正在做什么。它应该重命名为更合适的名称,例如 display_input_values
。它可以接受用户输入的值作为其参数。
声明为:
void display_input_values(float a, float b, float c);
并将其用作:
float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");
display_input_values(a, b, c);
scan
函数也没有明确表达其含义。您可以将 scan
分为两个函数 -- 一个返回给您选择是继续下一个输入还是调用一个函数来计算用户输入的内容。
您可以使用名为 read_int
的函数从用户处获取选项,如下所示:
int option = read_int("Enter 1 for next round of inputs and 2 to compute with current input:");
并将该返回值用作:
if ( option == 2 )
{
// Do the computations
}
您还需要另一个选项来退出程序。获取选项的调用需要是:
char const* prompt = "Enter 1 for next round of inputs.\n"
"Enter 2 to compute with current input.\n"
"Enter 3 to exit program.\n";
int option = read_int(prompt);
在继续使用您期望读入的数据之前,始终添加代码以检查 scanf
是否成功。
这是您的代码的更新版本。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float read_float(char const* prompt);
int read_int(char const* prompt);
void display_input_values(float a, float b, float c);
void math(float a, float b, float c);
int main()
{
while ( 1 )
{
float a = read_float("Enter first number: ");
float b = read_float("Enter second number: ");
float c = read_float("Enter third number: ");
display_input_values(a, b, c);
char const* prompt = "Enter 1 for next round of inputs.\n"
"Enter 2 to compute with current input.\n"
"Enter 3 to exit program.\n";
int option = read_int(prompt);
if ( option == 3 )
{
break;
}
else if ( option == 2 )
{
math(a, b, c);
}
}
}
float read_float(char const* prompt)
{
float num;
printf("%s", prompt);
// For flushing the output of printf before scanf.
fflush(stdout);
if (scanf("%f", &num ) != 1)
{
print("Unable to read number. Exiting.\n");
exit(1);
}
return num;
}
int read_int(char const* prompt)
{
int num;
printf("%s", prompt);
// For flushing the output of printf before scanf.
fflush(stdout);
if (scanf("%d", &num ) != 1)
{
print("Unable to read number. Exiting.\n");
exit(1);
}
return num;
}
void display_input_values(float a, float b, float c)
{
printf("You have entered a: %f, b: %f, c:%f\n", a, b, c);
}
void math(float a, float b, float c)
{
// Do whatever makes sense to you.
}
关于c - 如何从另一个函数调用局部变量c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017780/