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



poniedziałek, 4 stycznia 2016

Date and time in Visual C++




And code:
#pragma endregion
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
DateTime time1=DateTime::Now;
label1->Text=time1.Hour.ToString("D2")+":"+time1.Minute.ToString("D2")+
":"+time1.Second.ToString("D2");
label2->Text=time1.Year.ToString()+"-"+time1.Month.ToString()+"-"+
time1.Day.ToString()+" --- "+time1.DayOfWeek.ToString();
int second,minutes,hours,months,days;
second=time1.Second;
progressBar1->Value=second;
label6->Text=second.ToString();
minutes=time1.Minute;
progressBar2->Value=minutes;
label7->Text=minutes.ToString();
hours=time1.Hour;
progressBar3->Value=hours;
label8->Text=hours.ToString();
months=time1.Month;
dataGridView1->Rows[months-1]->DefaultCellStyle->BackColor=System::Drawing::Color::Yellow;
String^ day_of_week=time1.DayOfWeek.ToString();
if(day_of_week==Days[0])
dataGridView2->Rows[0]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[1])
dataGridView2->Rows[1]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[2])
dataGridView2->Rows[2]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[3])
dataGridView2->Rows[3]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[4])
dataGridView2->Rows[4]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[5])
dataGridView2->Rows[5]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;
else if(day_of_week==Days[6])
dataGridView2->Rows[6]->DefaultCellStyle->BackColor=System::Drawing::Color::Red;


 
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
timer1->Start();
int i;
dataGridView1->ColumnCount=1;
dataGridView1->RowCount=12;
                 array<String^>^ Months=gcnew array<String^>(12);
Months[0]=gcnew String("January");
Months[1]=gcnew String("February");
Months[2]=gcnew String("March");
Months[3]=gcnew String("April");
Months[4]=gcnew String("May");
Months[5]=gcnew String("June");
Months[6]=gcnew String("July");
Months[7]=gcnew String("August");
Months[8]=gcnew String("September");
Months[9]=gcnew String("Oktober");
Months[10]=gcnew String("November");
Months[11]=gcnew String("December");
for(i=0;i<12;i++)
dataGridView1->Rows[i]->Cells[0]->Value=Months[i];
dataGridView2->ColumnCount=1;
dataGridView2->RowCount=7;
Days=gcnew array<String^>(7);
Days[0]=gcnew String("Monday");
Days[1]=gcnew String("Tuesday");
Days[2]=gcnew String("Wednesday");
Days[3]=gcnew String("Thursday");
Days[4]=gcnew String("Friday");
Days[5]=gcnew String("Saturday");
Days[6]=gcnew String("Sunday");
for(i=0;i<7;i++)
dataGridView2->Rows[i]->Cells[0]->Value=Days[i];

}
private: System::Void Form1_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
timer1->Stop();
}
private: System::Void panel1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
}
};

niedziela, 3 stycznia 2016

Firtst numbers and difference

#include <iostream>
using namespace std;

bool my_first(int number)
{
int sum,i;
sum=0;
for(i=1;i<number+1;i++)
if(number%i==0)
 ++sum;
if(sum==2)
 return true;
else
 return false;
}

int main(int argc, char** argv)
{
int x,difference,r;
x=1;
r=0;
for(;;)
{
if(my_first(x))
{
   
   difference=r+1;
cout<<"+"<<difference<<"\n";
cout<<x<<" first"<<endl;
r=0;
}
else
{
++r;
   }
   ++x;
 }
return 0;
}

poniedziałek, 14 grudnia 2015

Testowałem funkcje graficzne w Visual C++ 2008 i przy okazji w pamięci dokonałem przekształceń symetrycznych linii i figur. Rezultatem działania jest taka kolorystyczna kompozycja kalejdoskopowa:

  

Jeśli się komuś przyda to kod do takiej zabawy:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
Graphics^ graf=panel1->CreateGraphics();
SolidBrush^ pedzel1=gcnew SolidBrush(System::Drawing::Color::DarkViolet);
SolidBrush^ pedzel2=gcnew SolidBrush(System::Drawing::Color::Orange);
SolidBrush^ pedzel3=gcnew SolidBrush(System::Drawing::Color::DarkBlue);
SolidBrush^ pedzel4=gcnew SolidBrush(System::Drawing::Color::SkyBlue);
SolidBrush^ pedzel5=gcnew SolidBrush(System::Drawing::Color::Chocolate);
Pen^ pen1=gcnew Pen(System::Drawing::Color::Green);
Pen^ pen2=gcnew Pen(System::Drawing::Color::Red);
Pen^ pen3=gcnew Pen(System::Drawing::Color::Blue);
Pen^ pen4=gcnew Pen(System::Drawing::Color::Brown);
Pen^ pen5=gcnew Pen(System::Drawing::Color::Black);
Pen^ pen6=gcnew Pen(System::Drawing::Color::DarkKhaki);
Pen^ pen7=gcnew Pen(System::Drawing::Color::Chartreuse);
System::Drawing::Font^ font1=gcnew
System::Drawing::Font(System::Drawing::FontFamily::GenericSansSerif,10,FontStyle::Underline);
int srodek_x,srodek_y,dl_x,dl_y,margines;
srodek_x=panel1->Width/2;
srodek_y=panel1->Height/2;
dl_x=panel1->Width;
dl_y=panel1->Height;
margines=5;
pen1->StartCap=System::Drawing::Drawing2D::LineCap::RoundAnchor;
pen1->EndCap=System::Drawing::Drawing2D::LineCap::ArrowAnchor;
pen1->Width=5;
graf->DrawLine(pen1,margines,dl_y-margines,dl_x-margines,margines);
graf->DrawLine(pen1,dl_x-margines,dl_y-margines,margines,margines);
pen2->DashStyle=System::Drawing::Drawing2D::DashStyle::Dot;
pen2->Width=3;
graf->DrawLine(pen2,margines,srodek_y,dl_x-margines,srodek_y);
graf->DrawLine(pen2,srodek_x,margines,srodek_x,dl_y-margines);
pen3->Width=4;
graf->DrawRectangle(pen3,srodek_x-150,srodek_y-150,300,300);
pen4->Width=4;
graf->DrawEllipse(pen4,srodek_x-100,srodek_y-100,200,200);
pen5->Width=4;
graf->DrawEllipse(pen5,srodek_x-200,srodek_y-200,100,100);
graf->DrawEllipse(pen5,srodek_x+100,srodek_y-200,100,100);
graf->DrawEllipse(pen5,srodek_x-200,srodek_y+100,100,100);
graf->DrawEllipse(pen5,srodek_x+100,srodek_y+100,100,100);
pen6->Width=4;
pen6->DashStyle=System::Drawing::Drawing2D::DashStyle::Dash;
pen6->DashCap=System::Drawing::Drawing2D::DashCap::Triangle;
graf->DrawLine(pen6,margines,srodek_y,srodek_x,margines);
graf->DrawLine(pen6,srodek_x,margines,dl_x-margines,srodek_y);
graf->DrawLine(pen6,dl_x-margines,srodek_y,srodek_x,dl_y-margines);
graf->DrawLine(pen6,srodek_x,dl_y-margines,margines,srodek_y);
pen7->Width=3;
graf->DrawRectangle(pen7,srodek_x-40,srodek_y-40,80,80);
graf->FillRectangle(pedzel1,margines+20,margines+20,75,75);
graf->FillRectangle(pedzel1,dl_x-(margines+20+75),margines+20,75,75);
graf->FillRectangle(pedzel1,margines+20,dl_y-(margines+20+75),75,75);
graf->FillRectangle(pedzel1,dl_x-(margines+20+75),dl_y-(margines+20+75),75,75);
graf->FillRectangle(pedzel2,srodek_x-(150+25),srodek_y-25,50,50);
graf->FillRectangle(pedzel2,srodek_x+(150-25),srodek_y-25,50,50);
graf->FillRectangle(pedzel2,srodek_x-25,srodek_y-(150+25),50,50);
graf->FillRectangle(pedzel2,srodek_x-25,srodek_y+(150-25),50,50);
graf->FillEllipse(pedzel3,srodek_x-25,srodek_y-25,50,50);
graf->FillEllipse(pedzel4,srodek_x-15,margines+55,30,30);
graf->FillEllipse(pedzel4,srodek_x-15,dl_y-(margines+85),30,30);
graf->FillEllipse(pedzel4,margines+55,srodek_y-15,30,30);
graf->FillEllipse(pedzel4,dl_x-(margines+85),srodek_y-15,30,30);
graf->DrawString("Graph of Functions in Visual C++",font1,pedzel5,srodek_x+15,margines);
 

 


}

sobota, 21 listopada 2015

Klasa służąca do badania występowania, prawa do czytania i zapisywania pliku z konstruktora.

class Dostep_do_pliku
{
private:
char *nazwa_pliku;
bool czy_wystepuje(int x)
{
if(x==0)
  return true;
else
  return false;
}
 
public:
Dostep_do_pliku(char *plik)
{
nazwa_pliku=plik;
     } 
void Czy_istnieje()
{
if(czy_wystepuje(access(nazwa_pliku,F_OK)))
 cout<<"plik - "<<nazwa_pliku<<" istnieje"<<endl;
else
{
if(errno==ENOENT)
 cout<<"Plik "<<nazwa_pliku<<" nie istnieje"<<endl;
 else
   if(errno==EACCES)
     cout<<"Plik "<<nazwa_pliku<<" nie jest dostepny"<<endl;
}
 
}
void Czy_do_odczytu()
{
if(czy_wystepuje(access(nazwa_pliku,R_OK)))
 cout<<"plik - "<<nazwa_pliku<<" mozna czytac"<<endl; 
 else
 cout<<"plik - "<<nazwa_pliku<<" nie mozna czytac"<<endl;
}
void Czy_do_zapisu()
{
if(czy_wystepuje(access(nazwa_pliku,W_OK)))
 cout<<"plik - "<<nazwa_pliku<<" mozna zapisywac"<<endl;
else
 if(errno==EACCES)
  cout<<"plik - "<<nazwa_pliku<< "nie mozna zapisac (brak dostepu)"<<endl;
 else 
   if(errno==EROFS)
  cout<<" plik - "<<nazwa_pliku<<" nie mozna zapisac (tylko do odczytu)"<<endl;
}
 
};