| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 659 人关注过本帖
标题:一个编译原理中的C++问题
只看楼主 加入收藏
first_love
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2006-11-11
收藏
 问题点数:0 回复次数:2 
一个编译原理中的C++问题

[急]求救高手::help
题目:从一个文件source.txt中读入,经过处理,输出结果为:"#PROGRAM#source#;#.....#:;#....#.#"具体我的编程如下,但就是不行;请高手帮忙指正,谢谢!!
我的程序如下:
#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;

int main()
{
char buf[128],ch;
buf[0]='#';
int i=1;
ifstream fin("source.txt",ios::in);
while (1) {
ch=fin.get();
if(ch==' '||ch=='/t'||ch=='/n');
else if(ch=='{'){
ch=fin.get();
while(isalpha(ch)||ch==' '||ch=='/t'||ch=='.')
ch=fin.get();
}
else if(ch=='}');
else if(isalpha(ch)){
buf[i++]='#';
buf[i++]=ch;
ch=fin.get();
while(isalpha(ch))
buf[i++]=ch;
buf[i++]='#';
}
else if(isdigit(ch)){
buf[i++]='#';
buf[i++]=ch;
ch=fin.get();
while(isdigit(ch))
buf[i++]=ch;
buf[i++]='#';
}
else if(ch==';'||ch==','||ch=='-'||ch=='('||ch==')')
buf[i++]=ch;
else if(ch=='+'||ch=='-'||ch=='*'){
buf[i++]=ch;
buf[i++]='#';
}
else if(ch==':'){
buf[i++]=ch;
ch=fin.get();
while(ch=='=')
buf[i++]=ch;
}
else if(ch==EOF){
buf[i]='#';
exit(1);
}
}
for(int j=0;j<=128;i++)
cout<<buf[j];
return 0;
}

其中source.txt里面的内容如下:
PROGRAM source;
{this little source program is used to
illustrate compiling proceduce.}
VAR x,y,z:integer;
a:integer;
BEGIN
{this program has only four executable statements.}
x:=23+5;
z:=x DIV - 3;
y:z+18*3;
a:=x+(y-2) DIV 4;
END.

搜索更多相关主题的帖子: 原理 source 编译 fin 
2007-04-08 23:09
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
把你程序的意图说清除

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-04-09 23:52
first_love
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2006-11-11
收藏
得分:0 

哈哈,我现在已经搞定了,虽然程序写得不是很好!

//输出的文件的程序格式为:
#PROGRAM#source#;#VAR#x#,#y#,#z#:#integer#;#a#:#integer#;#BEGIN#x#:=#23#+#5#;#z#:=#x#DIV#-#3#;#y#:=#z#+#18#*#3#;#a#:=#x#+#(#y#-#2#)#DIV#4#;#END#.#


2007-04-11 00:18
快速回复:一个编译原理中的C++问题
数据加载中...
 
   



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

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