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

📄 formula.c

📁 一个简单的解析器
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 语句终结符 T */
      ASC84:

      str++;

      /* 处理所有未处理的运算符 */
      while (ps > sign)
            {
            ps--;
            Calc[ps->op - 65].addr(&pack);
            }

      ps = sign;
      pack.sp = sta;
      pack.vp = pfield;

      continue;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 赋值变量 U~X */
      ASC85:

      str++;
      *(pack.vp) = Xarray + (int)*str++;
      (pack.vp)++;

      /* M~=, 压入符号堆栈 */
      ps->op = 77;
      ps->level = 7;
      ps++;

      continue;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 赋值变量 V~Y */
      ASC86:

      str++;
      *(pack.vp) = Yarray + (int)*str++;
      (pack.vp)++;

      /* M~=, 压入符号堆栈 */
      ps->op = 77;
      ps->level = 7;
      ps++;

      continue;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 赋值变量 W~Z */
      ASC87:

      str++;
      *(pack.vp) = Zarray + (int)*str++;
      (pack.vp)++;

      /* M~=, 压入符号堆栈 */
      ps->op = 77;
      ps->level = 7;
      ps++;

      continue;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 变量 X~x, 值压入堆栈 */
      ASC88:

      str++;
      i = *str++;

      if  (Xarray[i].type)
          pack.sp->num = Xarray[i].num;
      else
          strcpy(pack.sp->str, Xarray[i].str);

      pack.sp->type = Xarray[i].type;
      (pack.sp)++;

      continue;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 变量 Y~y, 值压入堆栈 */
      ASC89:

      str++;
      i = *str++;

      if  (Yarray[i].type)
          pack.sp->num = Yarray[i].num;
      else
          strcpy(pack.sp->str, Yarray[i].str);

      pack.sp->type = Yarray[i].type;
      (pack.sp)++;

      continue;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      /* 变量 Z~z, 值压入堆栈 */
      ASC90:

      str++;
      i = *str++;

      if  (Zarray[i].type)
          pack.sp->num = Zarray[i].num;
      else
          strcpy(pack.sp->str, Zarray[i].str);

      pack.sp->type = Zarray[i].type;
      (pack.sp)++;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      } /* End of while */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* 处理所有未处理的运算符 */
while (ps > sign)
      {
      ps--;
      Calc[ps->op - 65].addr(&pack);
      }

return (0);
}

/*****************************************************************************
 Function: static void CalcXX(PACK *pp)
 Purpose : 双目运算函数。
 Input   : 包指针 PACK *pp
 Return  : 无
 Modify  :
 Remark  :
 *****************************************************************************/

static void Calc65(PACK *pp)
{
/* 'A' == 65 表示 '*' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num *= (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc66(PACK *pp)
{
/* 'B' == 66 表示 '/' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num /= (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc67(PACK *pp)
{
/* 'C' == 67 表示 '+' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num += (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc68(PACK *pp)
{
/* 'D' == 68 表示 '-' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num -= (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc69(PACK *pp)
{
/* 'E' == 69 表示 '<' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num < (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc70(PACK *pp)
{
/* 'F' == 70 表示 '<=' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num <= (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc71(PACK *pp)
{
/* 'G' == 71 表示 '>' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num > (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc72(PACK *pp)
{
/* 'H' == 72 表示 '>=' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num >= (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc73(PACK *pp)
{
/* 'I' == 73 表示 '==' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num == (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc74(PACK *pp)
{
/* 'J' == 74 表示 '!=' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num != (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc75(PACK *pp)
{
/* 'K' == 75 表示 '&&' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num && (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc76(PACK *pp)
{
/* 'L' == 76 表示 '||' */
FIELD   *mysp;

/* 调整堆栈指针 */
(pp->sp)--;
mysp = (pp->sp) - 1;

mysp->num = mysp->num || (mysp+1)->num;
return;
}

/*****************************************************************************/
static void Calc77(PACK *pp)
{
/* 'M' == 77 表示 '=' 赋值运算 */
FIELD   *mysp, *myvp;

/* 调整堆栈指针 */
mysp = (pp->sp) - 1;

(pp->vp)--;
myvp = *(pp->vp);

if  (mysp->type)
    myvp->num = mysp->num;
else
    strcpy(myvp->str, mysp->str);

myvp->type = mysp->type;

return;
}

/*************************************************************************
 Function: main()
 Purpose : For test
 Input   :
 Return  :
 Modify  :
 Remark  :
 *************************************************************************/

main()
{
int             i, max;
long            repeat, j;

RULE            rule;
FIELD           Xarray[32], Yarray[32];
FILE            *fp;

char            buf[128];
char            c;
unsigned char   *cp;

time_t          begin, end;

// 重复计算次数
printf("Repeat times of Calculation = ");
scanf("%ld", &repeat);
if  (repeat <= 0)
    repeat = 1;

// 结构数组大小
printf("\nMenbers of Xarray = ");
scanf("%d", &max);
if  (max > 32)
    max = 32;

// 输入各个变量的值
if  (max > 0)
    printf("\nString: type == 0; Number: type != 0\n");

for (i = 0; i < max; i++)
    {
    printf("\nType of x%d : ", i);
    scanf("%d", &Xarray[i].type);

    printf("x%d = ", i);
    if  (Xarray[i].type == STRING)
        {
        fflush(stdin);
        for (j = 0; j < 255; )
            {
            c = getchar();

            if  (c == '\n')
                break;

            if  (isprint(c))
                buf[j++] = c;
            }
        buf[j] = '\0';

        strcpy(Xarray[i].str, buf);
        }
    else
        scanf("%lf", &Xarray[i].num);
    }

// 输入公式并计算之
while (1)
      {
      printf("\nRule file name : (Q for exit)\n");
      scanf("%s", buf);

      if  (*buf == 'q' || *buf == 'Q')
          break;

      if  ((fp = fopen(buf, "r")) == NULL)
          {
          printf("Cannot open %s !\n", buf);
          continue;
          }

      if  ((i = ReadRuleFile(fp, &rule)) != 0)
          {
          printf("Rule file error No: %d", i);
          fclose(fp);
          continue;
          }
      else
          {
          cp = rule.str;

          while (!(*cp == 0xFF && *(cp+1) == '\0'))
                if  (isprint(*cp))
                    putchar(*cp++);
                else
                    {
                    putchar('.');
                    cp++;
                    }

          putchar('\n');
          }

      fclose(fp);

      for (i = 0; i < 32; i++)
          Yarray[i].type = -1;

      begin = time(NULL);

      for (j = 0; j < repeat; j++)
          i = Formula(&rule, Xarray, Yarray);

      end = time(NULL);

      printf("\n");
      if  (i != 0)
          {
          printf("Formula error No: %d", i);
          continue;
          }

      for (i = 0; i < 32; i++)
          if  (Yarray[i].type != -1)
              {
              if  (Yarray[i].type)
                  printf("y[%d].num : %lf\n", i, Yarray[i].num);
              else
                  printf("y[%d].str : %s\n", i, Yarray[i].str);
              }

      if  (repeat > 1)
          printf("Usage time  : %ld seconds\n", end - begin);
      }
}

/* End of file */

⌨️ 快捷键说明

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