读书人

小弟我用的VC++6.0 感觉编译器好像出有

发布时间: 2012-04-22 18:34:46 作者: rapoo

我用的VC++6.0 感觉编译器好像出问题了,求帮助
//此程序为词法分析器,请注意,所给代码必须有终止符,否则程序有可能崩溃
#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;

//判断字符串是否为关键字,是关键字则返回关键字代码,否则返回0
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;
}
}
//词法分析器
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;
cout<<" ┃      词法分析器      ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃     (c)All Right Reserved       ┃"<<endl;
cout<<" ┃   由2009252班 20093464 叶阳伟 编写  ┃"<<endl;
cout<<" ┃      2012年3月21日完成       ┃"<<endl;
cout<<" ┃ 感谢您使用本程序 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
break;
case 'y':
analyse();
break;
default:
cout<<"输入有误 请重新输入!"<<endl;
}
if(t=='n')
break;
}

}


就是case 'n'的时候,为什么最后两个cout要这样写才能出现预想的方框对齐

[解决办法]
lz用Tab键了?
用空格才是最好的对齐方式。
Tab对齐有可能是 4个空格,有可能是 8个,甚至还见过2个的。
自定义的也有,所以:要么不用Tab,要么在使用Tab之后将指都转换为空格(当然,这个由文本编辑器来做)。

[解决办法]
是不是需要将VC6.0里面的tool->option->tab里设置一下空格的个数呢?
[解决办法]
vc6,有没装sp6补丁

读书人网 >C++

热点推荐