为什么赢了';t我的结果在第一次迭代后更新(?) #包括 #包括 无效扫描_数据(字符*运算,浮点*操作数); float do_next_op(字符运算、浮点操作数、浮点*累加器); 内部主(空) { char op; 浮点操作数; 浮动累加器=0; 做 { printf(“+add\n-subtract\n*乘\n/除\n^power\n q quit\n\n”); 扫描数据(&op,&O操作数); if(op='q'| | op='q') { printf(“最终结果是%.1f\n”,执行下一步操作(操作、操作数和累加器)); 打破 } 其他的 { printf(“到目前为止的结果是%.1f\n”,执行下一步操作(操作、操作数和累加器)); } }而(op!=“q”| op==“q”); } 无效扫描_数据(字符*运算,浮点*操作数) { scanf(“%c%f”,op,操作数); } float do_next_op(字符运算、浮点操作数、浮点*累加器) { 开关(op) { 大小写(“+”): *累加器=*累加器+操作数; 打破 格('-'): *累加器=*累加器-操作数; 打破 大小写('*'): *累加器=*累加器*操作数; 打破 格(“/”): *累加器=*累加器/操作数; 打破 格(“^”): *累加器=功率(*累加器,操作数); 打破 } 返回*累加器; }
我试图编写一个“简单”的计算器,如果我输入 +5.0 目前的结果是5.0 ^二, 目前的结果是25.0 /2.0 目前的结果是12.5 问题0 最终结果是12.5 问题是,代码将“正确地”输出第一个操作,但如果我在这之后再执行其他操作,则不会更新为新值。 如何修复代码以执行我打算执行的操作为什么赢了';t我的结果在第一次迭代后更新(?) #包括 #包括 无效扫描_数据(字符*运算,浮点*操作数); float do_next_op(字符运算、浮点操作数、浮点*累加器); 内部主(空) { char op; 浮点操作数; 浮动累加器=0; 做 { printf(“+add\n-subtract\n*乘\n/除\n^power\n q quit\n\n”); 扫描数据(&op,&O操作数); if(op='q'| | op='q') { printf(“最终结果是%.1f\n”,执行下一步操作(操作、操作数和累加器)); 打破 } 其他的 { printf(“到目前为止的结果是%.1f\n”,执行下一步操作(操作、操作数和累加器)); } }而(op!=“q”| op==“q”); } 无效扫描_数据(字符*运算,浮点*操作数) { scanf(“%c%f”,op,操作数); } float do_next_op(字符运算、浮点操作数、浮点*累加器) { 开关(op) { 大小写(“+”): *累加器=*累加器+操作数; 打破 格('-'): *累加器=*累加器-操作数; 打破 大小写('*'): *累加器=*累加器*操作数; 打破 格(“/”): *累加器=*累加器/操作数; 打破 格(“^”): *累加器=功率(*累加器,操作数); 打破 } 返回*累加器; },c,pointers,C,Pointers,我试图编写一个“简单”的计算器,如果我输入 +5.0 目前的结果是5.0 ^二, 目前的结果是25.0 /2.0 目前的结果是12.5 问题0 最终结果是12.5 问题是,代码将“正确地”输出第一个操作,但如果我在这之后再执行其他操作,则不会更新为新值。 如何修复代码以执行我打算执行的操作 如果我的问题措辞和格式不正确,我提前表示歉意,我不知道如何正确提问 问题在声明中: #include <stdio.h> #include <math.h> void scan_da
如果我的问题措辞和格式不正确,我提前表示歉意,我不知道如何正确提问 问题在声明中:
#include <stdio.h>
#include <math.h>
void scan_data(char *op, float *operand);
float do_next_op(char op, float operand, float *accumulator);
int main(void)
{
char op;
float operand;
float accumulator = 0;
do
{
printf(" + add\n - subtract\n * multiply\n / divide\n ^ power\n q quit\n\n");
scan_data(&op, &operand);
if(op == 'q' || op == 'Q')
{
printf("Final result is %.1f\n", do_next_op(op, operand, &accumulator));
break;
}
else
{
printf("result so far is %.1f\n", do_next_op(op, operand, &accumulator));
}
}while(op != 'q' || op == 'Q');
}
void scan_data(char *op, float *operand)
{
scanf("%c%f",op, operand);
}
float do_next_op(char op, float operand, float *accumulator)
{
switch(op)
{
case('+'):
*accumulator = *accumulator + operand;
break;
case('-'):
*accumulator = *accumulator - operand;
break;
case('*'):
*accumulator = *accumulator * operand;
break;
case('/'):
*accumulator = *accumulator / operand;
break;
case('^'):
*accumulator = pow(*accumulator,operand);
break;
}
return *accumulator;
}
scanf(" %c%f",op, operand);
在第一次迭代之后,stdin
换行符
是一个“空白”,通常是\n
该“空白必须在%c
之前使用,而不是%c
使用它。建议:
scanf("%c%f",op, operand);
*accumulator = pow(*accumulator,operand);
格式字符串中的前导空格占用了该“空白”
关于声明:
#include <stdio.h>
#include <math.h>
void scan_data(char *op, float *operand);
float do_next_op(char op, float operand, float *accumulator);
int main(void)
{
char op;
float operand;
float accumulator = 0;
do
{
printf(" + add\n - subtract\n * multiply\n / divide\n ^ power\n q quit\n\n");
scan_data(&op, &operand);
if(op == 'q' || op == 'Q')
{
printf("Final result is %.1f\n", do_next_op(op, operand, &accumulator));
break;
}
else
{
printf("result so far is %.1f\n", do_next_op(op, operand, &accumulator));
}
}while(op != 'q' || op == 'Q');
}
void scan_data(char *op, float *operand)
{
scanf("%c%f",op, operand);
}
float do_next_op(char op, float operand, float *accumulator)
{
switch(op)
{
case('+'):
*accumulator = *accumulator + operand;
break;
case('-'):
*accumulator = *accumulator - operand;
break;
case('*'):
*accumulator = *accumulator * operand;
break;
case('/'):
*accumulator = *accumulator / operand;
break;
case('^'):
*accumulator = pow(*accumulator,operand);
break;
}
return *accumulator;
}
scanf(" %c%f",op, operand);
pow()
函数预期使用的是double
值,而不是float
值。建议:
scanf("%c%f",op, operand);
*accumulator = pow(*accumulator,operand);
请注意,这是在调用
powf()
而不是pow()
语句中存在问题:
#include <stdio.h>
#include <math.h>
void scan_data(char *op, float *operand);
float do_next_op(char op, float operand, float *accumulator);
int main(void)
{
char op;
float operand;
float accumulator = 0;
do
{
printf(" + add\n - subtract\n * multiply\n / divide\n ^ power\n q quit\n\n");
scan_data(&op, &operand);
if(op == 'q' || op == 'Q')
{
printf("Final result is %.1f\n", do_next_op(op, operand, &accumulator));
break;
}
else
{
printf("result so far is %.1f\n", do_next_op(op, operand, &accumulator));
}
}while(op != 'q' || op == 'Q');
}
void scan_data(char *op, float *operand)
{
scanf("%c%f",op, operand);
}
float do_next_op(char op, float operand, float *accumulator)
{
switch(op)
{
case('+'):
*accumulator = *accumulator + operand;
break;
case('-'):
*accumulator = *accumulator - operand;
break;
case('*'):
*accumulator = *accumulator * operand;
break;
case('/'):
*accumulator = *accumulator / operand;
break;
case('^'):
*accumulator = pow(*accumulator,operand);
break;
}
return *accumulator;
}
scanf(" %c%f",op, operand);
在第一次迭代之后,stdin
换行符
是一个“空白”,通常是\n
该“空白必须在%c
之前使用,而不是%c
使用它。建议:
scanf("%c%f",op, operand);
*accumulator = pow(*accumulator,operand);
格式字符串中的前导空格占用了该“空白”
关于声明:
#include <stdio.h>
#include <math.h>
void scan_data(char *op, float *operand);
float do_next_op(char op, float operand, float *accumulator);
int main(void)
{
char op;
float operand;
float accumulator = 0;
do
{
printf(" + add\n - subtract\n * multiply\n / divide\n ^ power\n q quit\n\n");
scan_data(&op, &operand);
if(op == 'q' || op == 'Q')
{
printf("Final result is %.1f\n", do_next_op(op, operand, &accumulator));
break;
}
else
{
printf("result so far is %.1f\n", do_next_op(op, operand, &accumulator));
}
}while(op != 'q' || op == 'Q');
}
void scan_data(char *op, float *operand)
{
scanf("%c%f",op, operand);
}
float do_next_op(char op, float operand, float *accumulator)
{
switch(op)
{
case('+'):
*accumulator = *accumulator + operand;
break;
case('-'):
*accumulator = *accumulator - operand;
break;
case('*'):
*accumulator = *accumulator * operand;
break;
case('/'):
*accumulator = *accumulator / operand;
break;
case('^'):
*accumulator = pow(*accumulator,operand);
break;
}
return *accumulator;
}
scanf(" %c%f",op, operand);
pow()
函数预期使用的是double
值,而不是float
值。建议:
scanf("%c%f",op, operand);
*accumulator = pow(*accumulator,operand);
请注意,这是在调用
powf()
而不是pow()
您是否尝试调试程序以缩小问题范围?最好的方法是在调试器中运行程序。例如,您是否验证了变量是否符合预期,尤其是op
和操作数
?另一个技巧是始终检查所有函数的返回值。这将使您的代码更加健壮,并帮助您更容易地发现许多问题。在这种情况下,请检查scanf
结果,如果结果不符合预期,请打印一个错误。由于您使用了%c%f”
格式,几乎可以肯定,您的%c
没有跳过空白。在%c
之前添加一个空格。只有三个scanf()
指令不跳过空格-%c
、%[…]
(扫描集)和%n
。如果在调试器中运行程序,甚至只打印出op
和操作数
的值,您会发现op
在第二次迭代中包含一行新行。然后应该引导您搜索为什么scanf
会在这种情况下放一个换行符代码>只初始化了一次,因此将其移动到循环内,以便进行下一次计算。还是应该继续修改值?另外,如前所述,scanf(“%c%f”,op,operand)存在问题代码>读取缓冲区中的换行符,而不是下一个运算符。尝试将空格添加为scanf(“%c%f”,op,operand)代码>是否尝试调试程序以缩小问题范围?最好的方法是在调试器中运行程序。例如,您是否验证了变量是否符合预期,尤其是op
和操作数
?另一个技巧是始终检查所有函数的返回值。这将使您的代码更加健壮,并帮助您更容易地发现许多问题。在这种情况下,请检查scanf
结果,如果结果不符合预期,请打印一个错误。由于您使用了%c%f”
格式,几乎可以肯定,您的%c
没有跳过空白。在%c
之前添加一个空格。只有三个scanf()
指令不跳过空格-%c
、%[…]
(扫描集)和%n
。如果在调试器中运行程序,甚至只打印出op
和操作数
的值,您会发现op
在第二次迭代中包含一行新行。然后应该引导您搜索为什么scanf
会在这种情况下放一个换行符代码>只初始化了一次,因此将其移动到循环内,以便进行下一次计算。还是应该继续修改