niedziela, 21 lutego 2016

Czasami się przydaje. Standardowe równanie kwadratowe ujęte w jednej klasie:

class Pierwiastki
{
private:
int a,b,c;
double x1,x2;
int delta;
public:
Pierwiastki(int a, int b, int c);
void Wypisz();
};
Pierwiastki::Pierwiastki(int x, int y, int z)
{
a=x;
b=y;
c=z;
delta=(b*b)-(4*a*c);
}
void Pierwiastki::Wypisz()
{
if(delta<0)
{
cout<<"Delta<0, brak pieriwastków równania"<<endl;
}
else if(delta>0)
{
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
cout<<"Delta= "<<delta<<" x1 = "<<x1<<" x2  = "<<x2<<endl;
}
else
{
x1=-b/(2*a);
x2=x1;
cout<<"Delta = 0, x1 i x2 = "<<x1<<endl;
}
}
Creating new file with timestamp in class C++.




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


using namespace std;

class New_File_with_timestamp
{
private:
time_t time_now;
char *time1;
char *path;
int file;
mode_t authorization;
size_t length;
public:
New_File_with_timestamp(char *tekst)
{
time_now=time(NULL);

authorization=S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
path=tekst;
file=open(path,O_WRONLY | O_EXCL | O_CREAT, authorization);
time1=asctime(localtime(&time_now));
length=strlen(time1);
}
void Write()
{
write(file,time1,length);
close(file);
}
};

int main(int argc, char** argv)
{
New_File_with_timestamp NFWT("my_file_with_stamp");
NFWT.Write();

return 0;
}

wtorek, 2 lutego 2016

Check access files (C++ with class)

#include <iostream>
#include <errno.h>
#include <unistd.h>
using namespace std;

class File_Access
{
private:
char *file;
int x;

public:
File_Access(char *text)
{
file=text;

}
void Exist_Access()
{
x=access(file,F_OK);
if(x==0)
cout<<"File - "<<file<<" exists"<<endl;
else 
{
if(errno==ENOENT)
cout<<"File - "<<file<<" not exit"<<endl;
else if(errno==EACCES)
cout<<"File - "<<file<<"  is not accessbility"<<endl;
}

}
void Write_Access()
{
x=access(file,W_OK);
if(x==0)
cout<<"You can write - "<<file<<endl;
else if (errno==EACCES)
cout<<"You can't write (not accessbility) - "<<file<<endl;
else if (errno==EROFS)
cout<<"You can't write (system readonly) - "<<file<<endl;
}
void Read_Access()
{
x=access(file,R_OK);
if(x==0)
cout<<"You can read - "<<file<<endl;
else
cout<<"You can't read (not accessbility) - "<<file<<endl;
}
};

int main(int argc, char** argv)
{
char my_file[100];
cin>>my_file;
File_Access Check_Access(my_file);
Check_Access.Exist_Access();
Check_Access.Read_Access();
Check_Access.Write_Access();

return 0;
}