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;
}

niedziela, 21 lutego 2016

Czasami się przydaje. Standardowe równanie kwadratowe ujęte w jednej klasie:

class Pierwiastki
{
private:
int a,b,c;
double x1,x2;
int delta;
public:
Pierwiastki(int a, int b, int c);
void Wypisz();
};
Pierwiastki::Pierwiastki(int x, int y, int z)
{
a=x;
b=y;
c=z;
delta=(b*b)-(4*a*c);
}
void Pierwiastki::Wypisz()
{
if(delta<0)
{
cout<<"Delta<0, brak pieriwastków równania"<<endl;
}
else if(delta>0)
{
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
cout<<"Delta= "<<delta<<" x1 = "<<x1<<" x2  = "<<x2<<endl;
}
else
{
x1=-b/(2*a);
x2=x1;
cout<<"Delta = 0, x1 i x2 = "<<x1<<endl;
}
}
Creating new file with timestamp in class C++.




#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>


using namespace std;

class New_File_with_timestamp
{
private:
time_t time_now;
char *time1;
char *path;
int file;
mode_t authorization;
size_t length;
public:
New_File_with_timestamp(char *tekst)
{
time_now=time(NULL);

authorization=S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
path=tekst;
file=open(path,O_WRONLY | O_EXCL | O_CREAT, authorization);
time1=asctime(localtime(&time_now));
length=strlen(time1);
}
void Write()
{
write(file,time1,length);
close(file);
}
};

int main(int argc, char** argv)
{
New_File_with_timestamp NFWT("my_file_with_stamp");
NFWT.Write();

return 0;
}

wtorek, 2 lutego 2016

Check access files (C++ with class)

#include <iostream>
#include <errno.h>
#include <unistd.h>
using namespace std;

class File_Access
{
private:
char *file;
int x;

public:
File_Access(char *text)
{
file=text;

}
void Exist_Access()
{
x=access(file,F_OK);
if(x==0)
cout<<"File - "<<file<<" exists"<<endl;
else 
{
if(errno==ENOENT)
cout<<"File - "<<file<<" not exit"<<endl;
else if(errno==EACCES)
cout<<"File - "<<file<<"  is not accessbility"<<endl;
}

}
void Write_Access()
{
x=access(file,W_OK);
if(x==0)
cout<<"You can write - "<<file<<endl;
else if (errno==EACCES)
cout<<"You can't write (not accessbility) - "<<file<<endl;
else if (errno==EROFS)
cout<<"You can't write (system readonly) - "<<file<<endl;
}
void Read_Access()
{
x=access(file,R_OK);
if(x==0)
cout<<"You can read - "<<file<<endl;
else
cout<<"You can't read (not accessbility) - "<<file<<endl;
}
};

int main(int argc, char** argv)
{
char my_file[100];
cin>>my_file;
File_Access Check_Access(my_file);
Check_Access.Exist_Access();
Check_Access.Read_Access();
Check_Access.Write_Access();

return 0;
}



poniedziałek, 4 stycznia 2016

Date and time in Visual C++




And code:
#pragma endregion
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
DateTime time1=DateTime::Now;
label1->Text=time1.Hour.ToString("D2")+":"+time1.Minute.ToString("D2")+
":"+time1.Second.ToString("D2");
label2->Text=time1.Year.ToString()+"-"+time1.Month.ToString()+"-"+
time1.Day.ToString()+" --- "+time1.DayOfWeek.ToString();
int second,minutes,hours,months,days;
second=time1.Second;
progressBar1->Value=second;
label6->Text=second.ToString();
minutes=time1.Minute;
progressBar2->Value=minutes;
label7->Text=minutes.ToString();
hours=time1.Hour;
progressBar3->Value=hours;
label8->Text=hours.ToString();
months=time1.Month;
dataGridView1->Rows[months-1]->DefaultCellStyle->BackColor=System::Drawing::Color::Yellow;
String^ day_of_week=time1.DayOfWeek.ToString();
if(day_of_week==Days[0])
dataGridView2->Rows[0]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[1])
dataGridView2->Rows[1]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[2])
dataGridView2->Rows[2]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[3])
dataGridView2->Rows[3]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[4])
dataGridView2->Rows[4]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[5])
dataGridView2->Rows[5]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[6])
dataGridView2->Rows[6]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;


 
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
timer1->Start();
int i;
dataGridView1->ColumnCount=1;
dataGridView1->RowCount=12;
                 array<String^>^ Months=gcnew array<String^>(12);
Months[0]=gcnew String("January");
Months[1]=gcnew String("February");
Months[2]=gcnew String("March");
Months[3]=gcnew String("April");
Months[4]=gcnew String("May");
Months[5]=gcnew String("June");
Months[6]=gcnew String("July");
Months[7]=gcnew String("August");
Months[8]=gcnew String("September");
Months[9]=gcnew String("Oktober");
Months[10]=gcnew String("November");
Months[11]=gcnew String("December");
for(i=0;i<12;i++)
dataGridView1->Rows[i]->Cells[0]->Value=Months[i];
dataGridView2->ColumnCount=1;
dataGridView2->RowCount=7;
Days=gcnew array<String^>(7);
Days[0]=gcnew String("Monday");
Days[1]=gcnew String("Tuesday");
Days[2]=gcnew String("Wednesday");
Days[3]=gcnew String("Thursday");
Days[4]=gcnew String("Friday");
Days[5]=gcnew String("Saturday");
Days[6]=gcnew String("Sunday");
for(i=0;i<7;i++)
dataGridView2->Rows[i]->Cells[0]->Value=Days[i];

}
private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
timer1->Stop();
}
private: System::Void panel1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
}
};

niedziela, 3 stycznia 2016

Firtst numbers and difference

#include <iostream>
using namespace std;

bool my_first(int number)
{
int sum,i;
sum=0;
for(i=1;i<number+1;i++)
if(number%i==0)
 ++sum;
if(sum==2)
 return true;
else
 return false;
}

int main(int argc, char** argv)
{
int x,difference,r;
x=1;
r=0;
for(;;)
{
if(my_first(x))
{
   
   difference=r+1;
cout<<"+"<<difference<<"\n";
cout<<x<<" first"<<endl;
r=0;
}
else
{
++r;
   }
   ++x;
 }
return 0;
}