|
this is a try at GP in C++ whit strings
GP what :
from array or grafic to formula
GP how :
1 : write random formula's
2 : sort formula's on error
3 : best fumula's mix to kid's
4 : some kid's are mutated
5 : if genration < max and error > whised goto 2
version 0.1.1
building function run
get "list" outof formula
bluatigro 9 okt 2017
#include <math.h>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std ;
typedef VSTR vector<string> ;
VSTR split( string in , char cut = ' ' )
{
VSTR uit ;
size_type i = 0 ;
size_type j = in.find( cut ) ;
while ( j != string::npos )
{
uit.push_back( in.substr( i , j - i ) ) ;
i = ++j ;
j = in.find( cut , j ) ;
if ( j == string::npos )
in.push_back( in.substr( i , in.lenght() ) ) ;
}
return uit ;
}
string dbl2str( double x )
{
stringsream ss ;
ss << x ;
return ss.str() ;
}
double str2dbl( string x )
{
}
class GeneProg
{
private :
int numberMode ;
VSTR genes ;
double in[ 10 ] ;
int inmax ;
public :
GeneProg()
{
numberMode = 0 ;
inmax = 0 ;
}
string run( string prog )
{
int einde = prog.find( ']' ) ;
int begin = einde ;
while ( prog[ begin ] != '[' ) ;
begin-- ;
return prog.substr( begin , einde - begin ) ;
}
} ;
int main()
{
GeneProg GP ;
string a = "[ + 7 [ - 2 3 ] ]" ;
string b = "[ * 9 [ / 8 6 ] ]" ;
cout << "[ test run ]" << endl ;
cout << "prog a = " << a << endl ;
cout << "run a = " << GP.run( a ) << endl ;
cout << "check a = " << 7 + ( 2 - 3 ) << endl ;
cout << "prog b = " << b << endl ;
cout << "run b = " << GP.run( b ) << endl ;
cout << "check b = " << 9 * ( 8 / 6 ) << endl ;
cout << "[ game over ]" << endl ;
cin.get() ;
}
|
|
|
|
|
See here for the answers to all of your questions.
Yes, I know you did not actually ask any questions, but if you did, the link would still be considered valuable.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
In this program i have tried to store all the input in the array. The while should continue till the user input 0, but the loop exits only after one input.
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i, j;
int count = 0;
printf("Enter your integer: \n");
do{
scanf(" %d", &num[i]);
i++;
count++;
}while(num[i]!=0);
for(j=1; j<= count; j++){
if(num[1] > num[j]){
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]);
return 0;
}
|
|
|
|
|
After entering the first digit you increment i to the next empty cell in the num array. So when you get to the end of the block the expression while(num[i]!=0); is false, and the loop ends. This may not always be the case with an array that is not initialised. You should check the number at the time that it is entered and exit when that is zero.
|
|
|
|
|
can you write the correct code. please!
|
|
|
|
|
I have explained what you need to do, and where to do it. Please try for yourself, you will learn so much more.
|
|
|
|
|
|
First suspicious point : you declare i but you do not initialize it (you also declare j but at least you plan to initialize it in the for loop so it is acceptable).
Second suspicious point: you are to hasty to increment i. If you want to insist using a
do {
...
} while (condition)
loop , and not a
while (condition)
{ ... }
then you must realize that you need to check the value just read (if it was zero) and then , if it not 0 , meaning the user want to keep giving values , increase i++ , getting ready for reading the next element.
Try to implement those 2 points , and also as an extra exercise , implement the
while(condition) {
...
}
It is not hard, you can do it in around half an hour or less, and you will benefit a lot by pondering on how it will differ from your
do { ... } while(condition)
|
|
|
|
|
|
One more thing that I would like to add is that you should initialize variable i to 0 .
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
|
THE WORKING CODE
Quote: #include <conio.h>
#include <stdio.h>
int main(){
int num[50], i=0, j;
int count = 0;
printf("Enter your integer: \n");
do{
scanf(" %d", &num[i]);
i++;
count++;
}while(num[i-1]!=0);
for(j=1; j<= count-1; j++){
if(num[0] < num[j]){
num[0] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]);
return 0;
}
|
|
|
|
|
Suggestions:
1) The variable count is not necessary. You can use i instead.
2) The separate for() loop is not necessary. You can do the min/max checking in the while() loop.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Since you present a working solution you deserve to receive some more elegant alternatives.
Here is how I improved your code , there are some nice tricks for you to learn from it
#include <stdio.h>
#define SIZE 15
int main()
{
int num[SIZE], i=0 , max;
int count = 0; printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
do{
scanf(" %d", &num[i]);
count++;
} while(num[i++]!=0 && count < SIZE);
max = num[0];
for(i=1; i< count; i++)
if(max < num[i]) max = num[i];
printf("%d is the greatest of them all !\n", max);
return 0;
}
I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE !
Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].
modified 13-Oct-17 13:39pm.
|
|
|
|
|
Thank you very much..
|
|
|
|
|
here you go
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i = -1, j;
int count = 0;
printf("Enter your integer: \n");
do{
scanf(" %d", &num[i+1]);
i++;
count++;
}while(num[i]!=0);
for(j=1; j<= count; j++){
if(num[1] > num[j]){
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]);
return 0;
}
|
|
|
|
|
|
See here.
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
The first problem is that the variable i has not been initialized.
The second problem is that you are checking the wrong array index.
See the comments in the modified version of your code below.
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i, j;
int count = 0;
i = 0; printf("Enter your integer: \n");
do{
scanf(" %d", &num[i]);
count++;
}while(num[i++]!=0);
for(j=1; j<= count; j++){
if(num[1] > num[j]){
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]);
return 0;
}
|
|
|
|
|
asa76 wrote:
for(j=1; j<= count; j++)
{
if (num[1] > num[j])
{
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]); You do realize this is wrong, don't you?
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi all of you. I have to develop a project, where I need to use a CTree object, something very similar to CTreeCtrl. But this object, will contain only 2 things on every items: int and CString (just like CTreeCtrl does). I avoid to use CTreeCtrl, because this object will not be an visible object, just hold data as a tree structure. Could you advice me what should I use in this case ?
I mean, in this object I need to store something like this:
4 root
--- 5 child1
--------6 child11
--------7 child12
--------8 child13
--- 9 child2
--- 10 child3
--------12 child31
--------13 child32
--------14 child33
--- 11 child4
Any advice will be welcome, but I have to keep this object simple, as being possible ...
Yes, I could use a CTreeCtrl, but this object will had an ID, will be derived from CWnd, etc. ... and my data must keep it only in memory, not inside of CWnd control ...
Kindly thank you.
|
|
|
|
|
You could use a structure as below -
struct TreeNode
{
int i;
CString str;
std::vector<TreeNode> children;
};
Create one instance of TreeNode that would be the root.
The root can then have children who can have children etc.
You could use std::string or std::wstring instead of CString .
You can use the push_back or emplace_back methods to add children.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
modified 4-Oct-17 5:13am.
|
|
|
|
|
Thank you, I have solved the problem after your idea.
|
|
|
|
|
Does CImage perform automatic image rotation when a JPG image is loaded? In an application I am writing I changed the EXIF orientation tag (0x0112) and to my surprise, the image displayed when CImage loaded and displayed that image changed its orientation. I would like to find a way to display a JPG image that is not affected by the EXIF orientation information. Specifically, the code I used is:
CImage iPicture;
iPicture.Load(sFullPath);
CRect rDest(screen coordinates);
CDC *pDC = GetDC();
iPicture.TransparentBlt(pDC->m_hDC, rDest, RGB(0,0,0));
|
|
|
|
|
I don't actually know the answer for certain. However, it would seem logical that CImage will display the image in whatever orientation is specified in the image properties.
|
|
|
|