poniedziałek, 20 grudnia 2021

Mars oposition (example C++)

 #include <iostream>

#include <fstream>

#define mtime 7

using namespace std;


struct MARS

{

double angular_DIAM;

double distance_UA;

double distance_MLN;

string date;

};


int main(int argc, char **argv)

{

struct MARS *mars_OPOSITION=new struct MARS[mtime];

mars_OPOSITION[0].angular_DIAM=16.9;

mars_OPOSITION[0].distance_UA=0.550;

mars_OPOSITION[0].distance_MLN=82.3;

mars_OPOSITION[0].date="2022.12.08";

mars_OPOSITION[1].angular_DIAM=14.4;

mars_OPOSITION[1].distance_UA=0.643;

mars_OPOSITION[1].distance_MLN=96.2;

mars_OPOSITION[1].date="2025.01.16";

mars_OPOSITION[2].angular_DIAM=13.8;

mars_OPOSITION[2].distance_UA=0.678;

mars_OPOSITION[2].distance_MLN=101.4;

mars_OPOSITION[2].date="2027.02.19";

mars_OPOSITION[3].angular_DIAM=14.4;

mars_OPOSITION[3].distance_UA=0.649;

mars_OPOSITION[3].distance_MLN=97.1;

mars_OPOSITION[3].date="2029.03.25";

mars_OPOSITION[4].angular_DIAM=16.9;

mars_OPOSITION[4].distance_UA=0.559;

mars_OPOSITION[4].distance_MLN=83.6;

mars_OPOSITION[4].date="2031.05.04";

mars_OPOSITION[5].angular_DIAM=22.00;

mars_OPOSITION[5].distance_UA=0.427;

mars_OPOSITION[5].distance_MLN=63.9;

mars_OPOSITION[5].date="2033.06.27";

mars_OPOSITION[6].angular_DIAM=24.5;

mars_OPOSITION[6].distance_UA=0.382;

mars_OPOSITION[6].distance_MLN=57.1;

mars_OPOSITION[6].date="2035.09.15";

fstream m_FILE( "MARS.txt", ios::out );

if(m_FILE.good())

{

for(int i=0;i<mtime;i++)

{

m_FILE<<mars_OPOSITION[i].angular_DIAM<<" ";

m_FILE<<mars_OPOSITION[i].distance_UA<<" ";

m_FILE<<mars_OPOSITION[i].distance_MLN<<" ";

m_FILE<<mars_OPOSITION[i].date<<"\n";

m_FILE.flush();

}

m_FILE.close();

}

delete [] mars_OPOSITION;

return 0;

}


poniedziałek, 29 listopada 2021

Sleep() nad time() (C++)

 #include <iostream>

#include <fstream>

#include <string.h>

#include <time.h>

#include <unistd.h>

using namespace std;



class time_File

{

private:

char* nameF;

int timeS;

time_t now_T;

public:

time_File(char* fileName, int sleepTime)

{

nameF=fileName;

timeS=sleepTime;

}

void Save()

{

int i;

fstream file(nameF,ios::out);

if (file.good())

{

for(i=0;i<timeS;i++)

{

now_T=time(NULL);

char *temp_File=asctime(localtime(&now_T));

file<<temp_File;

file.flush();

sleep(1);

}

file.close();

}

}

};


int main(int argc, char **argv)

{

time_File Timer("date.txt",7);

Timer.Save();

return 0;

}

Result: date.txt


Tue Nov 30 08:32:10 2021
Tue Nov 30 08:32:11 2021
Tue Nov 30 08:32:12 2021
Tue Nov 30 08:32:13 2021
Tue Nov 30 08:32:14 2021
Tue Nov 30 08:32:15 2021
Tue Nov 30 08:32:16 2021

Copying to Multiple Files (C++)

 


#include <iostream>

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/sendfile.h>

#include <sys/stat.h>


using namespace std;



class copy_File

{

private:

string rname;

string *names;

int s;

int *files_W;

off_t temp_Off;

public:

copy_File(int s1,string* mNames, string rName)

{

int i;

s=s1;

names=new string[s];

for(i=0;i<s;i++)

names[i]=mNames[i];

rname=rName;

files_W=new int [s];

temp_Off=0;

}

~copy_File()

{

delete files_W;

delete names;

}

void send_F()

{

int file_R,i;

struct stat buff_Fil;

file_R=open(rname.c_str(),O_RDONLY);

fstat(file_R,&buff_Fil);

for(i=0;i<s;i++)

{

files_W[i]=open(names[i].c_str(),O_WRONLY | O_CREAT,buff_Fil.st_mode);

sendfile(files_W[i],file_R,&temp_Off,buff_Fil.st_size);

}

close(file_R);

for(i=0;i<s;i++)

close(files_W[i]);

}

};

 





int main(int argc, char **argv)

{

string read_Name="chromo.cxx";

string weeks[7]={"Monday","Tuesday","Wednesday","Thursday","Friday",

"Saturday","Sunday"};

copy_File m_File(7,weeks,read_Name);

m_File.send_F();

return 0;

}


piątek, 26 listopada 2021

Analysis and simulation lottery-"lotto" problems - C++

 #include <iostream>

#include <time.h>

#include <stdlib.h>


#define lotto_size 42

#define min_size 5

#define max_size 1000


using namespace std;

int main_Size[min_size];


int test_Size[max_size][min_size];

int temp_Size1[lotto_size];

int temp_Size2[max_size][lotto_size];


void sort_Lotto(int *tab, int m_Size)

{

    int i,j,k;

    for(i=1;i<m_Size;i++)

    {


        j=i;

        k=tab[j];

        while((j>0) && (tab[j-1]>k))

        {

            tab[j]=tab[j-1];

            j--;

        }

        tab[j]=k;

    }

}


bool is_Zero(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

        for(i=0;i<msize;i++)

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==0)

        return true;

    else

        return false;

}

bool is_One(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

        for(i=0;i<msize;i++)

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==1)

        return true;

    else

        return false;

}

bool is_Two(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

        for(i=0;i<msize;i++)

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==2)

        return true;

    else

        return false;

}


bool is_Three(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

        for(i=0;i<msize;i++)

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==3)

        return true;

    else

        return false;

}

bool is_Four(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

        for(i=0;i<msize;i++)

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==4)

        return true;

    else

        return false;

}

bool is_Five(int *tab1, int *tab2, int msize)

{

    int i,j,sum;

    sum=0;

    j=0;

    do

    {

        for(i=0;i<msize;i++)

            if(tab1[j]==tab2[i])

            ++sum;

        ++j;


    }while(j<msize);

    if(sum==5)

        return true;

    else

        return false;

}






void init_Tab()

{

    int i,j,k,l;

    for(i=0;i<lotto_size;i++)

        temp_Size1[i]=0;

    for(i=0;i<max_size;i++)

        for(j=0;j<lotto_size;j++)

        temp_Size2[i][j]=0;

    i=0;

    do

    {

        do

        {

           k=1+rand()%lotto_size;

        }while(temp_Size1[k-1]!=0);

        temp_Size1[k-1]=1;

        main_Size[i]=k;

        ++i;

    }while(i<min_size);

    l=0;

    do

    {

        i=0;

        do

        {

            do

            {

                k=1+rand()%lotto_size;

            }while(temp_Size2[i][k-1]!=0);

            temp_Size2[l][k-1]=1;

            test_Size[l][i]=k;

            ++i;


        }while(i<min_size);


        ++l;

    }while(l<max_size);

    sort_Lotto(main_Size,min_size);

    for(i=0;i<max_size;i++)

        sort_Lotto(test_Size[i],min_size);

}

int main()

{

    int i,a0,a1,a2,a3,a4,a5;

    a0=a1=a2=a3=a4=a5=0;

    srand(time(NULL));

    init_Tab();

    

    cout<<"The coupon is drawn (5/42) : \n";

    

    for(i=0;i<min_size;i++)

    {

        cout<<i+1<<") "<<main_Size[i]<<endl;


    }

    for(i=0;i<max_size;i++)

    {

        if(is_Zero(main_Size,test_Size[i],min_size))

            ++a0;

        if(is_One(main_Size,test_Size[i],min_size))

            ++a1;

        if(is_Two(main_Size,test_Size[i],min_size))

            ++a2;

        if(is_Three(main_Size,test_Size[i],min_size))

            ++a3;

        if(is_Four(main_Size,test_Size[i],min_size))

            ++a4;

        if(is_Five(main_Size,test_Size[i],min_size))

            ++a5;

    }

    

    cout<<"\nAnalysis after 1000 draws\n";


    cout<<"\n-------------------------------------------------\n";

    cout<<"0/5 - "<<a0<<endl;

    cout<<"1/5 - "<<a1<<endl;

    cout<<"2/5 - "<<a2<<endl;

    cout<<"3/5 - "<<a3<<endl;

    cout<<"4/5 - "<<a4<<endl;

    cout<<"5/5 - "<<a5<<endl;

    return 0;

}


czwartek, 18 listopada 2021

Sleep() example C++

 #include <iostream>

#include <fcntl.h>

#include <fstream>

#include <stdio.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <time.h>

#include <unistd.h>

using namespace std;




class time_in_File

{

private:

char* now_Time();

char* file_Name;

int sl;

public:

char* write_Time();

time_in_File(char* name, int m_Sleep);

void Save();

};


char* time_in_File::now_Time()

{

char *n_Time;

time_t act;

act=time(NULL);

n_Time=asctime(localtime(&act));

return n_Time;

}

char* time_in_File::write_Time()

{

return now_Time();

}

time_in_File::time_in_File(char* name, int m_Sleep)

{

file_Name=name;

sl=m_Sleep;

}

void time_in_File::Save()

{

int i;

fstream my_File(file_Name,ios::out);

if(my_File.good())

{

for(i=0;i<sl;i++)

{

my_File<<write_Time()<<"\n";

sleep(sl);

}

my_File.close();

}

}


int main(int argc, char **argv)

{

char* n="Example.txt";

int s=5;

time_in_File my_Class(n,s);

my_Class.Save();

return 0;

}

RESULT File: Example.txt

Thu Nov 18 18:26:52 2021

Thu Nov 18 18:26:57 2021

Thu Nov 18 18:27:02 2021

Thu Nov 18 18:27:07 2021

Thu Nov 18 18:27:12 2021

wtorek, 2 listopada 2021

Saving text to Word (C++ Builder)


 void __fastcall TForm1::Button1Click(TObject *Sender)

{

 Close();

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

 ListBox1->Items->Clear();

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)

{

 ListBox1->Items->Clear();

 if(FileExists(Edit1->Text))

  ListBox1->Items->LoadFromFile(Edit1->Text);

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)

{

 ListBox1->Items->SaveToFile(Edit2->Text);

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)

{

 ListBox1->Items->Add(Edit3->Text);

 Edit3->Text="";

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)

{

 Close();

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button7Click(TObject *Sender)

{

 int i;

 Variant mWord, mValue;

 for (i=0;i<ListBox1->Items->Count;i++)

 {

if(ListBox1->Selected[i])

  Edit4->Text=ListBox1->Items->Strings[i];

 }


 UnicodeString a1,a2;

 a1="Selection";

 a2="Select";

 mWord=Variant::GetActiveObject("Word.Application");

 mValue=mWord.OlePropertyGet(a1);

 mValue.OlePropertySet("Text",Edit4->Text.w_str());

 mValue.OleProcedure(a2);

 mWord=mValue=Unassigned;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

 Edit4->Text="Select text from the ListBox";

}

niedziela, 31 października 2021

Example of ordering array (C++)

 #include <iostream>

#include <stdlib.h>

#include <time.h>

#define kk 24


using namespace std;


int *tab1; int *tab2; int *tab3; int *tab4; int *tab5; int *tab6;


int min_T(int a, int b, int c)

{

int d;

d=a;

if(d>=b)

d=b;

if(d>=c)

d=c;

return d;


}

int max_T(int a, int b, int c)

{

int d;

d=a;

if(d<=b)

d=b;

if(d<=c)

d=c;

return d;

}

int mid_T(int a, int b, int c)

{

int d=0;

if(a<=b && a>=c)

d=a;

if(a<=c && a>=b)

d=a;


if(b<=a && b>=c)

d=b;

if(b<=c && b>=a)

d=b;

if(c<=a && c>=b)

d=c;

if(c<=b && c>=a)

d=c;

return d;


}


int main()

{


int i;

srand(time(NULL));

tab1=new int[kk]; tab2=new int[kk]; tab3=new int[kk];

tab4=new int[kk]; tab5=new int[kk]; tab6=new int[kk];

        for(i=0;i<kk;i++)

{

tab1[i]=1+rand()%kk;

tab2[i]=1+rand()%kk;

tab3[i]=1+rand()%kk;

tab4[i]=min_T(tab1[i],tab2[i],tab3[i]);

tab5[i]=max_T(tab1[i],tab2[i],tab3[i]);

tab6[i]=mid_T(tab1[i],tab2[i],tab3[i]);

cout<<tab4[i]<<"-"<<tab6[i]<<"-"<<tab5[i]<<endl;


}

delete [] tab6; delete [] tab5; delete [] tab4;

delete [] tab3; delete [] tab2; delete [] tab1;

return 0;

}


niedziela, 17 października 2021

C fprintf()

 #include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <math.h>

#define asize 30


int tab1[asize], tab2[asize], tab3[asize];

double sq1[asize], sq2[asize], sq3[asize];

int main()

{

int i,j;

FILE *f1,*f2,*f3;

srand(time(NULL));

for(i=0;i<asize;i++)

{

tab1[i]=rand()%asize+1;

tab2[i]=rand()%asize+1;

tab3[i]=rand()%asize+1;

if(tab1[i]>tab2[i])

        {

                      

                        j=tab1[i];

tab1[i]=tab2[i];

tab2[i]=j;

}

if(tab1[i]>tab3[i])

{

j=tab1[i];

tab1[i]=tab3[i];

tab3[i]=j;

}

if(tab3[i]<tab2[i])

{

j=tab1[i];

tab3[i]=tab2[i];

tab2[i]=j;

}

sq1[i]=sqrt(1.00*tab1[i]);

sq2[i]=sqrt(1.00*tab2[i]);

sq3[i]=sqrt(1.00*tab3[i]);

}

f1=fopen("MINVALUES.txt","wt");

for(i=0;i<asize;i++) {

fprintf(f1,"%d  %.2f\n",tab1[i],sq1[i]);

}

fclose(f1);

f2=fopen("MIDDLEVALUES.txt","wt");

for(i=0;i<asize;i++) {

fprintf(f2,"%d  %.2f\n",tab2[i],sq2[i]);

}

fclose(f2);

f3=fopen("MAXVALUES.txt","wt");

for(i=0;i<asize;i++) {

fprintf(f3,"%d  %.2f\n",tab3[i],sq3[i]);

}




return 0;

}


czwartek, 14 października 2021

Testing Lazarus in Linux Ubuntu (Free Pascal)

 Lazarus is free and therefore seems like an interesting alternative.


function is_First(mvalue:Integer):Boolean;
var i,suma:Integer;
  begin
    suma:=0;
    for i:=1 to mvalue+1 do
    begin
      if mvalue mod i=0 then
      suma:=suma+1;
    end;
    if suma=2 then
    is_First:=True
    else
    is_First:=False;
  end;
function sum_Tab(tab: array of Integer; size:Integer): Integer;
var i,sum:Integer;
begin
 sum:=0;
 for i:=0 to size do
  sum:=sum+tab[i];
 sum_Tab:=sum;

end;
function min_Tab(tab: array of Integer; size: Integer): Integer;
var i,min: Integer;
begin
 min:=tab[0];
 for i:=1 to size do
 begin
   if min>tab[i] then
    min:=tab[i];
 end;
 min_Tab:=min;
end;
function max_Tab(tab: array of Integer; size: Integer): Integer;
var i,max: Integer;
begin
 max:=tab[0];
 for i:=1 to size do
 begin
   if max<tab[i] then
    max:=tab[i];
 end;
 max_Tab:=max;;
end;

function aver_Tab1(tab: array of Integer; size: Integer):Real;
  begin
   aver_Tab1:=(1.00*sum_Tab(tab,size))/(1.00*size);
  end;
function aver_Tab2(tab: array of Integer; size: Integer):Real;
begin
  aver_Tab2:=((1.00*min_Tab(tab,size))+(1.00*max_Tab(tab,size)))/2.00;
end;

{ TForm1 }

procedure TForm1.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  pascalsA: array of Integer;
  i,j,m1,m2,s:Integer;
  a1,a2: Real;
begin
 setLength(pascalsA,30);

 i:=0;
 j:=1;
 repeat
   if is_First(j) then
   begin
     pascalsA[i]:=j;
     i:=i+1;
   end ;
   j:=j+1;

 until i>30;

 for i:=0 to 29 do
 begin
   ListBox1.Items.Add(IntToStr(i+1)+') '+IntToStr(pascalsA[i]));
 end;
 m1:=min_Tab(pascalsA,29);
 m2:=max_Tab(pascalsA,29);
 s:=sum_Tab(pascalsA,29);
 a1:=aver_Tab1(pascalsA,29);
 a2:=aver_Tab2(pascalsA,29);
 Label6.Caption:=IntToStr(m1);
 Label7.Caption:=IntToStr(m2);
 Label8.Caption:=IntToStr(s);
 Label9.Caption:=FloatToStr(a1);
 Label10.Caption:=FloatToStr(a2);


end;

end.               



piątek, 20 sierpnia 2021

Double array (malloc/free)

 #include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define mint 10

#define maxt 100

int **tab;


void my_Sort(int *t, int s);

void write_Date(char *file_Name, int **t, int x, int y);

int main(int argc, char **argv)

{

int i,j,k,l,m;

char *f_Name;

f_Name="DOUBLE_ARRAY";

srand(time(NULL));

i=mint+rand()%maxt;

j=mint+rand()%maxt;

tab=malloc(i*sizeof(int*));

for(k=0;k<i;k++)

tab[k]=malloc(j*sizeof(int));

for(k=0;k<i;k++)

{

for(l=0;l<j;l++)

{

m=mint+rand()%maxt;

tab[k][l]=m;

}

}

for(k=0;k<i;k++)

my_Sort(tab[k],j);

write_Date(f_Name,tab,i,j);

 

for(k=0;k<i;k++)

free(tab[k]);

free(tab);

return 0;

}

void my_Sort(int *t, int s)

{

int i,j,k;

for(i=1;i<s;i++)

{

j=i;

k=t[j];

while((j>0) && (t[j-1]>k))

{

t[j]=t[j-1];

j--;

}

t[j]=k;

}

}

void write_Date(char *file_Name, int **t, int x, int y)

{

FILE *f;

int i,j;

f=fopen(file_Name,"w");

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

fprintf(f,"%d   ",t[i][j]);

}

fprintf(f,"\n");

}

fclose(f);

}


niedziela, 25 lipca 2021

Summing arrays [C++]

 #include <iostream>

using namespace std;

int A[4]={10,100,1000,10000};

int B[4]={20,200,2000,20000};

int C[4]={30,300,3000,30000};

int D[4]={40,400,4000,40000};

int E[4]={50,500,5000,50000};

int F[4]={60,600,6000,60000};

int G[4]={70,700,7000,70000};

int H[4]={80,800,8000,80000};

int I[4]={90,900,9000,90000};

int sa[4],sb[4],sc[4],sd[4],se[4],sf[4],sg[4],sh[4],si[4];

int k1,k2,k3,k4,k5,k6,k7,k8,k9;

int main(int argc, char **argv)

{

int i,j;

for(i=0;i<4;i++)

{

sa[i]=0;

sb[i]=0;

sc[i]=0;

sd[i]=0;

se[i]=0;

sf[i]=0;

sg[i]=0;

sh[i]=0;

si[i]=0;

}

for(j=0;j<4;j++)

{

for(k1=1;k1<A[j]+1;k1++)

sa[j]+=k1;

for(k2=1;k2<B[j]+1;k2++)

sb[j]+=k2;

for(k3=1;k3<C[j]+1;k3++)

sc[j]+=k3;

for(k4=1;k4<D[j]+1;k4++)

sd[j]+=k4;

for(k5=1;k5<E[j]+1;k5++)

se[j]+=k5;

for(k6=1;k6<F[j]+1;k6++)

sf[j]+=k6;

for(k7=1;k7<G[j]+1;k7++)

sg[j]+=k7;

for(k8=1;k8<H[j]+1;k8++)

sh[j]+=k8;

for(k9=1;k9<I[j]+1;k9++)

si[j]+=k9; 

}

for(i=0;i<4;i++)

{

cout<<sa[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<sb[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<sc[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<sd[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<se[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<sf[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<sg[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<sh[i]<<"   ";

}cout<<endl;

for(i=0;i<4;i++)

{

cout<<si[i]<<"   ";

}cout<<endl;

return 0;

}

Interesting dependencies:

55   5050   500500   50005000   

210   20100   2001000   200010000   

and

820   80200   8002000   800020000

niedziela, 4 lipca 2021

The sides of right triangle (sorting) C

 #include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define mm 20

#define max1 1000

int das1[mm],das2[mm],das3[mm];

int eas1[mm],eas2[mm],eas3[mm];

int temp[mm];

void sort_T(int* tab, int s)

{

int i,j,k;

for(i=1;i<s;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 a,b,c;

int i,j;

srand(time(NULL));

i=0;

do

{

a=1+rand()%1000;

b=1+rand()%1000;

c=1+rand()%1000;

if((a*a)+(b*b)==(c*c))

{

das1[i]=a;

das2[i]=b;

das3[i]=c;

eas3[i]=das3[i];

++i;

}

}while(i<mm);

sort_T(eas3,mm);

for(i=0;i<mm;i++)

{

for(j=0;j<mm;j++)

{

if(eas3[i]==das3[j])

temp[i]=j;

}

}

for(i=0;i<mm;i++)

{

eas1[i]=das1[temp[i]];

eas2[i]=das2[temp[i]];

}

printf("THE SIDES OF RIGHT TRIANGLE: \n");

for(j=0;j<mm;j++)

{

printf("%d  -  %d  = %d SORTING %d  - %d = %d \n",

das1[j],das2[j],das3[j],eas1[j],eas2[j],eas3[j]);

}

return 0;

}

sobota, 8 maja 2021

Password generator (C)

 #include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>


char char_Sp[25]={'~','!','@','#','$','%','^','&','*','(',')','-','+','>','{','}',

':','|','<','?','`','[',']',';',',','.'};


char Numbers[10];

char LowL[26];

char UpperL[26];


void init_Tab()

{

char i1,i2;

int j;

j=0;

for(i1='0';i1<'9'+1;i1++)

{

Numbers[j]=i1;

++j;

}

j=0;

for(i1='a',i2='A';i1<'z'+1;i1++,i2++)

{

LowL[j]=i1;

UpperL[j]=i2;

++j;

}

 

}

char write_Char()

{

int i,j;

char c;

i=rand()%4;

switch(i)

{

case 0:

j=rand()%26;

c=char_Sp[j];

break;

case 1:

j=rand()%10;

c=Numbers[j];

break;

case 2:

j=rand()%26;

c=LowL[j];

break;

case 3:

j=rand()%26;

c=UpperL[j];

break;

default:

break;

}

return c;

}


int main(int argc, char **argv)

{

srand(time(NULL));

init_Tab();

char file_Name[20];

char **passwd,res;

FILE *my_FILE;

int pas,chr,i,j;

printf("How many passwdords ? : ");

scanf("%d", &pas);

printf("How many characters ? : ");

scanf("%d", &chr);

printf("File name (max 20) ? : ");

scanf("%s",file_Name);

passwd=(char**)malloc(pas*sizeof(char*));

for(i=0;i<pas;i++)

{

passwd[i]=malloc(chr*sizeof(char*));

}

for(i=0;i<pas;i++)

{

for(j=0;j<chr;j++)

{

res=write_Char();

passwd[i][j]=res;

}

}

my_FILE=fopen(file_Name,"w");

for(i=0;i<pas;i++)

{

fprintf(my_FILE,"%s \n",passwd[i]);

}

fclose(my_FILE);

for(i=0;i<pas;i++)

free(passwd[i]);

free(passwd);

return 0;

}