首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

哪位高手能帮忙调式这个程序!

2012-03-11 
谁能帮忙调式这个程序!!!!!有谁能不能帮忙看看这个程序啊?为什么在这里fgetc这个函数用不起来,C++不能兼容

谁能帮忙调式这个程序!!!!!
有谁能不能帮忙看看这个程序啊?
为什么在这里fgetc这个函数用不起来,C++不能兼容C语言中的函数吗?
跪求各位高手帮忙解决啊!
明天就要用了!



#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<iostream.h>
#include<fstream.h>

char*key[55]={"main","int","void","char","float","double","bool","long","short","signed","unsigned",
"true","false","const","inline","auto","static","register","extern","for","while","if","else","do",
"switch","case","default","break", "continue", "return", "goto","new", "delete","sizeof","class", "struct", "enum", "union", "typedef",
"this", "friend", "virtual", "mutable", "explicit", "operator","private", "protected", "public","template","typename",
"namespace","using","catch","throw","try"}; //定义关键字
char word[20],ch; //存储识别出的单词流


intIsAlpha(char c)//判断字母
{
if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A'))) return 1;
else return 0;
}

int IsNum(char c)//判断数字
{
if(c>='0'&&c<='9') return 1;
else return 0;
}

int IsKey(char *word)//判断是否为关键字
{
int m,i;
for(i=0;i<63;i++)
{
m=strcmp(word,key[i]);
if(m==0)
{
if(i==0)return 2;
else return 1;
}
}
return 0;
}

void Anaylse(FILE *fp ,ofstream &out){//扫描识别函数
char word[20]={'\0'};
char ch;
int i,c;
ch=fgetc(fp);//获取指针,指针fp自动指向下一个字符
if(IsAlpha(ch))
{
word[0]=ch;
ch=fgetc(fp);
i=1;
while(IsNum(ch)||IsAlpha(ch))
{
word[i]=ch;
i++;
ch=fgetc(fp);
}
word[i]='\0'; //'\0' 代表字符结束(空格)
fseek(fp,-1,1); //回退一个字符
c=IsKey(word); //判断是否是关键字
if(c==0) out<<word<<"******是普通标识符"<<endl;
else if(c==1)out<<word<<"******是主函数标识符"<<endl;
else out<<word<<"******是关键字"<<endl;
}//当字符串为字母时判断是那种标识符

else if(IsNum(ch))
{
word[0]=ch;
ch=fgetc(fp);
i=1;
while(IsNum(ch)){
word[i]=ch;
i++;
ch=fpetc(fp);
}
word[i]='\0';
fseek(fp,-1,1);
out<<word<<"******是无符号实数"<<endl;
}
else
{
word[0]=ch;
switch(ch){
case'[':
case']':
case'{':
case'}':
case';':
case'"':
case',':
case'.':out<<word<<"******是界符"<<endl;break;
case'+':ch=fgetc(fp);
word[1]=ch;
if(ch=='='){
out<<word<<"******是运算符"<<endl;//运算符"+="
}
else if(ch=='+'){
out<<word<<"******是运算符"<<endl;//判断结果为"++"
}
else {
fseek(fp,-1,1);
out<<word<<"******是运算符"<<endl;//判断结果为"+"
}
break;
case'-':ch=fgetc(fp);
word[1]=ch;
if(ch=='='){out<<word<<"******是运算符"<<endl;}//运算符是"-="
else if(ch=='-'){out<<word<<"******是运算符"<<endl;} //判断结果为"--"
else {
fseek(fp,-1,1);
out<<word<<"******是运算符"<<endl; //判断结果为"-"


}
break;
case'*':
case'/':
case'!':
case'=':ch=fgetc(fp);
if(ch=='='){
out<<word<<"******是运算符"<<endl;
}
else {
fseek(fp,-1,1);
out<<word<<"******是运算符"<<endl;
}
break;
case'<':ch=fgetc(fp);
word[1]=ch;
if(ch=='='){
out<<word<<"******是运算符"<<endl; //判断结果为运算符"<="
}
else if(ch=='<'){
out<<word<<"******是运算符"<<endl; //判断结果为"<<"

else {
fseek(fp,-1,1);
out<<word<<"******是运算符"<<endl; //判断结果为"<"
}
break;
case'>':ch=fgetc(fp); 
word[1]=ch;
if(ch=='=') { out<<word<<"******是运算符"<<endl;}//运算符">="
else {
fseek(fp,-1,1);
out<<word<<"******是运算符"<<endl;//运算符"="
}
break;
case'%':ch=fgetc(fp); 
word[1]=ch;
if(ch=='='){out<<word<<"******是运算符"<<endl;;}
if(IsAlpha(ch)){ out<<word<<"******类型标识符"<<endl;}
else {
fseek(fp,-1,1);
out<<word<<"******是运算符"<<endl;
}
break;
default:out<<word<<"******是运算符"<<endl; break;
}
}

int main()
{  
  char aword[10];
ofstream out;
out.open(aword);
char in_fn[30]; //文件路径
  FILE *fp;
  cout<<"请输入源文件名(包括路径和后缀名):"<<endl;
  while(true){
gets(in_fn);
//scanf("%s",in_fn);
  if((fp=fopen(in_fn,"r"))!=NULL) break; //读取文件内容,并返回文件指针,该指针指向文件的第一个字符
  else printf("文件路径错误!请重新输入:");
}
cout<<"请输入目的地文件名:"<<endl;
cin>>aword;
 do{
ch=fgetc(fp);
if(ch=='#') break; //文件以#结尾,作为扫描结束条件
else if(ch==' '||ch=='\t'||ch=='\n'){} //忽略空格,空白,和换行
else{
fseek(fp,-1,1); //回退一个字节开始识别单词流
Analyse(fp);
}
 }while(ch!='#');

}







}

[解决办法]
是fgetc,不是fpetc

热点排行