|
But how would this work for a complex variable such as +1,234.5E-1,234.5? And what is the command for parse (How do I begin to write such a code)?
|
|
|
|
|
You'd have to pull out the two numbers from both sides of the E to see if they are valid.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Starting point off top of my head:
(I emphasize STARTING. I wrote this in less than ten minutes, it probably doesn't compile or even work, but is similar to code I've written. There are a myriad of ways to do this. For error reporting, you could use an index instead of incrementing the string [i.e. pStr[offset] ]
The main point is that it's just plain code, nothing fancy. No tricks. And will be very fast.)
inline
bool IsNumber(TCHAR ch)
{ return ch >= '0' && ch <= '9'; }
bool ParseVal(LPCTSTR& pStr, float& val)
{
val = 0.0f;
bool isNegative = false;
if (*pStr == '+')
{
pStr++;
}
else if (*pStr == '-')
{
pStr++;
isNegative = true;
}
if (!IsNumber(*pStr))
return false;
bool firstSegment = true;
int count = 0;
while (*pStr)
{
TCHAR ch = *pStr++;
if (ch == 'E' || ch == ' ')
{
break;
}
else if (ch == ',' || ch == '.')
{
if (firstSegment)
{
firstSegment = false;
if (count > 3)
return false;
}
else
{
if (count != 3)
return false;
}
count == 0;
}
else if (IsNumber(ch))
{
if (++count > 3)
return false;
}
else
{
return false;
}
}
return true;
}
|
|
|
|
|
Thanks for your reply. I ended up creating the following code: (However, it cannot deal with commas in the exponential)
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str(argv[1]);
int count = 0;
int i = 0;
if((str[0] == '-')||(str[0] == '+'))
{
count = -1;
}
bool firstSegment = true;
while((count < 4)&&(i < str.size()))
{
if((str[i] == 'E' )||(str[i] == 'e')||(str[i] == '.'))
{
if((count == 3)||(firstSegment == true))
{
i = (str.size() - 1);
}
else if(firstSegment == false)
{
cout << "X" << endl;
count = 3;
i = (str.size() - 1);
}
}
else if(str[i] == ',')
{
if(firstSegment == true)
{
firstSegment = false;
if((count > 3)||(count == 0))
{
count = 4;
}
else
{
str.erase(i,1);
count = 0;
i--;
}
}
else
{
if(count != 3)
{
count = 4;
}
else
{
str.erase(i,1);
count = 0;
i--;
}
}
}
else
{
count++;
}
i++;
}
if((count!=3)&&(firstSegment == false))
{
cout << "Y" << endl;
}
cout << str << endl;
return 0;
}
What would be the best way to handle exponential commas? And is there any easier (smaller) way to handle the above code? (I had a small look into locales but cannot seem to get them to work).
|
|
|
|
|
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str(argv[1]);
int count = 0;
int i = 0;
if((str[0] == '-')||(str[0] == '+'))
{
count = -1;
}
bool firstSegment = true;
while((count < 4)&&(i < str.size()))
{
if((str[i] == 'E' )||(str[i] == 'e')||(str[i] == '.'))
{
if((count == 3)||(firstSegment == true))
{
i = (str.size() - 1);
}
else if(firstSegment == false)
{
cout << "X" << endl;
count = 3;
i = (str.size() - 1);
}
}
else if(str[i] == ',')
{
if(firstSegment == true)
{
firstSegment = false;
if((count > 3)||(count == 0))
{
count = 4;
}
else
{
str.erase(i,1);
count = 0;
i--;
}
}
else
{
if(count != 3)
{
count = 4;
}
else
{
str.erase(i,1);
count = 0;
i--;
}
}
}
else
{
count++;
}
i++;
}
if((count!=3)&&(firstSegment == false))
{
cout << "Y" << endl;
}
cout << str << endl;
return 0;
}
That might be a bit easier to read...
|
|
|
|
|
Just scanned over the code. With some work, you could probably get the entire algorithm smaller, but the size would be negligible (and since you've bloated it up using string and cout, why bother ) Joking aside, this will result in fairly small, fast assembly.
For the exponential commas, write another function! If you find a way to combine them, that's great, but not worth worrying about.
(A big mistake a lot of programmer's make is thinking there is an all-in-one algorithm to solutions. The irony is that multiple, simple, algorithms are usually faster, easier to debug and may ultimately even be smaller. At the very least, writing things first as multiple, simple algorithms may help you see how to make a more complex algorithm that does more.)
|
|
|
|
|
How do locales work? And can they be used for the type of output I'm looking for? Just wondering...
|
|
|
|
|
Don't know how in plain C/C++, I program only in Win32.
For Win32 the function is GetLocaleInfo().
To get the thousand's and decimal separators. Call:
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, separator, 2)
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, separator, 2)
The full list of constants is at:
http://msdn.microsoft.com/en-us/library/dd464799%28v=VS.85%29.aspx[^]
To speed things up, just make each call once at beginning of function.
|
|
|
|
|
Hi!
I'm working on a project (LogMap) using wxWidgets and glut, in linux. When I link dynamically everything goes ok. But when I try to link statically, ld gives an error message:
g++ *.cpp -static -lglut `wx-config --libs` `wx-config --cxxflags` `wx-config --gl-libs` -o logmap
/usr/lib/gcc/i586-suse-linux/4.4/../../../../i586-suse-linux/bin/ld: cannot find -lglut
collect2: ld returned 1 exit status
Does anyone have an idea of what is happening? Any idea of how to solve it?
Thanks in advance.
|
|
|
|
|
Does the libglut.a file exist on your system?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I cannot find neither libglut.a nor any wxWidgets libraries in .a format. It means I cannot statically compile, doesn't it?
I'm trying to find them in internet. Any help would be appreciated.
|
|
|
|
|
At least, I suppose, you may build yourself the static library.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
That's what I'm trying to do right now.
|
|
|
|
|
But I cannot find any info about how to build the static library... neither glut nor freeglut. Any idea?
|
|
|
|
|
I don't think the glut release includes a static lib build. You could create one yourself (wouldn't be too difficult), but you might want to check the glut license terms first to see if it's allowed. They may not want it distributed in any form other than the dll.
L u n a t i c F r i n g e
|
|
|
|
|
Perhaps you can tell me how do I build glut as static lib.
|
|
|
|
|
Create a static lib project, add the required files and build.
You'll probably have to redefine an export macro to make it compatible with static linking. Typically, a DLL will have some code block in it's header file that looks like this:
#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif
You'll have to redefine MYLIB_API for a static build to something like this:
#ifdef MYLIB_STATIC_BUILD
#define MYLIB_API
#else
#ifdef MYLIB_EXPORTS
#define MYLIB_API __declspec(dllexport)
#else
#define MYLIB_API __declspec(dllimport)
#endif
#endif
I say again, though, check the license terms and make sure you aren't violating them.
L u n a t i c F r i n g e
|
|
|
|
|
It is Linux...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
There are no export macros needed with gcc on Linux, not that I am aware of. Basically whatever functions/classes you link into your static library will be exported.
/Moak
Edit: Pallini, I replied to the wrong person, sorry.
|
|
|
|
|
CPallini wrote: It is Linux...
Eek! Missed that.
L u n a t i c F r i n g e
|
|
|
|
|
I may just guess (I haven't glut) there is an option in the makefile (possibly you may pass a parameter to 'make configure' command).
As extreme resource, you may hack the makefile (OMG).
You may also find a package containing the static library binary, I think you should search for libglut-devel .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Finally, I found libglut.a in an older version of Suse in my laptop. The problem now, when linking against wxwidgets libraries, is that it gives lots and lots of errors. Anyway, I'm resigning myself to link dynamically. Thanks guys for your help.
|
|
|
|
|
i read the data of a csv file in a buffer and then update the csv file.when i run the application and try to read the data again in buffer it shows empty even when file contains data.wat the problem may be?
|
|
|
|
|
what you want to do?if you have read data in buffer then must be contain data in buffer.I think some where in you code buffer have lost data.Please check it.
|
|
|
|
|
when i run the application first time buffer contains correct data of file but when i close and run the application again and read file data in buffer then buffer contains empty?
|
|
|
|