niedziela, 26 czerwca 2016

Table + text + int

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
using namespace std;
#define min 3
#define max 15
#define max_case 25

void rand_initialization()
{
srand(time(NULL));
}
char lower_case(int x)
{
char c,d;
int y;
y=0;
if(x<0)
x=0;
if(x>25)
x=25;
 
for(c='a';c<'z';c++)
{
if(y==x)
{
d=c;break; 
}
++y;
}
return d;
}
char upper_case(int x)
{
char c,d;
int y;
y=0;
if(x<0)
x=0;
if(x>25)
x=25;
for(c='A';c<'Z';c++)
{
if(y==x)
{
d=c; break;
}
++y;
}
return d;
}
char which_case(int x)
{
int y;
y=rand()%2;
if(y==0)
return lower_case(x);
else
return upper_case(x);
}


int main(int argc, char** argv)
{
rand_initialization();
char *f1,*f2,*f3;
int *h1,*h2,*h3;
int x,y,z;
int i,j,k;
int sum1,sum2,sum3;
sum1=sum2=sum3=0;
x=min+rand()%max;
y=min+rand()%max;
f1=new char[x];
h1=new int[x];
f2=new char[y];
h2=new int[y];
z=x+y;
f3=new char[z];
h3=new int[z];

for(i=0;i<x;i++)
{
k=rand()%max_case;
f1[i]=which_case(k);
h1[i]=k;
sum1+=h1[i];

   f3[i]=f1[i];
   h3[i]=h1[i];
   
   
}
for(j=0;j<y;j++)
{
k=rand()%max_case;
f2[j]=which_case(k);
   h2[j]=k;
   sum2+=h2[j];
f3[i+j]=f2[j];
h3[j+i]=h2[j];


}
printf("%s +  %s  = %s  ",f1,f2,f3);
printf("\n");
for(i=0;i<x;i++)
{
cout<<h1[i]<<"+";
}
cout<<" + ";
for(j=0;j<y;j++)
{
cout<<h2[j]<<"+";
}
cout<<" = ";
for(k=0;k<z;k++)
{
cout<<h3[k]<<"+";
sum3+=h3[k];
}
printf("\n");
printf("===============================================================");
printf("\n");
cout<<sum1<<" + "<<sum2<<" = "<<sum3;

delete h3;
delete h2;
delete h1;
delete f3;
delete f2;
delete f1;



return 0;
}

sobota, 11 czerwca 2016

Raplace 2 array with use class in C++:

#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#define min 10
#define max 20
using namespace std;

class Replace_array
{
private:
int size_array;
int *array1;
int *array2;
int *array3;
public:
Replace_array(int *arr1,int *arr2, int size);
~Replace_array();

};
Replace_array::Replace_array(int *arr1, int *arr2, int size)
{
array1=arr1;
array2=arr2;
size_array=size;
array3=new int[size_array];
int i;
for(i=0;i<size_array;i++)
{
array3[i]=array1[i];

array1[i]=array2[i];

array2[i]=array3[i];
}

}
Replace_array::~Replace_array()
{
delete array3;
}

void Init_random()
{
srand(time(NULL));
}

int main(int argc, char** argv)
{
Init_random();
int my_size_array,*my_array1,*my_array2,i;
my_size_array=max-rand()%min;
my_array1=new int[my_size_array];
my_array2=new int[my_size_array];
for(i=0;i<my_size_array;i++)
{
my_array1[i]=rand()%100;
my_array2[i]=rand()%100;
}
cout<<"Before replace array: \n";
cout<<"Array1: ";
for(i=0;i<my_size_array;i++)
{
cout<<my_array1[i]<<"  ";
}
cout<<endl;
cout<<"Array2: ";
for(i=0;i<my_size_array;i++)
{
cout<<my_array2[i]<<"  ";
}
cout<<endl;
cout<<"After replace array: \n";
Replace_array My_replace(my_array1,my_array2,my_size_array);
cout<<"Array1: ";
for(i=0;i<my_size_array;i++)
{
cout<<my_array1[i]<<"  ";
}
cout<<endl;
cout<<"Array2: ";
for(i=0;i<my_size_array;i++)
{
cout<<my_array2[i]<<"  ";
}

delete my_array2;
delete my_array1;
return 0;
}