|
Decare a CString variable to be your "stack" (or a CArray of type char )
Initialize it to be empty.
Write a function : void Push(char) which adds one character to the end of the CString.
Write a function : char Pop() which returns the character at the end of the CString then removes it (or returns an error value if the string is empty)
Now write a main loop that read in chars from your file one at a time.
1) If it finds a left brace, paren or bracket it must call Push(char) and add it to the string
2) If it finds a right brace, paren or bracket it must call Pop() and compare the result to the char just read in.
3) If it finds any other character ignore it keep looping :
In case 2) (right symbol read in from file)
If Pop() returns a matching left symbol it can carry on looping.
If Pop() returns a non-matching left symbol then the input was invalid.
If Pop() returns the error value because the CString is empty then the input was invalid.
If your reach the end of file with no previos fail:
Call Pop() and check for the error value (CString empty)
If the CString (your stack) is empty (Pop() returns error value) the input was valid.
If the CString (your stack) is not empty the input was invalid.
[edit]
I suggested the CString or CArray so you don't have to worry about memory management as the stack (whose max required size will be unknown) grows: If the exercise is intended to demonstrate your ability to allocate and free memory dynamically then you will need to use char* instead of CString/CArray and allocate an initial amount of memory for the stack and each time you add a char check to see if it needs more allocated.
[/edit]
Steve T
|
|
|
|
|
Ok, this is pretty much what i have so far....it works on some cases...but when i try to implement other strings, it gives me errors...here is the code i have so far...
<br />
#include <stack><br />
#include <fstream><br />
#include <iostream><br />
<br />
<br />
using namespace std;<br />
<br />
int main()<br />
{<br />
<br />
stack<char> charStack;<br />
<br />
char cChar;<br />
<br />
ifstream inFile;<br />
<br />
<br />
inFile.open("infile.txt"); <br />
<br />
if(!inFile) <br />
{<br />
cout<<"Input file does not exist."<<endl;<br />
return 1;<br />
}<br />
<br />
inFile>>cChar;<br />
<br />
while(inFile)<br />
{<br />
<br />
if (cChar == '{' || cChar == '[' || cChar == '(')<br />
{<br />
charStack.push(cChar);<br />
}<br />
<br />
else<br />
{<br />
if (charStack.top() == '(')<br />
{<br />
if (cChar == ')')<br />
{<br />
charStack.pop();<br />
}<br />
}<br />
else if (charStack.top() == '[')<br />
{<br />
if(cChar == ']')<br />
{<br />
charStack.pop();<br />
}<br />
}<br />
else if (charStack.top() == '{')<br />
{<br />
if(cChar == '}')<br />
{<br />
charStack.pop();<br />
}<br />
}<br />
}<br />
<br />
<br />
inFile>>cChar;<br />
}<br />
<br />
if(charStack.empty()) <br />
{<br />
cout<<endl<<"Valid"<<endl;<br />
}<br />
<br />
else <br />
{<br />
cout<<endl<<"Invalid"<<endl;<br />
}<br />
<br />
<br />
cin.get();<br />
<br />
return 0;<br />
<br />
}<br />
<br />
when i use these input files, I get a valid output....
<br />
input:<br />
{(a+5)-7*[1-{4/2}])/{2*(3-6)}
<br />
input:<br />
(((({[]})()()))){}
<br />
input:<br />
{{()}
and i get errors on the following
<br />
input:<br />
{()[()]}} <br />
<br />
input:<br />
{{a+5}-7*[1-{4/2}]}/{2*(3-6)}<br />
HELP!!!
|
|
|
|
|
rdt253 wrote:
and i get errors...
What errors?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I dont get any compile errors...but when I run stack.exe i get a windows error "stack.exe has encountered a problem and needs to close. We are sorry for the inconvenience. It then prompts me to debug, send error report, or ignore error report...
|
|
|
|
|
Step through the program one statement at a time...until it "crashes."
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi,
I'm working on a GUI interface that has worker theads. I want to be able to access data generated from the worker thread and display it on the GUI in real time. Right now, i'm able to access the data but I'm not able to display it until the thread finishes. Is there a way to access the data and display it in real time while the thread is running?
Thanks
Lien
|
|
|
|
|
What is preventing you from displaying it until after the worker thread has finished?
Is your UI thread waiting for the worker thread to finish? Are you storing your data on the worker thread in buffers that are accessible to the UI thread? What kind of data do you have, and how are you trying to display it on the UI?
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Two ideas:
1- Have your worker thread post user messages to your GUI thread informing it about the current status.
2- Use a data structure that your worker thread and GUI thread both have access to. Be careful about data access synchronisation when using this approach.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it! Honoured as one of The Most Helpful Members of 2004
|
|
|
|
|
Hi.. anyone help with some info to save a bitmap from the clipboard into a bitmap file?
|
|
|
|
|
if (OpenClipboard(NULL) != FALSE)
{
HBITMAP hBitmap = GetClipboardData(CF_BITMAP);
if (NULL != hBitmap)
{
}
CloseClipboard();
} [edit]
I just tried this and it works fine.
[/edit]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
|
Hello,
I am thinking of getting either a VC++ 2003 .NET or VC++ 6. I have to write an FTP application that would work on the internet. I will very much appreciate if somebody could tell me what would be the best platform for this application: VC++ 2003 .NET or VC++ 6? And, if it is VC++ 2003, could somebody please tell me how adding handlers works in this platform?
Thank you,
Victor.
|
|
|
|
|
You may as well get the more up-to-date version. VC++ 2003 will do everything that VC++ 6 does plus more.
If you intend to use MFC then you should be aware that ClassWizard is no longer present but the online help explains how to add event handlers. Many think that the new way is less usable than the old way but, nevertheless, the functionality and features of the framework, as opposed to the IDE, are richer.
Kevin
|
|
|
|
|
Hi Kevin,
Thank you very much for the reply. I think I will get VC++ 2003.
Victor.
|
|
|
|
|
You might want to investigate the differences between the Standard and Professional editions though. You can get the Standard Edition by itself. But for the Professional Edition, you have to purchase the entire Visual Studio 2003 (or use the free command line compiler with no IDE! ). I don't know whether this is an issue for you or not. I'm not entirely sure what you lose if you just get VC++ 2003 Standard but I'm sure there must be a feature matrix somewhere.
Kevin
|
|
|
|
|
I believe the major difference is that the professional version is able to perform more code optimizations than standard. I personally have had no problem with the standard version.
|
|
|
|
|
Dear All, does anyone have UDP socket sample code to able to send and receive UDP? or anyone can explain how to:-
a) Write and send a programme to send a single datagram.
b) A program to receive the datagram sent by the program (a above)
c) A program to wait for the reply from b above (question b)
d) A way of timing how long it takes for a datagram to get from the first program to the second and back again, i.e measuring the RTTs of a datagram.
Thank you for looking into this.
Regards.
Kim
|
|
|
|
|
Hi i want to write a simple thread , that will work in the background and i'll be able to do other things in my program until the thread is in progress,i do the following but get an error.
UINT CtestDlg::MyThreadProc(LPVOID pParam)//create thread
{
for(int i=0;i<10000;i++)
for(int j=0;j<10000;j++) {};
MessageBox("Finished");
return 0;
}
then call it : AfxBeginThread(MyThreadProc,0);
but i get an error :
c:\...\ : error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'UINT (LPVOID)'
Can anyone help ?
Regards
Giorgi Moniava
|
|
|
|
|
|
use:
AfxBeginThread(&MyThreadProc, (LPVOID)this, THREAD_PRIORITY_NORMAL);
inside MyThreadProc you can add the code:
CtestDlg *pTestDlg = (CtestDlg *)pParam;
... this will allow you to access any CtestDlg members.
|
|
|
|
|
Thanks Greg for you attention , but when i type :
AfxBeginThread(&MyThreadProc,(LPVOID)this,0);
i get an error :
c:\..\: error C2276: '&' : illegal operation on bound member function expression
Regards
m0n0
|
|
|
|
|
Your MyThreadProc need to be static.
And you only need to call like
AfxBeginThread(MyThreadProc,(LPVOID)this,0);
E.g
CWinThread* pThread = AfxBeginThread (ThreadFunc, lpVoid, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
ASSERT (NULL != pThread);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread ();
By the way, make sure you protect the variables that you are accessing in the 'this ' in the thread by using those synchronization classes like CMutex, Interlock functions and etc.
Sonork 100.41263:Anthony_Yio
|
|
|
|
|
Hello, just wondering if anyone has tried to backup NT permissions of a file or directory and store it in a flat file so the permissions could be restored at a later date if needed?
Is this even possible? If anyone has any experience in this can you give me a pointer where to start?
Thanks
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
To store:
CFileStatus stat;
CFile::GetStatus("file", stat);
CFile file("attr.out", CFile::modeWrite);
file.Write(&(stat.rStatus), sizeof(stat.rStatus));
file.Close(); To load:
CFileStatus stat;
CFile file("attr.out", CFile::modeRead);
file.Read(&(stat.rStatus), sizeof(stat.rStatus));
file.Close();
CFile::SetStatus("file", stat);
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi,
I utilize the IMPLEMENT_SERIAL(CFondationDoc, CDocument, VERSIONABLE_SCHEMA | 1) macro in my document class. I want to distinct between two versions of my files.
I have put "int nVersion = ar.GetObjectSchema();" in the loading portion of the Serialize method.
But nVersion returns always -1.
Do I have to store the schema number in the file ?
I have tried with "ar.SetObjectSchema(1);", but it is not stored in the file.
Thanks,
Claude
|
|
|
|