|
|
I would like to round a LONG to a multiple of an other LONG. For example: I have got the number 5 and i would like to round it up to a multiple of 3. I wrote the following code to do this:
lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement;
In this piece of code lMinimum is de value that gets rounded up. And lIncrement is the value to wich lMinimum is rounded up. So if we choose the numbers mentioned above we get 6. This works fine for positive numbers but when we choose a negative number for lMinimum it goes wrong. I wrote another piece of code to handle negative numbers
lCurrent=abs(lMinimum%lIncrement)+lMinimum;
So combining the two we get:
if(lMinimum>=0)
{
lCurrent=((lMinimum+(lIncrement-1))/lIncrement)*lIncrement;
}
else
{
lCurrent=abs(lMinimum%lIncrement)+lMinimum;
}
This is the obvious way and it contains one conditional branch. I was wondering of someone came accros a bit twiddling hack to do this more efficient.
Thanks
|
|
|
|
|
Remco Hoogenboezem wrote: I have got the number 5 and i would like to round it up to a multiple of 3.
From the example you suggested...
LONG round = 5 - ( 5 % 3 );
LONG round = ( 5 + 3 ) - (( 5 + 3 ) % 3 ); This should round it...
All 5's and 3's should be replaced by variable names...
You should replace 5 + 3 with another variable name so it will look good.
Nibu thomas
Software Developer
Programming Tips
|
|
|
|
|
Remco Hoogenboezem wrote: I would like to round a LONG to a multiple of an other LONG. For example: I have got the number 5 and i would like to round it up to a multiple of 3
Funny but it works
Suppose your number is 5 and round it with multiple of 3
LONG iNum=5;
LONG iMultipleof=3;
LONG iResult;
iResult=(iNum/iMultipleof)*iMultipleof; Try it! But it works for less rounding
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
The end objective is to save a file to disk with data from a linked list. First this function converts the data already in the list into a buffer (string), which is sent along with the filename to another function (not listed here) so that the buffer is written to file. The problem is where I try to set the temporary variables which store the fields in the linked list so that they can be concatinated together into individual lines...to be later concatinated into the buffer (a c++ string). I'm getting this error 4 times from the compiler:
error C2110: '+' cannot add two pointers
The function with the problem:
<br />
void WeaveList::SaveToFile(string FileName)<br />
{<br />
NodePtr currPtr = head;
bool WriteSucceeded = false;<br />
string WeaveLine1,<br />
WeaveLine2,<br />
WeaveLine3,<br />
WeaveLine4,<br />
Buffer,<br />
tempWeaveName,<br />
tempExampleSizes;<br />
double tempARMin,<br />
tempARMax = 0.0;<br />
int tempNumRings = 0;<br />
<br />
cout<<"Writing data to buffer..."<<endl;<br />
if(!IsEmpty())<br />
{<br />
while(currPtr != NULL)<br />
{<br />
tempARMin = currPtr->AR_Range_Min;<br />
tempARMax = currPtr->AR_Range_Max;<br />
tempNumRings = currPtr->NumRingsUsed;<br />
tempWeaveName = currPtr->WeaveName;<br />
tempExampleSizes = currPtr->ExampleSizes;<br />
<br />
WeaveLine1 = "Weave AR Range: " + "/t/t" + tempARMin + " - " + tempARMax;<br />
WeaveLine2 = "Number of Rings Used:" + "/t/t" + tempNumRings;<br />
WeaveLine3 = "Weave Name:" + "/t/t/t" + "!" + tempWeaveName;<br />
WeaveLine4 = "Example Ring Sizes:" + "/t/t" + "!" + tempExampleSizes;<br />
Buffer = Buffer + WeaveLine1 + "/n" + WeaveLine2 + "/n" + WeaveLine3 + "/n" + WeaveLine4;<br />
if(currPtr->link != NULL)<br />
Buffer = Buffer + "/n/n";<br />
currPtr = currPtr->link;
}<br />
}<br />
else<br />
Buffer = "";<br />
WriteSucceeded = SaveListToFile(FileName, Buffer);
if(WriteSucceeded)<br />
cout<<"SUCCESS!"<<endl;<br />
else<br />
cout<<"F***! Unable to write to file."<<endl;
I'm doing this totally on my own without an instructor to pester with questions, so any help would be appreciated. I can post the rest of the file if anyone wants, but it's kind of long.
-- modified at 15:53 Friday 19th May, 2006
|
|
|
|
|
CoffeeAddict19 wrote: "Weave AR Range: " + "/t/t" + tempARMin + " - "
That is not going to work. Take a look at the STL stringstream class.
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
Overload the "+" operator to adding two strings.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
CoffeeAddict19 wrote: WeaveLine1 = "Weave AR Range: " + "/t/t" + tempARMin + " - " + tempARMax;
Use assign method of string like following:
WeaveLine1.assign("Weave AR Range: " + "/t/t" + tempARMin + " - " + tempARMax); CoffeeAddict19 wrote: Buffer = Buffer + WeaveLine1 + "/n" + WeaveLine2 + "/n" + WeaveLine3 + "/n" + WeaveLine4;
for appending use following code
Buffer.append(WeaveLine1 + "/n" + WeaveLine2 + "/n" + WeaveLine3 + "/n" + WeaveLine4);
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
I am new to windows and C++ programming. I am developing an evc++ 4.0 application for a handheld computer running CE .NET 4.2.I have a dialog with many editbox, combobox and datetime picker controls. At runtime it may be determined that some of the data should be unavailable for update so I'd like to remove the corresponding controls from the tab order. I've tried modifying the z-order using SetWindowPos and I can modify the tab order,
however, what I really need to do is to remove some controls from the tab order. Is there any way to modify the tabstop property for a control at runtime?
sande
|
|
|
|
|
You've probably figured it out by now, but...
I think what you want to do is disable the controls that can't be updated. You should be able to use EnableWindow(false) function to disable the controls dynamically. The OS should exlude disabled controls from the tab order automatically. I assume it works in the embedded environment as well - give it a shot.
- S
50 cups of coffee and you know it's on!
|
|
|
|
|
Hi, Steve.
That is exactly what I was looking for. Thanks very much for your assistance.
|
|
|
|
|
I'm sure there is a simple solution to my problem but I'm just not seeing it. I'm trying to update my views as I read data from a file. I have implemented a separate thread so that the program is able to respond to other command while it is reading. Because of this I cannot use UpdateAllViews() . I tried using events, which worked, but with some undesired side effects. The next approach would be to post a message and have the views update on this. So far I have in my CDocument:
::PostMessage(HWND_BROADCAST, UWM_UPDATE_VIEW, 0, 0);
And in the CView I have:
BEGIN_MESSAGE_MAP(CAESView, CView)<br />
ON_REGISTERED_MESSAGE(UWM_UPDATE_VIEW, Update)<br />
END_MESSAGE_MAP()<br />
<br />
LRESULT CAESView::Update(WPARAM wParam, LPARAM lParam)<br />
{<br />
return 0;<br />
}
The code compiles and runs but my CView never responds to the message. Am I missing something?
Thanks.
|
|
|
|
|
Hi, I'm not sure about UWM_UPDATE_VIEW message and how MFC handles it but PostMessage with HWND_BROADCAST sends the message to all top-level windows and a CView usually is a child windows of a frame.
The main window should capture your registered message and make the view updates.
Best regards,
Mauro.
|
|
|
|
|
If you do some reading about how CCmdTarget dispatches messages to the MFC framework I bet you find that it does not support HWND_BROADCAST. You will have to handle that in a parent location like your CFrameWnd class and provide child notification yourself.
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
|
Hi, I'm a student playing around with c++ windows forms in .net 2003. That said, I have a quick question. I can't seem to access the data my iterator is pointing at. All the examples of iterators I can find show that I am doing it right. Is there something with windows forms that changes things?
The Code:
typedef std::vector<int> StatsVector;
//create a vector to hold the random numbers
StatsVector stats;
StatsVector::iterator statIter;
statIter = stats.begin();
int tempStat = 0;
//populate the vector with 6 random integers
for(int i = 0; i < 6; i++)
{
stats.push_back(randomNum());
}
//display the results in the designated text box
tempStat = *statIter;
str->Text = tempStat.ToString("G");
statIter++;
tempStat = *statIter;
dex->Text = tempStat.ToString("G");
statIter++;
tempStat = *statIter;
con->Text = tempStat.ToString("G");
statIter++;
tempStat = *statIter;
intel->Text = tempStat.ToString("G");
statIter++;
tempStat = *statIter;
wis->Text = tempStat.ToString("G");
statIter++;
tempStat = *statIter;
chr->Text = tempStat.ToString("G");
//this is how it worked without iterators randomNum returns an int
//str->Text = randomNum().ToString("G");
If anyone wants the project files to compile them, I can email them to you.
If you know of a good book that covers all these vc++ windows forms then please post it.
Thanks,
Matt
|
|
|
|
|
Not sure but try getting the iterator after you load the vector
statIter = stats.begin();
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
Thank You, That fixed it.
Now I feel kinda stupid, at least I won't forget how to use iterators in this way... I hope.
Matt
|
|
|
|
|
Hi again
In windef.h we can see these defines :
#define LOBYTE(w) ((BYTE)((DWORD_PTR)(w) & 0xff))
#define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8))
I don't understand how HIBYTE gets, most significant byte, by above code.
With LOBYTE all is clear.
I thought these macros were getting bytes from 32 bit value.
It found out they are for 16 bit values, now all is clear
thanks.
-- modified at 14:36 Friday 19th May, 2006
|
|
|
|
|
HI all
I have CDerivedStatic class and method :
void CDerivedStatic::foo()
{
...
SetDlgItemText( ,
"Some text" );
...
}
I know I can call the SetWindowText API, but I need this variant
thank you.
-- modified at 13:26 Friday 19th May, 2006
|
|
|
|
|
It's the ID of the objects that you defined in the resource editor.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
This will work fine:
void CDerivedStatic::foo()
{
SetWindowText("Some text");
}
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
|
I know Ican call SetWindowText API, but I need to get ID of current instance ?
thank you.
|
|
|
|
|