计算器程序代码(C语言)
2011-08-09 20:44
211 查看
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
double numStack
={0};//操作数栈
int numTop;
char opStack
;//运算符栈
int opTop;
void print_num(double str1[],int n)
{
int i;
printf("\n操作数栈:\n");
for(i=0;i<n;i++)
printf("%g ",str1[i]);
}
void print_op(char str2[],int m)
{
int j;
printf("\n运算符栈:\n");
for(j=0;j<m;j++)
printf("%c ",str2[j]);
}
int op(char ch)//判断运算符优先级
{
if(ch=='+'||ch=='-') return 2;
if(ch=='*'||ch=='/') return 3;
if(ch=='(') return -1;
return 0;
}
double result(double num1,char op,double num2)//计算
{
if(op=='+') return num1+num2;
if(op=='-') return num1-num2;
if(op=='*') return num1*num2;
if(op=='/') return num1/num2;
return 0;
}
int compute(char str[])
{
double num=0;
int i=0,j=1,k=1;
numTop=opTop=0;
while(str[i]!='\0'||opTop>0)
{
if(str[i]>='0'&&str[i]<='9')
num=num*10+str[i]-'0';
else if( k==1&&str[i]=='-'&&(i==0||op(str[i-1])) )
k=-1;
else
{
if(i>0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')')
{
numStack[numTop++]=num*k;
if(opTop!=0&&numTop!=0)
print_num(numStack,numTop);
num=0; j=1; k=1;
}
if(opTop==0||str[i]=='(')
{opStack[opTop++]=str[i];print_op(opStack,opTop);}
else if(str[i]==')')
{
while(opTop>0&&opStack[--opTop]!='(')
{
numStack[numTop-2]=result(numStack[numTop-2],opStack[opTop],numStack[numTop-1]);
if(opTop!=0&&numTop!=0)
{
print_num(numStack,numTop);
print_op(opStack,opTop);
}
numTop--;
}
if(opStack[opTop]!='(') return 0;
}
else
{
if(str[i]=='\0'&&numTop==0) return 0;
while(opTop>0&&op(str[i])<=op(opStack[opTop-1]))
{
numStack[numTop-2]=result(numStack[numTop-2],opStack[--opTop],numStack[numTop-1]);
if(opTop!=0&&numTop!=0)
{
print_num(numStack,numTop-1);
print_op(opStack,opTop);
}
numTop--;
}
if(str[i]!='\0')
opStack[opTop++]=str[i];
if(opTop!=0&&numTop!=0)
print_op(opStack,opTop);
}
}
if(str[i]!='\0')
i++;
}
if(numTop!=1||opTop!=0)
return 0;
return 1;
}
void menu()
{
system("cls");
printf("----------------------------------------------\n");
printf(" Again(A) | Equal(=) | Exit(E)|Introduction(I)\n");
printf("----------------------------------------------\n");
}
int main(void)
{
int i=0,j=0,k;
char str
="\0";
char num
="\0";
char save
="\0";
char ch;
double temp;
unsigned long temp2;
menu();
printf("input an expression,press key '=' to compute\n");
ch=getch();
while( 1 )
{
if(ch==')'||op(ch)||ch>='0'&&ch<='9')
{
str[i++]=ch;
str[i]='\0';
menu();
printf("input an expression,press key '=' to compute\n");
printf("%s",str);
if( ch=='-'&&(i==1||op(str[i-2]))||ch>='0'&&ch<='9' )
{
num[j++]=ch;
num[j]='\0';
}
else
j=0;
}
if(ch=='A'||ch=='a')
{
if(strlen(str))
str[--i]='\0';
menu();
printf("input an expression,press key '=' to compute\n");
printf("%s",str);
}
if(ch=='=')
{
if(compute(str))
{
printf("\n=%g\n",numStack[0]);
j=0; temp=numStack[0];
if(temp<0)
{
temp=-temp;
num[j++]='-';
num[j]='\0';
}
temp2=(unsigned long)temp;
k=1;
while(temp2/k>=10) k*=10;
while(k)
{
num[j++]=temp2/k+'0';
num[j]='\0';
temp2=temp2%k;
k/=10;
}
temp=temp-(int)temp;
if(temp!=0)
{
num[j++]='.';
num[j]='\0';
temp+=0.0000005;
}
for(k=6;k>0;k--)
{
if(temp==0) break;
temp*=10;
num[j++]=(int)temp+'0';
num[j]='\0';
temp=temp-(int)temp;
}
}
i=0; j=0; str[0]='\0';
}
if(ch=='A'||ch=='e')
{
printf("\nare you sure to quit?(Y/N)\n");
ch=getch();
if(ch=='Y'||ch=='y') break;
else
{
menu();
printf("input an expression,press key '=' to compute\n");
printf("%s",str);
}
}
ch=getch();
}
return 0;
}
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
double numStack
={0};//操作数栈
int numTop;
char opStack
;//运算符栈
int opTop;
void print_num(double str1[],int n)
{
int i;
printf("\n操作数栈:\n");
for(i=0;i<n;i++)
printf("%g ",str1[i]);
}
void print_op(char str2[],int m)
{
int j;
printf("\n运算符栈:\n");
for(j=0;j<m;j++)
printf("%c ",str2[j]);
}
int op(char ch)//判断运算符优先级
{
if(ch=='+'||ch=='-') return 2;
if(ch=='*'||ch=='/') return 3;
if(ch=='(') return -1;
return 0;
}
double result(double num1,char op,double num2)//计算
{
if(op=='+') return num1+num2;
if(op=='-') return num1-num2;
if(op=='*') return num1*num2;
if(op=='/') return num1/num2;
return 0;
}
int compute(char str[])
{
double num=0;
int i=0,j=1,k=1;
numTop=opTop=0;
while(str[i]!='\0'||opTop>0)
{
if(str[i]>='0'&&str[i]<='9')
num=num*10+str[i]-'0';
else if( k==1&&str[i]=='-'&&(i==0||op(str[i-1])) )
k=-1;
else
{
if(i>0&&!op(str[i-1])&&str[i]!='('&&str[i-1]!=')')
{
numStack[numTop++]=num*k;
if(opTop!=0&&numTop!=0)
print_num(numStack,numTop);
num=0; j=1; k=1;
}
if(opTop==0||str[i]=='(')
{opStack[opTop++]=str[i];print_op(opStack,opTop);}
else if(str[i]==')')
{
while(opTop>0&&opStack[--opTop]!='(')
{
numStack[numTop-2]=result(numStack[numTop-2],opStack[opTop],numStack[numTop-1]);
if(opTop!=0&&numTop!=0)
{
print_num(numStack,numTop);
print_op(opStack,opTop);
}
numTop--;
}
if(opStack[opTop]!='(') return 0;
}
else
{
if(str[i]=='\0'&&numTop==0) return 0;
while(opTop>0&&op(str[i])<=op(opStack[opTop-1]))
{
numStack[numTop-2]=result(numStack[numTop-2],opStack[--opTop],numStack[numTop-1]);
if(opTop!=0&&numTop!=0)
{
print_num(numStack,numTop-1);
print_op(opStack,opTop);
}
numTop--;
}
if(str[i]!='\0')
opStack[opTop++]=str[i];
if(opTop!=0&&numTop!=0)
print_op(opStack,opTop);
}
}
if(str[i]!='\0')
i++;
}
if(numTop!=1||opTop!=0)
return 0;
return 1;
}
void menu()
{
system("cls");
printf("----------------------------------------------\n");
printf(" Again(A) | Equal(=) | Exit(E)|Introduction(I)\n");
printf("----------------------------------------------\n");
}
int main(void)
{
int i=0,j=0,k;
char str
="\0";
char num
="\0";
char save
="\0";
char ch;
double temp;
unsigned long temp2;
menu();
printf("input an expression,press key '=' to compute\n");
ch=getch();
while( 1 )
{
if(ch==')'||op(ch)||ch>='0'&&ch<='9')
{
str[i++]=ch;
str[i]='\0';
menu();
printf("input an expression,press key '=' to compute\n");
printf("%s",str);
if( ch=='-'&&(i==1||op(str[i-2]))||ch>='0'&&ch<='9' )
{
num[j++]=ch;
num[j]='\0';
}
else
j=0;
}
if(ch=='A'||ch=='a')
{
if(strlen(str))
str[--i]='\0';
menu();
printf("input an expression,press key '=' to compute\n");
printf("%s",str);
}
if(ch=='=')
{
if(compute(str))
{
printf("\n=%g\n",numStack[0]);
j=0; temp=numStack[0];
if(temp<0)
{
temp=-temp;
num[j++]='-';
num[j]='\0';
}
temp2=(unsigned long)temp;
k=1;
while(temp2/k>=10) k*=10;
while(k)
{
num[j++]=temp2/k+'0';
num[j]='\0';
temp2=temp2%k;
k/=10;
}
temp=temp-(int)temp;
if(temp!=0)
{
num[j++]='.';
num[j]='\0';
temp+=0.0000005;
}
for(k=6;k>0;k--)
{
if(temp==0) break;
temp*=10;
num[j++]=(int)temp+'0';
num[j]='\0';
temp=temp-(int)temp;
}
}
i=0; j=0; str[0]='\0';
}
if(ch=='A'||ch=='e')
{
printf("\nare you sure to quit?(Y/N)\n");
ch=getch();
if(ch=='Y'||ch=='y') break;
else
{
menu();
printf("input an expression,press key '=' to compute\n");
printf("%s",str);
}
}
ch=getch();
}
return 0;
}
相关文章推荐
- 200行Java代码搞定计算器程序
- 如何让程序运行更快时间更短C语言代码(程序员必看)
- 打字测试程序代码(C语言)
- 程序设计基石与实践之内存里C语言代码布局
- 一个类似ifconfig功能的程序代码--C语言
- 学习C语言是的第一个程序(万年历C代码)
- 200行Java代码编写一个计算器程序
- 用Javasrcipt写一个计算器的小程序[代码]
- 逆波兰 计算器 C语言实现代码
- 【C语言】03-第一个C程序代码分析
- C语言处理图片二值化程序代码[亲测能用]
- 用js编写的简单的计算器代码程序
- C语言系列(一):最近重拾C语言的想法,谈到C中易错点,难点;以及开源代码中C语言的一些常用技巧,以及如何利用define、typedef、const等写健壮的C程序
- C语言系列(三):最近重拾C语言的想法,谈到C中易错点,难点;以及开源代码中C语言的一些常用技巧,以及如何利用define、typedef、const等写健壮的C程序
- 第四章计算器程序部分代码,结合练习
- Java计算器程序代码
- c语言判断是否素数程序代码
- 【C语言】03-第一个C程序代码分析
- 第一个swift程序:计算器,不到100行代码呀,简单吧!