本篇文章给大家谈谈c语言计算表达式,以及c语言计算表达式题目对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、c语言的逻辑表达式运算2、c语言 表达式求值3、C语言关于表达式求值4、C语言中表达式的值怎么算5、C语言计算并输出个表达式的值?6、求C语言表达式运算方法
c语言的逻辑表达式运算
第一个,算术运算逻辑运算,首先计算b+c=9
b-c=-1
这样三个值都为非0
故而结果不管怎么样都是ture(1)
第二个,考察了更多的优先级问题。首先括号之外,表达式里面的!运算优先级最高,所以有
!7+c-16.5
有0+116.5
116.5
两侧都是非0
故而结果也为ture(1)
c语言 表达式求值
#includestdio.h
#inlcudestdlib.h
void main()
{
char str[20][201],*p0,*p1;
int t,i,s,f,loop;
scanf(“%d”,t); if ( t20 ) t=20;
for ( i=0;it;i++ ) scanf(“%s”,str[i]);
for ( i=0;it;i++ )
{ p0=p1=str[i]; s=0; f=1; loop=1;
while ( loop )
{ while ( (*p1)=’0′ (*p1)=’9′ ) p1++;
s+=(f*atoi(p0));
switch ( (*p1) )
{ case 0: loop=0; break;
case ‘+’: f=1; break;
case ‘-‘: f=(-1); break;
default: loop=0; break;
}
p1++; p0=p1;
}
printf(“\nCase %d:\n”,i+1);
printf(“%s=%d\n”,str[i],s);
}
}
C语言关于表达式求值
这是我以前做的一个表达式求值的程序,要求和实现的功能是一样的:
#includestdio.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;iNUM;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(num1 || num4);
return(num);
}
C语言中表达式的值怎么算
此式即等同于 (4!=3)(2)(4+15)
1代表真,0代表非,则原式即 110 所以值为0。
例如:
-12/5 = -2 只为这个是整数运算,结果只取整数部分。
62 1*(6+7) + 7 *(6+1) = 13 +49 62
3.2 *(6+7+1) = 44.8
扩展资料:
一个表达式的赋值和算符的定义以及数值的定义域是有关联的。
两个表达式若被说是等值的,表示对于自由变量任意的定值,两个表达式都会有相同的输出,即它们代表同一个函数。
一个表达式必须是合式的。亦即,其每个算符都必须有正确的输入数量,在正确的地方。如表达式2+3便是合式的;而表达式*2+则不是合式的,至少不是算术的一般标记方式。
表达式和其赋值曾在20世纪30年代由阿隆佐·邱奇和Stephen Kleene在其λ演算中被公式化。λ演算对现代数学和电脑编程语言的发展都曾有过重大的影响。
参考资料来源:百度百科-表达式
C语言计算并输出个表达式的值?
#includestdio.h
int main()
{ int a=3,b=2,c=1,m;
printf(“%d\n”,ca+b);
printf(“%d\n”,ab==c);
printf(“%d\n”,a==bc);
printf(“%d\n”,m=bc);
printf(“%d\n”,m=abc);
printf(“%d\n”,c+1=a);
printf(“%d\n”,c*2!=b);
printf(“%d\n”,m=c==a%b);
return 0;
}
求C语言表达式运算方法
1.注意
abc都是整型的,整型运算结果也要取整,
a/b+0.4
就是
8/5+0.4
就是1+.04=1.4
,赋值到C,C是整型,取整为1
2.
+=
,-=
,
*
三个运算,先算*
再算-=
再+=,等号类的是从右向左的
a*a为9
所以a+=a-=9
a-=9结果为a是-6
所以a+=a是a+=-6
注意a为-6,相加为
-12
c语言计算表达式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言计算表达式题目、c语言计算表达式的信息别忘了在本站进行查找喔。