#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
//---------------------------------------------------------------------------
#define max_value 100
using namespace std;
void init_Random()
{
srand(time(NULL));
}
typedef struct temporary_next
{
temporary_next *next;
int value_str;
}position;
bool is_First(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
int *first_digit_Array;
class list_Direction
{
private:
typedef struct
{
position *start;
position *end;
}show;
show info_show;
public:
list_Direction()
{
info_show.start=info_show.end=NULL;
}
void plus_next(int value)
{
position *new_pos;
new_pos=new position;
new_pos->value_str=value;
new_pos->next=NULL;
if(info_show.start==NULL)
info_show.start=info_show.end=new_pos;
else
{
(info_show.end)->next=new_pos;
info_show.end=new_pos;
}
}
list_Direction& operator --()
{
if(info_show.start==info_show.end)
{
delete info_show.start;
info_show.start=info_show.end=NULL;
}
else
{
position *temp_position=info_show.start;
while((temp_position->next)!=info_show.end)
info_show.end=temp_position;
delete temp_position->next;
temp_position->next=NULL;
}
return (*this);
}
list_Direction friend& operator +(list_Direction &A, list_Direction &B)
{
list_Direction *temp_List=new list_Direction;
position *C=(A.info_show).start;
position *D=(B.info_show).start;
do
{
temp_List->plus_next(C->value_str);
C=C->next;
}while(C!=NULL);
do
{
temp_List->plus_next(D->value_str);
D=D->next;
}while(D!=NULL);
return (*temp_List);
}
bool is_Empty()
{
if(info_show.start==NULL)
return true;
else
return false;
}
void write_Elements()
{
position *temp_pos=info_show.start;
while(temp_pos!=NULL)
{
printf("%d ",temp_pos->value_str);
temp_pos=temp_pos->next;
printf("\n");
}
printf("\n");
}
~list_Direction()
{
while(!is_Empty())
(*this)--;
;
}
};
int main(int argc, char* argv[])
{
first_digit_Array=new int[max_value];
int i,j;
i=0;
j=2;
do
{
if(is_First(j))
{
first_digit_Array[i]=j;
++i;
}
++j;
}while(i<max_value);
list_Direction X,Y,Z;
for(i=0;i<max_value;i++)
{
X.plus_next(first_digit_Array[rand()%max_value]);
Y.plus_next(first_digit_Array[rand()%max_value]);
}
Z=X+Y;
Z.write_Elements();
delete first_digit_Array;
getch();
return 0;
}
środa, 14 czerwca 2017
wtorek, 13 czerwca 2017
Encoding data from a file (linux/g++)
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define path_file "/proc/cpuinfo"
#define new_path_file "cpuinfo_coding"
using namespace std;
void init_Random()
{
srand(time(NULL));
}
char int_to_Ascii(int x)
{
return char(x);
}
void double_Code(char *test_file)
{
int value_ascii,i;
char character;
value_ascii=rand()%255;
character=int_to_Ascii(value_ascii);
for(i=0;test_file[i]!=NULL;i++)
test_file[i]=test_file[i]^character;
for(i=0;test_file[i]!=NULL;i++)
test_file[i]=255-test_file[i];
}
int main(int argc, char **argv)
{
init_Random();
char *temporary_bufor;
temporary_bufor=new char[2048];
std::fstream test_file( path_file, std::ios::in );
test_file.read(temporary_bufor,2048);
test_file.close();
cout<<"Before coding:\n";
cout<<temporary_bufor<<endl;
double_Code(temporary_bufor);
cout<<"\n\nAfter coding:\n\n";
cout<<temporary_bufor<<endl;
int new_file=open(new_path_file,O_WRONLY | O_CREAT | O_APPEND, 0666);
size_t length_file=strlen(temporary_bufor);
write(new_file,temporary_bufor,length_file);
close(new_file);
delete temporary_bufor;
return 0;
}
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define path_file "/proc/cpuinfo"
#define new_path_file "cpuinfo_coding"
using namespace std;
void init_Random()
{
srand(time(NULL));
}
char int_to_Ascii(int x)
{
return char(x);
}
void double_Code(char *test_file)
{
int value_ascii,i;
char character;
value_ascii=rand()%255;
character=int_to_Ascii(value_ascii);
for(i=0;test_file[i]!=NULL;i++)
test_file[i]=test_file[i]^character;
for(i=0;test_file[i]!=NULL;i++)
test_file[i]=255-test_file[i];
}
int main(int argc, char **argv)
{
init_Random();
char *temporary_bufor;
temporary_bufor=new char[2048];
std::fstream test_file( path_file, std::ios::in );
test_file.read(temporary_bufor,2048);
test_file.close();
cout<<"Before coding:\n";
cout<<temporary_bufor<<endl;
double_Code(temporary_bufor);
cout<<"\n\nAfter coding:\n\n";
cout<<temporary_bufor<<endl;
int new_file=open(new_path_file,O_WRONLY | O_CREAT | O_APPEND, 0666);
size_t length_file=strlen(temporary_bufor);
write(new_file,temporary_bufor,length_file);
close(new_file);
delete temporary_bufor;
return 0;
}
piątek, 2 czerwca 2017
Classes with file creation and adding time values (linux - g++)
#include <iostream>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
using namespace std;
class now_Time
{
private:
time_t my_time;
char *now_time;
public:
now_Time()
{
my_time=time(NULL);
now_time=asctime(localtime(&my_time));
}
char *give_Time()
{
return now_time;
}
};
class uptime_Time
{
private:
FILE *uptime_file;
char *path;
double real_time;
double free_time;
public:
uptime_Time()
{
path="/proc/uptime";
uptime_file=fopen(path,"r");
fscanf(uptime_file,"%1f %1f\n",&real_time,&free_time);
}
int give_real_Time()
{
return (int) real_time;
}
int give_free_Time()
{
return (int) free_time;
}
};
class new_file_with_Infos
{
private:
char *name_file;
size_t size_class_time;
size_t size_free_time;
size_t size_real_time;
int file;
mode_t mode_value;
public:
new_file_with_Infos(char *name)
{
name_file=name;
mode_value=S_IRUSR | S_IROTH | S_IWGRP | S_IRGRP | S_IWUSR;
file=open(name_file, O_WRONLY | O_EXCL | O_CREAT, mode_value);
now_Time nT;
uptime_Time uT;
size_class_time=strlen(nT.give_Time());
char *temp_free_time;
char *temp_real_time;
snprintf(temp_free_time,sizeof(temp_free_time),"%d",uT.give_free_Time());
size_free_time=strlen(temp_free_time);
snprintf(temp_real_time,sizeof(temp_real_time),"%d",uT.give_real_Time());
size_real_time=strlen(temp_real_time);
write(file,nT.give_Time(),size_class_time);
write(file,temp_free_time,size_free_time);
write(file,temp_real_time,size_real_time);
close(file);
}
};
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
using namespace std;
class now_Time
{
private:
time_t my_time;
char *now_time;
public:
now_Time()
{
my_time=time(NULL);
now_time=asctime(localtime(&my_time));
}
char *give_Time()
{
return now_time;
}
};
class uptime_Time
{
private:
FILE *uptime_file;
char *path;
double real_time;
double free_time;
public:
uptime_Time()
{
path="/proc/uptime";
uptime_file=fopen(path,"r");
fscanf(uptime_file,"%1f %1f\n",&real_time,&free_time);
}
int give_real_Time()
{
return (int) real_time;
}
int give_free_Time()
{
return (int) free_time;
}
};
class new_file_with_Infos
{
private:
char *name_file;
size_t size_class_time;
size_t size_free_time;
size_t size_real_time;
int file;
mode_t mode_value;
public:
new_file_with_Infos(char *name)
{
name_file=name;
mode_value=S_IRUSR | S_IROTH | S_IWGRP | S_IRGRP | S_IWUSR;
file=open(name_file, O_WRONLY | O_EXCL | O_CREAT, mode_value);
now_Time nT;
uptime_Time uT;
size_class_time=strlen(nT.give_Time());
char *temp_free_time;
char *temp_real_time;
snprintf(temp_free_time,sizeof(temp_free_time),"%d",uT.give_free_Time());
size_free_time=strlen(temp_free_time);
snprintf(temp_real_time,sizeof(temp_real_time),"%d",uT.give_real_Time());
size_real_time=strlen(temp_real_time);
write(file,nT.give_Time(),size_class_time);
write(file,temp_free_time,size_free_time);
write(file,temp_real_time,size_real_time);
close(file);
}
};
czwartek, 1 czerwca 2017
Two useful classes for linux (g++)
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
using namespace std;
class copy_FILES
{
private:
struct stat st_value;
int file_begin;
int file_end;
char *File1;
char *File2;
off_t off_value;
public:
copy_FILES(char *f1, char *f2)
{
off_value=0;
File1=f1;
File2=f2;
file_begin=open(File1,O_RDONLY);
fstat(file_begin,&st_value);
file_end=open(File2,O_WRONLY | O_CREAT,st_value.st_mode);
sendfile(file_end,file_begin,&off_value,st_value.st_size);
close (file_begin);
close (file_end);
}
};
class show_Directory
{
private:
size_t path_size;
DIR *directory;
char *path_name;
struct dirent *dirent_value;
char path_max[PATH_MAX+1];
public:
show_Directory(char *path_directory)
{
char i='/';
char j='\0';
path_name=path_directory;
strncpy(path_max,path_name,sizeof(path_max));
path_size=strlen(path_name);
if(path_max[path_size-1]!=i)
{
path_max[path_size]=i;
path_max[path_size+1]=j;
++path_size;
}
}
void Write()
{
directory=opendir(path_name);
while((dirent_value=readdir(directory))!=NULL)
{
strncpy(path_max+path_size,dirent_value->d_name,
sizeof(path_max)-path_size);
cout<<path_max<<endl;
}
closedir(directory);
}
};
int main(int argc, char **argv)
{
show_Directory sD("/proc/");
sD.Write();
copy_FILES cF("/proc/cpuinfo","my_new_file_cpu");
return 0;
}
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
using namespace std;
class copy_FILES
{
private:
struct stat st_value;
int file_begin;
int file_end;
char *File1;
char *File2;
off_t off_value;
public:
copy_FILES(char *f1, char *f2)
{
off_value=0;
File1=f1;
File2=f2;
file_begin=open(File1,O_RDONLY);
fstat(file_begin,&st_value);
file_end=open(File2,O_WRONLY | O_CREAT,st_value.st_mode);
sendfile(file_end,file_begin,&off_value,st_value.st_size);
close (file_begin);
close (file_end);
}
};
class show_Directory
{
private:
size_t path_size;
DIR *directory;
char *path_name;
struct dirent *dirent_value;
char path_max[PATH_MAX+1];
public:
show_Directory(char *path_directory)
{
char i='/';
char j='\0';
path_name=path_directory;
strncpy(path_max,path_name,sizeof(path_max));
path_size=strlen(path_name);
if(path_max[path_size-1]!=i)
{
path_max[path_size]=i;
path_max[path_size+1]=j;
++path_size;
}
}
void Write()
{
directory=opendir(path_name);
while((dirent_value=readdir(directory))!=NULL)
{
strncpy(path_max+path_size,dirent_value->d_name,
sizeof(path_max)-path_size);
cout<<path_max<<endl;
}
closedir(directory);
}
};
int main(int argc, char **argv)
{
show_Directory sD("/proc/");
sD.Write();
copy_FILES cF("/proc/cpuinfo","my_new_file_cpu");
return 0;
}
niedziela, 28 maja 2017
The Game of LIFE (Visual C++)
#pragma endregion
private:
System::Void init_Random()
{
srand(time(NULL));
}
private:
array<System::Int64,2>^ game_table;
private:
System::Void init_Table()
{
game_table=gcnew array<System::Int64,2>(30,30);
System::Int64 i;
System::Int64 j;
for(i=0;i<30;i++)
for(j=0;j<30;j++)
game_table[i,j]=0;
}
private:
System::Void change_Table()
{
System::Int64 i;
System::Int64 j;
System::Int64 k;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
k=rand()%10;
if(k>6)
game_table[i,j]=1;
}
}
}
private:
System::Boolean in_Area(System::Int64 x, System::Int64 y)
{
if((x>=0) && (x<30) && (y>=0) && (y<30))
return true;
else
return false;
}
private:
System::Void step_GridView()
{
System::Int64 i;
System::Int64 j;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
if(game_table[i,j]==0)
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Yellow;
else
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Black;
}
}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
init_Random();
init_Table();
change_Table();
System::Int64 i;
System::Int64 j;
dataGridView1->ColumnCount=30;
dataGridView1->RowCount=30;
dataGridView1->AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode::Fill;
dataGridView1->ColumnHeadersVisible=false;
dataGridView1->RowHeadersVisible=false;
dataGridView1->AllowUserToResizeRows=false;
dataGridView1->AllowUserToResizeColumns=false;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
if(game_table[i,j]==0)
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Yellow;
else
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Black;
}
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::Int64 i;
System::Int64 j;
System::Int64 k;
System::Int64 l;
System::Int64 value;
value=0;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
for(k=i-1;k<i+2;k++)
{
for(l=j-1;l<j+2;l++)
{
if(in_Area(k,l))
{
if(game_table[k,l]==0)
if(!((l==j)&&(k==i)))
value++;
}
}
}
game_table[i,j]=1;
if(value==3)
game_table[i,j]=0;
if((value==2) && (dataGridView1->Rows[i]->Cells[j]->Style->BackColor==System::Drawing::Color::Yellow))
game_table[i,j]=0;
value=0;
}
}
step_GridView();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
};
}
środa, 3 maja 2017
Sending files via FTP to the server (Visual C++)
I want to check my Windows files and information to send to the company server.
I need includes:
using namespace System::IO;
using namespace System::Net;
And code:
#pragma endregion
array<String^>^ files_Windows;
String^ address_ftp;
Uri^ send_address;
array<Byte>^ table;
String^ path;
int size;
Stream^ f_stream;
FtpWebRequest ^send_FTP;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
int i;
address_ftp="ftp.company.com";
table=gcnew array<Byte>(2048);
path="info.txt";
files_Windows=Directory::GetFiles("c:\\Windows","*.*",System::IO::SearchOption::TopDirectoryOnly);
size=files_Windows->Length;
for(i=0;i<size;i++)
listBox1->Items->Add("( "+files_Windows[i]+" )");
;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
StreamWriter^ my_Stream=gcnew StreamWriter(path);
int i,length_bytes;
for(i=0;i<size;i++)
my_Stream->WriteLine(files_Windows[i]);
send_address=gcnew Uri("ftp://"+address_ftp);
send_FTP=dynamic_cast<FtpWebRequest^>(WebRequest::Create(send_address));
send_FTP->Credentials=gcnew NetworkCredential("employee_name","employee@company.com");
send_FTP->Method+WebRequestMethods::Ftp::UploadFile;
FileStream^ use_file=gcnew FileStream(path,FileMode::Open);
f_stream=send_FTP->GetRequestStream();
do
{
length_bytes=use_file->Read(table,0,2048);
f_stream->Write(table,0,length_bytes);
}while(length_bytes!=0);
f_stream->Close();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
};
}
poniedziałek, 1 maja 2017
Cuboid (Visual C++)
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Graphics^ g=panel1->CreateGraphics();
Pen^ pen1=gcnew Pen(System::Drawing::Color::Brown);
Pen^ pen2=gcnew Pen(System::Drawing::Color::Green);
Pen^ pen3=gcnew Pen(System::Drawing::Color::Blue);
Pen^ pen4=gcnew Pen(System::Drawing::Color::Black);
pen1->Width=3;
pen2->Width=3;
pen3->Width=3;
pen4->Width=4;
pen4->DashStyle=System::Drawing::Drawing2D::DashStyle::Dot;
int width,height,centr_x,centr_y,down_margin,down_margin2,line1=400,A,B,C,D,A1,B1,C1,D1;
width=panel1->Width;
height=panel1->Height;
down_margin=height-5;
centr_x=width/2;
centr_y=height/2;
A=centr_x-(line1/2);
B=centr_x+(line1/2);
g->DrawLine(pen1,A,down_margin,B,down_margin);
C=A+150;
D=B+150;
down_margin2=down_margin-150;
g->DrawLine(pen1,C,down_margin2,D,down_margin2);
g->DrawLine(pen1,A,down_margin,C,down_margin2);
g->DrawLine(pen1,B,down_margin,D,down_margin2);
g->DrawLine(pen4,A,down_margin,D,down_margin2);
A1=down_margin-400;
B1=down_margin-400;
C1=down_margin2-400;
D1=down_margin2-400;
g->DrawLine(pen2,A,down_margin,A,A1);
g->DrawLine(pen2,B,down_margin,B,B1);
g->DrawLine(pen2,C,down_margin2,C,C1);
g->DrawLine(pen2,D,down_margin2,D,D1);
g->DrawLine(pen4,D,down_margin2,C,C1);
g->DrawLine(pen4,C,C1,A,down_margin);
g->DrawLine(pen3,A,A1,B,B1);
g->DrawLine(pen3,C,C1,D,D1);
g->DrawLine(pen3,A,A1,C,C1);
g->DrawLine(pen3,B,B1,D,D1);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
};
}
Subskrybuj:
Posty (Atom)


