我有一个用VC++6.0编译正确的控制台程序,是关于编译原理的“算符优先的语法分析器”的。现在想用MFC做成一个非常简单的窗体,只需一个按钮和两个文本框。从用户编辑的文本框读入字符(即需分析的字串),从另一文本框中输出分析结果(即堆栈的进出情况)。由于字符的读入和输出都是分步进行,放入栈中,所以虽然给两个文本框设了变量,但还是不知如何写代码连接,请高手帮忙!另,以下处理代码如何在Button中引用呢?
以下源代码:
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"
#include "conio.h"struct Lchar{
char char_ch;
struct Lchar *next;
}LLchar,*p,*h,*temp,*top,*base;int table[8][8]={{1,1,-1,-1,-1,1,-1,1},
    {1,1,-1,-1,-1,1,-1,1},
    {1,1,1,1,-1,1,-1,1},
    {1,1,1,1,-1,1,-1,1},
    {-1,-1,-1,-1,-1,-1,-1,0},
    {1,1,1,1,0,1,0,1},
    {1,1,1,1,0,1,0,1},
    {-1,-1,-1,-1,-1,0,-1,-1}};
/*存储算符优先关系表,大于为1,小于或等于为-1,其它为0表示出错*/char curchar;/*待比较字符*/
char curcmp;/*移进栈后的栈顶字符*/
int right;/*设置开关项,当出错时为0*/
int i,j;
int k;/*比较字符在栈的位置*/void push(char pchar)/*入栈函数*/
{
 temp=(Lchar*)malloc(sizeof(LLchar));
 temp->char_ch=pchar;
 temp->next=top;
 top=temp;
}void pop(void)/*出栈函数*/
{
 if(top->char_ch!='#')
  top=top->next;
}int changchartoint(char ch)/*将字符转为数字,以得到算符优先值*/
{
 int t;
 switch(ch)
 {
  case '+':t=0;break;
  case '-':t=1;break;
  case '*':t=2;break;
  case '/':t=3;break;
  case '(':t=4;break;
  case ')':t=5;break;
  case 'i':t=6;break;
  case '#':t=7;
 }
 return t;
}void dosome(void)
{
 k=1;
 for(;;)
 {
  curchar=h->char_ch;
  temp=top;
  for(;;)
  {
   if(temp->char_ch=='N')
   {
    temp=temp->next;
    k++;
   }
   else
   {
    curcmp=temp->char_ch;
    break;
   }
  }
  printf("\n%d\t%d\t",table[i][j],k);
  temp=top;  /*打印栈*/
  for(;;)
  {
   printf("%c",temp->char_ch);
   if(temp->char_ch=='#')
    break;
   else
    temp=temp->next;
  }
  printf("\t");
  temp=h; /*打印待比较的字符*/
  for(;;)
  {
   printf("%c",temp->char_ch);
   
   if(temp->char_ch=='#')
    break;
   else
    temp=temp->next;
  }
  i=changchartoint(curcmp);
  j=changchartoint(curchar); 
  if(table[i][j]==0)/*算符优先值为空,出错处理*/
  {
   printf("\n%d\t%d\t%c\t%c\terror1",table[i][j],k,curcmp,curchar);
   right=0;
   break;
  }
  else/*算符优先值不为空*/
  {
   if(table[i][j]<0)/*算符优先值为-1,移进*/
   {
    if(curchar=='#')/*待比较字符为空*/
    {
     if(k==2)/*当前比较字符在栈的位置为两个元素*/
      break;
     else
     {
      printf("\n%d\t%d\t%c\t%c\terror2",table[i][j],k,curcmp,curchar);
      right=0;
      break;
     }
    }
    push(curchar);
    k=1;
    curcmp=curchar;
    h=h->next;
   }
   else/*算符优先值为1,归约*/
   {
    if(curcmp=='i')/*当前比较为i,出栈一次*/
     pop();
    else/*当前比较不为i,出栈三次*/
    {
     pop();
     pop();
     pop();
    }
    push('N');/*归约到N*/
    k=1;
   }
  }
 }
 }
void main(void)
{
 char ch;
 right=1;
 base=(Lchar*)malloc(sizeof(LLchar));
 base->next=NULL;
 base->char_ch='#';
 top=base;
 h=(Lchar*)malloc(sizeof(LLchar));
 h->next=NULL;
 p=h;
 /*输入待比较字符串,以'#'结束*/
 printf("input the string and end with '#':\n");
 do{   
 
  ch=getchar();
  putchar(ch);
  if(ch=='i'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#')
  {
   temp=(Lchar*)malloc(sizeof(LLchar));
   temp->next=NULL;
   temp->char_ch=ch;
   h->next=temp;
   h=h->next;
  }
  else
  {
   temp=p->next;
   printf("\nInput a wrong char!Input again:\n");
   for(;;)
   {    
    if (temp!=NULL)
     printf("%c",temp->char_ch);      
    else
     break;
    temp=temp->next;
   }
  }
 }while(ch!='#');/*输入待比较字符串,以'#'结束*/
 p=p->next;
 h=p;
 dosome();/*开始识别*/
 if(right)
  printf("\nOK!\n");
 else
  printf("\nError!\n");
 getchar();
 scanf("%c",&k);
}

解决方案 »

  1.   

    既然你给两个编辑控件关联了变量,那么这个关联变量本身就代表了了控件中显示的字符,可以用UpdateData(TRUE)使关联变量获取控件中的输入字符,用UpdateData(FALSE)将关联变量的值显示在控件上.
      

  2.   

    1、新添加一个对话框
    2、为对话框建立一个类,假设为CMyClass
    3、你上面的函数,除main外的全部在类头文件MyClass.h中声明
    类似在C中申明
    4、函数的实现写成这样:void CMyClass::dosome(){...}代码可以不变
    5、main函数放在Bottom的单击事件中,不要void main{},直接将里面的代码复制到bottom的响应函数中。
      

  3.   

    请问是新建一个类,还是分别添加source file 和header file呢?
    回复wltg,我只是设了变量,但是不知如何关联。因为在源程序中输出的变量各不同,我不知道怎样与文本框的一个变量关联。
    谢谢各位的热心回复,因为刚开始学,问的问题比较幼稚,大家不要嫌弃!我的QQ为605563953,希望能给我进一步的帮助,谢谢!
      

  4.   

    找本MFC入门之类的书看一看吧,看起来你基础不错.
      

  5.   

    谢谢大家鼓励!
    请问Ky310我建的本来就是MFC总的对话框而不是文档,可否将头文件和函数放于针对该对话框的source file 和 header file? 若重建一个类,那新建的对话框又有什么用呢?
      

  6.   

    你的工程是基于对话框的,那就是启动时有个主对话框默认启动.你可以将你的头文件放在该类的header file 中.这样的话不需要重新建立一个类,我原以为你是基于文档的.