1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits> //i used INT_MAX but doesnt worked
#include <climits>
#include <windows.h>
using namespace std;
int help()
{cout<<"What is this? ('y' for help)";char help;cin>>help;
if (help=='y'||help=='Y'){cout << "blablabla"; }} //purpose of this prog:P
int elements()
{
cout<< "\nShow element id-s?\ny(es) or (n)o: ";
char ans;
cin >> ans;
if (ans=='y'||ans=='Y') {cout << "Show id numbers.\n\n";return 1;}
else if (ans=='n'||ans=='N'){cout << "No id-s will be shown.\n\n";return 0;}
else {return 0;}
}
int group()
{
int eid=elements();
int sizeleft;
int sizeright;
sizeright=0;
sizeleft=0;
int * left = reinterpret_cast<int *>(malloc(sizeleft * sizeof(*left)));
int * right = reinterpret_cast<int *>(malloc(sizeright * sizeof(*left)));
int *m,n,l,k,h;
m=new int();
cout << "Number of elements: "; cin >> *m; cin.ignore(9000,'\n');
cout << "Return: " << *m << endl;
if (*m!=0&&*m<9000){
int *center = reinterpret_cast<int*>(malloc(*m * sizeof(*center)));
if (center == NULL ) {cout << "MEMORY ERROR.\n\n";return 0;}
else{
srand (time(NULL));
for (n=0;n<*m;n++)
{
center[n]=n;
l=rand()% 2;
if (l==0)
{sizeleft++; left = (int*)realloc(left, sizeleft * sizeof(int)); left[sizeleft-1]=n;}
else if (l==1)
{sizeright++; right = (int*)realloc(right, sizeright * sizeof(int)); right[sizeright-1]=n;}
}
cout << "Groups(left-right): " << sizeleft << "-" << sizeright << "\n";
if(eid==1){
cout << "Center 'core' element id-s: ";
for (k=0;k<n;k++)
{cout << center[k]<<",";}
cout << "\b\n";
cout << "Left id-s: ";
for (k=0;k<sizeleft;k++)
{cout <<left[k] << ",";}
cout << "\b\n";
cout << "Right id-s: ";
for (k=0;k<sizeright;k++)
{cout << right[k]<<",";}
cout << "\b\n";}
int eid=elements();
int counter;
cout << "\nNumber of bounces: "; cin >> counter;
int done;
for(done=0;done<counter;done++)
{
int templeftsize;
int temprightsize;
templeftsize=0;
temprightsize=0;
int * templeft=reinterpret_cast<int *>(malloc (templeftsize * sizeof(*templeft)));
int * tempright=reinterpret_cast<int *>(malloc (temprightsize * sizeof(*tempright)));
int i,il,ir;
for (il=0;il<sizeleft;il++)
{
int ll=rand()%2;
if(ll==0)
{temprightsize++; tempright = (int*)realloc(tempright, temprightsize * sizeof(int)); tempright[temprightsize-1]=left[il];}
else if(ll==1)
{templeftsize++; templeft = (int*)realloc(templeft, templeftsize * sizeof(int)); templeft[templeftsize-1]=left[il];}
}
for (ir=0;ir<sizeright;ir++)
{
int lr=rand()%2;
if(lr==0)
{templeftsize++; templeft = (int*)realloc(templeft, templeftsize * sizeof(int));templeft[templeftsize-1]=right[ir];}
else if(lr==1)
{temprightsize++; tempright = (int*)realloc(tempright, temprightsize * sizeof(int));tempright[temprightsize-1]=right[ir];}
}
cout << "\nNew groups:(left-right): " << templeftsize << "-" << temprightsize << endl;
free(left);free(right);left=NULL;right=NULL;
left = (int*)realloc(left, templeftsize * sizeof(int));
right = (int*)realloc(right, temprightsize * sizeof(int));
if (eid==1){cout<<"New_Left: ";}
for(i=0;i<templeftsize;i++)
{
left[i]=templeft[i];
if (eid==1){cout << left[i] << ",";}
}
if (eid==1){cout <<"\nNew_Right: ";}
for(i=0;i<temprightsize;i++)
{
right[i]=tempright[i];
if (eid==1){cout << right[i] << ",";}
}
sizeleft=templeftsize;
sizeright=temprightsize;
cout << endl;
free(templeft);free(tempright);templeft=NULL;tempright=NULL;
}
cout <<"\nMemory address of 'center': "<< ¢er << ".\n"; //i checked something with these...
cout <<"Memory address of 'left': "<< &left << ".\n";
cout <<"Memory address of 'right': "<< &right << ".\n\n";
free(center);
free(right);
free(left);
center=NULL;
right=NULL;
left=NULL;
return 0;}}
else {cout << "ERROR:OVERFLOW/IT'S OVER 9000!?\n";delete m; return (1);} //check if ITS OVER 9000!!!
}
int main()
{
help();
cout << "-Entropy-\n";
cout << "Groups: center'core', left, right.\n\n";
while (group()!=1)
{group();}
cout << "END\n";
Sleep(3000);
return 0;
}
|