這裡蒐索程式師資訊,查找有用的技術資料
当前位置:首页 » 编程语言 » c语言计算器遇到双重括号
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言计算器遇到双重括号

发布时间: 2022-05-12 07:23:53

① 用c语言编写表达式计算器时,括号该不该入表达式的栈还是在入栈前先计算括号内的内容

用的是逆波兰表达式吧,在没有遇到右括号之前,所有的左括号都要入栈,不然怎么计算子表达式的值呢。

② 找了一个用C语言编写的带括号运算的简易计算器,运行结果错的很离谱,求大神改正一下。

intmid(void)
{
intdiv;
intresult=high();
while((token=='*')||(token=='/'))//改为==,原来是=
if(token=='*')
{
match('*');
result*=high();
break;
}
elseif(token=='/')
{
match('/');
div=high();
if(div==0)
{
printf("除数为零. ");
exit(1);
}
result/=div;
break;
}
returnresult;
}

害我一顿调试。。。楼主太粗心了


其实就改了一个判断

③ 求按要求用C语言作的一个计算器

计算器的c语言程序设计
一.功能概述:本程序能计算带二重括号的计算式的结果,如输入((3+2)*(3+2)+5)*((3+2)*(3+2)+5)*(5+5)= 将输出结果为9000
二.源程序:
#include<stdio.h>
int w;
double B[4][40];

shuru(char *pt)
{
int i=0,m;
do
{
*(pt+i)=getchar();
m=i;
i++;
}
while(*(pt+m)!='=');
*(pt+m)='+';
*(pt+m+1)='=';
}

tlchli(char *p)
{
int a,b;
if(*p=='-'||*p=='+')
{
for(b=0;*(p+b)!='=';b++)
;
for(;b>=0;b--)
*(p+b+1)=*(p+b);
*p='0';
}
else
{
for(a=1;*(p+a)!='=';a++)
{
if(*(p+a)=='-'||*(p+a)=='+')
{
if(*(p+a-1)=='('&&*(p+a+1)>='0'&&*(p+a+1)<='9')
{
for(b=0;*(p+a+b)!='=';b++)
;
for(;b>=0;b--)
*(p+a+b+1)=*(p+a+b);
*(p+a)='0';
a=(a+1);
}
}
}
}
}

jgh(double B[4][40],char *p)
{
int a,b,c=0,d,e,f,h,i,k,n,m;
double g=0,j=0;
for(a=0;a<4;a++)
for(b=0;b<40;b++)
B[a]=0;
for(d=0;*(p+d)!='=';d++)
{
if(*(p+d)=='+'||*(p+d)=='-'||*(p+d)=='*'||*(p+d)=='/')
{
if(*(p+d)=='+') B[3][c]='+';
if(*(p+d)=='-') B[3][c]='-';
if(*(p+d)=='*') B[3][c]='*';
if(*(p+d)=='/') B[3][c]='/';
if(*(p+d-1)==')'&&*(p+d-2)==')')
{
B[0][c]=')';
B[1][c]=')';
}
if(*(p+d-1)==')'&&*(p+d-2)!=')')
B[1][c]=')';
for(e=d-1;*(p+e)<'0'||*(p+e)>'9';e--)
;
for(f=e;*(p+f)!='.'&&(f>0)&&*(p+f)>='0'&&*(p+f)<='9';f--)
;
if(*(p+f)=='.')
{
for(h=f-1;(h>=0)&&*(p+h)>='0'&&*(p+h)<='9';h--)
;
h++;
if(*(p+h-1)=='('&&*(p+h-2)=='(')
{
B[0][c]='(';
B[1][c]='(';
}
if(*(p+h-1)=='('&&*(p+h-2)!='(')
B[1][c]='(';
for(i=h;*(p+i)>='0'&&*(p+i)<='9';i++)
g=(*(p+i)-48)+g*10.0;
for(k=f+1;*(p+k)>='0'&&*(p+k)<='9';k++)
;
k--;
for(n=k;*(p+n)>='0'&&*(p+n)<='9';n--)
j=(j/10.0+(*(p+n)-48)/10.0);
B[2][c]=(g+j);
g=j=0;
}
else
{
for(m=e;*(p+m)>='0'&&*(p+m)<='9';m--);
m++;
if(*(p+m-1)=='('&&*(p+m-2)=='(')
{
B[0][c]='(';
B[1][c]='(';
}
if(*(p+m-1)=='('&&*(p+m-2)!='(')
B[1][c]='(';
for(i=m;*(p+i)>='0'&&*(p+i)<='9';i++)
g=(*(p+i)-48)+g*10.0;
B[2][c]=g;
g=0;
}
c++;
}
}
w=(c-1);
}

qkh1(double B[4][40])
{
int a=1,b,c,d,e,f,j,k;
for(b=0;b<w;b++)
{
if(B[a]=='(')
{
for(c=b;B[a][c]!=')';c++)
;
for(d=b+1;B[a][d]!='('&&d<c;d++)
;
if(B[a][d]=='(')
k=d;
else
k=b;
B[a][k]=0;B[a][c]=0;
for(e=k;e<c;e++)
{
if(B[3][e]=='*')
{
B[2][e+1]=B[2][e]*B[2][e+1];
if(B[3][e-1]=='-'&&(e>0))
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}

}
if(B[3][e]=='/')
{
B[2][e+1]=B[2][e]/B[2][e+1];
if(B[3][e-1]=='-'&&e>0)
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
}
for(f=k;f<c;f++)
{
if(B[3][f]=='+')
{
B[2][f+1]=B[2][f]+B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
if(B[3][f]=='-')
{
B[2][f+1]=B[2][f]-B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
}
b=c-1;
if(B[3][k-1]=='*'&&k>0)
{
for(;k<c;k++)
{
B[3][k]='*';
B[2][k]=1.0;
}
}
if(B[3][k-1]=='/'&&k>0)
{
for(;k<c;k++)
{
B[3][k]='/';
B[2][k]=1.0;
}
}
}
}
for(j=0;j<=w;j++)
{
if(B[1][j]!='('&&B[1][j]!=')')
B[1][j]=B[0][j];
}
}

qkh2(double B[4][40])
{
int a,b,c,d,e,f,j;
for(b=0;b<w;b++)
{
if(B[1]=='(')
{
for(c=b;B[1][c]!=')';c++)
;
B[1]=0;B[1][c]=0;
for(e=b;e<c;e++)
{
if(B[3][e]=='*')
{
B[2][e+1]=B[2][e]*B[2][e+1];
if(B[3][e-1]=='-'&&(e>0))
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
if(B[3][e]=='/')
{
B[2][e+1]=B[2][e]/B[2][e+1];
if(B[3][e-1]=='-'&&(e>0))
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
}
for(f=b;f<c;f++)
{
if(B[3][f]=='+')
{
B[2][f+1]=B[2][f]+B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
if(B[3][f]=='-')
{
B[2][f+1]=B[2][f]-B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
}
if(B[3][b-1]=='*'&&b>0)
{
for(;b<c;b++)
{
B[3]='*';
B[2]=1.0;
}
}
if(B[3][b-1]=='/'&&b>0)
{
for(;b<c;b++)
{
B[3]='/';
B[2]=1.0;
}
}
}
}
}

jshjg(double B[4][40])
{
int b,d,e,f;
for(e=0;e<w;e++)
{
if(B[3][e]=='*')
{
B[2][e+1]=B[2][e]*B[2][e+1];
if(B[3][e-1]=='-'&&e>0)
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
if(B[3][e]=='/')
{
B[2][e+1]=B[2][e]/B[2][e+1];
if(B[3][e-1]=='-'&&e>0)
{
B[2][e]=0;
B[3][e]='-';
}
else
{
B[2][e]=0;
B[3][e]='+';
}
}
}
for(f=0;f<w;f++)
{
if(B[3][f]=='+')
{
B[2][f+1]=B[2][f]+B[2][f+1];
B[2][f]=0;B[3][f]='+';
}
if(B[3][f]=='-')
{
B[2][f+1]=(B[2][f]-B[2][f+1]);
B[2][f]=0;B[3][f]='+';
}
}
}

shuchu(double B[4][40],char *p)
{
printf("answer %f\n",B[2][w]);
}

main()
{
char *p,A[100];
shuru(A);
p=&A[0];
tlchli(p);
jgh(B,p);
qkh1(B);
qkh2(B);
jshjg(B);
shuchu(B,p);
}

#include"string.h"
#include"graphics.h"
#include"conio.h"
#include"dos.h"
#include"time.h"
#include"stdlib.h"
#include"stdio.h"
#include"math.h"
/*调用一系列的库函数*/
int py;/*定义一个全局变量*/
void mode(int py)/*mode函数,用来设置的,来使用计算器的多种运算功能*/
{ double x1,x2,ans,ans2;
if(py<11)
{ printf("input x1,x2:");
scanf("%lf,%lf",&x1,&x2); }
else {printf("input x:");scanf("%lf",&x1);}
switch(py)
{ case 6 : ans=x1+x2;printf("=%.3lf",ans);break;
case 7 : ans=x1-x2;printf("=%.3lf",ans);break;
case 8 : ans=x1*x2;printf("=%.3lf",ans);break;
case 9 : ans=x1/x2;printf("=%.3lf",ans);break;
case 10 : ans=pow(x1,x2);printf("x^%.1lf=%.3lf",x2,ans);break;
case 11 : ans=sqrt(x1);printf("=%.3lf",ans);break;
case 12 : ans=sin(x1);ans2=cos(x1);printf("sin x=%.3lf,cos x=%.3lf",ans,ans2);break;
case 13 : ans=asin(x1);ans2=acos(x1);printf("asin x=%.3lf,acos x=%.3lf",ans,ans2);break;
}
getch();clrscr();
}
int join(char *ps) /*join函数是输入用户名和密码的,可于已注册的用户进行匹配,注册用户可使用高级功能*/
{ char name[30],pass[20];
int len,i;
printf("ID:");
scanf("%s",name);
printf("\npass word:");
scanf("%s",pass);
clrscr();
for(i=0;i<strlen(ps);i++)
if(pass[i]!=*(ps+i)) return 0;
return 1;
}

int menu(int pass)/*menu函数,菜单*/
{int py=6;char key=0;
gotoxy(1,4);
printf("MENU\n\n");
printf("ADD +\n");
printf("SUB -\n");
printf("MUL *\n");
printf("DIV /");
gotoxy(10,6);printf("<-");
if(pass) {gotoxy(1,10);printf("INV ^\n");
printf("EVO ~\n");
printf("SIN COS\n");
printf("ASIN ACOS\n");
printf("modify\n");
printf("QUIT"); }
else {gotoxy(1,10);printf("QUIT");}
while(key!=13) /*以下都是控制选择箭头的位子的语句*/
{ key=getch();
switch(key)
{ case 80 : gotoxy(10,py);printf(" ");if(!pass) py=py%5+6; else {++py;if(py==16) py=6;}break;

case 72 : gotoxy(10,py);printf(" ");--py; if(py==5) py=10+5*pass; break;
}
gotoxy(10,py);printf("<-");
}
return py;
}

main() /*主函数,关于函数分配和开始动画的语句*/
{ int gd=DETECT,gm,x=320,y=10,i,l=10,h=10,t,co=3,v=0;
int pass;
FILE *fp;char ps[20];
initgraph(&gd,&gm,"");/*初始化图形模式*/
registerbgidriver(EGAVGA_driver);
for(i=1;i<31;co=(co++%10),v++,y+=2+v,i++)
{ delay(9e10);cleardevice();
setcolor(co);
setfillstyle(1,co);
sector(x,y,0,360,l,h);/*画圆*/
}
for(i=1;i<300;i++)
{ setcolor(i%10);
circle(x,y,i);
delay(1000);/*延时*/
}
cleardevice();/*清屏*/
closegraph();
fp=fopen("file1","r");
if(fp==NULL) pass=1;
else {fgets(ps,20,fp);
pass=join(ps);}
ld: py=menu(pass);
if(py==pass*5+10) {clrscr();printf("bye bye!");getch();return 0;}
else if(py!=14){clrscr();mode(py);} else { fp=fopen("file1","w");
printf("\npass word:");
scanf("%s",ps);
fputs(ps,fp);
fclose(fp); getch();clrscr();
}
goto ld; /*函数内跳转*/
}
#include <stdio.h> /*库函数*/
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;

link push(link stack,int value)
{
link newnode;

newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}

link pop(link stack,int *value)
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}

int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;

}

int is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}

int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}

int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}

void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;

printf("\nPlease input the inorder expression:");
gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}

④ C语言计算器的问题

在scanf那儿换成:while(scanf("%d%c%d",",&iFirNum,&ch,&iSecNum,&ch1)!=EOF)
{
switch(ch)
{
case '+':
iResult=iFirNum+iSecNum;
printf("%d+%d=%d\n",iFirNum,iSecNum,iResult);
break;
case '-':
iResult=iFirNum-iSecNum;
printf("%d-%d=%d\n",iFirNum,iSecNum,iResult);
break;
case '*':
iResult=iFirNum*iSecNum;
printf("%d*%d=%d\n",iFirNum,iSecNum,iResult);
break;
case '/':
iResult=iFirNum/iSecNum;
printf("%d/%d=%d\n",iFirNum,iSecNum,iResult);
break;
default:
printf("输入表达式错误或该计算器不具备 %ch 功能\n",ch);
}
想退出的话按ctrl+c

⑤ C语言简易科学计算器加减乘除括号

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
inti,j,h,g,num[20]={0},z,m,n,s;//num[20]可以这样初始化
charch[80],r,sign[20];

gets(ch);//字符串输入

for(i=0;i<strlen(ch);i++)
{
s=0;
switch(ch[i])
{
case'+':sign[s]='+';s++;break;//缺少break;
case'-':sign[s]='-';s++;break;
case'*':sign[s]='*';s++;break;
case'/':sign[s]='/';s++;break;
case'(':sign[s]='(';s++;break;
case')':sign[s]=')';s++;break;

}
}/*读入符号*/

do{
m=0;
num[m]=atoi(ch);
n=sizeof(num[0]);
for(i=n+2;i<=(z-n-2);i++)
{
ch[i-n-2]=ch[i];
}
m++;
}while(ch[0]==' ');/*进行数字的读入转换*/

/////发现下面好多死循环,怎么可能会有结果呢////////////////
for(i=0;;i++)/*开始进行计算*/
{
if(sign[i]=='(')
{
for(j=i+1;j<=z;j++)
{
if(sign[j]==')')
{
for(h=i+1;h<=j;h++)
{
if(sign[h]=='*')
{
num[h-1]=num[h-1]*num[h];
for(g=h;;g++)
{
num[g]=num[g+1];
}
}
if(sign[h]=='/')
{
num[h-1]=num[h-1]/num[h];
for(g=h;;g++)
{
num[g]=num[g+1];
}
}
}
for(h=i+1;;h++)
{
if(sign[h]=='+')
{
num[h-1]=num[h-1]+num[h];
for(g=h;;g++)
{
num[g]=num[g+1];
}
}
if(sign[h]=='-')
{
num[h-1]=num[h-1]-num[h];
for(g=h;;g++)//死循环
{
num[g]=num[g+1];
}
}
}
}
}
}
for(h=i+1;h<=j;h++)
{
if(sign[h]=='*')
{
num[h-1]=num[h-1]*num[h];
for(g=h;;g++)//死循环
{
num[g]=num[g+1];
}
}
if(sign[h]=='/')
{
num[h-1]=num[h-1]/num[h];
for(g=h;;g++)//死循环
{
num[g]=num[g+1];
}
}
}
for(h=i+1;;h++)//死循环
{
if(sign[h]=='+')
{
num[h-1]=num[h-1]+num[h];
for(g=h;;g++)
{
num[g]=num[g+1];
}
}
if(sign[h]=='-')
{
num[h-1]=num[h-1]-num[h];
for(g=h;;g++)
{
num[g]=num[g+1];
}
}
}
}
printf("%d ",num[0]);
r=getchar();
return0;
}

⑥ C语言,计算器中的括号怎么做

用堆栈实现。将含有多重括号的中缀表达式翻译成后缀表达式,然后进行计算。步骤是:
从左到右依次检查每个字符
1.如果该字符是数字,直接加入到输出队列中
2.如果该字符是左括号(,将其加入堆栈
3.如果该字符是右括号),开始出栈,将栈中元素依次加入到输出队列中,直到碰到左括号'('。左括号本身不加入队列。如果栈中没有左括号,返回匹配错误。
4.如果该字符是非括号运算符,将该字符的优先级和栈顶元素作比较。如果优先级高于栈顶元素则入栈,否则添加到输出队列中。
表达式全部检查完毕后,将栈中所有剩余元素加入到输出队列中。如果栈中含有括号,返回匹配错误。
最后得到的输出队列就是后缀表达式。

⑦ 关于c语言计算器的问题

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

char token[61]; /*存放表达式字符串的数组*/
int n=0;
void error(void) /*报告错误函数*/
{
printf("ERROR!\n");
exit(1);
}

void match(char expected) /*检查字符匹配的函数*/
{
if(token[n]==expected)
token[++n]=getchar();
else error();
}

double term(void); /*计算乘除的函数*/

double factor(void); /*处理括号和数字的函数*/

double power(void); //添加乘方功能

double exp(void) /*计算加减的函数*/
{
double temp=term();
while((token[n]=='+')||(token[n]=='-'))
switch(token[n])
{
case'+':
match('+');
temp+=term();
break;
case'-':
match('-');
temp-=term();
break;
}
return temp;
}
double term(void)
{
double div;
double temp=power();
while((token[n]=='*')||(token[n]=='/'))
switch(token[n])
{
case'*':
match('*');
temp*=power();
break;
case'/':
match('/');
div=power();
if(div==0) /*处理除数为零的情况*/
{
printf("The divisor is zero!\n");
exit(1);
}
temp/=div;
break;
}
return temp;
}
double factor(void)
{
double temp;
char number[61];
int i=0;
if(token[n]=='(')
{
match('(');
temp=exp();
match(')');
}
else if(isdigit(token[n])||token[n]=='.')
{
while(isdigit(token[n])||token[n]=='.') /*将字符串转换为浮点数*/
{
number[i++]=token[n++];
token[n]=getchar();
}
number[i]='\0';
temp=atof(number);
}
return temp;
}

double power(void) //添加乘方功能
{
double temp = factor();
if(token[n] == '^')
{
match('^');
temp = pow(temp, power());
}
return temp;
}

int main()
{
double result;

while(true)
{
FILE *data=fopen("61590_4.dat","at");
if(data==NULL)
data=fopen("61590_4.dat","wt");
if(data==NULL)
return 0;
printf("请输入四则混合运算\n");
token[n]=getchar();
result=exp();
if(token[n]=='\n')
{
token[n]='\0';
printf("%s=%g\n",token,result);
fprintf(data,"%s=%g\n",token,result);
}
else error();
fclose(data);
n=0;
}

return 0;
getch();
}

⑧ 用C语言编译一个简单计算器的程序,要求其中有括号和幂函数

既然大家都没发程序,I am coming.
说明:由于幂的运算应该比括号低比乘除要高,所以为避免混淆用幂运算时用括号括起来,比如2+9/3^2-5表示2+(9/3)^2-5,要是想要9除以3^2,这样输入:2+9/(3^2)-5,以免引起歧义。
程序功能强大代码简练。可以对任意数值类型计算,这里全部按double型处理,程序还可以使用大括号{}中括号[]小括号()三种括号,当然优先级是小括号>中括号>大括号>运算符。
要是只想结果取整,将所有double类型改为int即可,不过不主张这样做,既然是计算器,当然要精确计算。
一个完整的c程序如下,程序在win-tc和Dev-c++下都调试通过。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

char s[201];/*全局变量s[]与i分别表示算术表达式字符串以及其当前位置指针变量*/
int i=0;
double proc1(char ch);
void proc2(double *a,char *c);

void str_read(double *a,char *c) /*读取下一个操作数*/
{ int j=0;
char s1[30];
while ((s[i]!='\0')&&(s[i]!='+')&&(s[i]!='-')&&(s[i]!='(')&&(s[i]!=')')
&&(s[i]!='=')&&(s[i]!='^')&&(s[i]!='*')&&(s[i]!='/')&&(s[i]!='[')&&(s[i]!=']')
&&(s[i]!='{')&&(s[i]!='}')) /*操作数结束标志*/
s1[j++]=s[i++];
s1[j]='\0';
*a=atof(s1);
*c=s[i++];
}

void input_str() /*表达式接收模块*/
{ int j,k,t=0;
char c,s1[80];
printf("\n请输入任一算术表达式:\n");
while (!t)
{ gets(s);
j=k=0; t=1;
while ((s[j]!='=')&&(s[j]!='\0')&&(t)) /*判断括号配对*/
{if ((s[j]=='(')||(s[j]=='[')||(s[j]=='{'))
switch(s[j])
{ case '(':s1[k++]=')';break;
case '[':s1[k++]=']';break;
case '{':s1[k++]='}';
}
else if ((s[j]==')')||(s[j]==']')||(s[j]=='}'))
if (s1[--k]!=s[j])
{ printf("输入出错!请重新输入(直接回车退出):\n");
t=0;
}
j++;
}
if ((k!=0)&&(t))
{printf("输入出错!请重新输入(直接回车退出):\n");
t=0;
}
}
}

void proc2(double *a,char *c)
/*算术运算符之后只有两种情况:要么是各种左括号,要么是操作数*/
{ char ch;
if ((s[i]=='(')||(s[i]=='[')||(s[i]=='{'))
{ switch(s[i])
{ case '(':ch=')';break;
case '[':ch=']';break;
case '{':ch='}';
}
++i;
*a=proc1(ch);
/*计算从当前左括号开始到与其相对应的右括号结束这段表达式的值*/
*c=s[i++]; /*跳过右括号*/
}
else str_read(a,c); /*读取一个操作数*/
}

double proc1(char ch)
/*该过程对算术表达式s从当前位置s[i]开始到表达式结束或s[i]==ch这部分进行求值*/
{ double a1=0,a2,a=0;
char c1='+',c='+',c2='+';
/*给表达式前面加上"0+"(第一操作数与第一操作符)*/
while((c!='=')&&(c!='\0')&&(c!=ch))
{ proc2(&a,&c); /*读取第二操作数与第二操作符.*/
while (c=='^' || c=='*' || c=='/')
{ proc2(&a2,&c2); /*读取第三操作数与第三操作符.*/
switch (c) /*对第二操作符进行运算*/
{ case '*':a=a*a2; break;
case '/':a=a/a2; break;
case '^':a=pow(a,a2);
}
c=c2; /*把第三操作符赋给第二操作符变量*/
}
switch (c1) /*对第一操作符进行运算*/
{ case '+':a1=a1+a;break;
case '-':a1=a1-a;
}
c1=c; /*把第二操作符赋给第一操作符变量*/
}
return(a1); /*返回这部分表达式的值*/
}

int main(void)
{ system("cls");
input_str();
if (s[0]=='\0') return 0;
printf("该表达式的值是: ");
printf("%-8f\n",proc1('\0'));
system("pause");
return 0;
}

⑨ C语言中的括号运算

理论上来说

增加括号只会增加编译的时间 并且对运行的时间是没有差别的

也就是增加了把你从c/c++代码变成exe的时间,但是不会影响exe运行的时间

而且 增加一两个括号 对于计算机编译时间的影响是微乎其微的

计算机一秒计算数量以亿算 怎么会差这一个括号呢

关于代码运行效率 更应该注意的是算法,而不是这些语句 那个才能从本质上提高程序运行效率

多行的代码 运行效率就不一定比单行的要低

作为源代码重要的一是算法 二是可读性 增加括号和增加有必要的换行不但不会降低运行效率,反而会使代码更加容易理解 减少歧义的产生

⑩ C语言编写计算器要求括号也要求在内,不要指针只求数组和循环,给个思路,关键是括号怎么加上

括号配对呗,一个简单的算法。
如果式子不太复杂就递归比如
碰到运算符return 上一步与下一步值计算
多少个优先算的比如括号,比如乘积存起来
碰到奇数括号,开始新的计算
碰到偶数括号,括号内计算结果返回上一级计算