我对此代码有一些问题。我已将错误包含在代码的末尾。
#include <stdio.h>
int main()
{
void addition(double number1, double number2); /* create the functions */
void subtraction(double number1, double number2);
void division(double number1, double number2);
void multiplication(double number1, double number2);
int inputfunc=1;
double inputnum1=0;
double inputnum2=0;
int number1;
int number2;
int answer;
while (inputfunc >= 1 && inputfunc <= 4) /* If function to be performed are those below then continue performing loop */
{
printf("Press 1 to add two numbers.\n");
printf("Press 2 to subtract two numbers.\n");
printf("Press 3 to multiply two numbers.\n");
printf("Press 4 to divide two numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf_s("%d", &inputfunc);
if( inputfunc == 5) /* Exit program if requested via 5 function */
return(0);
printf("Enter both numbers with a space in between.");
scanf_s("%lf %lf", &inputnum1, &inputnum2);
void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
(*func[inputfunc-1])(inputnum1, inputnum2);
return(0);
}
}
void addition(double number1, double number2)
{
double answer;
answer=number1+number2;
printf("Addition of the two numbers = %lf + %lf = %lf\n", number1, number2, answer);
return;
}
void subtraction(double number1, double number2)
{
double answer;
answer=number1-number2;
printf("By subtracting the two numbers results are %lf - %lf = %lf\n", number1,
number2, answer);
return;
}
void multiplication(double number1, double number2)
{
double answer;
answer=number1*number2;
printf("By multiplying the two numbers results are %lf * %lf = %lf\n", number1,
number2, answer);
return;
}
void division(double number1, double number2)
{
double answer;
answer=number1/number2;
printf("By dividing the two numbers results are %lf / %lf = %lf\n", number1,
number2, answer);
return ;
}
错误C2143:语法错误:缺少&#39;;&#39;之前&#39;键入&#39; 错误C2065:&#39; func&#39; :未声明的标识符 错误C2109:下标需要数组或指针类型
答案 0 :(得分:0)
我在你的代码中发布了下面的一行,你正在用第二个大括号结束主方法。 这不是一个好主意,因为你的代码低于该大括号。
void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
(*func[inputfunc-1])(inputnum1, inputnum2);
return(0);
} //end while
} //end main method
答案 1 :(得分:0)
代码编译和工作 - 请参阅it in action here
有些观点:
return(0);
位于while
循环内 - 这将阻止它向用户询问多次运行。将其移至while
循环结束下方。void(*func[4])(double, double) = { &addition, &subtraction, &division, &multiplication };
移到while
之上int number1; int number2; int answer;
的最顶层声明是多余的,应该删除(特别是因为它们在4个算术函数中用作具有不同类型的局部变量名称)。我已对代码段进行了上述更改(scanf_s
已替换为简单scanf
,因为IdeOne不使用MS编译器。)
答案 2 :(得分:-1)
// the scanf_s function is the secure function for string input, using scanf
// this version compiles without any warnings, etc under linux gcc
// this version checks for input errors
// (except the actual value of the variable inputFunc)
// I think the use of a switch() statement would be much more robust
// rather than the use of the function ptr table, although not quite as flexable
// Notice the function prototypes are outside of any function
// so the compiler will create the proper code
#include <stdio.h>
#include <stdlib.h> // contains exit() and EXIT_FAILURE
// function prototypes
void addition(double number1, double number2);
void subtraction(double number1, double number2);
void division(double number1, double number2);
void multiplication(double number1, double number2);
void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
int main()
{
int inputFunc=1;
double inputnum1=0;
double inputnum2=0;
/* If function to be performed are those
below then continue performing loop */
// note:
// if the inputFunc is (for instance) 6 then this while loop is exited
// however, there was no return statement
// for that execution path
while (inputFunc >= 1 && inputFunc <= 4)
{
printf("Press 1 to add two numbers.\n");
printf("Press 2 to subtract two numbers.\n");
printf("Press 3 to multiply two numbers.\n");
printf("Press 4 to divide two numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
if( 1 != scanf(" %d", &inputFunc) )
{
perror( "scanf_s" );
exit( EXIT_FAILURE ) ;
}
// implied else, scanf for which command was successful
// note: there should be some checking here
// to assure that the input was in the valid range '1...5'
if( inputFunc == 5)
{ /* then, Exit while loop if requested via 5 function */
// note: good program practice is to put the return
// at the bottom of the function
break;
}
printf("Enter both numbers with a space in between.");
if( 2 != scanf(" %lf %lf", &inputnum1, &inputnum2) )
{
perror( "scanf for 2 input numbers" );
exit(EXIT_FAILURE);
}
// implied else, scanf for two input numbers successful
// exec the desired function
(*func[inputFunc-1])(inputnum1, inputnum2);
}
return(0); // to avoid compiler warning
}
void addition(double number1, double number2)
{
double answer;
answer=number1+number2;
printf("Addition of the two numbers = %lf + %lf = %lf\n", number1, number2, answer);
return;
}
void subtraction(double number1, double number2)
{
double answer;
answer=number1-number2;
printf("By subtracting the two numbers results are %lf - %lf = %lf\n",
number1,
number2,
answer);
}
void multiplication(double number1, double number2)
{
double answer;
answer=number1*number2;
printf("By multiplying the two numbers results are %lf * %lf = %lf\n",
number1,
number2,
answer);
}
void division(double number1, double number2)
{
// note: this should check that number2 is NOT 0, to avoid divide by zero error
double answer;
answer=number1/number2;
printf("By dividing the two numbers results are %lf / %lf = %lf\n",
number1,
number2,
answer);
}