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

📄 two.c

📁 这是一个表达式程序
💻 C
字号:

#include <stdio.h>
#include <conio.h>

#define MAX 101

typedef struct
{
   char value[MAX];
   int top;
}SepStack;

SepStack *s;
int n_bracket;                          /*当前括号计数*/
char array[MAX];
char det_error[MAX];

void createemptystack(void);
void getinf(void);
void postfix(void);
int detect(void);
int proceed(char prior,char next);


main()
{
   int i,det;

      createemptystack();                /*创建空栈*/
   for(;;)
   {

      n_bracket=0;                       /*初始化*/

      for(i=0;i<MAX;i++)
      {
         array[i]='\0';
         s->value[i]='\0';
         det_error[i]='\0';
      }

      getinf();
      det=detect();

      if(det==0||det==2)
      {
         if(det==0)
         {
            printf("The arith is wrong!\n\r");
            printf("It is right until: %s\n\r",det_error);
         }

         else
            printf("Over floated!\n\r");
         printf("\n\rPress ' 'to try again, press others to quit\n\r");
         if(getch()==' ')
            continue;                   /*重复操作*/
         else
            break;                       /*推出*/
      }
      printf("\n\rThe arith inputted is: %s\n\r",array);
      postfix();
      printf("\n\rPress ' ' to try another arith; press others to quit.\n\r");
      if(getch()==' ')
         continue;                   /*重复操作*/
      else
         break;                      /*推出*/
   }/*end for*/
   return;


}

void createemptystack(void)             /*创建空栈*/
{
   s=(SepStack *)malloc(sizeof(SepStack));
   s->top=0;
   return;
}


void getinf(void)
{
   printf("Input the arith: ");
   scanf("%s",array);
   return;
}

void postfix(void)
{
   char ch;
   int i;

   printf("The changed arith is: ");
   for(i=0;;i++)         /*扫描整个array[]*/
   {
      ch=array[i];

      if(ch=='\0')                  /*当ch==空时,退出循环*/
         break;
      if(ch>='0'&&ch<='9')          /*当ch==数字时,直接输出该数字*/
         printf("%c",ch);
      if(ch=='(')                   /*当ch=='('时,直接将'('压入栈*/
         s->value[s->top++]=ch;
      if(ch==')')                   /*当ch==')'时*/
      {
         while(s->value[--s->top]!='(')        /**/
         {
            printf("%c",s->value[s->top]);
            s->value[s->top]='\0';
         }
         s->value[s->top]='\0';                /*将栈中原'('位置位空*/

         if(  proceed(s->value[s->top-2],s->value[s->top-1])==2  )
         {
            printf("%c",s->value[--s->top]);
            s->value[s->top]='\0';
         }
      }/*end if*/
      if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
      {
         if(i==1)
            s->value[s->top++]=ch;
         else
         {
            s->value[s->top++]=ch;
            if(proceed(s->value[s->top-2],s->value[s->top-1])==1)
            {
               printf("%c",s->value[s->top-2]);
               s->value[s->top-2]=s->value[s->top-1];
               s->value[--s->top]='\0';
            }
         }/*end else*/
      }/*end if*/
   }/*end for*/

   if(s->top!=0)
   {
      printf("%c",s->value[--s->top]);
      s->value[s->top]='\0';
   }
   printf("\n\r");

   return;
}/*end postfix()*/


int detect(void)
{
   char ch,ch1,ch2;
   int i;

   for(i=0;array[i]!='\0';i++)
   {
      ch1=array[i];
      ch2=array[i+1];

      if(i==0)
         if(!((ch1>='0'&&ch1<='9')||(ch1=='(')))
            return 0;


      if(ch1>='0'&&ch1<='9')
      {
         if(!(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='%'||ch2==')'||ch2=='\0'))
         {
            det_error[i]=ch1;           /*补上最后一个正确的字符*/
            return 0;
         }
      }
      else if(ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='%')
      {
         if(!(ch2=='('||(ch2>='0'&&ch2<='9')))
         {
            det_error[i]=ch1;           /*补上最后一个正确的字符*/
            return 0;
         }
      }
      else if(ch1=='(')
      {
         n_bracket++;
         if(!((ch2>='0'&&ch2<='9')||ch2=='('))
         {
            det_error[i]=ch1;           /*补上最后一个正确的字符*/
            return 0;
         }
      }
      else if(ch1==')')
      {
         n_bracket--;
         if(n_bracket<0)
         {
            return 0;
         }

         if(!(ch2=='+'||ch2=='-'||ch2=='*'||ch2=='/'||ch2=='%'||ch2=='\0'||ch2==')'))
         {
            det_error[i]=ch1;           /*补上最后一个正确的字符*/
            return 0;
         }

      }
      else
         return 0;
      det_error[i]=ch1;
   }/*end for*/

   if(n_bracket!=0)
      return 0;
   ch=array[--i];
   if(!(ch==')'||(ch>='0'&&ch<='9')))
      return 0;
   if(i==100)
      return 2;  /*溢出,返回2*/
   return 1;  /*正确的表达式,返回1*/
}

int proceed(char prior,char next)    /*优先级比较*/
{
   if(  (prior=='+'||prior=='-') && (next=='*'||next=='/'||next=='%')  )
      return 2;                                             /*返回特殊信息2,以应付...(+*(...的情况*/
   if(  (prior=='*'||prior=='/'||prior=='%') && (next=='+'||next=='-')  )
      return 1;
   if(  (prior=='*'||prior=='/'||prior=='%') && (next=='*'||next=='/'||next=='%')  )
      return 1;
   if(  (prior=='+'||prior=='-') && (next=='+'||next=='-')  )
      return 1;                                             /*允许输出栈顶下面的那个元素,返回1*/

   return 0;                                                /*不允许输出栈顶下面的那个元素,返回0*/
}



⌨️ 快捷键说明

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