我这有个完整的无错误的程序,想改成多文件结构的,我对多文件结构不是很会,有人帮忙改下吗?C/C++ code//此
我这有个完整的无错误的程序,想改成多文件结构的,我对多文件结构不是很会,有人帮忙改下吗?
- C/C++ code
//此程序为词法分析器,请注意,所给代码必须有终止符,否则程序有可能崩溃#include <iostream>#include <string>#include <fstream>#include <cctype>#include <list>using namespace std;ofstream fin;//定义关键字表和特殊字符表的结构体struct code_page{ char *name; int num;};//关键字表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}};//构建存储词法分析结果的链表list<code_page>clist;//判断字符串是否为关键字,是关键字则返回关键字代码,否则返回0int 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;}//判断字符是否为特殊字符,如果是则返回字符代码,否则返回0int 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; }}//词法分析器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);}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; }}
要求analyse()在analyse.cpp一个文件中main()在main.cpp中,其他的函数在function.cpp中,也就是说这个改编过的程序包含
analyse.h
function.h
analyse.cpp
funvtion.cpp
main.cpp
我也试着改写了,但是总是出错,希望哪位帮我改一下,我学习一下
[解决办法]
