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

📄 3a.cpp

📁 数据库的几个程序
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct
{
	double  num[100] ;
	int      t;
}Dstack;
typedef struct
{
	char ch[100];
	int  top;
}Wstack;

void instack(Dstack *s,Wstack *m)
{
	
	s=(Dstack *)malloc(sizeof(Dstack));
	m=(Wstack *)malloc(sizeof(Wstack));
	if(s==NULL)
		printf("no space!\n");
	else
        s->t=-1;
	if(m==NULL)
		printf("no space!\n");
	else 
		m->top=-1;
}
int Emptystack(Dstack *s)
{
	return(s->t==-1);
}
int Emptystack(Wstack *m)
{
	return(m->top==-1);
}
void push_num(Dstack *s,double x)
{
        s->num[s->t]=x;
        s->t++;
	
}
void push_char(Wstack *c,char x)
{
        c->ch[c->top]=x;
        c->top++;
	
}
double pop_num(Dstack *s)     
{ 
  
  s->t--;
  return s->num[s->t];

}
char pop_char(Wstack *m)     
{ 
  
  m->top--;
  return m->ch[m->top];
}

char precede(char e,char f)  
{
  char c='<';
  switch(f)
  {
  case '+':
  case '-':if(e=='#')c='>';break;
	       
  case '*':
  case '/': if(e=='+'||e=='-'||e=='#')c='>';break;
  case '#':if(e=='#')c='=';break;
  default:c='e';
  }
  return c;
	       
}

double operate(double a,char c,double b)
{   
	double s;
	switch(c)
	{
	    case '+':s=a+b;
                 break;
		case '-':s=a-b;
			     break;
		case '*':s=a*b;
			     break;
		case '/':if(b==0)
				 printf("input is error\n");
			     else
				 s=a/b;
				 break;
	   default:  break;
	}
    return s;
}
double calculate()
{
    Dstack *data;
	Wstack *word;
	int k;
	char  theta,ch[100],e[100],c,c1;
	double f,a,b;
	/*instack(data,word);*/
	data=(Dstack *)malloc(sizeof(Dstack));
	data->t=0;
	word=(Wstack *)malloc(sizeof(Wstack));
	word->top=0;

	push_char(word,'#');
	gets(ch);
    k=strlen(ch);
	ch[k]='#';
	ch[k+1]='\0';
    int i=0,x=0,y=0;
	  while(ch[i]!='\0')
	  {
		if(isdigit(ch[i]))
		{   
			while(isdigit(ch[i]))
			{   
                
				e[x]=ch[i];
				i++;x++;
				if(ch[i]=='.')
				{
					e[x]=ch[i];
                    i++;x++;y++;
				}
			}
			e[x]='\0';
            f=atof(e);
			x=0;
		    push_num(data,f);
			
		}
		else
         {
            switch(precede(word->ch[word->top-1],ch[i]))
			{
	           case '>':  push_char(word,ch[i]);break;
	           case '<':   theta=pop_char(word);
		                 b=pop_num(data);
			             a=pop_num(data);
			             push_num(data,operate(a,theta,b));
                         if(ch[i]!='#')i--;
						 break;
			}
		    i++;
		}
	  }
	  while(word->ch[word->top-1]!='#')
      {
             theta=pop_char(word);
			 b=pop_num(data);a=pop_num(data);
			 push_num(data,operate(a,theta,b));
	  }
      
	 return data->num[data->t-1];
	 
}
void main()
{   
	double result;
	int n,i;
	char c;
	scanf("%d%c",&n,&c);
	for(i=0;i<n;i++)
	{
      result=calculate();
	  printf("%.2f\n",result);
	}
}

/*2
2.45+3.45*2.0-4.5
1.2-3.4*2.5/2.0-3.5*2.0*/

⌨️ 快捷键说明

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