『壹』 用c語言編寫一個簡單的計算器1
#include<stdio.h>
int main() {
double num1 = 0; //輸入1
double num2 = 0; //輸入2
char ch; //操作
double ret = 0; //結果 printf( "輸入第一個數:" );
scanf( "%lf", &num1 );
printf( "輸入第二個數:" );
scanf( "%lf", &num2 );
printf( "操作[+ - * /]:" );
getchar();
scanf( "%c", &ch ); switch( ch ) {
case '+':
ret = num1 + num2;
break;
case '-':
ret = num1 - num2;
break;
case '*':
ret = num1 * num2;
break;
case '/':
ret = num1 / num2;
break;
default:
break;
}
printf( "結果:%.2lf\n", ret ); return 0;
} 寫個簡單易懂的,你操作計算器的步驟就是編寫程序的思路呀
『貳』 c語言編寫一計算器
我有 發給你了
『叄』 c語言編寫計算器
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
//expression evaluate
#define iMUL 0
#define iDIV 1
#define iADD 2
#define iSUB 3
#define iCap 4
//#define LtKH 5
//#define RtKH 6
#define MaxSize 100
void iPush(float);
float iPop();
float StaOperand[MaxSize];
int iTop=-1;
//
char Srcexp[MaxSize];
char Capaexp[MaxSize];
char RevPolishexp[MaxSize];
float NumCapaTab[26];
char validexp[]="*/+-()";
char NumSets[]="0123456789";
char StackSymb[MaxSize];
int operands;
//
void NumsToCapas(char [], int , char [], float []);
int CheckExpress(char);
int PriorChar(char,char);
int GetOperator(char [], char);
void counterPolishexp(char INexp[], int slen, char Outexp[]);
float CalcRevPolishexp(char [], float [], char [], int);
void main()
{
int ilen;
float iResult=0.0;
printf("enter a valid number string:\n");
memset(StackSymb,0,MaxSize);
memset(NumCapaTab,0,26); //A--NO.1, B--NO.2, etc.
gets(Srcexp);
ilen=strlen(Srcexp);
//printf("source expression:%s\n",Srcexp);
NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);
printf("Numbers listed as follows:\n");
int i;
for (i=0; i<operands; ++i)
printf("%.2f ",NumCapaTab[i]);
printf("\nCapaexp listed in the following:\n");
printf("%s\n",Capaexp);
ilen=strlen(Capaexp);
counterPolishexp(Capaexp,ilen,RevPolishexp);
printf("RevPolishexp:\n%s\n",RevPolishexp);
ilen=strlen(RevPolishexp);
iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);
printf("\ncounterPolish expression:\n%s%.6f\n",Srcexp,iResult);
}
void iPush(float value)
{
if(iTop<MaxSize) StaOperand[++iTop]=value;
}
float iPop()
{
if(iTop>-1)
return StaOperand[iTop--];
return -1.0;
}
void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])
{
char ch;
int i, j, k, flg=0;
int sign;
float val=0.0,power=10.0;
i=0; j=0; k=0;
while (i<slen)
{
ch=Srcexp[i];
if (i==0)
{
sign=(ch=='-')?-1:1;
if(ch=='+'||ch=='-')
{
ch=Srcexp[++i];
flg=1;
}
}
if (isdigit(ch))
{
val=ch-'0';
while (isdigit(ch=Srcexp[++i]))
{
val=val*10.0+ch-'0';
}
if (ch=='.')
{
while(isdigit(ch=Srcexp[++i]))
{
val=val+(ch-'0')/power;
power*=10;
}
} //end if
if(flg)
{
val*=sign;
flg=0;
}
} //end if
//write Capaexp array
// write NO.j to array
if(val)
{
Capaexp[k++]='A'+j;
Capaexp[k++]=ch;
NumCapaTab[j++]=val; //A--0, B--1,and C, etc.
}
else
{
Capaexp[k++]=ch;
}
val=0.0;
power=10.0;
//
i++;
}
Capaexp[k]='\0';
operands=j;
}
float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)
{
float sval=0.0, op1,op2;
int i, rt;
char ch;
//recursive stack
i=0;
while((ch=RevPolishexp[i]) && i<slen)
{
switch(rt=GetOperator(validexp, ch))
{
case iMUL: op2=iPop(); op1=iPop();
sval=op1*op2;
iPush(sval);
break;
case iDIV: op2=iPop(); op1=iPop();
if(!fabs(op2))
{
printf("overflow\n");
iPush(0);
break;
}
sval=op1/op2;
iPush(sval);
break;
case iADD: op2=iPop(); op1=iPop();
sval=op1+op2;
iPush(sval);
break;
case iSUB: op2=iPop(); op1=iPop();
sval=op1-op2;
iPush(sval);
break;
case iCap: iPush(NumCapaTab[ch-'A']);
break;
default: ;
}
++i;
}
while(iTop>-1)
{
sval=iPop();
}
return sval;
}
int GetOperator(char validexp[],char oper)
{
int oplen,i=0;
oplen=strlen(validexp);
if (!oplen) return -1;
if(isalpha(oper)) return 4;
while(i<oplen && validexp[i]!=oper) ++i;
if(i==oplen || i>=4) return -1;
return i;
}
int CheckExpress(char ch)
{
int i=0;
char cc;
while((cc=validexp[i]) && ch!=cc) ++i;
if (!cc)
return 0;
return 1;
}
int PriorChar(char curch, char stach)
{
//棧外優先順序高於(>)棧頂優先順序時,才入棧
//否則(<=),一律出棧
if (curch==stach) return 0; //等於時應該出棧
else if (curch=='*' || curch=='/')
{
if(stach!='*' && stach!='/')
return 1;
}
else if (curch=='+' || curch=='-')
{
if (stach=='(' || stach==')')
return 1;
}
else if (curch=='(')
{
if (stach==')')
return 1;
}
return 0;
}
void counterPolishexp(char INexp[], int slen, char Outexp[])
{
int i, j, k,pr;
char t;
i=0;
j=k=0;
while (INexp[i]!='=' && i<slen)
{
if (INexp[i]=='(')
StackSymb[k++]=INexp[i];
//iPush(*(INexp+i));
else if(INexp[i]==')')
{
//if((t=iPop())!=-1)
while((t=StackSymb[k-1])!='(')
{
Outexp[j++]=t;
k--;
}
k--;
}
else if (CheckExpress(INexp[i])) // is oparator
{
// printf("operator %c k=%d\n",INexp[i],k);
while (k)
{
// iPush(*(INexp+i));
if(pr=PriorChar(INexp[i],StackSymb[k-1]))
break;
else
{
//if ((t=iPop())!=-1)
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
} //end while
StackSymb[k++]=INexp[i]; //common process
}
else //if() 變數名
{
// printf("operand %c k=%d\n",INexp[i],k);
Outexp[j++]=INexp[i];
}
i++; //
}
while (k)
{
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
Outexp[j]='\0';
}
『肆』 關於用C語言編的計算器
如果你計算的數很大的話,可以考慮用大數處理,藉助數組的力量模擬計算過程。
『伍』 要用C語言編輯一個計算器,怎麼編
實現四則運算就可以了,
用命令行輸入算式,需要判斷算式是否正確,括弧是否匹配,
然後根據四則運算優先順序計算,
學習數據結構 棧 結構的時候,就有這個練習
『陸』 C語言編寫計算器
#include<stdio.h>
#define NUM 1000;
main(){
int ys,i;
int a,b,c,d;
printf("welcome...\n");
printf("請選擇:\n");
printf("1.加法\n");
printf("2.減法\n");
printf("3.乘法\n");
printf("4.除法\n");
scanf("%d",&ys);
srand(time(NULL));
a=rand()%NUM;
b=rand()%NUM;
if(ys==1){
c=a+b;
printf("%d+%d=",a,b);
}
else if(ys==2){
c=a-b;
printf("%d-%d=",a,b);
}
else if(ys==3){
c=a*b;
printf("%d*%d=",a,b);
}
else{
c=a/b;
printf("%d/%d=",a,b);
}
for(i=0;i<2;i++)
{
scanf("%d",&d);
if(d==c){
printf("回答正確\n");
break;
}
if(i==0){
printf("錯誤\n請重新輸入:");
}
}
}
我做的第一版,初步測試運行正常,如果有bug,或者不符合要求可以提出.我再修改
『柒』 用C語言編計算器程序
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
//expression evaluate
#define iMUL 0
#define iDIV 1
#define iADD 2
#define iSUB 3
#define iCap 4
//#define LtKH 5
//#define RtKH 6
#define MaxSize 100
void iPush(float);
float iPop();
float StaOperand[MaxSize];
int iTop=-1;
//
char Srcexp[MaxSize];
char Capaexp[MaxSize];
char RevPolishexp[MaxSize];
float NumCapaTab[26];
char validexp[]="*/+-()";
char NumSets[]="0123456789";
char StackSymb[MaxSize];
int operands;
//
void NumsToCapas(char [], int , char [], float []);
int CheckExpress(char);
int PriorChar(char,char);
int GetOperator(char [], char);
void counterPolishexp(char INexp[], int slen, char Outexp[]);
float CalcRevPolishexp(char [], float [], char [], int);
void main()
{
int ilen;
float iResult=0.0;
printf("enter a valid number string:\n");
memset(StackSymb,0,MaxSize);
memset(NumCapaTab,0,26); //A--NO.1, B--NO.2, etc.
gets(Srcexp);
ilen=strlen(Srcexp);
//printf("source expression:%s\n",Srcexp);
NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);
printf("Numbers listed as follows:\n");
int i;
for (i=0; i<operands; ++i)
printf("%.2f ",NumCapaTab[i]);
printf("\nCapaexp listed in the following:\n");
printf("%s\n",Capaexp);
ilen=strlen(Capaexp);
counterPolishexp(Capaexp,ilen,RevPolishexp);
printf("RevPolishexp:\n%s\n",RevPolishexp);
ilen=strlen(RevPolishexp);
iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);
printf("\ncounterPolish expression:\n%s%.6f\n",Srcexp,iResult);
}
void iPush(float value)
{
if(iTop<MaxSize) StaOperand[++iTop]=value;
}
float iPop()
{
if(iTop>-1)
return StaOperand[iTop--];
return -1.0;
}
void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])
{
char ch;
int i, j, k, flg=0;
int sign;
float val=0.0,power=10.0;
i=0; j=0; k=0;
while (i<slen)
{
ch=Srcexp[i];
if (i==0)
{
sign=(ch=='-')?-1:1;
if(ch=='+'||ch=='-')
{
ch=Srcexp[++i];
flg=1;
}
}
if (isdigit(ch))
{
val=ch-'0';
while (isdigit(ch=Srcexp[++i]))
{
val=val*10.0+ch-'0';
}
if (ch=='.')
{
while(isdigit(ch=Srcexp[++i]))
{
val=val+(ch-'0')/power;
power*=10;
}
} //end if
if(flg)
{
val*=sign;
flg=0;
}
} //end if
//write Capaexp array
// write NO.j to array
if(val)
{
Capaexp[k++]='A'+j;
Capaexp[k++]=ch;
NumCapaTab[j++]=val; //A--0, B--1,and C, etc.
}
else
{
Capaexp[k++]=ch;
}
val=0.0;
power=10.0;
//
i++;
}
Capaexp[k]='\0';
operands=j;
}
float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)
{
float sval=0.0, op1,op2;
int i, rt;
char ch;
//recursive stack
i=0;
while((ch=RevPolishexp[i]) && i<slen)
{
switch(rt=GetOperator(validexp, ch))
{
case iMUL: op2=iPop(); op1=iPop();
sval=op1*op2;
iPush(sval);
break;
case iDIV: op2=iPop(); op1=iPop();
if(!fabs(op2))
{
printf("overflow\n");
iPush(0);
break;
}
sval=op1/op2;
iPush(sval);
break;
case iADD: op2=iPop(); op1=iPop();
sval=op1+op2;
iPush(sval);
break;
case iSUB: op2=iPop(); op1=iPop();
sval=op1-op2;
iPush(sval);
break;
case iCap: iPush(NumCapaTab[ch-'A']);
break;
default: ;
}
++i;
}
while(iTop>-1)
{
sval=iPop();
}
return sval;
}
int GetOperator(char validexp[],char oper)
{
int oplen,i=0;
oplen=strlen(validexp);
if (!oplen) return -1;
if(isalpha(oper)) return 4;
while(i<oplen && validexp[i]!=oper) ++i;
if(i==oplen || i>=4) return -1;
return i;
}
int CheckExpress(char ch)
{
int i=0;
char cc;
while((cc=validexp[i]) && ch!=cc) ++i;
if (!cc)
return 0;
return 1;
}
int PriorChar(char curch, char stach)
{
//棧外優先順序高於(>)棧頂優先順序時,才入棧
//否則(<=),一律出棧
if (curch==stach) return 0; //等於時應該出棧
else if (curch=='*' || curch=='/')
{
if(stach!='*' && stach!='/')
return 1;
}
else if (curch=='+' || curch=='-')
{
if (stach=='(' || stach==')')
return 1;
}
else if (curch=='(')
{
if (stach==')')
return 1;
}
return 0;
}
void counterPolishexp(char INexp[], int slen, char Outexp[])
{
int i, j, k,pr;
char t;
i=0;
j=k=0;
while (INexp[i]!='=' && i<slen)
{
if (INexp[i]=='(')
StackSymb[k++]=INexp[i];
//iPush(*(INexp+i));
else if(INexp[i]==')')
{
//if((t=iPop())!=-1)
while((t=StackSymb[k-1])!='(')
{
Outexp[j++]=t;
k--;
}
k--;
}
else if (CheckExpress(INexp[i])) // is oparator
{
// printf("operator %c k=%d\n",INexp[i],k);
while (k)
{
// iPush(*(INexp+i));
if(pr=PriorChar(INexp[i],StackSymb[k-1]))
break;
else
{
//if ((t=iPop())!=-1)
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
} //end while
StackSymb[k++]=INexp[i]; //common process
}
else //if() 變數名
{
// printf("operand %c k=%d\n",INexp[i],k);
Outexp[j++]=INexp[i];
}
i++; //
}
while (k)
{
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
Outexp[j]='\0';
}
『捌』 怎麼用c語言編寫一個計算器
C語言因為缺乏圖形界面,是不太適合編寫計算器的。可使用圖形界面較豐富的語言編寫,如VB。
『玖』 C語言編計算器
C語言就足夠了