sobota, 28 grudnia 2019

Save from text file to html (c++ function)

#define max_Size 256
void Save_to_HTML(char *text_File, char *new_html_File)
{

    int i,j,line=0;
    fstream file_Text;
    string buff[max_Size];
    file_Text.open(text_File,ios::in);
    while(!file_Text.eof())
    {
        getline(file_Text,buff[line]);
        ++line;
    }
    file_Text.close();
    int sum_Count=line;
    string begin_Page[6]={"<HTML>","<HEAD>","<TITLE>","</TITLE","</HEAD>","<BODY>"};
    string end_Page[2]={"</BODY>","</HTML>"};
    int size_html_Page=sum_Count+6+2;
    string *page_HTML=new string[size_html_Page];
    for(i=0;i<6;i++)
   {
    page_HTML[i]=begin_Page[i];
   }
   for(i=0,j=6;i<sum_Count;i++,j++)
   {
    page_HTML[j]=buff[i]+" <br> ";
   }
   for(i=0,j=6+sum_Count;i<2;i++,j++)
   {
    page_HTML[j]=end_Page[i];
   }
   fstream file_HTML;
   file_HTML.open(new_html_File, ios::out | ios::app);
   if(file_HTML.good() == true)
   {
       for(i=0;i<size_html_Page;i++)
       {
           file_HTML<<page_HTML[i]<<"\n";
       }
       file_HTML.close();
   }
   delete [] page_HTML;

}


int main()
{
 Save_to_HTML("readme.txt","READ2.html");
    return 0;
}

czwartek, 26 grudnia 2019

utsname values (html) gcc (linux)

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>

struct uts_Values
{
char* sysname;
char* release;
char* machine;
char* version;
};

int main(int argc, char **argv)
{
size_t length_Table;
int file_HTML;
char *filename="Version.html";
struct utsname sys_Utsname;
struct uts_Values m_Values;
uname(&sys_Utsname);
m_Values.sysname=sys_Utsname.sysname;
m_Values.release=sys_Utsname.release;
m_Values.machine=sys_Utsname.machine;
m_Values.version=sys_Utsname.version;
static char* html_page=
"<html>\n"
"<body>\n"
"<table>\n"
"<thead>\n"
"<tr>\n"
"<th>m_Values.sysname</th>\n"
"<th>m_Values.release</th>\n"
"<th>m_Values.machine</th>\n"
"<th>m_Values.version</th>\n"
"</tr>\n"
"</thead>\n"
"</table>\n"
"</body>\n"
"</thml>\n";
length_Table=strlen(html_page);
file_HTML=open(filename,O_WRONLY);
write(file_HTML,html_page,length_Table);
close(file_HTML);


return 0;
}

wtorek, 19 listopada 2019

Short Multiplication Formulas (example C++)

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
bool is_Minus()
{
int value;
value=rand()%2;
if(value==1)
return true;
else
return false;
}
int plus_Minus(int value)
{
if(is_Minus())
{
value=value*-1;
}
return value;
}
int sum_Q1(int a, int b)
{
int sum;
sum=(a*a)+(2*a*b)+(b*b);
return sum;
}
int minus_Q1(int a, int b)
{
int minus;
minus=(a-b)*(a+b);
return minus;
}
int sum_Q2(int a, int b)
{
int sum;
sum=(a*a)-(2*a*b)+(b*b);
return sum;
}
int minus_Q3(int a, int b)
{
int minus;
minus=(a-b)*((a*a)+(a*b)+(b*b));
return minus;
}
int sum_Q3(int a, int b)
{
int sum;
sum=(a+b)*((a*a)-(a*b)+(b*b));
return sum;
}
int sum_Q3_2(int a, int b)
{
int sum;
sum=(a*a*a)+(3*(a*a)*b)+(3*a*(b*b))+(b*b*b);
return sum;
}
int minus_Q3_2(int a, int b)
{
int minus;
minus=(a*a*a)-(3*(a*a)*b)+(3*a*(b*b))-(b*b*b);
return minus;
}

int *results,*a_v,*b_v;


int main(int argc, char **argv)
{
srand(time(NULL));
int i;
results=new int[7];
a_v=new int[7];
b_v=new int[7];
for(i=0;i<7;i++)
{
a_v[i]=plus_Minus(rand()%20+1);
b_v[i]=plus_Minus(rand()%20+1);

}
results[0]=sum_Q1(a_v[0],b_v[0]);
results[1]=minus_Q1(a_v[1],b_v[1]);
results[2]=sum_Q2(a_v[2],b_v[2]);
results[3]=minus_Q3(a_v[3],b_v[3]);
results[4]=sum_Q3(a_v[4],b_v[4]);
results[5]=sum_Q3_2(a_v[5],b_v[5]);
results[6]=minus_Q3_2(a_v[6],b_v[6]);
for(i=0;i<7;i++)
{
cout<<a_v[i]<<"  "<<b_v[i]<<"  =  "<<results[i]<<endl;
}

delete b_v;
delete a_v;
delete results;
return 0;
}

poniedziałek, 11 listopada 2019

Text function and different types (C/C++ Builder)

UnicodeString __fastcall TForm1::first_and_Last(UnicodeString text_Ex)
{
wchar_t* date_Text=text_Ex.w_str();
int size_Text=wcslen(date_Text);
int size_Result=size_Text-2;
int i;
wchar_t* new_Text=new wchar_t[size_Result];
for(i=0;i<size_Result;i++)
new_Text[i]=date_Text[i+1];
return (UnicodeString) new_Text;
}
AnsiString __fastcall TForm1::first_and_Last2(AnsiString text_Ex)
{
char* date_Text=text_Ex.c_str();
int size_Text=strlen(date_Text);
int size_Result=size_Text-2;
int i;
char* new_Text=new char[size_Result];
for(i=0;i<size_Result;i++)
new_Text[i]=date_Text[i+1];
return (AnsiString) new_Text;
}

String __fastcall TForm1::first_and_Last3(String text_Ex)
{
char* date_Text;
sprintf(date_Text,"%s",text_Ex.c_str());
int size_Text=strlen(date_Text);
int size_Result=size_Text-2;
int i;
char* new_Text=new char[size_Result];
for(i=0;i<size_Result;i++)
new_Text[i]=date_Text[i+1];
return (String) new_Text;
}
string first_and_Last4(string text_Ex)
{
const char* date_Text=text_Ex.c_str();
int size_Text=strlen(date_Text);
int size_Result=size_Text-2;
int i;
char* new_Text=new char[size_Result];
for(i=0;i<size_Result;i++)
new_Text[i]=date_Text[i+1];
return (string) new_Text;
}
char* first_and_Last5(char* text_Ex)
{
const char* date_Text=text_Ex;
int size_Text=strlen(date_Text);
int size_Result=size_Text-2;
int i;
char* new_Text=new char[size_Result];
for(i=0;i<size_Result;i++)
new_Text[i]=date_Text[i+1];
return  new_Text;
}

sobota, 9 listopada 2019

Example files and array (g++)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int *date_Tab;
int *date_Temp;
int *sum_Array;
int double_Tab[50][20];
FILE *tab_Result,*sum_Result;

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;
}
int sum_Tab(int *tab, int tab_Size)
{
int i,sum;
sum=0;
for(i=0;i<tab_Size;i++)
sum+=tab_Size;
return sum;
}
void sort_Tab(int *tab, int tab_Size)
{
int i,j,k;
for(i=1;i<tab_Size;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}


int main(int argc, char **argv)

int i,j,k;
date_Tab=new int[1000];
date_Temp=new int[1000];
sum_Array=new int[50];

srand(time(NULL));

i=0;
j=2;
do
{
if(is_First(j))
{
date_Tab[i]=j;
date_Temp[i]=0;
++i;
}
++j;
}while(i<1000);
i=0;
do
{
j=0;
do
{
do
{
k=rand()%1000;
}while(date_Temp[k]!=0);
date_Temp[k]=1;
double_Tab[i][j]=date_Tab[k];
++j;
  }while(j<20);
  ++i;
}while(i<50);
for(i=0;i<50;i++)
{
sort_Tab(double_Tab[i],20);
sum_Array[i]=sum_Tab(double_Tab[i],20);
}
tab_Result=fopen("table_First.txt","wt");
for(i=0;i<50;i++)
{
for(j=0;j<20;j++)
{
fprintf(tab_Result,"%d ",double_Tab[i][j]);
}
fprintf(tab_Result,"\n");
}
fclose(tab_Result);
sum_Result=fopen("table_Sum.txt","wt");
for(i=0;i<50;i++)
{
fprintf(sum_Result,"%d \n",sum_Array[i]);
}
fclose(sum_Result);
delete sum_Array;
delete date_Tab;
delete date_Temp;
return 0;
}

niedziela, 27 października 2019

OS version (linux C++)

#include <iostream>
#include <stdio.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>

using namespace std;

class info_Name
{
private:
char *my_File;
char *os_Machine,*os_Version,*os_Release,*os_Name;
double free_Ram,total_Ram;
struct utsname my_Os;
struct sysinfo my_RAM;
public:
info_Name(char *file_Name)
{
my_File=file_Name;
uname(&my_Os);
sysinfo(&my_RAM);
os_Name=my_Os.sysname;
os_Release=my_Os.release;
os_Version=my_Os.version;
os_Machine=my_Os.machine;
total_Ram=my_RAM.totalram/(1024.*1024.);
free_Ram=my_RAM.freeram/(1024.*1024.);
}
void Save()
{
FILE* save_File;
save_File=fopen(my_File,"wt");
fprintf(save_File,"%f\n",total_Ram);
fprintf(save_File,"%f\n",free_Ram);
fprintf(save_File,"%s\n",os_Name);
fprintf(save_File,"%s\n",os_Release);
fprintf(save_File,"%s\n",os_Version);
fprintf(save_File,"%s\n",os_Machine);
fclose(save_File);
}
};

int main(int argc, char **argv)
{
info_Name my_Version("version.txt");
my_Version.Save();
return 0;
}

piątek, 18 października 2019

Array manipulation (C++ Builder)



private: // User declarations
        int **A,**B,**C,**D, random_Col;
        void __fastcall init_Arrays();
        void __fastcall destroy_Arrays();
        int* __fastcall change_Position(int *tab, int size_Tab);
        void __fastcall max_Sort(int *tab, int tab_Size);
        void __fastcall min_Sort(int *tab, int tab_Size);


int* __fastcall TForm1::change_Position(int *tab, int size_Tab)
{
 int *result_Tab=new int[size_Tab];
 int i;
 for(i=0;i<size_Tab;i++)
 {
  result_Tab[i]=tab[(size_Tab-1)-i];

 }
 return result_Tab;
}
void __fastcall TForm1::max_Sort(int *tab, int tab_Size)
{
  int i,j,k;
    for(i=1;i<tab_Size;i++)
    {
        j=i;
        k=tab[j];
        while((j>0) && (tab[j-1]<k))
        {
            tab[j]=tab[j-1];
            j--;
        }
        tab[j]=k;
    }
}
void __fastcall TForm1::min_Sort(int *tab, int tab_Size)
{
  int i,j,k;
    for(i=1;i<tab_Size;i++)
    {

        j=i;
        k=tab[j];
        while((j>0) && (tab[j-1]>k))
        {
            tab[j]=tab[j-1];
            j--;
        }
        tab[j]=k;
    }
}

void __fastcall TForm1::init_Arrays()
{
 int i,j,k,sum;
 srand(time(NULL));
 A=new int *[5];
 B=new int *[5];
 C=new int *[5];
 D=new int *[5];
 for(i=0;i<5;i++)
 {
  A[i]=new int[10];
  B[i]=new int[10];
  C[i]=new int[10];
  D[i]=new int[10];
 }

 random_Col=50+rand()%50;
 Edit1->Text=IntToStr(random_Col);
 i=0;
 do
 {
  do
  {
    sum=0;
   for(j=0;j<10;j++)
   {
    k=rand()%10+1;
    sum+=k;
    A[i][j]=k;
   }
   }while(sum!=random_Col);
   ++i;

 }while(i<5);
  for(i=0;i<5;i++)
 {
  B[i]=change_Position(A[i],10);

 }
 for(i=0;i<5;i++)
 {
  for(j=0;j<10;j++)
  {
   C[i][j]=A[i][j];
   D[i][j]=B[i][j];
  }
 }
 for(i=0;i<5;i++)
 {
  min_Sort(C[i],10);
  max_Sort(D[i],10);
 }


  for(i=0;i<5;i++)
  {
   for(j=0;j<10;j++)
   {
    StringGrid1->Cells[j][i]=IntToStr(A[i][j]);
    StringGrid2->Cells[j][i]=IntToStr(B[i][j]);
    StringGrid3->Cells[j][i]=IntToStr(C[i][j]);
    StringGrid4->Cells[j][i]=IntToStr(D[i][j]);
   }
  }



}
void __fastcall TForm1::destroy_Arrays()
{
 int i;
 for(i=0;i<5;i++)
 {
  delete [] D[i];
  delete [] C[i];
  delete [] B[i];
  delete [] A[i];
 }
 delete [] A;
 delete [] B;
 delete [] C;
 delete [] D;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 init_Arrays();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 destroy_Arrays();
}
        

czwartek, 17 października 2019

Data and file (C++ Builder)

struct DATE_
{
int year;
UnicodeString month_name;
int number_month;
int number_day;
UnicodeString day_name;
}my_Date;

UnicodeString __fastcall TForm1::day_of_Week(int day)
{
UnicodeString result;
switch (day)
{
case 1: result="Sunday"; break;
case 2: result="Monday"; break;
case 3: result="Tuesday"; break;
case 4: result="Wednesday";break;
case 5: result="Thursday"; break;
case 6: result="Friday";break;
case 7: result="Saturday"; break;


default: result="error";break;
}
return result;

}
UnicodeString __fastcall TForm1::month_of_Year(int month)
{
UnicodeString result;
switch (month)
{
case 1: result="January"; break;
case 2: result="February"; break;
case 3: result="March"; break;
case 4: result="April"; break;
case 5: result="May";break;
case 6: result="June";break;
case 7: result="July";break;
case 8: result="August";break;
case 9: result="September"; break;
case 10: result="Oktober"; break;
case 11: result="November"; break;
case 12: result="December"; break;
default: result="error";break;
}
return result;
}
wchar_t* __fastcall TForm1::read_from_Datefile()
{
ifstream file_Date;
wchar_t *buf=new wchar_t[128];
file_Date.open("today_date.txt");
for(int i=0;(i<128) && (file_Date);i++)
{
buf[i]=file_Date.get();
}
file_Date.close();
    return buf;

}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  unsigned short a[4];
  DecodeDateFully(Now(),a[0],a[1],a[2],a[3]);
  my_Date.year=int(a[0]);
  my_Date.month_name=month_of_Year(int(a[1]));
  my_Date.number_month=int(a[1]);
  my_Date.number_day=int(a[2]);
  my_Date.day_name=day_of_Week(int(a[3]));
  FILE *save_date_project;
  save_date_project=fopen("today_date.txt","w");
  fprintf(save_date_project,"%d\n",my_Date.year);
  fprintf(save_date_project,"%s\n",my_Date.month_name.w_str());
  fprintf(save_date_project,"%d\n",my_Date.number_day);
  fprintf(save_date_project,"%s\n",my_Date.day_name.w_str());
  fclose(save_date_project);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ShowMessage(UnicodeString(read_from_Datefile()));
}

wtorek, 15 października 2019

Copying a group of files (C++ Linux)

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/types.h>



using namespace std;



void COPY(string* files1, string *files2, int how_Many)
{
 int i;
 struct stat* buf_Files;
 int *from_F,*to_F;
 off_t *set;
 buf_Files=new struct stat[how_Many];
 from_F=new int[how_Many];
 to_F=new int[how_Many];
 set=new off_t[how_Many];
 for(i=0;i<how_Many;i++)
 {
from_F[i]=open(files1[i].c_str(),O_RDONLY);
fstat(from_F[i],&buf_Files[i]);
to_F[i]=open(files2[i].c_str(),O_WRONLY | O_CREAT, buf_Files[i].st_mode);
sendfile(to_F[i],from_F[i],&set[i],buf_Files[i].st_size);
close(from_F[i]);
close(to_F[i]);
 }
 delete set;
 delete to_F;
 delete from_F;
 delete buf_Files;
}

int main(int argc, char **argv)
{
 string to_Files[5]={"home/file1","home/file2","home/file3","home/file4","home/file5"};
 string from_Files[5]={"proc/zoneinfo","proc/vstat","proc/version","proc/uptime","proc/stat"};
 COPY(from_Files,to_Files,5);

return 0;
}

niedziela, 13 października 2019

Data from /proc/meminfo (Linux C++)


#include <iostream>
#include <string.h>
#include <stdio.h>
#define size_B 1024
using namespace std;

struct mem_I
{
double Slab;
double SReclaimable;
double SUnreclaim;
double KernelStack;
double PageTables;

};
char mem_I_Val1[5][20]={"Slab","Sreclaimable","SUnreclaim","KernelStack","PageTables"};
char mem_I_Val2[5][20]={"Slab : %f","Sreclaimable : %f","SUnreclaim %f","KernelStack %f","PageTables %f"};

float write_Value(char *text1, char *text2)
{
size_t read_Size;
float result;
char  buff[size_B];
char *temp;
FILE *test_File;
test_File=fopen("/proc/meminfo","r");
read_Size=fread(buff,1,sizeof(buff),test_File);
fclose(test_File);
buff[read_Size]='\0';
temp=strstr(buff,text1);
if(temp==NULL)
return 0;
sscanf(temp,text2,&result);
return result;


}

int main(int argc, char **argv)
{
int i;
float results[5];
for(i=0;i<5;i++)
{
results[i]=write_Value(mem_I_Val1[i],mem_I_Val2[i]);
}
for(i=0;i<5;i++)
{
cout<<mem_I_Val1[i]<<" - "<<results[i]<<" kB"<<endl;
}
return 0;
}

piątek, 11 października 2019

fprintf, fscanf - example

#define SIZE_TAB 20
#define MAX_VALUE SIZE_TAB*SIZE_TAB


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#include <iostream>
using namespace std;


double *finish_Result;
int main_Tab[SIZE_TAB][SIZE_TAB];
int first_Values[MAX_VALUE];
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 init_Arrays()
{
int i,j,k;
i=0,j=2;
do
{
if(is_First(j))
{
first_Values[i]=j;
++i;
}
++j;
}while(i<MAX_VALUE);
for(i=0;i<SIZE_TAB;i++)
{
for(j=0;j<SIZE_TAB;j++)
{
k=rand()%MAX_VALUE;
main_Tab[i][j]=first_Values[k];
}
}

}

int min_Element(int *tab, int size)
{
int min_E,i;
min_E=tab[0];
for(i=1;i<size;i++)
if(min_E>tab[i])
  min_E=tab[i];
return min_E;
}
int max_Element(int *tab, int size)
{
int max_E,i;
max_E=tab[0];
for(i=1;i<size;i++)
if(max_E<tab[i])
  max_E=tab[i];
return max_E;
}
int sum_Array(int *tab, int size)
{
 int i,sum;
 sum=0;
 for(i=0;i<size;i++)
  sum+=tab[i];
 return sum;
}

double aver_Array(int *tab, int size)
{
double aver;
aver=(1.0*sum_Array(tab,size))/(1.0*size);
return aver;
}

double func_A(int a, int b, int c, double d)
{
double func;
func=(c*1.0)-((1.0*a)+(1.0*b))*d;
return func;
}

struct info_Array
{
int a_Min;
int a_Max;
int a_Sum;
double a_Aver;
};
 int main()
{
 int i,j;
 FILE *base_File,*result_File,*read_Date;
 srand(time(NULL));
 finish_Result=new double[SIZE_TAB];
 struct info_Array *i_Array=new struct info_Array[SIZE_TAB];
 struct info_Array *j_Array=new struct info_Array[SIZE_TAB];
 init_Arrays();
 for(i=0;i<SIZE_TAB;i++)
 {
i_Array[i].a_Min=min_Element(main_Tab[i],SIZE_TAB);
i_Array[i].a_Max=max_Element(main_Tab[i],SIZE_TAB);
i_Array[i].a_Sum=sum_Array(main_Tab[i],SIZE_TAB);
i_Array[i].a_Aver=aver_Array(main_Tab[i],SIZE_TAB);
 }

 base_File=fopen("base.txt","w");
 for(i=0;i<SIZE_TAB;i++)
 {
for(j=0;j<SIZE_TAB;j++)
{
fprintf(base_File,"%d  ",main_Tab[i][j]);

}
fprintf(base_File,"\n");
 }

 fclose(base_File);
 result_File=fopen("result.txt","w");
 for(i=0;i<SIZE_TAB;i++)
 {
fprintf(result_File,"%d %d %d %f ",i_Array[i].a_Min,i_Array[i].a_Max,
i_Array[i].a_Sum,i_Array[i].a_Aver);
fprintf(result_File,"\n");
 }
 fclose(result_File);
 read_Date=fopen("result.txt","r");
 for(i=0;i<SIZE_TAB;i++)
 {
fscanf(read_Date,"%d %d %d %f ",&j_Array[i].a_Min,&j_Array[i].a_Max,
&j_Array[i].a_Sum,&j_Array[i].a_Aver);
 }
 fclose(read_Date);
 for(i=0;i<SIZE_TAB;i++)
 {
  finish_Result[i]=func_A(j_Array[i].a_Min,j_Array[i].a_Max,j_Array[i].a_Sum,
  j_Array[i].a_Aver);
 }
 for(i=0;i<SIZE_TAB;i++)
 {
     cout<<i+1<<") "<<finish_Result[i]<<endl;
 }
 delete i_Array;
 delete j_Array;
 delete finish_Result;

 getch();
return 0;
}