⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 四则运算.cpp

📁 四则运算简易计算器(用纯C语言实现)并具有纠错功能
💻 CPP
字号:
#include<malloc.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10
#define OK 1
#define Error 0
typedef char SElemType;
typedef double TElemType;
typedef struct {
	int  stacksize;
	SElemType *base;
	SElemType *top;
}SqStack;
typedef struct {
	int  stacksize;
	TElemType *base;
	TElemType *top;
}Stack;
SqStack OPTR,KUO;
Stack OPND,SHARE,TWO;
int choose;
InitStack_1(SqStack &S)
{ S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
  S.top=S.base;
  S.stacksize=STACK_INIT_SIZE;
  return OK;
}
InitStack_2(Stack &T)
{ T.base=(TElemType *)malloc(STACK_INIT_SIZE * sizeof(TElemType));
  T.top=T.base;
  T.stacksize=STACK_INIT_SIZE;
  return OK;
}
StackEmpty(SqStack &S)
{ if(S.base==S.top) return(1);
  else return(0);
}
StackEmpty1(Stack &S)
{ if(S.base==S.top) return(1);
  else return(0);
}
Push_1(SqStack &S,SElemType a)
{ *S.top++=a;
  return OK;
}
Pop_1(SqStack &S,SElemType a)
{ 
  a=*--S.top;
  return(a);
}
GetTop_1(SqStack &S)
{
  SElemType a;
  a=*(S.top-1);
  return(a);
}
Push_2(Stack &T,TElemType a)
{ *T.top++=a;
  return OK;
}
double Pop_2(Stack &T,TElemType a)
{ 
  a=*--T.top;
  return(a);
}
double GetTop_2(Stack &T)
{
  TElemType a;
  a=*(T.top-1);
  return(a);
}
Check(char str[1000])
{
  int i,a,j,l;
  char c;
  i=0;
  j=0;
  c=str[i];
  a=1;
  l=strlen(str);
  for(j=0;j<l;++j)
  {
    if(c=='(')
	{ 
	Push_1(KUO,c);
	i=i+1;
	c=str[i];
	}
    else if(c==')')
	{
	  if(StackEmpty(KUO))
	  {
		a=0;
		break;
	  }
	  else
	  {
	    Pop_1(KUO,c);
	    i=i+1;
	    c=str[i];
	  }
	}
	else
	{
		i=i+1;
		c=str[i];
	}
  }
  if(StackEmpty(KUO)&&a)
  {
	a=1;
    return(a);
  }
  else
  {
	a=0;
	return(a);
  }
}
Sign(char str[1000])
{
  int i,j,a,l;
  int p;
  char c;
  i=0;
  j=0;
  c=str[0];
  if(c=='+'||c=='*'||c=='/')
  {
	a=0;
	return(a);
  }
  else
  {
	i=i+1;
	c=str[i];
	l=strlen(str);
	for(j=1;j<l;++j)
	{
	if(c=='\0')
	{ return(1);break;}
    else if(c=='+'||c=='-'||c=='*'||c=='/')
	{
	  a=0;
	  p=i;
	  if(str[p-1]!='+'&&str[p+1]!='+') a=0;
	  if(str[p-1]!='-'&&str[p+1]!='-') a=0;
      if(str[p-1]!='*'&&str[p+1]!='*') a=0;
	  if(str[p-1]!='/'&&str[p+1]!='/') a=0;
	  if(str[p-1]!='+'&&str[p+1]!='-') a=0;
	  if(str[p-1]!='+'&&str[p+1]!='*') a=0;
	  if(str[p-1]!='+'&&str[p+1]!='/') a=0;
	  if(str[p-1]!='-'&&str[p+1]!='+') a=0;
	  if(str[p-1]!='-'&&str[p+1]!='*') a=0;
	  if(str[p-1]!='-'&&str[p+1]!='/') a=0;
	  if(str[p-1]!='*'&&str[p+1]!='+') a=0;
	  if(str[p-1]!='*'&&str[p+1]!='-') a=0;
	  if(str[p-1]!='*'&&str[p+1]!='/') a=0;
	  if(str[p-1]!='/'&&str[p+1]!='+') a=0;
	  if(str[p-1]!='/'&&str[p+1]!='-') a=0;
	  if(str[p-1]!='/'&&str[p+1]!='*') a=0;
	  
	  if(str[p-1]!='('&&str[p+1]!=')') a=0;
	  if(str[p-1]!='('&&str[p+1]!='+') a=0;
	  if(str[p-1]!='('&&str[p+1]!='-') a=0;
	  if(str[p-1]!='('&&str[p+1]!='*') a=0;
	  if(str[p-1]!='('&&str[p+1]!='/') a=0;

      if(str[p-1]!='/'&&str[p+1]!=')') a=0;
      if(str[p-1]!='+'&&str[p+1]!=')') a=0;
	  if(str[p-1]!='-'&&str[p+1]!=')') a=0;
	  if(str[p-1]!='*'&&str[p+1]!=')') a=1;
	  if(a==0) { return(a);break;}
	  	i=i+1;
		c=str[i];
	}
	else if(c=='(')
	{
	  a=0;
	  p=i;
	  if((str[p-1]=='+'||str[p-1]=='-'||str[p-1]=='*'||str[p-1]=='/'||str[p-1]=='(')&&(str[p+1]!='+'&&str[p+1]!='*'&&str[p+1]!='/'&&str[p+1]!=')')) a=1;
	  if(a==0) { return(a);break;}
	  	i=i+1;
		c=str[i];
	}
	else if(c==')')
	{
	  a=0;
	  p=i;
      if((str[p-1]!='+'&&str[p-1]!='-'&&str[p-1]!='*'&&str[p-1]!='/'&&str[p-1]!='(')&&(str[p+1]=='+'||str[p+1]=='-'||str[p+1]=='*'||str[p+1]=='/'||str[p+1]==')'||str[p+1]==')'||str[p+1]=='\0')) a=1;
	  if(a==0) { return(a);break;}
	  	i=i+1;
		c=str[i];
	}
	else
	{
	
		i=i+1;
		c=str[i];
	}
	}
  }
  return(a);
}
Right(char str[1000])
{
  int i,k,l,a;
  char c;
  i=0;
  a=1;
  c=str[i];
  l=strlen(str);
  if(choose==1)
  { return(1);}
  else if(choose==2)
  { do{
	  if(c=='+'||c=='-')
	  { i=i+1;
	    c=str[i];
	  }
	  else
	  {
	        if(c!='0'&&c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7'&&c!='8'&&c!='9')
			{
	         c=c-87;
			}
	        else 
			{
	         c=c-48;
			}
	        k=(int)c;
	        if(k>16||c=='*'||c=='/') { a=0;break;}
	        else { i=i+1; c=str[i];}
	  }
  }while(i<l);
  return(a);
  }
  else if(choose==3)
  {
	do{
            if(c=='+'||c=='-')
			{ i=i+1;
	          c=str[i];
			}
	        else
			{
	          c=c-48;
	          k=(int)c;
	          if(k>2||c=='*'||c=='/') { a=0;break;}
	          else { i=i+1; c=str[i];}
			}
  }while(i<l);
	return(a);
  }
  else
  {
	 	do{
            if(c=='+'||c=='-')
			{ i=i+1;
	          c=str[i];
			}
	        else
			{
	          c=c-48;
	          k=(int)c;
	          if(k>8||c=='*'||c=='/') { a=0;break;}
	          else { i=i+1; c=str[i];}
			}
  }while(i<l);
		return(a);
  }
}
In(int i,char str[1000])
{ 
  int j,k,x,q,h,z,a;
  double p,temp;
  int m[20];
  char c;
  x=i;
  j=0;
  h=0;
  temp=0;
  a=0;
  c=str[x];
  if(c=='+'||(str[x-1]!='('&&c=='-'&&x!=0)||c=='*'||c=='/'||c=='\0'||c=='('||c==')') return(x);
  else{
  do{
  if(str[x-1]=='('&&c=='-'&&str[x+1]=='(')
  {
  Push_2(OPND,0);
  Push_1(OPTR,'-');
  Push_1(OPTR,'(');
  x=x+2;
  c=str[x];
  }
  else if(str[x-1]!='('&&c=='-'&&str[x+1]=='(')
  {
  Push_2(OPND,0);
  Push_1(OPTR,'-');
  Push_1(OPTR,'(');
  x=x+2;
  c=str[x];
  }
  else if(c=='-'&&str[x+1]!='(')
  {
  Push_2(OPND,0);
  Push_1(OPTR,'-');
  x=x+1;
  c=str[x];
  }
  }while(c=='-'||str[x+1]=='(');
  while(c!='+'&&c!='*'&&c!='/'&&c!='('&&c!=')'&&c!='\0'&&c!='-')
  { 
	if(c!='.')
	{
	  if(c=='a'||c=='b'||c=='c'||c=='d'||c=='e'||c=='f')
	  {
	   c=c-87;
	  }
	  else 
	  {
	   c=c-48;
	  }
    m[j]=(int)c;
    x=x+1;
	j=j+1;
	c=str[x];
	}
	else
	{
    h=j;
	x=x+1;
	c=str[x];
	}
  }
  if(h==0) h=j;
  k=j-1;
  q=j;
  p=1.0;
    if(choose==1)
	{
     for(z=0;z<j-h;++z)
	 {
      p=p*0.1;
	 }
	}
	else if(choose==2)
	{
      for(z=0;z<j-h;++z)
	 {
      p=p*(1/16);
	 }
	}
	else if(choose==3)
    {
      for(z=0;z<j-h;++z)
	 {
      p=p*(1/2);
	 }
	}
	else if(choose==4)
    {
      for(z=0;z<j-h;++z)
	 {
      p=p*(1/8);
	 }
	}
  for(j=0;j<q;++j)
  {
	if(choose==1)
	{
    temp=temp+m[k]*p;
	p=p*10;
	k=k-1;
	}
	else if(choose==2)
	{
    temp=temp+m[k]*p;
	p=p*16;
	k=k-1;
	}
	else if(choose==3)
	{
    temp=temp+m[k]*p;
	p=p*2;
	k=k-1;
	}
	else if(choose==4)
	{
    temp=temp+m[k]*p;
	p=p*8;
	k=k-1;
	}
  }
  Push_2(OPND,temp);
  return(x);
  }
}
char Precede(char c1,char c2)
{
  int i,j;
  if(c1=='+') i=0;
  if(c1=='-') i=1;
  if(c1=='*') i=2;
  if(c1=='/') i=3;
  if(c1=='(') i=4;
  if(c1==')') i=5;
  if(c1=='\0') i=6;

  if(c2=='+') j=0;
  if(c2=='-') j=1;
  if(c2=='*') j=2;
  if(c2=='/') j=3;
  if(c2=='(') j=4;
  if(c2==')') j=5;
  if(c2=='\0') j=6;
 
  char op[7][7]={
	  {'>','>','<','<','<','>','>'},
	  {'>','>','<','<','<','>','>'},
	  {'>','>','>','>','<','>','>'},
	  {'>','>','>','>','<','>','>'},
	  {'<','<','<','<','<','=','0'},
	  {'>','>','>','>','0','>','>'},
	  {'<','<','<','<','<','0','='},
  };
  return (op[i][j]);
}
double Operate(double m,char c2,double n)
{ 
  double k;
  if(c2=='+') { k=m+n;return(k);}
  else if(c2=='-') { k=m-n;return(k);}
  else if(c2=='*') { k=m*n;return(k);}
  else if(c2=='/') { if(n!=0){k=m/n;return(k);}else { return(999999);}}
  else return Error;
}
void conversion(Stack &S,int number)
{ while(number){
	Push_2(S,number%2);
	number=number/2;
}
printf("计算的结果为:");
while(!StackEmpty1(S)){
	int b;
	b=Pop_2(S,b);
	printf("%d",b);
}
printf("\n");
}
double EvaluateExpression()
{ 
  double a,b,k,e,j,q;
  int i,y;
  char c,theta,x;
  y=0;
  char str[1000];
  scanf("%s",str);
  InitStack_1(OPTR);
  InitStack_1(KUO);
  Push_1(OPTR,'\0');
  InitStack_2(OPND);
  InitStack_2(SHARE);
  InitStack_2(TWO);
  if(Check(str)&&Sign(str)&&Right(str))
  {
  i=0;
  c=str[i];
  while(c!='\0'||GetTop_1(OPTR)!='\0')
  { if(k==999999) { printf("无穷大!\n");break;}
	if((c!='+'&&c!='*'&&c!='/'&&c!='('&&c!=')'&&c!='\0'&&c!='-')||(c=='-'&&str[i-1]=='(')||(!y&&str[0]=='-')||(str[i-1]=='('&&c=='-'&&str[i+1]=='('))
	{
	  i=In(i,str);
      c=str[i];
	  y=y+1;
	}
	else
	{
		x=Precede(GetTop_1(OPTR),c);
		switch(x){
		case '<':
			Push_1(OPTR,c);
			i=i+1;
			i=In(i,str);
			c=str[i];
			break;
		case '=':
			Pop_1(OPTR,c);
			i=i+1;
			i=In(i,str);
			c=str[i];
			break;
		case '>':
			theta=Pop_1(OPTR,theta);
			b=Pop_2(OPND,b);
			a=Pop_2(OPND,a);
			k=Operate(a,theta,b);
			if(k!=999999)
			{
			Push_2(OPND,k);
			break;
			}
			else
			{ break;}
		}
	}
  }
    if(k!=999999)
	{
	 if(choose!=3)
     {
	  e=GetTop_2(OPND);
      return (e);
	 }
	 else
	 {
      e=GetTop_2(OPND);
	  q=(int)e;
      conversion(TWO,q);
	  return(0);
	 }
	}
	else
	{
    printf("无穷大!\n");
	return(0);
	}
  }
  else 
  { 
	printf("表达式错误,请重新输入!\n");
	j=0;
	return(j);
  }
}
void main()
{ 
  double p;
  int q;
  printf("****************************************************");
  printf("\n");
  printf("\n");
  printf("                 人工智能计算器                     ");
  printf("\n");
  printf("\n");
  printf("****************************************************");
  printf("\n");
  int n;
  do{
  printf("开始计算-1 结束-0:");
  scanf("%d",&n);
  printf("\n");
  switch(n){
  case 0:
  	  break;
  case 1:
	  do
	  {
		printf("十进制运算-1   十六进制运算-2   二进制运算-3   八进制运算-4   结束-0:");
		scanf("%d",&choose);
		printf("\n");
		switch(choose){
		case 0:
			break;
		case 1:
            printf("请输入您要计算的表达式:");
            p=EvaluateExpression();
            printf("计算的结果为:%f\n",p);
			printf("\n");
			break;
		case 2:
			printf("请输入您要计算的表达式:");
            p=EvaluateExpression();
			q=(int)p;
            printf("计算的结果为:%x\n",q);
			printf("\n");
			break;
		case 3:
			printf("请输入您要计算的表达式:");
            EvaluateExpression();
			printf("\n");
			break;
		case 4:
			printf("请输入您要计算的表达式:");
            p=EvaluateExpression();
			q=(int)p;
            printf("计算的结果为:%o\n",q);
			printf("\n");
			break;
		default:
			printf("选择错误!请重新选择!\n");
			printf("\n");
		}
	  }while(choose);
      break;
  default:
  	  printf("请重新选择!\n");
	  printf("\n");
  }
  }while(n);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -