|
please help me this code logic
where make I the mistake this code
It is running console wizard in C++ but I trasformation to form enviroment borland C++ 6.0
i hope to your find to my mistake
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "Unit1D.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define MAXPOP 25
struct gene {
int alleles[4];
int fitness;
float likelihood;
// Test for equality.
operator==(gene gn) {
for (int i=0;i<4;i++) {
if (gn.alleles[i] != alleles[i]) return false;
}
return true;
}
};
class CDiophantine {
public:
CDiophantine(int, int, int, int, int);
int Solve();
gene GetGene(int i) { return population[i];}
protected:
int ca,cb,cc,cd;
int result;
gene population[MAXPOP];
int Fitness(gene &);
void GenerateLikelihoods();
float MultInv();
int CreateFitnesses();
void CreateNewPopulation();
int GetIndex(float val);
gene Breed(int p1, int p2);
};
CDiophantine::CDiophantine(int a, int b, int c, int d, int res) : ca(a), cb(b), cc(c), cd(d), result(res) {}
int CDiophantine::Solve() {
int fitness = -1;
srand((unsigned)time(NULL));
for(int i=0;i<maxpop;i++) {
="" for="" (int="" j="0;j<4;j++)" population[i].alleles[j]="rand()" %="" (result="" +="" 1);
="" }
="" }
="" if="" (fitness="=" createfitnesses())="" return="" fitness;
="" int="" iterations="0;
" while="" !="0" ||="" <="" 50)="" generatelikelihoods();
="" createnewpopulation();
=""
="" iterations++;
="" -1;
}
int="" cdiophantine::fitness(gene="" &gn)="" float="" total="ca" *="" gn.alleles[0]="" cb="" gn.alleles[1]="" cc="" gn.alleles[2]="" cd="" gn.alleles[3];
="" gn.fitness="abs(total" -="" result);
}
int="" cdiophantine::createfitnesses()="" avgfit="0;
" fitness="0;
" for(int="" i="0;i<MAXPOP;i++)" 0)="" i;
="" 0;
}
="" cdiophantine::multinv()="" sumi="0;
" k="10;
" {
="" k;
="" sumi;
}
void="" cdiophantine::generatelikelihoods()="" multinv="MultInv();
" last="1;
" aktr="1;
" ((1="" (population[i].fitness)="" multinv)="" 100);
="" last+="last;
" }
}
int="" cdiophantine::getindex(float="" val)="" (last="" &&="" val="" else="" 4;
}
gene="" cdiophantine::breed(int="" p1,="" p2)="" crossover="rand()" 3+1;
="" first="rand()" 100;
="" gene="" child="population[p1];
" initial="0," final="3;
" (first="" {="" child.alleles[i]="population[p2].alleles[i];
" (rand()="" 101="" 5)="" child;
}
void="" cdiophantine::createnewpopulation()="" temppop[maxpop];
="" parent1="0," parent2="0," while(parent1="=" population[parent1]="=" population[parent2])="" 101));
="" (++iterations=""> 25) break;
}
temppop[i] = Breed(parent1, parent2); // Create a child.
}
for(int i=0;i<maxpop;i++) population[i]="temppop[i];
}
//---------------------------------------------------------------------------
__fastcall" tform1::tform1(tcomponent*="" owner):="" tform(owner)
{
}
="" ---------------------------------------------------------------------------
void="" __fastcall="" tform1::button1click(tobject="" *sender)
{
int="" solve();
int="" fitness;
edit1-="">Text=StrToInt(fitness);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
CDiophantine dp(1,2,3,4,30);
gene gn;
int ans;
ans = dp.Solve();
if (ans == -1) {
//cout << "No solution found." << endl;
} else {
gene gn = dp.GetGene(ans);
Memo1->Text= gn.alleles[0];
}
}
//---------------------------------------------------------------------------
|
|
|
|
|
oz-gur wrote: i hope to your find to my mistake
i hope nobody will, and that you'll learn to use your debugger and learn also how to ask a question on a forum propery
|
|
|
|
|
I keep reading articles which say virtual function has overhead on performance, but I'd like to know how much is it?
For example, the MFC framework, if all the functions inside are virtual, how much will MFC's performance decrease, for the current 1GHz/512M PC hardware ?
|
|
|
|
|
Yes there is overhead on performance. But it is very important to understand the virtue/performace relation when using virtual functions. I mean, one should avoid using virtual functions in low level operations such as accessing each character in a sequence. But for not so heavy and/or intensive operations you can [and sometimes should] ignore performance issues.
As for MFC, a good candidate area of using virtual functions is message handlers. But MFC refused virtual functions and used message maps. Really, so many classes with so many virtual functions would indeed decrease speed and increase memory usage.
--
=====
Arman
|
|
|
|
|
Dear all
How I can make a tooltip for a button in Visual C++ 6. The message should be dynamic. I want to change it on the fly(at run time).
Best regards
|
|
|
|
|
|
Thanks
It was what I need.
|
|
|
|
|
hi guys,
DO anyone know what is the reason that i can only play avi file which is original in Microsoft, but when i import a avi format from external i cannot play it..
How can i play my avi in animation control by displaying it full on the screen?
Pls advice, am using VC++ 2003 MFC.
Thanks ..
|
|
|
|
|
I think Animation Controls [^] will be helpful for you and also it has description of this control
|
|
|
|
|
Hi
thanks white sky..dun get what u mean.
The avi file i play can't be display full on the animation control box.
SOme avi file cannot even display on the animation control
Pls advice..
Thanks !!
|
|
|
|
|
I said before use of MCI functions but you said I can't you them Animation control can show avi file that are standard
|
|
|
|
|
I also said before you'll probably need to use some other method (Windows multimedia, for example)
to get the flexibility you need. An animation control isn't going to do it.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi guys,
How do u write using MCI to open and open a avi file??
do i need to specify any box etc animation ctrl box??
Tks
|
|
|
|
|
You can play the AVI to a window - Playing a Device[^]
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi guys,
I have tried to add a bitmap on my picture control using the add resource method.
this is the code that i place on my button the display the bitmap.
void CPictureDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CBitmap Bmp;
Bmp.LoadBitmap(IDB_BITMAP1);
m_pic.SetBitmap(Bmp);
}
However no picture was display on the picture control please advice..
Thanks in advance.
|
|
|
|
|
Who is actually drawing the picture?
How does it know the picture has changed?
Try invalidating the window (or button) that is supposed to display the picture.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Hi,
There is no drawing of picture involved,just one click on the button to display the bitmap.
However seem like the my source code is not working..
Pls advice.
|
|
|
|
|
Set Bitmap to true on the property window of Button and use of global CBitmap,now what happens?
|
|
|
|
|
Hi all:
Say:
char *string = NULL, *buffer = NULL;
int string_len = 0;
fgets(buffer);
string_len = strlen(buffer);
buffer[string_len] = '\0';
string = malloc(string_len+1);
strncpy(string, buffer, string_len);
return string;
Is my codes correct?
Thank you
|
|
|
|
|
Its been a while since if coded in C, but did you allocate memory for buffer?
You should free the memory allocated for string as well.
|
|
|
|
|
That is what confuses me, David.
This is what I thought:
I have to put the string I read into somewhere (e.g. into a buffer) before I can use strlen() to get the length of the string.
Is there another way to accomplish this task? E.g. how do I know the length of the string I read before I put it to a buffer?
Thanks
|
|
|
|
|
But where do you allocate memory for *buffer?
What about this:
char* buffer = new char[10];
gets(buffer);
char* st=buffer;
printf("%s %s",st,buffer);
delete buffer;
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
Hi VuNic:
Thank you for your reply.
I am still wondering if there is a way for me somehow get the length of the word I will read without declaring a pointer (buffer) which points to a char array of certain length?
Thank you
|
|
|
|
|
Are you supposed to stick to "C" ? or you have the option of using std::string s?
why not use them? or you need to go for linked lists. like :
typedef struct stringz
{
char c;
struct stringz* next;
}st;
st* str =(st*)malloc(sizeof(stringz));
st* head = str;
char c;
while(c=getch())
{
if(c==13)
break;
str->c=c;
printf("%c",str->c);
str->next=(st*)malloc(sizeof(stringz));
str=str->next;
}
str->next=NULL;
str =head;
while(str->next!=NULL)
{
printf("%c",str->c);
str=str->next;
}
purely dynamic , hey but never mess up trying your own stringz!. If allowed, I suggest you go with std::string.
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|