#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
using namespace std;
class example_MMAP
{
private:
#define width 0x100
void *temp_void;
char *file_name;
int f_file;
public:
example_MMAP(char *file)
{
file_name=file;
f_file=open(file_name,O_RDWR);
lseek(f_file,width+1,SEEK_SET);
write(f_file,"",1);
lseek(f_file,0,SEEK_SET);
temp_void=mmap(0,width,PROT_WRITE,MAP_SHARED,f_file,0);
close(f_file);
}
char *read_time()
{
char *text_time;
struct tm *temp_tm;
struct timeval temp_timeval;
gettimeofday(&temp_timeval,NULL);
temp_tm=localtime(&temp_timeval.tv_sec);
strftime(text_time,sizeof(text_time),"%D-%m-%y %H:%M:%S",temp_tm);
return text_time;
}
void m_write()
{
sprintf((char *)temp_void, "%s\n",read_time());
}
~example_MMAP()
{
munmap(temp_void,width);
}
};
int main(int argc, char **argv)
{
example_MMAP e_M("test_file");
e_M.m_write();
return 0;
}
poniedziałek, 29 stycznia 2018
środa, 24 stycznia 2018
Checking files and sending info to the server (Visual C++)
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::Int32 i;
System::Int32 value;
listBox1->Items->Clear();
listBox2->Items->Clear();
array<Byte>^ c_Buf=gcnew array<Byte>(1024);
array<Byte>^ wind_Buf=gcnew array<Byte>(1024);
StreamWriter^ file_C=gcnew StreamWriter("InfoC.txt",0,System::Text::Encoding::Default);
StreamWriter^ file_Wind=gcnew StreamWriter("InfoWind.txt",0,System::Text::Encoding::Default);
DirectoryInfo^ my_C=gcnew DirectoryInfo("c:");
DirectoryInfo^ my_Wind=gcnew DirectoryInfo("c:\\Windows");
array<FileInfo^>^files_C=my_C->GetFiles("*.*");
array<FileInfo^>^files_Wind=my_Wind->GetFiles("*.*");
for(i=0;i<files_C->Length;i++)
{
listBox1->Items->Add(files_C[i]->Name+"---"+files_C[i]->Length);
}
for(i=0;i<files_Wind->Length;i++)
{
listBox2->Items->Add(files_Wind[i]->Name+"---"+files_Wind[i]->Length);
}
Uri^ u_Adress1=gcnew Uri("ftp://myName.edu.com/InfoC.txt");
Uri^ u_Adress2=gcnew Uri("ftp://myName.edu.com/InfoWind.txt");
FtpWebRequest^ req_C=dynamic_cast<FtpWebRequest^>(WebRequest::Create(u_Adress1));
FtpWebRequest^ req_Wind=dynamic_cast<FtpWebRequest^>(WebRequest::Create(u_Adress2));
req_C->Credentials=gcnew NetworkCredential("anonymus","my_name@.com");
req_Wind->Credentials=gcnew NetworkCredential("anonymus","my_name@.com");
req_C->Method=WebRequestMethods::Ftp::UploadFile;
req_Wind->Method=WebRequestMethods::Ftp::UploadFile;
FileStream^ c_Stream=gcnew FileStream("InfoC.txt",FileMode::Open);
FileStream^ wind_Stream=gcnew FileStream("InfoWind.txt",FileMode::Open);
Stream^ c_St=req_C->GetRequestStream();
Stream^ wind_St=req_Wind->GetRequestStream();
do
{
value=c_Stream->Read(c_Buf,0,1024);
c_St->Write(c_Buf,0,value);
}while(value!=0);
do
{
value=wind_Stream->Read(wind_Buf,0,1024);
wind_St->Write(wind_Buf,0,value);
}while(value!=0);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
};
Using pthread in gcc
touch pthread_example.c
#include <stdio.h>
#include <pthread.h>
#include <math.h>
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return 1;
else
return 0;
}
void *print_First(void *pF)
{
int i=2;
while(1)
{
for(;;)
{
if(is_First(i))
{
printf("%d\n",i);
}
++i;
}
}
return NULL;
}
void *print_Sqrt(void *pS)
{
int i;
i=1;
for(;;)
{
if(is_First(i))
{
printf("%f\n",sqrt(i*1.0));
}
++i;
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t first_Id;
pthread_t first_Sqrt;
pthread_create(&first_Id,NULL,&print_First,NULL);
pthread_create(&first_Sqrt,NULL,&print_Sqrt,NULL);
pthread_join(first_Id,NULL);
pthread_join(first_Sqrt,NULL);
return 0;
}
gcc -o pthread_example pthread_example.c -lpthread -lm
#include <stdio.h>
#include <pthread.h>
#include <math.h>
int is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return 1;
else
return 0;
}
void *print_First(void *pF)
{
int i=2;
while(1)
{
for(;;)
{
if(is_First(i))
{
printf("%d\n",i);
}
++i;
}
}
return NULL;
}
void *print_Sqrt(void *pS)
{
int i;
i=1;
for(;;)
{
if(is_First(i))
{
printf("%f\n",sqrt(i*1.0));
}
++i;
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t first_Id;
pthread_t first_Sqrt;
pthread_create(&first_Id,NULL,&print_First,NULL);
pthread_create(&first_Sqrt,NULL,&print_Sqrt,NULL);
pthread_join(first_Id,NULL);
pthread_join(first_Sqrt,NULL);
return 0;
}
gcc -o pthread_example pthread_example.c -lpthread -lm
wtorek, 23 stycznia 2018
Using malloc and free in structures and calculations - C (gcc)
I want to find such prime numbers where the difference between the previous and the next is equal. Interestingly, this difference is always 6 or 12.
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <fcntl.h> #define max 1000 int is_First(int value) { int i,sum; sum=0; for(i=1;i<value+1;i++) if(value%i==0) ++sum; if(sum==2) return 1; else return 0; } int prev_First(int value) { int i; i=value-1; for(;;) { if(is_First(i)) break; --i; } return i; } int next_First(int value) { int i; i=value+1; for(;;) { if(is_First(i)) break; ++i; } return i; } struct first_Point { int prev; int central; int next; int width1; int width2; }; int *tab_First; int main() { int i,j; tab_First=(int*)malloc(max+1); struct first_Point *f_Point; f_Point=malloc(max*sizeof(*f_Point)); if(tab_First==NULL) exit(1); if(f_Point==NULL) exit(1); i=0; j=3; do { if(is_First(j)) { tab_First[i]=j; f_Point[i].central=j; ++i; } ++j; }while(i<max); for(i=0;i<max;i++) { f_Point[i].prev=prev_First(f_Point[i].central); f_Point[i].next=next_First(f_Point[i].central); f_Point[i].width1=f_Point[i].central-f_Point[i].prev; f_Point[i].width2=f_Point[i].next-f_Point[i].central; } for(i=0;i<max;i++) { if(f_Point[i].width1==f_Point[i].width2) { printf("%d ---- %d ---- %d ---- %d ---- %d\n",f_Point[i].prev,f_Point[i].width1,f_Point[i].central, f_Point[i].width2,f_Point[i].next); } } free(f_Point); free(tab_First); getch(); return 0; }
poniedziałek, 1 stycznia 2018
Write sorted tables to file and StringGrid (C++ Builder)
private: // User declarations
int **tab;
int **first_tab;
int *f_tab;
int X;
int Y;
void __fastcall Sort(int *tab, int size_tab);
bool __fastcall is_First(int value);
void __fastcall init_First();
void __fastcall random_Values();
void __fastcall sorting_Values();
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
int i;
tab=new int *[size];
first_tab=new int *[size];
f_tab=new int[size*size];
for(i=0;i<size;i++)
{
tab[i]=new int[size];
first_tab[i]=new int[size];
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
int i;
for(i=0;i<size;i++)
{
delete [] first_tab[i];
delete [] tab[i];
}
delete [] *first_tab;
delete [] *tab;
delete f_tab;
}
void __fastcall TForm1::Sort(int *tab, int size_tab)
{
int i,j,k;
for(i=1;i<size_tab;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}
bool __fastcall TForm1::is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
void __fastcall TForm1::init_First()
{
int i,j,k;
i=0;
j=2;
do
{
if(is_First(j))
{
f_tab[i]=j;
++i;
}
++j;
}while(i<(size*size));
k=0;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
first_tab[i][j]=f_tab[k];
++k;
}
}
}
void __fastcall TForm1::random_Values()
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
X=rand()%size;
Y=rand()%size;
tab[i][j]=first_tab[X][Y];
}
}
}
void __fastcall TForm1::sorting_Values()
{
int i,j;
for(i=0;i<size;i++)
{
Sort(tab[i],size);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
srand(time(NULL));
init_First();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i,j;
random_Values();
sorting_Values();
for(i=0;i<size;i++)
for(j=0;j<size;j++)
StringGrid1->Cells[i][j]=IntToStr(tab[j][i]);
fstream test_file("test.txt",ios::out);
if(test_file.good())
{
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
test_file<<tab[i][j]<<", ";
test_file.flush();
}
}
}
test_file.close();
}
Subskrybuj:
Posty (Atom)