środa, 7 lutego 2018
Information about Excel (GetActiveObject,OleProprtyGet - C++ Builder)
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant Info;
UnicodeString e_openFile,e_Value,e_Address,e_activeSheet,e_Object;
Info=Null;
e_Object="Excel.Application";
try
{
try
{
Variant Info=Variant::GetActiveObject(e_Object);
e_openFile=Info.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Name");
Edit1->Text=e_openFile;
e_activeSheet=Info.OlePropertyGet("ActiveSheet").OlePropertyGet("Name");
Edit2->Text=e_activeSheet;
e_Value=Info.OlePropertyGet("ActiveCell").OlePropertyGet("Value");
Edit3->Text=e_Value;
e_Address=Info.OlePropertyGet("ActiveCell").OlePropertyGet("Address");
Edit4->Text=e_Address;
}
catch(EOleSysError* text_Error)
{
Edit1->Text="Error: "+text_Error->Message;
Edit2->Text=Edit1->Text;
Edit3->Text=Edit1->Text;
Edit4->Text=Edit1->Text;
}
catch(...)
{
Edit1->Text="Fatal error";
Edit2->Text=Edit1->Text;
Edit3->Text=Edit1->Text;
Edit4->Text=Edit1->Text;
}
}
__finally
{
Info=Unassigned;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
poniedziałek, 5 lutego 2018
Using StringGrid for calculations (C++ Builder)
int tab1[20][20];
int tab2[20][20];
bool __fastcall is_First(int value);
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
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::Button1Click(TObject *Sender)
{
int i,j,k,sum1,sum2;
float average1,average2;
int temp_tab1[20*20];
int temp_tab2[20*20];
k=sum1=sum2=0;
i=1;
do
{
if(i%2!=0)
{
temp_tab1[k]=i;
sum1+=i;
++k;
}
++i;
}while(k<20*20);
k=0;
i=1;
do
{
if(i%2==0)
{
temp_tab2[k]=i;
sum2+=i;
++k;
}
++i;
}while(k<20*20);
k=0;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
tab1[i][j]=temp_tab1[k];
tab2[i][j]=temp_tab2[k];
++k;
}
}
int temp_sum;
int sum_first;
float average_first1;
float average_first2;
sum_first=0;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
StringGrid1->Cells[i][j]=IntToStr(tab1[j][i]);
StringGrid2->Cells[i][j]=IntToStr(tab2[j][i]);
temp_sum=tab1[i][j]+tab2[i][j];
if(is_First(temp_sum))
{
ListBox1->Items->Add(IntToStr(tab1[i][j])+" + "+IntToStr(tab2[i][j])+" = "+IntToStr(temp_sum));
sum_first+=temp_sum;
}
}
}
average1=(1.*sum1)/(20.*20.);
average2=(1.*sum2)/(20.*20.);
average_first1=(100.*sum_first)/(1.*sum1);
average_first2=(100.*sum_first)/(1.*sum2);
Edit1->Text=IntToStr(sum1);
Edit2->Text=IntToStr(sum2);
Edit3->Text=FloatToStr(average1);
Edit4->Text=FloatToStr(average2);
Edit5->Text=FloatToStr(average_first1)+"%";
Edit6->Text=FloatToStr(average_first2)+"%";
}
czwartek, 1 lutego 2018
Class - reading from the server (g++/linux)
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define max_buffer 50000
#define sp_text "GET /\n"
using namespace std;
class read_WWW
{
private:
char *buf;
struct hostent *temp_host;
struct sockaddr_in s_in;
int r_socket;
ssize_t temp_read;
public:
read_WWW(char *www,int port_number)
{
buf=new char[max_buffer];
s_in.sin_family=AF_INET;
r_socket=socket(PF_INET,SOCK_STREAM,0);
temp_host=gethostbyname(www);
s_in.sin_addr=*((struct in_addr*) temp_host->h_addr);
s_in.sin_port=htons(port_number);
sprintf(buf,sp_text);
}
void write_WWW()
{
write(r_socket,buf,strlen(buf));
while(1)
{
temp_read=read(r_socket,buf,max_buffer);
fwrite(buf,sizeof(char),temp_read,stdout);
}
}
~read_WWW()
{
delete buf;
}
};
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define max_buffer 50000
#define sp_text "GET /\n"
using namespace std;
class read_WWW
{
private:
char *buf;
struct hostent *temp_host;
struct sockaddr_in s_in;
int r_socket;
ssize_t temp_read;
public:
read_WWW(char *www,int port_number)
{
buf=new char[max_buffer];
s_in.sin_family=AF_INET;
r_socket=socket(PF_INET,SOCK_STREAM,0);
temp_host=gethostbyname(www);
s_in.sin_addr=*((struct in_addr*) temp_host->h_addr);
s_in.sin_port=htons(port_number);
sprintf(buf,sp_text);
}
void write_WWW()
{
write(r_socket,buf,strlen(buf));
while(1)
{
temp_read=read(r_socket,buf,max_buffer);
fwrite(buf,sizeof(char),temp_read,stdout);
}
}
~read_WWW()
{
delete buf;
}
};
poniedziałek, 29 stycznia 2018
Using memory-mapped. class C++/g++ (linux)
#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;
}
#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;
}
ś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; }
Subskrybuj:
Posty (Atom)


