| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1549 人关注过本帖
标题:如何从文档中读出小段程序
只看楼主 加入收藏
三木
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-12-15
收藏
得分:0 

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

#define M 11
#define N 10

int main()
{
char FileName[12];
string WordInFile;

char WORD[M][N]={"and","if","else","int","while","struct",
"define","include","void","main","printf"};

cout<<"input your file name please: ";
cin>>FileName;
ifstream fin(FileName);

while ( fin>>WordInFile ) //WordInFile ????????????
{
cout<<WordInFile<<" -------- ";
if ( WordInFile.length() > N ) cout<<"The word is too long!"<<endl;
else
{
for ( int i = 0 ; i < M ; ++i )
if ( WORD[i] == WordInFile ) //WordInFile ????????????
{
cout<<"key word!"<<endl;
break;
}
if ( i >= M ) cout<<"user-defined!"<<endl;
}
}

fin.close();
return 0;
}

2006-12-26 20:32
三木
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-12-15
收藏
得分:0 

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int max=100;//长度最大值
const int key_num=11;
int main()
{ string FileName;
string Testword;
ifstream Read_from_file;
string WORD[key_num]={"and","if","else","int","while","struct",
"define","include","void","main","printf"};
cout<<"input your file name please: ";
cin>>FileName;
Read_from_file.open(FileName.c_str());
Testword=Read_from_file.get(max,' ');
while ( Testword!=EOF)
{for(i=0;i<key_num;i++,Testword=Read_from_file.get(max,' '))
{if ( Testword== WORD[i]) {
{cout<<"key word!"<<endl;break;}
}
if(i==key_num)cout<<"user-defined!"<<endl;
}
fin.close();
return 0;
}
你的词法分析功能过于简单,也没怎么说详细要求
我只写了判别关键字的部分,如果还有其他功能,程序会很长,得几百行的代码!!!
自己好好琢磨吧!!!
关键得靠自己!!
别人帮不了多大的忙!!!!!!!!!!!!!!!!!!!

2006-12-26 20:51
janetding
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-4-11
收藏
得分:0 
谢谢大家的帮忙~
对于我这初学者已经是很大的帮助了``

2007-01-15 09:20
快速回复:如何从文档中读出小段程序
数据加载中...
 
   



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

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