ś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;
    }
};