回 帖 发 新 帖 刷新版面

主题:***让QB 支持中文 变量名,函数名***续

这里提供一个转换的C程序源码,供参考

你可以用 TC,DJGPP,VC 什么的编译一下


/* 让QBASIC支持中文,并且生成表文件d:\\list.txt
*.txt -> *.bas */
#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"malloc.h"

#define true 1
#define false 0

/*在 main 和 readach 中使用-->*/
FILE * fp1,*fp2;
char *infile,*outfile;
/*<--在 main 和 readach 中使用*/

/*在 chtoe 中使用-->*/
char *b[1000];/*保存汉字标识符*/
unsigned int ib=0;       /*总个数*/
/*<--在 chtoe 中使用*/

huangbiao( char);
char readach();
zhuanHuang();
chtoe( char c[]);
huangyei();
int chbiao(char ch);
void quitram();
void OutList();

/*----------------------*/
main(int argc,char *argv[])
{
char *iargv;

if(argc!=2){
  printf("You forgot to enter a filename\n");
  printf("请输入一个文件名\n");
  return(1);
}
infile=(char*)malloc(256);
  if (!infile) {
     printf("not enough memory\n"); return -1;
  }
outfile=(char*)malloc(256);
  if (!outfile) {
     printf("not enough memory\n"); return -1;
  }
/*打开原始文本文件*/
/* .txt-->.bas */
strcpy(infile,argv[1]);
iargv=strchr(infile,'.');
if( *iargv!=NULL)
  *iargv='\0';
strcpy(outfile,infile);
strcat(outfile,".bas");

strcat(infile,".txt");

if((fp1=fopen(infile,"r"))==NULL)
{
printf("Cannot open in file\n");quitram();
}

/*生成新的文件*/
if((fp2=fopen(outfile,"w"))==NULL)
{
printf("Cannot open out file\n");quitram();
}

/*开始转换*/
zhuanHuang();

  OutList();
/*结束*/
quitram();
}

/*----------读一个字符------------*/
char readach()
{
    char ch;
    static MyEof=0;
    if(MyEof==0){
      ch=fgetc(fp1);
      if(ch==EOF){ ch='\n';MyEof++;}
    }
    else if(MyEof==1){
      ch=EOF;
      MyEof++;
    }
    else if(MyEof==2){
      quitram();
    }

    return(ch);
}


/*-----------转换-----------*/
zhuanHuang()
{
char ch;
ch=readach();
while(ch!=EOF){
  /*开始*/
  if(ch=='"'){/*字符串*/
    fputc(ch,fp2);
    huangyei();
  }
  else if(ch=='\''){/*注解 不变输出*/
    
    do{
      fputc(ch,fp2);ch=readach();
    }
    while  (ch!='\n');
    fputc(ch,fp2);
  }
  else if( chbiao(ch)) /*字母,下划线,数字,汉字*/
    huangbiao(ch);
  else
    fputc(ch,fp2);
  ch=readach();
}

}


/*-----------"-->字符串 不变输出-----------*/
huangyei()
{
  char ch;
  ch=readach();
  while(ch!='"'){
    fputc(ch,fp2);
    ch=readach();
  }
  fputc(ch,fp2);
}


/*----------标识符------------*/
huangbiao( char ch)
{
  int biao=true,hai=false;
  char str[128];
  int i=0,j;
  while(biao==true){
    if(ch<-1)
      hai=true;

    /*ch->字符串*/
    str[i]=ch;str[++i]='\0';

    ch=readach();
    if (chbiao(ch)){/*ch是标识符*/
      biao=true;
      if(ch<-1)hai=true;
    }
    else
      biao=false;
  }
  if(hai==true){
    /*汉字 替换一个到fp2*/
    chtoe( str);
  }
  else
    /*字符串到fp2 不变*/
    for(j=0;str[j]!='\0';j++)
      fputc(str[j],fp2);
  fputc(ch,fp2);
}


/*----------判断 ch 是否标识符-字母,下划线,数字,汉字-----------*/
int chbiao(char ch)
{
int i;

if(isalnum(ch))
  i=true;
else if(ch=='_')
  i=true;
else if(ch<-1)
  i=true;
else
  i=false;
return( i);
}

/*-----------------替换一个到fp2-----------------*/
chtoe( char c[])
{
  char * str;
  int j;
  int you=false;
  str=c;
  for(j=0; j<ib;j++){
    if(strcmp(str,b[j])==0){
      fprintf(fp2,"%s","ctoe");
      fprintf(fp2,"%d",j);
      you=true;
    }
  }
  if(you==false){/*原来没有*/
    b[ib]=(char *)malloc(strlen(str)+1);
    strcpy(b[ib],str);
    fprintf(fp2,"%s","ctoe");
    fprintf(fp2,"%d",ib);
    ib++;
  }

}

void quitram()
{
int j;
fclose(fp1);
fclose(fp2);
OutList();
  for(j=0; j<ib;j++){
    free(b[j]);
  }

free(outfile);
free(infile);
exit(0);
}

void OutList()
{

FILE * fpList;
int j;

/*生成表文件*/
if((fpList=fopen("d:\\list.txt","w"))==NULL)
{
printf("Cannot open list file\n");return;
}
  fprintf(fpList,"%s\n",outfile);
  for(j=0; j<ib;j++){
    
    fprintf(fpList,"ctoe%d,%s\n",j,b[j]);
  }
fclose(fpList);
}

回复列表 (共3个回复)

沙发

无奈了
这种实现没有什么实用价值
只能是自己做好玩的

板凳

智者见智,仁者见仁
你也是一家之言

3 楼

&frac14;&sup1;&ordm;&ordm;&ordm;
&sup1;&cedil;&cedil;&raquo;&cedil;&sup1;&para;&micro;·&iexcl;—”‘‘}&#382;|

我来回复

您尚未登录,请登录后再回复。点此登录或注册