① 用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 上一步與下一步值計算
多少個優先算的比如括弧,比如乘積存起來
碰到奇數括弧,開始新的計算
碰到偶數括弧,括弧內計算結果返回上一級計算