请教一下关于如何读入txt文档的问题

yeshouwu 2008-10-08 10:29:12
最近在学编译原理,老师要求编写一个求first集和follow集的小程序
把下面的产生式放入一个txt的文档,然后求出产生式右端的first集和每个非终结符的follow集。
我想问一下该如何读入比较方便?读入后怎么指定我要的一个或多个字符串?另外需要用到的头文件希望也能写一下,谢谢!
S : AB;
S : bC;
A : ;
A : b;
B : ;
B : aD;
C : AD;
C : b;
D : aS;
D : c;
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2008-10-11
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{

fstream fin;
vector< vector<string> >result;
string str;
fin.open("in.txt", ios::in);
while (!fin.eof())
{
char left[32]={0}, right[32]={0};
if (!getline(fin, str)) break;
sscanf(str.c_str(), "%s : %s", left, right);
right[strlen(right)-1]=0;
printf("%s : %s\n", left, right);
//自己写到某个结果中去;
}
fin.close();
return 0;
}
jia_xiaoxin 2008-10-11
  • 打赏
  • 举报
回复
可以从文件流输入

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

const int size=100;
char b[26];
char a[size][size];

void OutputF(int i,int x)
{
if(a[i][x]>=97&&a[i][x]<=122) //a[i][x]为终结符时
cout<<a[i][x]<<"}"<<endl;
else
if(a[i][x]==36) //$的asc值为36
{
cout<<a[i][x]<<"$,";
OutputF(i,x+1);
}
else
{
for(int l=0;l<size;l++)
while(a[i][x]==a[l][0])
OutputF(l,x);
}
}

int main()
{

ifstream file("a.txt");
if(!file)
{
cout << "a.txt 放到生成的EXE在的目录下\n";
return 1;
}


for(int i=0;i<size;i++) //输入
{
for(int j=0;j<size;j++)
{
file>>a[i][j]; //这从文件流输入
if(a[i][j]!=35) //#对应35
{
if(a[i][j]>=65&&a[i][j]<=90) //a[i][j]为非终结符
b[i]=a[i][j]-65;
if(a[i][j]==59)break; //;对应 59
}
else break;
}
}
for(i=0;i<size;i++) //first
{
if(a[i][0]>=65&&a[i][0]<=90&&a[i][1]==58) //58对应 :
{
cout<<"First(";
for(int j=2;j<size;j++)
if(a[i][j]!=59)
cout<<a[i][j];
cout<<")={";
}
int x=2;
OutputF(i,x);
}

file.close();
system("Pause");
return 0;
}
星羽 2008-10-11
  • 打赏
  • 举报
回复
Up
ecchi 2008-10-11
  • 打赏
  • 举报
回复
单纯把产生式都读进去的话, 可以参考下面的代码.
要得到一个非终结符(如S)对应的一系列产生式, 可以vvProduct[GetIndex(Map, string("S"))]; 得到一个vector<string>
去掉注释看读入的效果.


#include <string>
#include <map>
#include <vector>
#include "stdio.h"

using namespace std;

bool ParseProduction(string& Symbol, string& Product){
char c = 0;
Symbol.clear();
Product.clear();
while(!isalpha(c)){
if(scanf("%c", &c) == EOF){
return false;
}
}
Symbol += c;
while(true){
c = getchar();
if(!isalpha(c)){
break;
}
Symbol += c;
}
while(c != ':'){
c = getchar();
}

c = 0;
while(!isalpha(c) && c != ';'){
c = getchar();
}
if(c != ';'){
Product += c;
while(true){
c = getchar();
if(!isalpha(c)){
break;
}
Product += c;
}
while(c != ';'){
c = getchar();
}
}
return true;
}

int GetIndex(map<string, int>& Map, const string& String){
int Size = Map.size();
if(Map.find(String) == Map.end()){
Map[String] = Size;
return Size;
}
else{
return Map[String];
}
}

int main()
{
freopen("text.txt","r",stdin);
vector<vector<string> > vvProduct;
map<string, int> Map;
string Symbol, Production;
while(ParseProduction(Symbol, Production)){
int Index = GetIndex(Map, Symbol);
if(Index >= vvProduct.size()){
vvProduct.resize(Index + 1);
}
vvProduct[Index].push_back(Production);
}

//test
//map<string, int>::iterator it = Map.begin();
//int Pos = 0;
//while(it != Map.end()){
// printf("%s : ", (*it).first.c_str());
// Pos = (*it).second;
// const vector<string>& vProduct = vvProduct[Pos];
// int Size = vProduct.size();
// int i;
// for(i = 0 ; i < Size ; ++i){
// printf("(%s) ", vProduct[i].c_str());
// }
// printf("\n");
// ++it;
//}
return 0;
}


first follow集这些东西...忘光了...
OenAuth.Net 2008-10-11
  • 打赏
  • 举报
回复
FIRST集??-_-|||怕怕

最近被FIRST FOLLOW..
yeshouwu 2008-10-11
  • 打赏
  • 举报
回复
ls的理解错我的意思了,我是要程序读入一个已经有产生式的txt文档,然后对它进行编译。
我现在编了点程序,只有txt文档的读入和求first集。用的是数组的形式连续存储的,但是觉得用链表的结构应该会更好,可是链表结构的程序编写不太会。另外我现在的程序不知道为什么txt文档一直没办法读入,程序运行时也会一闪就没了,大家能帮我看看是什么问题吗?
#include<iostream>
#include<fstream>
using namespace std;
const int size=100;
char b[26];
char a[size][size];

void OutputF(int i,int x)
{
if(a[i][x]>=97&&a[i][x]<=122) //a[i][x]为终结符时
cout<<a[i][x]<<"}"<<endl;
else
if(a[i][x]==36) //$的asc值为36
{
cout<<a[i][x]<<"$,";
OutputF(i,x+1);
}
else
{
for(int l=0;l<size;l++)
while(a[i][x]==a[l][0])
OutputF(l,x);
}
}

int main()
{

ifstream file("a.txt");
if(!file)
{
cout<<"a.txt 放到生成的EXE在的目录下\n";
return 1;
}


for(int i=0;i<size;i++) //输入
{
for(int j=0;j<size;j++)
{
cin>>a[i][j];
if(a[i][j]!=35) //#对应35
{
if(a[i][j]>=65&&a[i][j]<=90) //a[i][j]为非终结符
b[i]=a[i][j]-65;
if(a[i][j]==59)break; //;对应 59
}
else break;
}
}
for(int i=0;i<size;i++) //first
{
if(a[i][0]>=65&&a[i][0]<=90&&a[i][1]==58) //58对应 :
{
cout<<"First(";
for(int j=2;j<size;j++)
if(a[i][j]!=59)
cout<<a[i][j];
cout<<")={";
}
int x=2;
OutputF(i,x);
}

file.close();
system("Pause");
return 0;
}
zclever 2008-10-08
  • 打赏
  • 举报
回复
什么叫怎么指定?
下面是读入字符串,读入后放在text.txt中:

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

int main()
{
char buff[100];
memset(buff,0,sizeof(buff));
fstream fout;

fout.open("text.txt",ios::out);
cout<<"Please input your data, end with *"<<endl;
cin.get(buff,sizeof(buff),'*');//提取数据,以*结束
fout.write(buff,strlen(buff));
fout.close();

return 0;
}

/*
Please input your data, end with *
S:AB;
S:bC;
A:;
B:;
B:aD;
C:AD;
C:b;
D:aS;
D:c;
*
*/

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧