ś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();
}
sobota, 23 grudnia 2017
Status CD Drive (class C++ Builder)
#include <mmsystem.h>
#include <ExtCtrls.hpp>
#include <FileCtrl.hpp>
class state_CD
{
private:
MCI_OPEN_PARMS cd_Parameters;
MCI_STATUS_PARMS cd_State;
int sent_Value;
UnicodeString cD;
public:
state_CD()
{
cD="CDAudio";
cd_Parameters.dwCallback=0;
cd_Parameters.lpstrDeviceType=cD.w_str();
}
int read_Value(UnicodeString type_CD)
{
cd_Parameters.lpstrElementName=type_CD.w_str();
mciSendCommand(0,MCI_OPEN,MCI_OPEN_ELEMENT | MCI_OPEN_TYPE,(long)&cd_Parameters);
cd_State.dwItem=MCI_STATUS_MODE;
mciSendCommand(cd_Parameters.wDeviceID,MCI_STATUS,MCI_WAIT | MCI_STATUS_ITEM,(long)&cd_State);
sent_Value=cd_State.dwReturn;
mciSendCommand(cd_Parameters.wDeviceID,MCI_CLOSE,MCI_NOTIFY,(long)&cd_Parameters);
return sent_Value;
}
void write_ListBox(TDriveComboBox *tDCB,TListBox *tLB)
{
UnicodeString test_Value;
test_Value=UnicodeString(tDCB->Drive+":");
if(read_Value(test_Value)==526)
tLB->Items->Add("Playing");
else if(read_Value(test_Value)==525)
tLB->Items->Add("Pause");
else if(read_Value(test_Value)==530)
tLB->Items->Add("No CD");
else if(read_Value(test_Value)==1242732)
tLB->Items->Add("No CD Drive");
else if(read_Value(test_Value)==1242348)
tLB->Items->Add("Symbol is not CD Drive");
else
tLB->Items->Add("Default operation: "+IntToStr(read_Value((UnicodeString)tDCB->Drive+":")));
}
};
And example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
state_CD test_CD;
test_CD.write_ListBox(DriveComboBox1,ListBox1);
}
#include <ExtCtrls.hpp>
#include <FileCtrl.hpp>
class state_CD
{
private:
MCI_OPEN_PARMS cd_Parameters;
MCI_STATUS_PARMS cd_State;
int sent_Value;
UnicodeString cD;
public:
state_CD()
{
cD="CDAudio";
cd_Parameters.dwCallback=0;
cd_Parameters.lpstrDeviceType=cD.w_str();
}
int read_Value(UnicodeString type_CD)
{
cd_Parameters.lpstrElementName=type_CD.w_str();
mciSendCommand(0,MCI_OPEN,MCI_OPEN_ELEMENT | MCI_OPEN_TYPE,(long)&cd_Parameters);
cd_State.dwItem=MCI_STATUS_MODE;
mciSendCommand(cd_Parameters.wDeviceID,MCI_STATUS,MCI_WAIT | MCI_STATUS_ITEM,(long)&cd_State);
sent_Value=cd_State.dwReturn;
mciSendCommand(cd_Parameters.wDeviceID,MCI_CLOSE,MCI_NOTIFY,(long)&cd_Parameters);
return sent_Value;
}
void write_ListBox(TDriveComboBox *tDCB,TListBox *tLB)
{
UnicodeString test_Value;
test_Value=UnicodeString(tDCB->Drive+":");
if(read_Value(test_Value)==526)
tLB->Items->Add("Playing");
else if(read_Value(test_Value)==525)
tLB->Items->Add("Pause");
else if(read_Value(test_Value)==530)
tLB->Items->Add("No CD");
else if(read_Value(test_Value)==1242732)
tLB->Items->Add("No CD Drive");
else if(read_Value(test_Value)==1242348)
tLB->Items->Add("Symbol is not CD Drive");
else
tLB->Items->Add("Default operation: "+IntToStr(read_Value((UnicodeString)tDCB->Drive+":")));
}
};
And example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
state_CD test_CD;
test_CD.write_ListBox(DriveComboBox1,ListBox1);
}
piątek, 22 grudnia 2017
Bit prime numbers (C++)
#include <iostream>
#include <conio.h>
#define max 20
#define max_2 4*max
#define yes '1'
#define no '0'
using namespace std;
char *bits_F;
int *value_F;
bool 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 test_Bits(char bit,char *tab_Bytes)
{
int value,i;
for(i=max-1;i>=0;i--)
{
value=(tab_Bytes[i]&bit);
if(value!=0)
cout<<yes;
else
cout<<no;
}
}
int main()
{
int i,j;
int negation;
bits_F=new char[max];
value_F=new int[max_2];
i=0;
j=2;
do
{
if(is_First(j))
{
bits_F[i]=j;
++i;
}
++j;
}while(i<max);
i=0;
j=2;
do
{
if(is_First(j))
{
value_F[i]=j;
++i;
}
++j;
}while(i<max_2);
for(i=0;i<max_2;i++)
{
negation=~i;
cout<<i+1<<"\t";
cout<<value_F[i]<<"\t";
test_Bits(i+1,bits_F);cout<<"\t";
test_Bits(negation+1,bits_F);
cout<<endl;
}
delete bits_F;
delete value_F;
getch();
return 0;
}
#include <conio.h>
#define max 20
#define max_2 4*max
#define yes '1'
#define no '0'
using namespace std;
char *bits_F;
int *value_F;
bool 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 test_Bits(char bit,char *tab_Bytes)
{
int value,i;
for(i=max-1;i>=0;i--)
{
value=(tab_Bytes[i]&bit);
if(value!=0)
cout<<yes;
else
cout<<no;
}
}
int main()
{
int i,j;
int negation;
bits_F=new char[max];
value_F=new int[max_2];
i=0;
j=2;
do
{
if(is_First(j))
{
bits_F[i]=j;
++i;
}
++j;
}while(i<max);
i=0;
j=2;
do
{
if(is_First(j))
{
value_F[i]=j;
++i;
}
++j;
}while(i<max_2);
for(i=0;i<max_2;i++)
{
negation=~i;
cout<<i+1<<"\t";
cout<<value_F[i]<<"\t";
test_Bits(i+1,bits_F);cout<<"\t";
test_Bits(negation+1,bits_F);
cout<<endl;
}
delete bits_F;
delete value_F;
getch();
return 0;
}
niedziela, 17 grudnia 2017
Drawing a cuboid (DrawLine - Visual C++)
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Graphics^ cu=panel1->CreateGraphics();
System::Drawing::Color my_Color;
my_Color=System::Drawing::Color::Black;
Pen^ my_Pen=gcnew Pen(my_Color);
Pen^ dot_Pen1=gcnew Pen(System::Drawing::Color::Red);
Pen^ dot_Pen2=gcnew Pen(System::Drawing::Color::Blue);
Pen^ dot_Pen3=gcnew Pen(System::Drawing::Color::Green);
dot_Pen1->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDotDot;
dot_Pen2->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDotDot;
dot_Pen3->DashStyle=System::Drawing::Drawing2D::DashStyle::DashDotDot;
System::Int16 margin_up_Y;
System::Int16 margin_down_Y;
System::Int16 margin_left_X;
System::Int16 margin_right_X;
margin_up_Y=10;
margin_down_Y=panel1->Height-10;
margin_left_X=10;
margin_right_X=panel1->Width-10;
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y,margin_right_X-(4*margin_left_X),margin_down_Y);
cu->DrawLine(my_Pen,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y,(4*margin_left_X),margin_down_Y-(8*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(4*margin_left_X),margin_down_Y,margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y,(2*margin_left_X),margin_down_Y-(20*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(4*margin_left_X),margin_down_Y,margin_right_X-(4*margin_left_X),margin_down_Y-(20*margin_up_Y));
cu->DrawLine(my_Pen,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),(4*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y-(20*margin_up_Y),(4*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,(2*margin_left_X),margin_down_Y-(20*margin_up_Y),margin_right_X-(4*margin_left_X),margin_down_Y-(20*margin_up_Y));
cu->DrawLine(my_Pen,(4*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(my_Pen,margin_right_X-(4*margin_left_X),margin_down_Y-(20*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(dot_Pen1,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(4*margin_left_X),margin_down_Y);
cu->DrawLine(dot_Pen2,(4*margin_left_X),margin_down_Y-(8*margin_up_Y),margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y));
cu->DrawLine(dot_Pen3,margin_right_X-(2*margin_left_X),margin_down_Y-(8*margin_up_Y)-(20*margin_up_Y),margin_right_X-(4*margin_left_X),margin_down_Y);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
};
}
Subskrybuj:
Posty (Atom)


