没有产生输出5 .... [英] no output is generated5....

查看:68
本文介绍了没有产生输出5 ....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它正常工作并在输出文件中生成输出但是在main()体中调用punctuatorcheck函数后它没有给出任何输出请检查它....这是代码



It is working properly and generating output in output file but after calling punctuatorcheck function in main() body it is not giving any output please check it.... Here is the code

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;

/* The grammar follwed by this analyser is : you have to give a blank to differentite between two different entities, for eg 
instead of writing "int c=a+b;", write it as "int c = a + b ;".

To execute this program first create an "input.txt" file where the program is written or specify the path of the file which has 
to be analysed, after compiling an "output.txt" file will be created where you have stored the program.
*/

bool keycheck(string str)
{
	string keyword[] ={"num","alpha","fnum","lnum","snum","string","struct","bool","true","false","for","while","repeat","void","main()","if","else","switch","default","case","break","continue","return","goto"};
	int flag=0;
    if(!(str[0]>='a' && str[0]<='z'))
		return false;
	for(int i=0;i<2;i++)
	{
		if(str == keyword[i])
		{
			flag = 1;
			break;
		}
	}
	if(flag == 1)
        return true;
	else
		return false;
} 

string opcheck(string str)
{
	string AsgOperators ="=";
    string MDPOperators[5] ={"*","/","^"};
    string A_SOperators[4]={"+","-"};
    string RelOperators[10]={">=","<=","<",">","||","!="};
    string NOT="!";
    string OR="$";
    string AND="&";
    string S_C[2]={"SRT","CRT"};
    int flag1=0,flag2=0,flag=0,flag3=0,flag4=0,flag5=0,flag6=0,flag7=0;

    for(int i=0;i<3;i++)
	{
		if(str == MDPOperators[i])
		{
			flag = 1;
			break;
		}
	}
	if(flag == 1)
	{
		return "MDP Operators";
	}
	else
	{
		for(int i=0;i<2;i++)
		{ 
			if(str == A_SOperators[i])
			{
				flag1 = 1;
				break;
			}
		}
		if(flag1 == 1)
		{
			return "A_S Operator";
		}
		else
		{
			for(int i=0;i<6;i++)
			{ 
				if(str == RelOperators[i])
				{
					flag2 =1;
					break;
				}
			}
			if(flag2 == 1)
				return "Relational Operator";
			else
			{
				for(int i=0;i<2;i++)
				{ 
					if(str == S_C[i])
					{
						flag3 =1;
						break;
					}
				}
				if(flag3 == 1)
					return "S_C Operators";
				else
				{
					if(str == "=")
					{
						flag4=1;
					}
					if(flag4 == 1)
						return "Asg Operator";
					else
					{
						if(str == "!")
						{
							flag5=1;
						}
						if(flag5 == 1)
							return "NOT";
						else
						{
							if(str == "$")
							{
								flag6=1;
							}
							if(flag6 == 1)
								return "OR";
							else
							{
								if(str == "&")
								{
									flag7=1;
								}
								if(flag7 == 1)
								{
									return "AND";
								}
								else
								{
									return "Error";
								} 
							}
						}
					}
				}
			}
		}
	}
}
string Punctuatorcheck(string str)
{
	string punctuators[]={".",",","/>","{","}","(",")","]","["};
    for(int i=0; i<9;i++)
                    {
                        if(str==punctuators[i])
                        {
							return "Punctuator";                            
                        }
                    }
                    return "Error";
                }


int ischar(char c)
{
	if((c>='A' && c<'Z') || (c>='a' && c<='z'))
		return 1;
    else
		return 0; 
} 

int isnum(char c)
{
	if(c>='0' && c<='9')
		return 1;
    else
		return 0;
}

int isnums(string str)
{
	int flag=0;
	for(int i = 0;i<str.length();i++)>
	{
		if(!isnum(str[i]))
		{
			if(str[i] != '.') 
			{
				flag=1;
				break;
			}
		}
	}
	if(flag == 1)
		return 0;
	else
		return 1;
}

int isidentifier(string str)
{
	int flag =0;
    for(int i=1;i<str.length();i++)>
	{
		if(!ischar(str[i]))
		{
			if(!isnum(str[i]))
			{
				if(str[i] != '_')
				{
					if(str[i] == '[')
					{
						i++;
						for(;str[i]!= ']';)
						{
							if(!isnum(str[i]))
							{
								flag =1;
								break;
							}
							i++;
						}
					}
					else
					{ 
						flag = 1;

					}
					if(flag ==1)
						break;
				}
			}
		}
	}
return flag;
}

int main()
{
	ifstream ifs("input.txt");
    ofstream ofs("output.txt");
	int line=0,flag=0;
	bool check;
	string str="",strch,strline,strp;
	while(!ifs.eof())
	{
		getline(ifs,strline);
		line++;
		ofs<<"---------\n";
		ofs<<line<<"\n";
		strline = strline + " ";
		for(int j=0;j<strline.length();j++)>
		{ 
			if(strline[j] ==' ' || strline[j]=='\t')
			{
				if(str != "")
				{
					if(ischar(str[0]))
					{
						check = keycheck(str);

						if(check)
						{
							ofs<<str<<"\t --> Keywords\n"; 
						} 
						else
						{
							flag = isidentifier(str);
							if(flag == 1)
							{
								ofs<<str<<"\t --> Error\n";
								flag = 0;

							}
							else
							{
								ofs<<str<<"\t --> Identifier\n";
							}
						}
						if(isnum(str[0]))
						{
							if(isnums(str))
								ofs<<str<<"\t -->Number\n";
							else
								ofs<<str<<"\t -->Error\n";
						}
						else
						{
							strch = opcheck(str);
							if(strch == "Error")
								ofs<<str<<"\t -->"<<strch<<"\n";
							else
							{
								strch = Punctuatorcheck(str);
								if(strp == "Error")
									ofs<<str<<"\t -->"<<strp<<"\n";
								else
									ofs<<str<<"\t -->"<<strp<<"\n";
							}
						}
						str=""; 
					}
					else
					{
						str=str+strline[j]; 
					} 
				} 
			}
		}
	}
			cout<<"output file is generated : output.txt";
			getch();
			return 0;
}</fstream></string></conio.h></iostream>

推荐答案

;
string AND = &;
字符串S_C [ 2 ] = { SRT CRT};
int flag1 = 0 ,flag2 = 0 ,flag = 0 ,flag3 = 0 ,flag4 = 0 ,flag5 = 0 ,flag6 = 0 ,flag7 = 0 ;

for int i = 0 ; i< 3; i ++)
{
if (str == MDPOperators [i])
{
flag = 1 ;
break ;
}
}
如果(flag == 1
{
return MDP运营商;
}
else
{
for (< span class =code-keyword> int
i = 0 ; i< 2; i ++)
{
if (str == A_SOperators [i])
{
flag1 = 1 ;
break ;
}
}
如果(flag1 == 1
{
return A_S Operator ;
}
else
{
for (< span class =code-keyword> int
i = 0 ; i< 6; i ++)
{
if (str == RelOperators [i])
{
flag2 = 1 ;
break ;
}
}
如果(flag2 == 1
return 关系运算符 ;
else
{
for int i = 0 ; i< 2; i ++)
{
if (str == S_C [i])
{
flag3 = 1 ;
break ;
}
}
如果(flag3 == 1
return S_C Operators ;
else
{
if (str == =
{
flag4 = 1 ;
}
如果(flag4 == 1
return Asg Operator;
else
{
if (str ==
{
flag5 = 1 ;
}
如果(flag5 == 1
return NOT;
else
{
if (str ==
"; string AND="&"; string S_C[2]={"SRT","CRT"}; int flag1=0,flag2=0,flag=0,flag3=0,flag4=0,flag5=0,flag6=0,flag7=0; for(int i=0;i<3;i++) { if(str == MDPOperators[i]) { flag = 1; break; } } if(flag == 1) { return "MDP Operators"; } else { for(int i=0;i<2;i++) { if(str == A_SOperators[i]) { flag1 = 1; break; } } if(flag1 == 1) { return "A_S Operator"; } else { for(int i=0;i<6;i++) { if(str == RelOperators[i]) { flag2 =1; break; } } if(flag2 == 1) return "Relational Operator"; else { for(int i=0;i<2;i++) { if(str == S_C[i]) { flag3 =1; break; } } if(flag3 == 1) return "S_C Operators"; else { if(str == "=") { flag4=1; } if(flag4 == 1) return "Asg Operator"; else { if(str == "!") { flag5=1; } if(flag5 == 1) return "NOT"; else { if(str == "



{
flag6 = 1 ;
}
if (flag6 == 1
return ;
else
{
if (str == &
{
flag7 = 1 ;
}
如果(flag7 == 1
{
return AND;
}
其他
{
返回 错误;
}
}
}
}
}
}
}
}
}
string Punctuatorcheck(string str)
{
string punctuators [] = { < span class =code-string> />
{ } ] [};
for int i = 0 ; i< 9; i ++)
{
if (str == punctuators [i])
{
return Punctuator;
}
}
return 错误;
}


int ischar( char c )
{
if ((c> = ' A'&& c< ' Z')| |(c> = ' a'&& c< = ' z'))
return 1 ;
else
return 0 < /跨度>;
}

int isnum( char c)
{
if (c> = ' 0'&& c< = ' 9'
< span class =code-keyword> return
1 ;
else
return 0 < /跨度>;
}

int isnums(string str)
{
int flag = 0 ;
for int i = 0 ; I< str.length();我++)>
{
if (!isnum(str [i]))
{
if (str [i]!= ' 。'
{
flag = 1 ;
break ;
}
}
}
如果(flag == 1
return 0 ;
else
return 1 < /跨度>;
}

int isidentifier(string str)
{
int flag = 0 ;
for int i = 1 ; I< str.length();我++)>
{
if (!ischar(str [i]))
{
if (!isnum(str [i]))
{
if (str [i]!= < span class =code-string>'
_'
{
if (str [i] == ' ['
{
i ++;
for (; str [i]!= ' ]';)
{
if (!isnum(str [i]))
{
flag = 1 ;
break ;
}
i ++;
}
}
其他
{
flag = 1 ;

\t\t\t\t\t}
\t\t\t\t\tif(flag ==1)
\t\t\t\t\t\tbreak;
\t\t\t\t}
\t\t\t}
\t\t}
\t}
return flag;
}

int main()
{
\tifstream ifs(\"input.txt\");
ofstream ofs(\"output.txt\");
\tint line=0,flag=0;
\tbool check;
\tstring str=\"\",strch,strline,strp;
\twhile(!ifs.eof())
\t{
\t\tgetline(ifs,strline);
\t\tline++;
\t\tofs<<\"---------\n\";
\t\tofs<<line<<\"\n\";
\t\tstrline = strline + \" \";
\t\tfor(int j=0;j<strline.length();j++)>
\t\t{
\t\t\tif(strline[j] ==' ' || strline[j]=='\t')
\t\t\t{
\t\t\t\tif(str != \"\")
\t\t\t\t{
\t\t\t\t\tif(ischar(str[0]))
\t\t\t\t\t{
\t\t\t\t\t\tcheck = keycheck(str);

\t\t\t\t\t\tif(check)
\t\t\t\t\t\t{
\t\t\t\t\t\t\tofs<<str<<\"\t --> Keywords\n\";
\t\t\t\t\t\t}
\t\t\t\t\t\telse
\t\t\t\t\t\t{
\t\t\t\t\t\t\tflag = isidentifier(str);
\t\t\t\t\t\t\tif(flag == 1)
\t\t\t\t\t\t\t{
\t\t\t\t\t\t\t\tofs<<str<<\"\t --> Error\n\";
\t\t\t\t\t\t\t\tflag = 0;

\t\t\t\t\t\t\t}
\t\t\t\t\t\t\telse
\t\t\t\t\t\t\t{
\t\t\t\t\t\t\t\tofs<<str<<\"\t --> Identifier\n\";
\t\t\t\t\t\t\t}
\t\t\t\t\t\t}
\t\t\t\t\t\tif(isnum(str[0]))
\t\t\t\t\t\t{
\t\t\t\t\t\t\tif(isnums(str))
\t\t\t\t\t\t\t\tofs<<str<<\"\t -->Number\n\";
\t\t\t\t\t\t\telse
\t\t\t\t\t\t\t\tofs<<str<<\"\t -->Error\n\";
\t\t\t\t\t\t}
\t\t\t\t\t\telse
\t\t\t\t\t\t{
\t\t\t\t\t\t\tstrch = opcheck(str);
\t\t\t\t\t\t\tif(strch == \"Error\")
\t\t\t\t\t\t\t\tofs<<str<<\"\t -->\"<<strch<<\"\n\";
\t\t\t\t\t\t\telse
\t\t\t\t\t\t\t{
\t\t\t\t\t\t\t\tstrch = Punctuatorcheck(str);
\t\t\t\t\t\t\t\tif(strp == \"Error\")
\t\t\t\t\t\t\t\t\tofs<<str<<\"\t -->\"<<strp<<\"\n\";
\t\t\t\t\t\t\t\telse
\t\t\t\t\t\t\t\t\tofs<<str<<\"\t -->\"<<strp<<\"\n\";
\t\t\t\t\t\t\t}
\t\t\t\t\t\t}
\t\t\t\t\t\tstr=\"\";
\t\t\t\t\t}
\t\t\t\t\telse
\t\t\t\t\t{
\t\t\t\t\t\tstr=str+strline[j];
\t\t\t\t\t}
\t\t\t\t}
\t\t\t}
\t\t}
\t}
\t\t\tcout<<\"output file is generated : output.txt\";
getch();
return 0 ;
}</fstream></string></conio.h></iostream>
") { flag6=1; } if(flag6 == 1) return "OR"; else { if(str == "&") { flag7=1; } if(flag7 == 1) { return "AND"; } else { return "Error"; } } } } } } } } } string Punctuatorcheck(string str) { string punctuators[]={".",",","/>","{","}","(",")","]","["}; for(int i=0; i<9;i++) { if(str==punctuators[i]) { return "Punctuator"; } } return "Error"; } int ischar(char c) { if((c>='A' && c<'Z') || (c>='a' && c<='z')) return 1; else return 0; } int isnum(char c) { if(c>='0' && c<='9') return 1; else return 0; } int isnums(string str) { int flag=0; for(int i = 0;i<str.length();i++)> { if(!isnum(str[i])) { if(str[i] != '.') { flag=1; break; } } } if(flag == 1) return 0; else return 1; } int isidentifier(string str) { int flag =0; for(int i=1;i<str.length();i++)> { if(!ischar(str[i])) { if(!isnum(str[i])) { if(str[i] != '_') { if(str[i] == '[') { i++; for(;str[i]!= ']';) { if(!isnum(str[i])) { flag =1; break; } i++; } } else { flag = 1; } if(flag ==1) break; } } } } return flag; } int main() { ifstream ifs("input.txt"); ofstream ofs("output.txt"); int line=0,flag=0; bool check; string str="",strch,strline,strp; while(!ifs.eof()) { getline(ifs,strline); line++; ofs<<"---------\n"; ofs<<line<<"\n"; strline = strline + " "; for(int j=0;j<strline.length();j++)> { if(strline[j] ==' ' || strline[j]=='\t') { if(str != "") { if(ischar(str[0])) { check = keycheck(str); if(check) { ofs<<str<<"\t --> Keywords\n"; } else { flag = isidentifier(str); if(flag == 1) { ofs<<str<<"\t --> Error\n"; flag = 0; } else { ofs<<str<<"\t --> Identifier\n"; } } if(isnum(str[0])) { if(isnums(str)) ofs<<str<<"\t -->Number\n"; else ofs<<str<<"\t -->Error\n"; } else { strch = opcheck(str); if(strch == "Error") ofs<<str<<"\t -->"<<strch<<"\n"; else { strch = Punctuatorcheck(str); if(strp == "Error") ofs<<str<<"\t -->"<<strp<<"\n"; else ofs<<str<<"\t -->"<<strp<<"\n"; } } str=""; } else { str=str+strline[j]; } } } } } cout<<"output file is generated : output.txt"; getch(); return 0; }</fstream></string></conio.h></iostream>


if(str==punctuators[i])



will always fail as the two objects are not the same. If you wish to compare strings then you need to use one of the std::string functions. It would be simpler if you just use character comparisons, although you would need to do something about /> (not sure why that is in there).


will always fail as the two objects are not the same. If you wish to compare strings then you need to use one of the std::string functions. It would be simpler if you just use character comparisons, although you would need to do something about /> (not sure why that is in there).


这篇关于没有产生输出5 ....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆