读书人

哪位耐心人士帮小弟我看看小弟我这多文

发布时间: 2012-04-23 13:17:38 作者: rapoo

哪位耐心人士帮我看看我这多文件结构怎么错了!!!我要疯了···
//这是function.h的内容

#ifndef FUNCTION_H
#define FUNCTION_H

#include <string>
using namespace std;


//定义关键字表和特殊字符表的结构体
struct code_page
{
char *name;
int num;
};

//判断字符串是否为关键字,是关键字则返回关键字序号,否则返回0
int IsKey(string a);

//判断字符是否为特殊字符,如果是则返回字符序号,否则返回0
int Iscode(char ch);

//输出链表
void display();

#endif

//这是analyse.h的内容

#ifndef ANALYSE_H
#define ANALYSE_H

void analyse();
#endif

//这是function.cpp的内容

#include "function.h"
#include <iostream>
#include <cctype>
#include <list>
#include <fstream>
//构建存储词法分析结果的链表
list<code_page> Clist;
ofstream fin;
//关键字表
code_page key[6]={{"main",26},{"int",21},{"if",22},{"then",23},{"else",24},{"return",25}};

//特殊字符表
code_page code[12]={{"(",1},{")",2},{"{",3},{"}",4},{";",5},{"=",6},{"+",7},{"*",8},{">",9},{"<",10},{",",11},{"\'",12}};

int IsKey(string a)
{
int i=0,j=-1;
for(i=0;i<6;i++)
{
if(strcmp(key[i].name,a.c_str())==0)
j=i;
}
return j;
}

//判断字符是否为特殊字符,如果是则返回字符代码,否则返回0
int Iscode(char ch)
{
int i=0,j=-1;
for(i=0;i<12;i++)
{
if(ch==code[i].name[0])
j=i;
}
return j;
}
//输出链表
void display()
{
list<code_page>::iterator i;
for(i=clist.begin();i!=clist.end();i++)
{
cout<<"("<<(*i).num<<","<<"\'"<<(*i).name<<"\'"<<")"<<endl;
fin<<"("<<(*(i)).num<<","<<"\'"<<((*(i))).name<<"\'"<<")"<<endl;
}
}

//这是analyse.cpp的内容

#include "analyse.h"
#include "function.h"
#include "iostream.h"
#include "cctype.h"


//词法分析器
void analyse()
{
clist.clear();
FILE *txt;
char name[40];
cout<<"请输入要分析的代码的完整路径及后缀:";
while(1)
{
cin>>name;
if((txt=fopen(name,"r"))==NULL)
cout<<"文件不存在,请重新输入!"<<endl;
else
break;
}
cout<<"读取代码成功!"<<endl;
cout<<"词法分析结果如下:"<<endl<<endl;
cout<<"<<<<<<<<<<<<<<<<<<<"<<endl;

fin.open("词法分析结果.txt");
char ch=fgetc(txt);

//开始对代码进行分析
while(ch!=EOF)
{
string carry;
if(ch==' '||ch=='\t'||ch=='\n')//对空格、制表符tab、换行符的处理
{

}


//判断是否为变量名或关键字
else if(isalpha(ch))// isalpha(ch)判断是否为字母
{
while(isalnum(ch))// isalnum(ch)判断是否为字母或数字
{
ch=tolower(ch);//把字母转换成小写字母
carry+=ch;
ch=fgetc(txt);
}

fseek(txt,-1L,SEEK_CUR);

if(IsKey(carry)!=-1)//判断是否为关键字
{
int i=IsKey(carry);
clist.insert(clist.end(),key[i]);//讲该关键字插入链表尾
}
else
{
const char *arr=carry.c_str();
char *r=new char[strlen(arr)+1];
strcpy(r,arr);
code_page other;
other.name=r;
other.num=0;
clist.insert(clist.end(),other);
}
}//一个变量名或关键字分析完毕


else if(isdigit(ch))//分析是否为数字
{
while(isdigit(ch))
{
carry+=ch;
ch=fgetc(txt);
}
fseek(txt,-1L,SEEK_CUR);
const char *arr=carry.c_str();


char *r=new char[strlen(arr)+1];
strcpy(r,arr);
code_page other;
other.name=r;
other.num=0;
clist.insert(clist.end(),other);
}//一个数字分析完毕

//判断是否为特殊字符
else if(Iscode(ch)!=-1)
{
int i=Iscode(ch);
clist.insert(clist.end(),code[i]);
}//是否为特殊字符判断完毕

else//其他字符种类都归结为100
{
code_page other;
other.name="-";
other.num=100;
clist.insert(clist.end(),other);
}//其他情况判断完毕


ch=fgetc(txt);//再读取一个字符
}
display();
cout<<"<<<<<<<<<<<<<<<<<<<"<<endl<<endl;
cout<<"词法分析完毕,结果已存放在目录下的\"词法分析结果.txt\" 里"<<endl<<endl;
fin.close();
fclose(txt);
}

//这是main.cpp的内容

#include "analyse.h"
#include "function.h"
#include "iostream.h"
#include <cctype>
int main()
{
analyse();
while(1)
{
char t;
cout<<"还要继续进行其他代码分析吗?(y/n)"<<endl;
cin>>t;
t=tolower(t);
switch(t)
{
case 'n':
cout<<"谢谢使用"<<endl;
break;
case 'y':
analyse();
break;
default:
cout<<"输入有误 请重新输入!"<<endl;
}
if(t=='n')
break;
}

}


[解决办法]
#include <string>
using namespace std;

function.h中看到了这个, using namespace std;这个放到 .cpp中
并且 string改为 std::string.

其它部分:每个.cpp中加 using namespace std;

iostream.h 之类的干掉,改为 iostream.

其它问题暂时没看到。
报什么错误?

读书人网 >C++

热点推荐