
25-11-2008, 08:26
|
|
|
|
חבר מתאריך: 25.11.08
הודעות: 1
|
|
|
בעיה בבדיקת תקינות סוגריים
שלום כתבתי תכנית שבודקת אם הקלט שמוקלד תקין מבחינת הסוגריים באמצעות מחסנית.משום מה יש לי באג והתוכנית נכנסת ללולאה אינסופית.הקלט נקלט מתוך קובץ בו כל שורה היא קלט שונה.
זה הקוד שלי
קוד:
# include <iostream>
#include <fstream>
# include <cstring>
# include <stl.h>
using std::cout;
using std::endl;
int main ()
{
vector<char> stack;
int NumOfLine=0,flagEnd=0,i;
ifstream OpenFile("compiler1.txt");
char line[160]; //a whole line will be stored here
while((!OpenFile.eof())&&(line!=NULL))
{
OpenFile.getline(line,160); //where 160 is the size of the array
NumOfLine++;
if (strlen(line)!=0)
{ cout<<"yes"<<endl;
for (i=0;(i<strlen(line))&&(flagEnd==0);i++)
{
switch (line[i])
{
case '(':
stack.push_back(line[i]);
break;
case '[':
stack.push_back(line[i]);
break;
case '{':
stack.push_back(line[i]);
break;
case ')':
if ((stack.empty())||(stack.back()!='('))
{
cout<<NumOfLine<<endl;
flagEnd=1;
}
if (stack.back()=='(')
stack.pop_back();
break;
case ']':
if ((stack.empty())||(stack.back()!='['))
{
cout<<NumOfLine<<endl;
flagEnd=1;
}
if (stack.back()=='[')
stack.pop_back();
break;
case '}':
if ((stack.empty())||(stack.back()!='{'))
{
cout<<NumOfLine<<endl;
flagEnd=1;
}
if (stack.back()=='{')
stack.pop_back();
break;
}
}
if (!stack.empty())
cout<<NumOfLine<<endl;
stack.clear();
line[0]='\0';
}
}
OpenFile.close();
return 0;
}
הקלט הוא:
ציטוט:
1.if( Links[ToRemove.front()].first.empty() )
2.for(std::map<unsigned int,unsigned short int>::iterator erase1=Links[ToRemove.front()].second.begin();erase1!=Links[ToRemove.front()].second.end();++erase1){
3.new_end = remove ( Links[erase1->first].first.begin( ), Links[erase1->first].first.end( ),ToRemove.front());
4.Links[erase1->first].first.erase (new_end, Links[erase1->first].first.end( ) );
5.if(Links[erase1->first].first.empty())
6.ToRemove.push_back(erase1->first);
7.}
|
הספרות הם מהצד השני
נערך לאחרונה ע"י takachi1 בתאריך 25-11-2008 בשעה 08:32.
|