sobota, 19 listopada 2016
Using structs and class in C++
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
class sum_Table
{
private:
int *t;
int n,sum;
public:
sum_Table(int *table, int x)
{
sum=0;
n=x;
int i;
t=new int[n];
for(i=0;i<n;i++)
{
t[i]=table[i];
sum+=t[i];
}
}
int sum_Tab()
{
return sum;
}
~sum_Table()
{
delete t;
}
};
class Connect_Table
{
private:
int *t1,*t2,*t3;
int n;
int sum;
int m1,m2,m3;
public:
int *connect_t;
Connect_Table(int *table1,int n1,int *table2, int n2,int *table3, int n3)
{
t1=new int[n1];
t2=new int[n2];
t3=new int[n3];
sum=n=0;
m1=n1;
m2=n2;
m3=n3;
n=n1+n2+n3;
connect_t=new int[n+1];
int i,j;
for(i=0;i<m1;i++)
{
t1[i]=table1[i];
connect_t[i]=t1[i];
}
for(i=m1,j=0;i<m1+m2;i++,++j)
{
t2[j]=table2[j];
connect_t[i]=t2[j];
}
for(i=m1+m2,j=0;i<m1+m2+m3;i++,++j)
{
t3[j]=table3[j];
connect_t[i]=t3[j];
}
}
int sum_Connected()
{
Connect_Table Temp_Connect(t1,m1,t2,m2,t3,m3);
sum_Table sum_C(Temp_Connect.connect_t,n);
sum=sum_C.sum_Tab();
return sum;
}
void write_Con()
{
int i;
for(i=0;i<n;i++)
{
cout<<connect_t[i]<<endl;
}
}
~Connect_Table()
{
delete connect_t;
delete t1,t2,t3;
}
};
struct str_month
{
string name;
int days;
};
str_month str_m[12];
struct time_values
{
int days;
int hours;
int minuts;
int seconds;
};
struct time_values *total_values;
void init_struct()
{
total_values=new struct time_values[4];
str_m[0].name="January";
str_m[0].days=31;
str_m[1].name="February";
str_m[1].days=28;
str_m[2].name="March";
str_m[2].days=31;
str_m[3].name="April";
str_m[3].days=30;
str_m[4].name="May";
str_m[4].days=31;
str_m[5].name="June";
str_m[5].days=30;
str_m[6].name="Jule";
str_m[6].days=31;
str_m[7].name="August";
str_m[7].days=31;
str_m[8].name="September";
str_m[8].days=30;
str_m[9].name="Oktober";
str_m[9].days=31;
str_m[10].name="November";
str_m[10].days=30;
str_m[11].name="December";
str_m[11].days=31;
}
class Spring
{
private:
int sum_days,sum_minuts,sum_hours,sum_seconds,size,i;
int *table_days1,*table_minuts1,*table_hours1,*table_seconds1;
int *table_days2,*table_minuts2,*table_hours2,*table_seconds2;
int *table_days3,*table_minuts3,*table_hours3,*table_seconds3;
string month1,month2,month3;
int days1,days2,days3,sum;
public:
Spring()
{
month1=str_m[2].name;
days1=str_m[2].days;
table_days1=new int[days1];
table_minuts1=new int[days1];
table_hours1=new int[days1];
table_seconds1=new int[days1];
sum_days=sum_minuts=sum_seconds=sum_hours=0;
for(i=0;i<days1;i++)
{
table_days1[i]=1;
table_hours1[i]=24;
table_minuts1[i]=24*60;
table_seconds1[i]=24*60*60;
}
month2=str_m[3].name;
days2=str_m[3].days;
table_days2=new int[days2];
table_minuts2=new int[days2];
table_hours2=new int[days2];
table_seconds2=new int[days2];
for(i=0;i<days2;i++)
{
table_days2[i]=1;
table_hours2[i]=24;
table_minuts2[i]=24*60;
table_seconds2[i]=24*60*60;
}
month3=str_m[4].name;
days3=str_m[4].days;
table_days3=new int[days3];
table_minuts3=new int[days3];
table_hours3=new int[days3];
table_seconds3=new int[days3];
for(i=0;i<days3;i++)
{
table_days3[i]=1;
table_hours3[i]=24;
table_minuts3[i]=24*60;
table_seconds3[i]=24*60*60;
}
Connect_Table Connect_D(table_days1,days1,table_days2,days2,table_days3,days3);
sum_days=Connect_D.sum_Connected();
Connect_Table Connect_H(table_hours1,days1,table_hours2,days2,table_hours3,days3);
sum_hours=Connect_H.sum_Connected();
Connect_Table Connect_M(table_minuts1,days1,table_minuts2,days2,table_minuts3,days3);
sum_minuts=Connect_M.sum_Connected();
Connect_Table Connect_S(table_seconds1,days1,table_seconds2,days2,table_seconds3,days3);
sum_seconds=Connect_S.sum_Connected();
total_values[0].days=sum_days;
total_values[0].hours=sum_hours;
total_values[0].minuts=sum_minuts;
total_values[0].seconds=sum_seconds;
}
void write_Spring()
{
cout<<month1<<" has "<<days1<<" days"<<endl;
cout<<month2<<" has "<<days2<<" days"<<endl;
cout<<month3<<" has "<<days3<<" days"<<endl;
cout<<"Total: "<<endl;
cout<<"Days -"<<sum_days<<endl;
cout<<"Hours -"<<sum_hours<<endl;
cout<<"Minutes -"<<sum_minuts<<endl;
cout<<"Seconds -"<<sum_seconds<<endl;
int i;
}
~Spring()
{
delete table_days1;
delete table_days2;
delete table_days3;
delete table_hours1;
delete table_hours2;
delete table_hours3;
delete table_minuts1;
delete table_minuts2;
delete table_minuts3;
delete table_seconds1;
delete table_seconds2;
delete table_seconds3;
}
};
class Summer
{
private:
int sum_days,sum_minuts,sum_hours,sum_seconds,size,i;
int *table_days1,*table_minuts1,*table_hours1,*table_seconds1;
int *table_days2,*table_minuts2,*table_hours2,*table_seconds2;
int *table_days3,*table_minuts3,*table_hours3,*table_seconds3;
string month1,month2,month3;
int days1,days2,days3,sum;
public:
Summer()
{
month1=str_m[5].name;
days1=str_m[5].days;
table_days1=new int[days1];
table_minuts1=new int[days1];
table_hours1=new int[days1];
table_seconds1=new int[days1];
sum_days=sum_minuts=sum_seconds=sum_hours=0;
for(i=0;i<days1;i++)
{
table_days1[i]=1;
table_hours1[i]=24;
table_minuts1[i]=24*60;
table_seconds1[i]=24*60*60;
}
month2=str_m[6].name;
days2=str_m[6].days;
table_days2=new int[days2];
table_minuts2=new int[days2];
table_hours2=new int[days2];
table_seconds2=new int[days2];
for(i=0;i<days2;i++)
{
table_days2[i]=1;
table_hours2[i]=24;
table_minuts2[i]=24*60;
table_seconds2[i]=24*60*60;
}
month3=str_m[7].name;
days3=str_m[7].days;
table_days3=new int[days3];
table_minuts3=new int[days3];
table_hours3=new int[days3];
table_seconds3=new int[days3];
for(i=0;i<days3;i++)
{
table_days3[i]=1;
table_hours3[i]=24;
table_minuts3[i]=24*60;
table_seconds3[i]=24*60*60;
}
Connect_Table Connect_D(table_days1,days1,table_days2,days2,table_days3,days3);
sum_days=Connect_D.sum_Connected();
Connect_Table Connect_H(table_hours1,days1,table_hours2,days2,table_hours3,days3);
sum_hours=Connect_H.sum_Connected();
Connect_Table Connect_M(table_minuts1,days1,table_minuts2,days2,table_minuts3,days3);
sum_minuts=Connect_M.sum_Connected();
Connect_Table Connect_S(table_seconds1,days1,table_seconds2,days2,table_seconds3,days3);
sum_seconds=Connect_S.sum_Connected();
total_values[1].days=sum_days;
total_values[1].hours=sum_hours;
total_values[1].minuts=sum_minuts;
total_values[1].seconds=sum_seconds;
}
void write_Summer()
{
cout<<month1<<" has "<<days1<<" days"<<endl;
cout<<month2<<" has "<<days2<<" days"<<endl;
cout<<month3<<" has "<<days3<<" days"<<endl;
cout<<"Total: "<<endl;
cout<<"Days -"<<sum_days<<endl;
cout<<"Hours -"<<sum_hours<<endl;
cout<<"Minutes -"<<sum_minuts<<endl;
cout<<"Seconds -"<<sum_seconds<<endl;
int i;
}
~Summer()
{
delete table_days1;
delete table_days2;
delete table_days3;
delete table_hours1;
delete table_hours2;
delete table_hours3;
delete table_minuts1;
delete table_minuts2;
delete table_minuts3;
delete table_seconds1;
delete table_seconds2;
delete table_seconds3;
}
};
class Autumn
{
private:
int sum_days,sum_minuts,sum_hours,sum_seconds,size,i;
int *table_days1,*table_minuts1,*table_hours1,*table_seconds1;
int *table_days2,*table_minuts2,*table_hours2,*table_seconds2;
int *table_days3,*table_minuts3,*table_hours3,*table_seconds3;
string month1,month2,month3;
int days1,days2,days3,sum;
public:
Autumn()
{
month1=str_m[8].name;
days1=str_m[8].days;
table_days1=new int[days1];
table_minuts1=new int[days1];
table_hours1=new int[days1];
table_seconds1=new int[days1];
sum_days=sum_minuts=sum_seconds=sum_hours=0;
for(i=0;i<days1;i++)
{
table_days1[i]=1;
table_hours1[i]=24;
table_minuts1[i]=24*60;
table_seconds1[i]=24*60*60;
}
month2=str_m[9].name;
days2=str_m[9].days;
table_days2=new int[days2];
table_minuts2=new int[days2];
table_hours2=new int[days2];
table_seconds2=new int[days2];
for(i=0;i<days2;i++)
{
table_days2[i]=1;
table_hours2[i]=24;
table_minuts2[i]=24*60;
table_seconds2[i]=24*60*60;
}
month3=str_m[10].name;
days3=str_m[10].days;
table_days3=new int[days3];
table_minuts3=new int[days3];
table_hours3=new int[days3];
table_seconds3=new int[days3];
for(i=0;i<days3;i++)
{
table_days3[i]=1;
table_hours3[i]=24;
table_minuts3[i]=24*60;
table_seconds3[i]=24*60*60;
}
Connect_Table Connect_D(table_days1,days1,table_days2,days2,table_days3,days3);
sum_days=Connect_D.sum_Connected();
Connect_Table Connect_H(table_hours1,days1,table_hours2,days2,table_hours3,days3);
sum_hours=Connect_H.sum_Connected();
Connect_Table Connect_M(table_minuts1,days1,table_minuts2,days2,table_minuts3,days3);
sum_minuts=Connect_M.sum_Connected();
Connect_Table Connect_S(table_seconds1,days1,table_seconds2,days2,table_seconds3,days3);
sum_seconds=Connect_S.sum_Connected();
total_values[2].days=sum_days;
total_values[2].hours=sum_hours;
total_values[2].minuts=sum_minuts;
total_values[2].seconds=sum_seconds;
}
void write_Autumn()
{
cout<<month1<<" has "<<days1<<" days"<<endl;
cout<<month2<<" has "<<days2<<" days"<<endl;
cout<<month3<<" has "<<days3<<" days"<<endl;
cout<<"Total: "<<endl;
cout<<"Days -"<<sum_days<<endl;
cout<<"Hours -"<<sum_hours<<endl;
cout<<"Minutes -"<<sum_minuts<<endl;
cout<<"Seconds -"<<sum_seconds<<endl;
int i;
}
~Autumn()
{
delete table_days1;
delete table_days2;
delete table_days3;
delete table_hours1;
delete table_hours2;
delete table_hours3;
delete table_minuts1;
delete table_minuts2;
delete table_minuts3;
delete table_seconds1;
delete table_seconds2;
delete table_seconds3;
}
};
class Winter
{
private:
int sum_days,sum_minuts,sum_hours,sum_seconds,size,i;
int *table_days1,*table_minuts1,*table_hours1,*table_seconds1;
int *table_days2,*table_minuts2,*table_hours2,*table_seconds2;
int *table_days3,*table_minuts3,*table_hours3,*table_seconds3;
string month1,month2,month3;
int days1,days2,days3,sum;
public:
Winter()
{
month1=str_m[11].name;
days1=str_m[11].days;
table_days1=new int[days1];
table_minuts1=new int[days1];
table_hours1=new int[days1];
table_seconds1=new int[days1];
sum_days=sum_minuts=sum_seconds=sum_hours=0;
for(i=0;i<days1;i++)
{
table_days1[i]=1;
table_hours1[i]=24;
table_minuts1[i]=24*60;
table_seconds1[i]=24*60*60;
}
month2=str_m[0].name;
days2=str_m[0].days;
table_days2=new int[days2];
table_minuts2=new int[days2];
table_hours2=new int[days2];
table_seconds2=new int[days2];
for(i=0;i<days2;i++)
{
table_days2[i]=1;
table_hours2[i]=24;
table_minuts2[i]=24*60;
table_seconds2[i]=24*60*60;
}
month3=str_m[1].name;
days3=str_m[1].days;
table_days3=new int[days3];
table_minuts3=new int[days3];
table_hours3=new int[days3];
table_seconds3=new int[days3];
for(i=0;i<days3;i++)
{
table_days3[i]=1;
table_hours3[i]=24;
table_minuts3[i]=24*60;
table_seconds3[i]=24*60*60;
}
Connect_Table Connect_D(table_days1,days1,table_days2,days2,table_days3,days3);
sum_days=Connect_D.sum_Connected();
Connect_Table Connect_H(table_hours1,days1,table_hours2,days2,table_hours3,days3);
sum_hours=Connect_H.sum_Connected();
Connect_Table Connect_M(table_minuts1,days1,table_minuts2,days2,table_minuts3,days3);
sum_minuts=Connect_M.sum_Connected();
Connect_Table Connect_S(table_seconds1,days1,table_seconds2,days2,table_seconds3,days3);
sum_seconds=Connect_S.sum_Connected();
total_values[3].days=sum_days;
total_values[3].hours=sum_hours;
total_values[3].minuts=sum_minuts;
total_values[3].seconds=sum_seconds;
}
void write_Winter()
{
cout<<month1<<" has "<<days1<<" days"<<endl;
cout<<month2<<" has "<<days2<<" days"<<endl;
cout<<month3<<" has "<<days3<<" days"<<endl;
cout<<"Total: "<<endl;
cout<<"Days -"<<sum_days<<endl;
cout<<"Hours -"<<sum_hours<<endl;
cout<<"Minutes -"<<sum_minuts<<endl;
cout<<"Seconds -"<<sum_seconds<<endl;
int i;
}
~Winter()
{
delete table_days1;
delete table_days2;
delete table_days3;
delete table_hours1;
delete table_hours2;
delete table_hours3;
delete table_minuts1;
delete table_minuts2;
delete table_minuts3;
delete table_seconds1;
delete table_seconds2;
delete table_seconds3;
}
};
int main(int argc, char **argv)
{
init_struct();
Spring Sp;
cout<<"-------------SPRING-------------";
cout<<endl;
Sp.write_Spring();
cout<<endl;
cout<<"-------------SUMMER--------------";
cout<<endl;
Summer Su;
Su.write_Summer();
cout<<endl;
cout<<"-------------AUTUMN--------------";
cout<<endl;
Autumn Au;
Au.write_Autumn();
cout<<endl;
cout<<"-------------WINTER--------------";
cout<<endl;
Winter Wi;
Wi.write_Winter();
cout<<endl;
int i;
int total_day=0,total_hour=0,total_minuts=0,total_seconds=0;
for(i=0;i<4;i++)
{
total_day+=total_values[i].days;
total_hour+=total_values[i].hours;
total_minuts+=total_values[i].minuts;
total_seconds+=total_values[i].seconds;
}
cout<<"-------------TOTAL YEAR-------------";
cout<<endl;
cout<<"days - "<<total_day<<endl;
cout<<"hours - "<<total_hour<<endl;
cout<<"minuts - "<<total_minuts<<endl;
cout<<"seconds - "<<total_seconds<<endl;
delete total_values;
return 0;
}
Subskrybuj:
Komentarze do posta (Atom)
Brak komentarzy:
Prześlij komentarz