#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
char *path;
char *text;
int length_text;
int my_socket;
struct sockaddr_un name_value;
void init_Socket(char *p, char *t)
{
path=p;
text=t;
my_socket=socket(PF_LOCAL,SOCK_STREAM,0);
name_value.sun_family=AF_LOCAL;
length_text=strlen(text)+1;
strcpy(name_value.sun_path,path);
}
void write_Socket()
{
connect(my_socket,&name_value,SUN_LEN(&name_value));
write(my_socket,&length_text,sizeof(length_text));
write(my_socket,text,length_text);
close(my_socket);
}
int main(int argc, char **argv)
{
char *path="/home/tmp/text";
char text[128];
printf("Write text - ");
scanf("%s",text);
init_Socket(path,text);
write_Socket();
return 0;
}
niedziela, 30 kwietnia 2017
czwartek, 27 kwietnia 2017
Array, first digit, statistic and StringGrid
private: // User declarations
bool __fastcall is_First(int x);
void __fastcall init_Random();
int **table_A;
int **table_B;
int **table_C;
int **table_D;
int **main_Table;
int *pool_Table;
int result_A;
int result_B;
int result_C;
int result_D;
void __fastcall init_Table();
void __fastcall destroy_Table();
bool __fastcall different_Value(int a, int b, int c, int d);
UnicodeString __fastcall max_Symbol(int a, int b, int c, int d);
void __fastcall prepare_Values(int a, int b, int c, int d);
void __fastcall TForm1::init_Random()
{
srand(time(NULL));
}
bool __fastcall TForm1::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;
}
void __fastcall TForm1::init_Table()
{
int i,j;
table_A=new int*[10];
table_B=new int*[10];
table_C=new int*[10];
table_D=new int*[10];
main_Table=new int*[10];
pool_Table=new int[100];
for(i=0;i<10;i++)
{
table_A[i]=new int[10];
table_B[i]=new int[10];
table_C[i]=new int[10];
table_D[i]=new int[10];
main_Table[i]=new int[10];
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
table_A[i][j]=0;
table_B[i][j]=0;
table_C[i][j]=0;
table_D[i][j]=0;
}
}
i=0;
j=1;
do
{
if(is_First(j))
{
pool_Table[i]=j;
++i;
}
++j;
} while(i<100);
}
void __fastcall TForm1::destroy_Table()
{
int i;
for(i=0;i<10;i++)
{
delete [] table_A[i];
delete [] table_B[i];
delete [] table_C[i];
delete [] table_D[i];
delete [] main_Table[i];
}
delete [] *table_A;
delete [] *table_B;
delete [] *table_C;
delete [] *table_D;
delete [] *main_Table;
delete pool_Table;
}
bool __fastcall TForm1::different_Value(int a, int b, int c, int d)
{
if(a!=b && a!=c && a!=d && b!=c && b!=d && c!=d)
return true;
else
return false;
}
UnicodeString __fastcall TForm1::max_Symbol(int a, int b, int c, int d)
{
if(a>b && a>c && a>d)
return "A";
if(b>a && b>c && b>d)
return "B";
if(c>a && c>b && c>d)
return "C";
if(d>a && d>b && d>c)
return "D";
}
void __fastcall TForm1::prepare_Values(int a, int b, int c, int d)
{
if(a>b && a>c && a>d)
result_A+=3;
if(b>a && b>c && b>d)
result_B+=3;
if(c>a && c>b && c>d)
result_C+=3;
if(d>a && d>b && d>c)
result_D+=3;
if(a>b && a>c && a<d)
result_A+=2;
if(a>b && a>d && a<c)
result_A+=2;
if(a>c && a>d && a<b)
result_A+=2;
if(a<b && a<c && a>d)
result_A+=1;
if(a<b && a<d && a>c)
result_A+=1;
if(a<c && a<d && a>b)
result_A+=1;
if(b>a && b>c && b<d)
result_B+=2;
if(b>a && b>d && b<c)
result_B+=2;
if(b>c && b>d && b<a)
result_B+=2;
if(b<a && b<c && b>d)
result_B+=1;
if(b<a && b<d && b>c)
result_B+=1;
if(b<c && b<c && b>a)
result_B+=1;
if(c>a && c>b && c<d)
result_C+=2;
if(c>a && c>d && c<b)
result_C+=2;
if(c>b && c>d && c<a)
result_C+=2;
if(c<a && c<b && c>d)
result_C+=1;
if(c<a && c<d && c>b)
result_C+=1;
if(c<b && c<d && c>a)
result_C+=1;
if(d>a && d>b && d<c)
result_D+=2;
if(d>a && d>c && d<b)
result_D+=2;
if(d>b && d>c && d<a)
result_D+=2;
if(d<a && d<b && d>c)
result_D+=1;
if(d<a && d<c && d>b)
result_D+=1;
if(d<b && d<c && d>a)
result_D+=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
init_Random();
init_Table();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
int i,j,k;
result_A=result_B=result_C=result_D=0;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
;
while(!different_Value(table_A[i][j],table_B[i][j],table_C[i][j],table_D[i][j]))
{
k=rand()%100;
table_A[i][j]=pool_Table[k];
k=rand()%100;
table_B[i][j]=pool_Table[k];
k=rand()%100;
table_C[i][j]=pool_Table[k];
k=rand()%100;
table_D[i][j]=pool_Table[k];
}
StringGrid1->Cells[i][j]=IntToStr(table_A[i][j]);
StringGrid2->Cells[i][j]=IntToStr(table_B[i][j]);
StringGrid3->Cells[i][j]=IntToStr(table_C[i][j]);
StringGrid4->Cells[i][j]=IntToStr(table_D[i][j]);
StringGrid5->Cells[i][j]=max_Symbol(table_A[i][j],table_B[i][j],
table_C[i][j],table_D[i][j]);
prepare_Values(table_A[i][j],table_B[i][j],table_C[i][j],table_D[i][j]);
}
}
Edit1->Text=IntToStr(result_A);
Edit2->Text=IntToStr(result_B);
Edit3->Text=IntToStr(result_C);
Edit4->Text=IntToStr(result_D);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
destroy_Table();
Close();
}
niedziela, 23 kwietnia 2017
C++ - set (class)
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
class SET
{
private:
char *ascii_TABLE;
public:
SET()
{
ascii_TABLE=new char[256];
}
~SET()
{
delete ascii_TABLE;
}
void init_TABLE()
{
int i;
for(i=0;i<256;i++)
ascii_TABLE[i]=0;
}
SET &operator +(char x)
{
ascii_TABLE[x]=1;
return *this;
}
SET &operator -(char x)
{
ascii_TABLE[x]=0;
return *this;
}
bool is_in_TABLE(char x)
{
if(ascii_TABLE[x]==1)
return true;
else
return false;
}
SET &Connect(SET Next)
{
int i;
for(i=0;i<256;i++)
if(Next.is_in_TABLE(i))
ascii_TABLE[i]=1;
return *this;
}
void Write_SET()
{
int i;
for(i=0;i<256;i++)
if(ascii_TABLE[i]==1)
printf("%c",(char)i);
if(i==0)
printf("Is empty\n");
printf("\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
SET new_Set1,new_Set2;
new_Set1.init_TABLE();
new_Set2.init_TABLE();
new_Set1=new_Set1+'N';
new_Set1=new_Set1+'e';
new_Set1=new_Set1+'w';
new_Set2=new_Set2+'Y';
new_Set2=new_Set2+'o';
new_Set2=new_Set2+'r';
new_Set2=new_Set2+'k';
new_Set1.Write_SET();
new_Set2.Write_SET();
new_Set1.Connect(new_Set2);
new_Set1.Write_SET();
new_Set1=new_Set1-'N';
new_Set1=new_Set1-'e';
new_Set1=new_Set1-'w';
new_Set1.Write_SET();
getch();
return 0;
}
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
class SET
{
private:
char *ascii_TABLE;
public:
SET()
{
ascii_TABLE=new char[256];
}
~SET()
{
delete ascii_TABLE;
}
void init_TABLE()
{
int i;
for(i=0;i<256;i++)
ascii_TABLE[i]=0;
}
SET &operator +(char x)
{
ascii_TABLE[x]=1;
return *this;
}
SET &operator -(char x)
{
ascii_TABLE[x]=0;
return *this;
}
bool is_in_TABLE(char x)
{
if(ascii_TABLE[x]==1)
return true;
else
return false;
}
SET &Connect(SET Next)
{
int i;
for(i=0;i<256;i++)
if(Next.is_in_TABLE(i))
ascii_TABLE[i]=1;
return *this;
}
void Write_SET()
{
int i;
for(i=0;i<256;i++)
if(ascii_TABLE[i]==1)
printf("%c",(char)i);
if(i==0)
printf("Is empty\n");
printf("\n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
SET new_Set1,new_Set2;
new_Set1.init_TABLE();
new_Set2.init_TABLE();
new_Set1=new_Set1+'N';
new_Set1=new_Set1+'e';
new_Set1=new_Set1+'w';
new_Set2=new_Set2+'Y';
new_Set2=new_Set2+'o';
new_Set2=new_Set2+'r';
new_Set2=new_Set2+'k';
new_Set1.Write_SET();
new_Set2.Write_SET();
new_Set1.Connect(new_Set2);
new_Set1.Write_SET();
new_Set1=new_Set1-'N';
new_Set1=new_Set1-'e';
new_Set1=new_Set1-'w';
new_Set1.Write_SET();
getch();
return 0;
}
wtorek, 18 kwietnia 2017
class access file (linux)
#include <iostream>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
class access_File
{
private:
char *my_path;
int amode[3];
int test_value;
public:
access_File(char *name)
{
my_path=name;
amode[0]=F_OK;
amode[1]=W_OK;
amode[2]=R_OK;
}
void is_Exist()
{
test_value=access(my_path,amode[0]);
if(test_value==0)
cout<<my_path<<" exists"<<endl;
else
{
if(errno==ENOENT)
cout<<my_path<<" no exist"<<endl;
else if(errno==EACCES)
cout<<my_path<<" is no access"<<endl;
}
}
void is_Write()
{
test_value=access(my_path,amode[1]);
if(test_value==0)
cout<<my_path<<" you can write "<<endl;
else if (errno==EACCES)
cout<<my_path<<" you can't write (not access) "<<endl;
else if (errno==EROFS)
cout<<my_path<<" you can't write (read-only system) "<<endl;
}
void is_Read()
{
test_value=access(my_path,amode[2]);
if(test_value==0)
cout<<my_path<<" you can read "<<endl;
else
cout<<my_path<<" tou can't read (not access) "<<endl;
}
};
int main(int argc, char **argv)
{
access_File File(argv[1]);
File.is_Exist();
File.is_Read();
File.is_Write();
return 0;
}
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
using namespace std;
class access_File
{
private:
char *my_path;
int amode[3];
int test_value;
public:
access_File(char *name)
{
my_path=name;
amode[0]=F_OK;
amode[1]=W_OK;
amode[2]=R_OK;
}
void is_Exist()
{
test_value=access(my_path,amode[0]);
if(test_value==0)
cout<<my_path<<" exists"<<endl;
else
{
if(errno==ENOENT)
cout<<my_path<<" no exist"<<endl;
else if(errno==EACCES)
cout<<my_path<<" is no access"<<endl;
}
}
void is_Write()
{
test_value=access(my_path,amode[1]);
if(test_value==0)
cout<<my_path<<" you can write "<<endl;
else if (errno==EACCES)
cout<<my_path<<" you can't write (not access) "<<endl;
else if (errno==EROFS)
cout<<my_path<<" you can't write (read-only system) "<<endl;
}
void is_Read()
{
test_value=access(my_path,amode[2]);
if(test_value==0)
cout<<my_path<<" you can read "<<endl;
else
cout<<my_path<<" tou can't read (not access) "<<endl;
}
};
int main(int argc, char **argv)
{
access_File File(argv[1]);
File.is_Exist();
File.is_Read();
File.is_Write();
return 0;
}
piątek, 14 kwietnia 2017
wchar_t, wcslen, algorithm Robin-Karp C++
bool TForm1::is_True(int x1,int y1,int x2,int y2)
{
if((x1<y1) && (x2<y2))
return true;
else
return false;
}
wchar_t TForm1::wchr_to_int(wchar_t symbol)
{
if(islower(symbol))
return symbol-'a'+1;
else
return symbol-'A'+27;
}
int TForm1::how_many_Phrase(wchar_t *text1, wchar_t *text2)
{
int i,j,size_text1,size_text2;
int value;
size_text1=wcslen(text1);
size_text2=wcslen(text2);
i=j=0;
while(is_True(j,size_text1,i,size_text2))
{
if(text2[i]!=text1[j])
{
i-=j-1;
j=-1;
}
j++;
i++;
}
if(j==size_text1)
value=(i-size_text1);
else
value=-1;
return value;
}
int TForm1::size_rk(wchar_t *text1, wchar_t *text2)
{
int size_text1,size_text2;
size_text1=wcslen(text1);
size_text2=wcslen(text2);
int i;
unsigned long value_function1,value_function2,value_function3;
value_function1=value_function2=0;
value_function3=1;
for(i=0;i<size_text1;i++)
{
value_function1=(value_function1*32+wchr_to_int(text1[i]))% 70051;
value_function2=(value_function2*32+wchr_to_int(text2[i]))% 70051;
}
for(i=1;i<size_text1;i++)
value_function3=(value_function3*32)%70051;
for(i=0;value_function1!=value_function2;i++)
{
value_function2=(value_function2+32*70051-wchr_to_int(text2[i])*value_function3)%70051;
value_function2=(value_function2*32+wchr_to_int(text2[i+size_text1]))%70051;
if(i>size_text2-size_text1)
return -1;
}
return i;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
wchar_t **m_text1=new wchar_t*[14];
wchar_t **m_text2=new wchar_t*[14];
int value[14];
int i;
for(i=0;i<14;i++)
{
m_text1[i]=new wchar_t[64];
m_text2[i]=new wchar_t[64];
}
UnicodeString w_text1[14]={"telphon","tablebooktablepen",
"potatoes","Cualarumpur","Anthropology","Sociocultural","international",
"Characterisation","possession","nothing","capocannoniere","cicomjekocmsckocomcomsocm",
"010201012011010343210101010101010","gold"};
UnicodeString w_text2[14]={"el","bo","to","al","po","oc","na","ar","ss","xxx",
"oca","com","10","gold"};
for(i=0;i<14;i++)
{
ListBox1->Items->Add(w_text1[i]);
ListBox2->Items->Add(w_text2[i]);
m_text1[i]=w_text1[i].w_str();
m_text2[i]=w_text2[i].w_str();
value[i]=how_many_Phrase(m_text2[i],m_text1[i]);
ListBox3->Items->Add(IntToStr(value[i]));
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
wchar_t **m_text1=new wchar_t*[14];
wchar_t **m_text2=new wchar_t*[14];
int value[14];
int i;
for(i=0;i<14;i++)
{
m_text1[i]=new wchar_t[64];
m_text2[i]=new wchar_t[64];
}
UnicodeString w_text1[14]={"telphon","penbooktablebook",
"potatoes","Cualarumpur","Anthropology","Sociocultural","international",
"Characterisation","possession","nothing","capocannoniere","cicomjekocmsckocomcomsocm",
"010201012011010343210101010101010","gold"};
UnicodeString w_text2[14]={"el","bo","to","al","po","oc","na","ar","ss","xxx",
"oca","com","10","old"};
for(i=0;i<14;i++)
{
ListBox1->Items->Add(w_text1[i]);
ListBox2->Items->Add(w_text2[i]);
m_text1[i]=w_text1[i].w_str();
m_text2[i]=w_text2[i].w_str();
value[i]=size_rk(m_text2[i],m_text1[i]);
ListBox3->Items->Add(IntToStr(value[i]));
}
}
czwartek, 6 kwietnia 2017
Mmap and munmap (gcc Linux)
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#define max_size 100
int *first_Table;
void *test_memory;
void init_Random()
{
srand(time(NULL));
}
int 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 EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
void init_Table()
{
int i,j;
first_Table=malloc(max_size);
i=0;
j=2;
do
{
if(is_First(j))
{
first_Table[i]=j;
++i;
}
++j;
}while(i<max_size);
}
int random_from_Table()
{
int value;
value=rand()%max_size;
return first_Table[value];
}
void delete_Table()
{
free(first_Table);
}
int file;
void open_File(char *name)
{
file=open(name,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
}
void lseek_File()
{
lseek(file,0x200,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
test_memory=mmap(0,0x200,PROT_WRITE,MAP_SHARED,file,0);
}
void write_Memory()
{
close(file);
int i;
i=random_from_Table();
sprintf((char*)test_memory,"%d\n",i);
}
int main(int argc, char **argv)
{
init_Random();
init_Table();
char file_name[20];
scanf("%s",file_name);
open_File(file_name);
lseek_File();
write_Memory();
munmap(test_memory,0x200);
delete_Table();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#define max_size 100
int *first_Table;
void *test_memory;
void init_Random()
{
srand(time(NULL));
}
int 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 EXIT_SUCCESS;
else
return EXIT_FAILURE;
}
void init_Table()
{
int i,j;
first_Table=malloc(max_size);
i=0;
j=2;
do
{
if(is_First(j))
{
first_Table[i]=j;
++i;
}
++j;
}while(i<max_size);
}
int random_from_Table()
{
int value;
value=rand()%max_size;
return first_Table[value];
}
void delete_Table()
{
free(first_Table);
}
int file;
void open_File(char *name)
{
file=open(name,O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
}
void lseek_File()
{
lseek(file,0x200,SEEK_SET);
write(file,"",1);
lseek(file,0,SEEK_SET);
test_memory=mmap(0,0x200,PROT_WRITE,MAP_SHARED,file,0);
}
void write_Memory()
{
close(file);
int i;
i=random_from_Table();
sprintf((char*)test_memory,"%d\n",i);
}
int main(int argc, char **argv)
{
init_Random();
init_Table();
char file_name[20];
scanf("%s",file_name);
open_File(file_name);
lseek_File();
write_Memory();
munmap(test_memory,0x200);
delete_Table();
return 0;
}
poniedziałek, 3 kwietnia 2017
Class my_Path (C++linux)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
using namespace std;
class my_Path
{
private:
char *symbolic_ln;
char my_buffer[PATH_MAX];
char *last_symbol;
char symbol;
size_t size;
public:
my_Path();
size_t size_Path();
void write_Path();
};
my_Path::my_Path()
{
symbolic_ln="/proc/self/exe";
size=sizeof(my_buffer);
symbol='\0';
}
size_t my_Path::size_Path()
{
if(readlink(symbolic_ln,my_buffer,size)<=0)
return -1;
last_symbol=strrchr(my_buffer,'/');
if(last_symbol==NULL)
return -1;
last_symbol++;
*last_symbol=symbol;
return (size_t) (last_symbol-my_buffer);
}
void my_Path::write_Path()
{
size_Path();
cout<<"Application path: "<<my_buffer<<endl;
}
int main(int argc, char **argv)
{
my_Path now_Path;
now_Path.size_Path();
now_Path.write_Path();
return 0;
}
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
using namespace std;
class my_Path
{
private:
char *symbolic_ln;
char my_buffer[PATH_MAX];
char *last_symbol;
char symbol;
size_t size;
public:
my_Path();
size_t size_Path();
void write_Path();
};
my_Path::my_Path()
{
symbolic_ln="/proc/self/exe";
size=sizeof(my_buffer);
symbol='\0';
}
size_t my_Path::size_Path()
{
if(readlink(symbolic_ln,my_buffer,size)<=0)
return -1;
last_symbol=strrchr(my_buffer,'/');
if(last_symbol==NULL)
return -1;
last_symbol++;
*last_symbol=symbol;
return (size_t) (last_symbol-my_buffer);
}
void my_Path::write_Path()
{
size_Path();
cout<<"Application path: "<<my_buffer<<endl;
}
int main(int argc, char **argv)
{
my_Path now_Path;
now_Path.size_Path();
now_Path.write_Path();
return 0;
}
Subskrybuj:
Posty (Atom)