| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 871 人关注过本帖
标题:一个翻译源程序的代码,往下不知道该怎么做了?
只看楼主 加入收藏
一剑飘红
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2009-4-5
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
一个翻译源程序的代码,往下不知道该怎么做了?
编一个程序,要求:
a:本程序从一个磁盘文件上读入一个c语言源程序,将其翻译成一个单词内部码表示的C语言程序。
b:把C单词分成如下几类:
* 保留字(例如:for,if,int,struct等);
*分隔符(例如:( # ;等);
*运算符
*标字符(例如:color,main等);
*常量(包括整数类型常量,浮点类型常量,字符类型常量,字符串类型常量)
c:定义C语言中的所有保留字,分隔符,运算符编码,造出编码表,编出C语言翻译程序。
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
void printf_0();
void printf_1();
void printf_2();
void printf_3();
void printf_4();
void search_0(char a[]);
void search_1();
void search_2();
void search_3();
void search_4();
typedef struct cell{
        char ch;
        struct cell *next;
    }celltype;
    typedef celltype *ptype;
    ptype top;
void main(){
 printf_0();.................打印保留字表
 printf_1();.................打印分隔符表
 printf_2();.................打印运算符号表
 printf_3();................打印标识符表,还没想出来。
 printf_4();....................打印数字表,包括整数和浮点数
FILE *fp;
    char p1[20],p2[20],ch;
       int i;
      i=0;
    if((fp=fopen("e:\1.txt","r"))==NULL){....................打开文件
         printf("can not open file!");
    exit(0);
           }
while((ch=fgetc(fp))!=EOF){
                   while(ch==' '||ch=='\n'){
                    printf("%c",ch);
                    ch=fgetc(fp);
                 }
                if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){
                    p1[i]=ch;
                    ch=fgetc(fp);
                  while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>=0&&ch<=9||ch=='_'){.........检查保留字
                            p1[i++]=ch;
                            ch=fgetc(fp);
                  }
                  search_0(p1);.................检索保留字表
                }
                if(ch>=0&&ch<=9){
                    p1[i]=ch;
                    ch=fgetc(fp);
                    while(ch>=0&&ch<=9||ch=='.'){
                        p1[i++]=ch;
                        ch=fgetc(fp);
                    }
                    search_1(p1);..........数字这块不知道怎么弄
                }

                if(ch=='''){
                    p1[i]=ch;
                    ch=fgetc(fp);
                    while(ch!='''){
                        p1[i++]=ch;
                        ch=fgetc(fp);.............字符常量也不知道怎么弄
                    }
                    search_3(p1);
                }
                if(ch=='"'){
                    p1[i]=ch;
                    ch=fgetc(fp);
                    while(ch!='"'){
                        p1[i++]=ch;
                        ch=fgetc(fp);
                    }
                    search_4(p1);..............字符串常量还没想出来
                }
}
void printf_0(){
    char p[36][13]={"auto","bool","break","case","char_complex","const","continue","default","restrict","do","double","else","enum","extern","float","for","goto","if","_imaginary","inline","int","long","register","return","short","singed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
    int i;
    for(i=0;i<36;i++){
                printf("%-20s   0%d",p[i],i);
                printf("\n");
                }
}
void printf_1(){
    char a[]={'#','(',')','\'',';','\"','\,','{','}','[',']','\\',':'};
    for(int i=0;i<13;i++){
        printf("1 ");
        printf("%2d %c",i,a[i]);
        printf("\n");
    }
}
void printf_2(){
char * p[14]={"+","-","&","==","&&","||","!","*","/","%","!=","<<",">>",","};
    int i;
    for(i=0;i<14;i++){
        printf("%-20c   2 %d  ",p[i],i);
        printf("\n");
    }
}
void printf_4(){
    int j;
    float k;
    for(j=0;j++;j<10){
        printf("%d 4 0 %d",j,j)
    }
    for(j=0,k=1.0;j++,k++;j<10,k<10.0){
        printf("%f 4 1  %d",k,j);
    }
}
void search_0(char a[]){
char p[36][13]={"auto","bool","break","case","char_complex","const","continue","default","restrict","do","double","else","enum","extern","float","for","goto","if","_imaginary","inline","int","long","register","return","short","singed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
  int i;
  for(i=0;i<36;i++){
      if((strcmp(p[i][13],a[]))==0)
          printf("0 %d",i);
  }
}
void search_1(char a[]){
    int i;
    i=0;
    while(a[i]!='\0'){
        if(

哪位高手有更简单的代码,发出来借鉴一下吧
搜索更多相关主题的帖子: 翻译 
2009-08-17 19:41
一剑飘红
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2009-4-5
收藏
得分:0 
自己顶一下

天下英雄出我辈, 一入江湖岁月催.
黄图霸业谈笑中, 不胜人生一场醉.
2009-08-17 21:11
一剑飘红
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2009-4-5
收藏
得分:0 
版主在吗?来看一下吧

天下英雄出我辈, 一入江湖岁月催.
黄图霸业谈笑中, 不胜人生一场醉.
2009-08-17 21:30
zhengzhihui
Rank: 2
来 自:汕头大学
等 级:论坛游民
帖 子:36
专家分:57
注 册:2009-7-4
收藏
得分:14 
还是版主在“提问的智慧”中说得好,发这样的问题直接就<--出来了,谁还看啊
2009-08-17 22:13
快速回复:一个翻译源程序的代码,往下不知道该怎么做了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021904 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved