c语言 分段函数求值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言 分段函数求值相关的知识,希望对你有一定的参考价值。

题目描述
有如下分段函数
F(x) = x^2 + 1 当x> 0时;
F(x) = -x 当x<0时;
F(x) = 100.00 当x=0时;
编程根据输入的不同x(x为实数且|x| <= 1000),输出其对应的函数值

输入

多组输入,每组一个实数x。处理到文件结束。
输出

对于每组输入x,输出其对应的F(x),每组一行,结果保留1位有效数字。
我的代码:(错的)
#include <stdio.h>
int main()

float x,f;
while(scanf("%f",&x)!=EOF)

if(x>0)

f = x*x+1;
else if(x<0)

f = -x;
else if(x == 0)

f = 100.00;

printf("%.1f\n",f);

return 0;

你这个题是ACM的题目?我看了下你的程序,正经的数字是可以的,但你说错了,那就该就是要考虑极限情况了。譬如x=0.0000000000000000000000000000000001的时候,你的程序输出是100.0。。。。。应该就是出错在这里了。
还有我的老师告诉我,使用float不要用x==0这种表达方式,,因为float类型的等于其实就是在有限的位数上比较大小。。。当数字极小或者极大的时候就会出现不相等的数字会出现相等的情况了。
换成double就行了
#include "stdio.h"
void main()

double a=0.0;
while(scanf("%lf",&a)!=EOF)

if(a>0)

a=a*a+1;


else if(a<0)

a=-a;


else if(a==0)

a=100.0;


printf("%.1lf\n",a);
a=0.0;


追问

我会永远记住0.000000000000000000000000000000001和0是有差距的。
谢谢你!又不懂得还会再问你的。

追答

ok

参考技术A 代码如下,不懂问哦o(∩_∩)o

#include<stdio.h>
int main()
double x=1,r[4];
for(int i=1;i<5;i++)
printf("请输入第%d个实数\n",i);
scanf("%lf",&x);
if(x>0)
r[i-1]=x*x+1;

else if(x<0)
r[i-1]=-x;

else
r[i-1]=100;


printf("处理得到的结果如下:\n");
for(int i=0;i<4;i++)
printf("%.1lf\n",r[i]);

return 0;
参考技术B 回答

您好,我这边正在为您查询,请稍等片刻,我这边马上回复您~

您好,很高兴为您解答。#include#include#includetypedef structlong long int molecular;long long int denominator;FRACTION;int change_to_proper_fraction(FRACTION *fraction);void fraction_add(FRACTION *frac_sum, FRACTION *frac_num);main()FRACTION frac[4];int ret = 0;long long int quo = 0;frac[0].molecular = 1;frac[0].denominator = 2;frac[1].molecular = 1;frac[1].denominator = 3;frac[2].molecular = 1;frac[2].denominator = 6;frac[3].molecular = 0;frac[3].denominator = 0;if (change_to_proper_fraction(&frac[0])<0 || change_to_proper_fraction(&frac[1])<0|| change_to_proper_fraction(&frac[2])<0)

printf("%lld/%lld+%lld/%lld+%lld/%lld=%lld\\n", frac[0].molecular, frac[0].denominator,frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular/frac[3].denominator);elseprintf("%lld/%lld+%lld/%lld+%lld/%lld=%lld/%lld\\n", frac[0].molecular, frac[0].denominator,frac[1].molecular, frac[1].denominator, frac[2].molecular, frac[2].denominator, frac[3].molecular, frac[3].denominator);exit(1);int change_to_proper_fraction(FRACTION *fraction)if (fraction->denominator == 0)return -1;if (fraction->molecular == 0)fraction->denominator = 0;return 0;if ((fraction->molecular%fraction->denominator) == 0)fraction->molecular = fraction->molecular/fraction->denominator;fraction->denominator = 1;return 0;long long int diff = 0;long long int larger = 0;long long int smaller = 0;long long int mole_posi = 0;long long int deno_posi = 0;mole_posi = fraction->molecular > 0 ? fraction->molecular : (0 - fr

long long int diff = 0;long long int larger = 0;long long int smaller = 0;long long int mole_posi = 0;long long int deno_posi = 0;mole_posi = fraction->molecular > 0 ? fraction->molecular : (0 - fraction->molecular);deno_posi = fraction->denominator > 0 ? fraction->denominator : (0 - fraction->denominator);smaller = mole_posidiff = deno_posi + mole_posi - 2 * smaller;larger = diff>smaller ? diff : smaller;smaller = diff + smaller - larger;while (larger%smaller != 0)diff = larger - smaller;larger = diff>smaller ? diff : smaller;smaller = diff + smaller - larger;fraction->molecular = fraction->molecular/smaller;fraction->denominator = fraction->denominator/smaller;return 0;void fraction_add(FRACTION *frac_sum, FRACTION *frac_num)

参考技术C 说实话,没看出什么,描述一下你的现象吧追问

我会了,谢谢

C语言关于表达式求值

一个算术表达式是由操作数(operand)、运算符(operator)和界限符(delmiter)组成的。假设操作数是正整数,运算符只含加减乘除等四种运算符,界限符有左右括号和表达式起始、结束符“#”,如:#(7+15)*(23-28/4)#.引入表达式起始、结束符式为了方便。编程利用“算符优先法”求算术表达式的值。要求:(1)从键盘读入一个合法的算术表达式,输出正确的结果。(2)显示输入序列和栈的变化过程。选作内容:操作数类型扩充到实数。提示:(1)中缀表达式转换成后缀表达式,顺序扫描中缀算术表达式,当读到数字时直接将其送至输出队列中:当读到运算符时,将栈中所有优先级高于或等于该运算符的运算符弹出,送至输出队列中,再将当前运算符入栈:当读入左括号时,即入栈;当读入右括号时,将靠近栈的第一个左括号上面的运算符依次弹出,送至输出队列中,再删除栈中左括号。(2)后缀表达式的计算。在计算后缀表达式时,最后保存的值是最先取出参与运算,所以要用到栈
6.0环境下可以运行的,用c不是c++

这是我以前做的一个表达式求值的程序,要求和实现的功能是一样的:
#include<stdio.h>
#include <string.h>
#include <conio.h>
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
char a[]='+','-','*','/','(',')','#';
int PriorityTable[7][7]= 1, 1,-1,-1,-1, 1, 1,
1, 1,-1,-1,-1, 1, 1,
1, 1, 1, 1,-1, 1, 1,
1, 1, 1, 1,-1, 1, 1,
-1,-1,-1,-1,-1, 0, NO,
1, 1, 1, 1,NO, 1, 1,
-1,-1,-1,-1,-1,NO, 0;
int menu(void);
void InputExpression(char str[])

int len;
printf("Input expression string:\n");
scanf("%s",str);
len=strlen(str);
str[len]='#';
str[len+1]='\0';

int GetCharType(char ch)

int i;
for(i=0;i<NUM;i++) if(ch==a[i]) return(i);
if(ch>='0' && ch<='9') return(DIGIT);
if(ch=='.') return(POINT);
return(-1);

double Operate(double a,int theta,double b)

double x;
switch(theta)

case 0:x=a+b;break;
case 1:x=a-b;break;
case 2:x=a*b;break;
case 3:x=a/b;break;

return (x);

int EXCUTE(char *str,double *Result)

int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE];
double number,temp,OPND[STACKSIZE];
OPTR[0]=STARTEND;
topTr=1;
topNd=0;
pp=0;
while((str[pp]))

CharType=GetCharType(str[pp]);
switch(CharType)

case -1:
return(0);
case DIGIT:
number=0;
while(str[pp]>='0' && str[pp]<='9')

number=number*10+(str[pp]-48);
pp++;

if(str[pp]=='.')

temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')

number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;


OPND[topNd]=number;
topNd++;
break;
case POINT:
number=0;
temp=10.0;
pp++;
while(str[pp]>='0' && str[pp]<='9')

number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;

OPND[topNd]=number;
topNd++;
break;
case PLUS:
case MINUS:
case POWER:
case DIVIDE:
if(PriorityTable[OPTR[topTr-1]][CharType]==-1)

OPTR[topTr]=CharType;
topTr++;
pp++;

else

OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--;
topTr--;

break;
case LEFTP:
OPTR[topTr]=CharType;
topTr++;
pp++;
break;
case RIGHP:
while(OPTR[topTr-1]!=LEFTP)

if(OPTR[topTr-1]==STARTEND)return(0);
if(PriorityTable[OPTR[topTr-1]][CharType]==1)

OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--;
topTr--;

else
break;

topTr--;
pp++;
break;
case STARTEND:
while(OPTR[topTr-1]!=STARTEND)

OPND[topNd-2]=Operate(OPND[topNd-2],OPTR[topTr-1],OPND[topNd-1]);
topNd--;
topTr--;

if(topNd==1)

*Result=OPND[0];
return(1);

else
return(0);


return(1);

void main()
int num,flag;
double result;
char str[256];
str[0]='0';
while(1)
num=menu();
switch(num)

case 1:
InputExpression(str);
flag=0;
printf("%s\n",str);
getchar();
break;
case 2:
if(str[0]=='0')
printf("Expression is Empty!");
getchar();
break;

if(!EXCUTE(str,&result))
printf("The expression has error!\n");
getchar();

else
printf("calulation has finished!\n");
getchar();
flag=1;

break;
case 3:
if(flag)
printf("#%s=%lf\n",str,result);
getchar();

break;
case 4:
break;

if(num==4) break;


int menu(void)

int num;
printf("%20c1--input expression\n",' ');
printf("%20c2--calculation expression\n",' ');
printf("%20c3--print result\n",' ');
printf("%20c4--Quit\n",' ');
printf(" please select 1,2,3,4:");
do

scanf("%d",&num);
while(num<1 || num>4);
return(num);
参考技术A c语言有丰富的表达式,这是它的特点之一,表达式主要有4类,算术表达式,赋值表达式,逗号表达式,关系表达式
1.算术表达式就是包含算术运算符(如+
-
/
*
%等)的表达式(不是语句,后面没有分号),如:a+b
,a%b,a+b-c*d,3+5等,算术表达式的值就是最后算出的结果,如3+5这个表达式的值就是8
2.赋值表达式,就是含有赋值运算符=的表达式,如a=5,b=3,c='a'等,=左边的a,b,c称为左值,必须为变量,=右边的5,3,'a'称为右值,必须为常量,赋值表达式的值为右值,如a=3的值为3,c='a'的值为字母a的ascii码65(当然也可以认为它的值就是字母a)
3.逗号表达式就是含有逗号的表达式,形式:表达式1,表达式2,表达式3.......如a,b,c
3,5,7
a=3,b=4,c=6
3,a=5,b=6等
逗号表达式的值为,最右边的表达式的值,如3,4,5的值就是5,表达式a=3,b=4,c=6的值就是表达式b=6的值,由上述分析知,表达式b=6的值就是6,所以表达式a=3,b=4,c=6的值就是6
4.关系表达式,指含有关系运算符(如>
<
>=
==
=<等)的表达式(其实也是算术表达式的一种)如a>b,a>6,6>5,3<2,4==6等,如果表达式的关系是正确的,那么表达式的值为1,否则为0
如6>5正确,表达式的值为1,3<2,和4==6错误,表达式的值为0
当然可以细分为很多种表达式,不过主要也就是这几种的变型,希望对你有所帮助
参考技术B 这种后缀自增的表达式,在最开始整个表达式作为运算的时候(比如是在for循环的时候),是先把a的值拿过去做运算的,然后在去把a
的值加一。
还有种前缀自增的表达式,比如++a
是先把a的值加一,然后再参与整个运算。。。
这些都是C语言的一些规定,你可以这么去记忆,就是“加加号”在前面的表达式就先做加法然后在运算,“加加号”在后面的表达式就先去运算然后再加。。。同样地,可以用于自减,如a--或者--a的情况。。。望采纳
参考技术C a++是a参与计算之后再进行对a的加1计算,而++a是先加1再进行计算,上面三个式子计算下来分别是:
3-5-4
=
-6,
a
=
4,
b
=
-3
(4+1)-5+(-3+1)
=
-2,
a
=
5,
b
=
-2
(5+1)-5-4
=
-3,
a
=
6,
c
=
6(注意这里c+++b会认为前两个++在一起,后面一个是单独的)
这里认为上面三个式子是连续的三个式子,也就是说前面的运算结果会影响到后面的自变量,如果不是这样的,只要掌握方法也很容易就可以就算出来。
最后要说的一点是,如果是你自己编程,最好不要用上面这样的写法,因为不管对你本人还是读代码的人都会造成不小的麻烦,如果误解的话要查处错误来就很不容易了。最好是将++的式子提取出来,单独计算。
参考技术D 原理是比较简单的
假设表达式是正确的,不再进行表达式检查。
表达式就是一个字符串了,在字符串中从头开始找乘号或除号,假设符号为B找到的话就找这个符号前面和后面的运算数字A和C,然后把结果ABC运算出来,替换原表达式中的ABC字串,组成新的字符串。
对新的字符串进行上述操作,一直到没有乘号或者除号为止。
然后继续寻找加号和减号,不过这个就没必要一次次的找了,把字符串遍历一遍运算完毕即可。

以上是关于c语言 分段函数求值的主要内容,如果未能解决你的问题,请参考以下文章

ZZNUOJ_C语言1035:分段函数求值(完整代码)

ZZNUOJ_C语言1035:分段函数求值(完整代码)

C语言中函数怎么自己调用自己

c语言分段函数流程图怎么画

C语言关于表达式求值

C语言编程-逆波兰表达式求值