Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question for anyone good at C++ ....

Status
Not open for further replies.

MajinVegeta20

Programmer
Mar 18, 2003
1
0
0
US
I was wondering how I would write coding if a person entered data exactly as follows below:

{(2,1),(3,2)}

And you had to read in only the numbers and discard the brackets and commas and you had to read it into a two dimensional array that would print an adjaceny matrix similar to this:

0 0 0 0
0 0 1 0
0 1 0 1
0 0 1 0

Thanks. :)

 
Well, you could read your data into a char array and then go over each element of the array and see if the ASCII code is between 48-57 (numbers 0 to 9).
I don't understand how you're creating the matrix. Are you converting the numbers to radix 2? If so, where's the 1?
Radu
 
Use a CString

Use Remove with "(){}"

you should now have a string that looks like

2,1,3,2

Then use this function, take the return value and use strtod or atoi

Code:
CString GetCommaDelimitedWord(CString buffer,int& nStart)
{
	// stop processing
	if(nStart>=buffer.GetLength())
		return _T("");

	// if we got something, return it
	if(!ret.IsEmpty())
	{
		while(nStart<buffer.GetLength() && (buffer.GetAt(nStart) == _T(' ') || buffer.GetAt(nStart) == _T(',')))
			nStart++;

		return ret;
	}

	int loc = buffer.Find(_T(&quot;,&quot;),nStart);

	if(loc == -1)
	{
		ret = buffer.Mid(nStart);
		nStart = buffer.GetLength();
	}
	else
	{
		ret = buffer.Mid(nStart,loc-nStart);
		nStart = loc+1;

		// no spaces
		while(nStart< buffer.GetLength() && buffer.GetAt(nStart) == _T(' '))
			nStart++;
	}


	ret.TrimLeft();
	ret.TrimRight();
	return ret;
}

Call as

int x = 0;

// buffer is the CString after the removals

str = GetCommaDelimitedWord(str,x);
// call n times or put in a loop while str.getLength

value = atoi(str);

Matt

 
sorry for this blurb, forgot to remove it
you need to declare a CString ret in place of it.

Matt

// if we got something, return it
if(!ret.IsEmpty())
{
while(nStart<buffer.GetLength() && (buffer.GetAt(nStart) == _T(' ') || buffer.GetAt(nStart) == _T(',')))
nStart++;

return ret;
}
 
Your could write a token check ?

If you use the standard Library Function

i.e.

char szDataBuffer[1024];
char *szToken;
char szSeps[]=&quot;{}(),&quot;;


szToken = strtok(szDataBuffer, szSeps);
while(szToken != NULL)
{
szToken = strtok(NULL, szSeps);
}

szToken should hold the number you could the use atoi to retrieve the number.

int nValue

nValue = atoi(szToken);

Use the above statement within the while loop.

hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top