这应该工作吗 [英] should this work
问题描述
程序很简单,我知道我应该使用开关代替
,但我这样做是有原因的。但是我不确定它是否应该工作
当我编译它时我收到警告信息然后突然
它开始工作。我编写程序的原因是习惯使用
函数,if语句和逻辑运算符。
/ * ARITH1.C简单的计算程序* /
#include< stdio.h>
浮动添加(浮动a,浮动b);
浮动sub(float a,float b);
float multi(float a,float b);
float div(float a,float b);
float per(float a,float b);
char call(float math);
float a,b,c,math;
char d;
int main(无效)
{
printf("输入第一个值:" );
scanf("%f"& a);
printf("输入第二个值:");
scanf("%f"& b);
电话(d);
返回0;
}
char call(浮动数学)
{
while(d!=''q''){
printf("输入操作数或q退出:");
scanf("%s",& d);
if(d ==''+'')
{math = add(a,b); }
if(d =='' - '')
{math = sub(a,b); }
if(d ==''*'')
{math = multi(a,b); }
if(d ==''/'')
{math = div(a,b); }
if(d ==''%'')
{math = per(a,b); }
if(d!=''+''&& d!='' - ''&& d!=''*''&& d!= ''/''&& d!=''%''&& d!=''q'')
{printf("操作数不被接受再试一次) N'QUOT);继续; }
if(d ==''q'')
{puts(" Exiting");打破; }
printf(总数是%.3f \ n,数学);
}
返回数学;
}
浮动添加(浮动a,浮动b)
{
c = a + b;
返回c;
}
浮动子(浮动a,浮动b)
{
c = a - b;
返回c;
}
浮动多(浮动a,浮动b)
{
c = a * b;
返回c;
}
浮动div(浮动a,浮动b)
{
c = a / b;
返回c;
}
浮动每(浮动a,浮动b)
{
c = a * b / 100;
返回c;
}
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using
functions, if statement and logical operators.
/* ARITH1.C SIMPLE CALCULATOR PROGRAM */
#include<stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);
call(d);
return 0;
}
char call(float math)
{
while(d != ''q''){
printf("Enter operand or q to exit: ");
scanf("%s",&d);
if(d == ''+'')
{ math = add(a,b); }
if(d == ''-'')
{ math = sub(a,b); }
if(d == ''*'')
{ math = multi(a,b); }
if(d == ''/'')
{ math = div(a,b); }
if(d == ''%'')
{ math = per(a,b); }
if(d != ''+'' && d != ''-'' && d != ''*'' && d != ''/'' && d != ''%'' && d != ''q'')
{ printf("Operand not accepted try again\n"); continue; }
if(d == ''q'')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
}
float add(float a, float b)
{
c = a + b;
return c;
}
float sub(float a,float b)
{
c = a - b;
return c;
}
float multi(float a, float b)
{
c = a * b;
return c;
}
float div(float a, float b)
{
c = a / b;
return c;
}
float per(float a, float b)
{
c = a * b / 100;
return c;
}
推荐答案
amanayin写道:
amanayin writes:
程序很简单,我知道我应该使用switch而不是
但是我这样做是有原因的。但我不确定它是否应该工作
当我编译它时我收到了警告信息,但随后它突然开始工作了。我编写程序的原因是为了习惯使用函数
语句和逻辑运算符。
/ * ARITH1.C简单的计算程序* /
#include< stdio.h>
float add(浮动a,浮动b);
浮动子(浮动a,浮动b);
浮动多(浮动a,浮动b);
浮点div(浮点数a,浮点数b);
浮点数(浮点数a,浮点数b);
字符调用(浮点数学);
浮点数a,b ,c,math;
char d;
int main(void)
{
printf("输入第一个值:");
scanf("%f",& a);
printf("输入第二个值:");
scanf("%f"& b);
电话(d);
我不会这样做。当你需要一个浮点数时,你传递call()一个char。
在这样的代码中没有共振。对于你曾经说过*曾经*的问题,它可能没有任何影响。
返回0;
}
char call(浮动数学)
{while = d!=
printf("输入操作数或q退出:");
scanf("%s"& d);
if(d ==''+'')
{math = add(a,b);}
if(d =='' - '')
{math = sub(a,b);}
if(d ==''*'')
{math = multi (a,b);}
if(d ==''/'')
{math = div(a,b);}
if(d ==''%' ')
{math = per(a,b);}
if(d!=''+''&& d!='' - ''&& d!= '''''&& d!=''/''&& d!=''%''&& d!=
''q''){printf(" ;操作数不被接受再次尝试\ n;继续;}
if(d ==''q'')
{puts(" Exiting"); break;}
printf(Total is%.3f\ n,math);
}
返回数学;
浮点数(浮点数a,浮点数b) )
{
c = a + b;
返回c;
}
浮动子(浮动a,浮动b)
{
c = a - b;
返回c;
}
浮多(浮动a,浮动b)
{/ / c = a * b;
返回c;
}
浮动div(浮动a,浮动b)
{
c = a / b;
返回c;
}
浮动每(浮动a,浮动b)
{
c = a * b / 100;
返回c ;
}
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using functions, if statement and logical operators.
/* ARITH1.C SIMPLE CALCULATOR PROGRAM */
#include<stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);
call(d);
I wouldn''t do that. You are passing call() a char when it wants a float.
There is not reson to do that in code such as this. It may have no bearing
on the problem you say you *used to have*.
return 0;
}
char call(float math)
{
while(d != printf("Enter operand or q to exit: ");
scanf("%s",&d);
if(d == ''+'')
{ math = add(a,b); }
if(d == ''-'')
{ math = sub(a,b); }
if(d == ''*'')
{ math = multi(a,b); }
if(d == ''/'')
{ math = div(a,b); }
if(d == ''%'')
{ math = per(a,b); }
if(d != ''+'' && d != ''-'' && d != ''*'' && d != ''/'' && d != ''%'' && d != ''q'') { printf("Operand not accepted try again\n"); continue; }
if(d == ''q'')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
}
float add(float a, float b)
{
c = a + b;
return c;
}
float sub(float a,float b)
{
c = a - b;
return c;
}
float multi(float a, float b)
{
c = a * b;
return c;
}
float div(float a, float b)
{
c = a / b;
return c;
}
float per(float a, float b)
{
c = a * b / 100;
return c;
}
amanayin< ng ****** @ netscape.net>写道:
amanayin <ng******@netscape.net> wrote:
该程序很简单,我知道我应该使用开关而不是如果
但我这样做是有原因的。但我不确定它是否应该工作
当我编译它时我收到了警告信息,但随后它突然开始工作了。我编写程序的原因是习惯使用
函数,如果语句和逻辑运算符。
/ * ARITH1.C简单的计算程序* /
#include< stdio.h>
float add(浮动a,浮动b);
浮动子(浮动a,浮动b);
浮动多(浮动a,浮动b);
浮动div(浮动a,浮动b);
''div''是函数的错误名称,因为在stdlib.h中声明了一个与
相同名称的函数。还没问题,但是如果你必须在后面包含stdlib.h,那么这两个定义会发生冲突。称它为''除''
等。
float per(float a,float b);
char call(float math);
浮动a,b,c,数学;
char d;
int main(无效)
{
printf("输入第一个值:" );
scanf("%f",& a);
printf("输入第二个值:");
scanf("%f"& b) );
致电(d);
你将一个角色传递给一个期望加倍的功能。
返回0;
}
char call (浮动数学)
1.你为什么要传递一个参数?
2.你的函数被声明为返回一个角色。
稍后您返回双倍。
{
while(d!=''q''){
printf(" Enter operand)或者q退出:");
scanf("%s",& d);
%s转换说明符用于读取字符串。它会在& d存储任何
用户输入加上一个空字符,但是只有一个
字符的空间。使用%c (矫枉过正)或只是d = getchar(); (推荐)。
if(d ==''+'')
{math = add(a,b); }
if(d =='' - '')
{math = sub(a,b); }
if(d ==''*'')
{math = multi(a,b); }
if(d ==''/'')
{math = div(a,b); }
if(d ==''%'')
{math = per(a,b); }
if(d!=''+''&& d!='' - ''&& d!=''*''&& d!=''/' '&& d!=''%''&& d!=''q'')
{printf("操作数不被接受再试一次'\\ n');继续; }
if(d ==''q'')
{puts(" Exiting");打破; }
printf(Total is%.3f\ n,math);
}
返回数学;
见上文。
}
浮动加(浮动a,浮动b)
{
c = a + b;
返回c;
}
浮子(浮动a,浮动b)
{
c = a - b;
返回c;
}
浮多(浮动a,浮动b)
{
c = a * b;
返回c ;
}
浮动div(浮动a,浮动b)
见上文。
{
c = a / b;
返回c;
}
浮动每(浮动a,浮动b)
{
c = a * b / 100;
返回c;
}
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using
functions, if statement and logical operators.
/* ARITH1.C SIMPLE CALCULATOR PROGRAM */
#include<stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
''div'' is a bad name for a function, because there''s a function with the
same name declared in stdlib.h. Not a problem yet, but if you have to
include stdlib.h later the two definitions will clash. Call it ''divide''
or the like.
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);
call(d);
You pass a character to a funtion that expects a double.
return 0;
}
char call(float math)
1. Why do you pass an argument at all?
2. Your function is declared to return a character.
Later on you return a double.
{
while(d != ''q''){
printf("Enter operand or q to exit: ");
scanf("%s",&d);
The "%s" conversion specifier is for reading strings. It will store any
user input plus a null character at &d, but there''s only space for one
character. Use "%c" (overkill) or just d = getchar(); (recommended).
if(d == ''+'')
{ math = add(a,b); }
if(d == ''-'')
{ math = sub(a,b); }
if(d == ''*'')
{ math = multi(a,b); }
if(d == ''/'')
{ math = div(a,b); }
if(d == ''%'')
{ math = per(a,b); }
if(d != ''+'' && d != ''-'' && d != ''*'' && d != ''/'' && d != ''%'' && d != ''q'')
{ printf("Operand not accepted try again\n"); continue; }
if(d == ''q'')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
See above.
}
float add(float a, float b)
{
c = a + b;
return c;
}
float sub(float a,float b)
{
c = a - b;
return c;
}
float multi(float a, float b)
{
c = a * b;
return c;
}
float div(float a, float b)
See above.
{
c = a / b;
return c;
}
float per(float a, float b)
{
c = a * b / 100;
return c;
}
除了上述问题,您的代码看起来很好AFAICT。
HTH
问候
-
Irrwahn
(ir *** ****@freenet.de)
Besides of the mentioned problems your code looks fine AFAICT.
HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
On Sun,2003年10月19日16:41:20 +0000(UTC)
amanayin<纳克****** @ netscape.net>写道:
On Sun, 19 Oct 2003 16:41:20 +0000 (UTC)
amanayin <ng******@netscape.net> wrote:
该程序很简单,我知道我应该使用开关而不是如果
但我这样做是有原因的。但我不确定它是否应该在我编译时工作我收到了警告信息然后
突然它开始工作了。我编写该程序的原因是为了使用函数,如果声明和逻辑运算符。
/ * ARITH1.C简单的计算程序* /
#include< stdio.h>
float add(浮动a,浮动b);
浮动子(浮动a,浮动b);
浮动多(浮动a,浮动b);
浮点div(浮点数a,浮点数b);
浮点数(浮点数a,浮点数b);
字符调用(浮点数学);
浮点数a,b ,c,数学;
char d;
为什么将这些声明为全局变量?如果你最小化变量的范围,那么把事情弄清楚要容易得多。
int main(无效)
{
printf("输入第一个值:);
scanf("%f",& a);
printf("输入第二个值:");
scanf("% f,& b);
致电(d);
调用被声明为浮动,但你传递的是一个角色。你好b $ b编译器应该警告过你。另外d还没有初始化
并将其作为参数传递给你评估它。
返回0;
}
char call(float math)
你永远不会使用传入的值,为什么要把它作为参数?
{
虽然(d!=''q''){
d仍然没有被初始化所以绝对可以发生任何事情
这里。
printf("输入操作数或q退出:");
这可能实际上不会在scanf之前打印出来。阅读常见问题解答
进一步信息。
scanf("%s"& d);
%s获取一个以空字符结尾的字符串,而不是单个字符但是你只需要传递一个字符的地址,所以这保证<任何输入都有
溢出。
if(d ==''+'')
{math = add(a,b); }
if(d =='' - '')
{math = sub(a,b); }
if(d ==''*'')
{math = multi(a,b); }
if(d ==''/'')
{math = div(a,b); }
if(d ==''%'')
{math = per(a,b); }
if(d!=''+''&& d!='' - ''&& d!=''*''&& d!=''/' '&& d!='''%''&& d
!=''q'')
{printf("操作数不被接受再试一次'\\ n');继续; }
if(d ==''q'')
{puts(" Exiting");打破; }
休息将退出while循环使条件完全恢复
一旦所有其他错误得到修复就没有意义。
printf (Total is%.3f\ n,math);
}
返回数学;
浮点数(浮点数a,浮点数b)
{
c = a + b;
返回c;
为什么你在这里使用全局变量?为什么不直接使用
返回a + b;
}
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should
work when i was compiling it i was getting warning messages but then
suddenly it started working. The reason i wrote the program was to get
used to using functions, if statement and logical operators.
/* ARITH1.C SIMPLE CALCULATOR PROGRAM */
#include<stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
Why declare these as globals? It is far easier to get things right if
you minimise the scope of variables.
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);
call(d);
call is declared as taking a float but you are passing a character. You
compiler should have warned you about this. Also d has not been
initialised and by passing it as a parameter you are evaluating it.
return 0;
}
char call(float math)
You never use the value passed in so why have it as a parameter?
{
while(d != ''q''){
d still has not been initialised so absolutely ANYTHING could happen
here.
printf("Enter operand or q to exit: ");
This may not actually be printed before the scanf. Read the FAQ for
further information.
scanf("%s",&d);
%s fetches a null terminated string, not a single character but you are
only passing the address of a single character, so this is guaranteed to
overflow on any input.
if(d == ''+'')
{ math = add(a,b); }
if(d == ''-'')
{ math = sub(a,b); }
if(d == ''*'')
{ math = multi(a,b); }
if(d == ''/'')
{ math = div(a,b); }
if(d == ''%'')
{ math = per(a,b); }
if(d != ''+'' && d != ''-'' && d != ''*'' && d != ''/'' && d != ''%'' && d
!= ''q'')
{ printf("Operand not accepted try again\n"); continue; }
if(d == ''q'')
{ puts("Exiting"); break; }
The break will exit the while loop making the condition completely
pointless once all the other bugs are fixed.
printf("Total is %.3f\n",math);
}
return math;
}
float add(float a, float b)
{
c = a + b;
return c;
Why are you using a global variable here? Why not just use
return a + b;
}
< snip>
我认真地认为你需要通过一本体面的教科书来完成工作。你可以在comp.lang.c的常见问题解答中找到建议(问题18.10)
-
Mark Gordon
支付成为极客&高级软件开发人员
虽然我的电子邮件地址是垃圾邮件,但它是真实的,我读了它。
<snip>
I seriously think you need to do work through a decent text book. You
will find recommendations in the FAQ for comp.lang.c (Q 18.10)
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
这篇关于这应该工作吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!