PDA

Просмотр полной версии : Задачка на С =/


HellFire
17.12.2009, 23:59
Помогите решить задачку.

Текущее показание часов(h от 0 до 23) ,минут(м от 0 до 59),секунд (s от 0 до 59).
Какое время покажут часы через p часов, d минут, с секунд.

вот код написал:

#include <conio.h>
#include <iostream.h>



main()
{
clrscr();

int a,h,m,s,p,d,c;

start: cout<<"Введите время"<<endl;
chasi: cout<<"часы:";
cin>>h;
if((h<0)||(h>23))
{
cout<<"Ошибка времени.Введите заного ";
goto chasi;
}

min: cout<<"минуты:";
cin>>m;
if((m<0)||(m>59))
{
cout<<"Ошибка времени.Введите заного ";
goto min;
}
sec: cout<<"секунды:";
cin>>s;
if((s<0)||(s>59))
{
cout<<"Ошибка времени.Введите заного ";
goto sec;
}

cout<<"Введите интервал времени через,который будет показано время"<<endl;

cout<<"часы:";
cin>>p;

cout<<"минуты:";
cin>>d;

cout<<"секунды:";
cin>>c;

s=s+c;
m=m+d+s/60;
s=s%60;
h=h+p+m/60;
m=m%60;
d=h/24;
h=h%24;

cout<<"Будет "<<h<<" часов "<<m<<" минут "<<s<<" секунд через "<<d<<" дней"<<endl;

cout<<"\n \n \n \n \n \n Повторить ещё раз ? 1-ДА,Любое число-HET"<<endl;
cin>>a;
if(a==1) goto start;
return 0;
}


Вот в чём проблемка:
Есть вторая задача, в которой надо сделать то-же самое,что и в первой, но во второй надо изменить код так,чтоб выводилось время в АМ, PM.
(AM до 12 часов, PM после 12 часов)

как я понял надо красную часть заменить + мелкие дописки ввиде расчётов,но как расчитывать хз=/
заранее большое спасибо.

Ivan_64
18.12.2009, 12:20
Решение неправильное - как всегда намутил невесть что с этим ООП... Не работает зато со свистом, называется.


#include <iostream>
#include <conio.h>
using namespace std;
class Clock
{
public:
void printTime()
{
cout<<"Current time: "<<this->hours<<" : "<<this->minutes<<" : "<<this->seconds<<endl;
}
void printExTime()
{
cout<<"Current time: "<<this->TChours<<" : "<<this->TCminutes<<" : "<<this->TCseconds;
if(PM=true)cout<<"PM";else cout<<"AM";
cout<<endl;
}
Clock(int dHou,int dMin,int dSec):hours(dHou),minutes(dMin),seconds(dSec){prin tTime();};
Clock(){Clock(0,0,0);}
void SetTime(int a,int b,int c)
{
this->hours=a>23?0:a;
this->minutes=b>59?0:b;
this->seconds=c>59?0:c;
this->printExTime();
}
void AddTime(int a,int b,int c)
{
int temp=a*3600+b*60+c;
temp=(temp+(this->hours*3600+this->minutes*60+this->seconds))%86400;
this->absTime=temp;
this->hours=temp/3600;
this->minutes=(temp%3600)/60;
this->seconds=(temp%60);
if(this->hours>11)
{
this->TChours=this->hours-12;
PM=true;
}
else
{
this->TChours=this->hours-12;
PM=false;
}
this->TCminutes=this->minutes;
this->TCseconds=this->seconds;
this->printExTime();
}
private:
int hours,TChours;
int minutes,TCminutes;
int seconds,TCseconds;
int absTime;
bool PM; // 0= AM 0= PM
};
int main()
{
Clock* clk=new Clock();
//clk->SetTime(21,54,59);
clk->AddTime(742,89,38);
getch();
return 0;
}



#include <iostream>
#include <conio.h>
using namespace std;
class Clock
{
public:
void printTime()
{
cout<<"Current time: "<<this->hours<<" : "<<this->minutes<<" : "<<this->seconds<<endl;
}
void printExTime()
{
cout<<"Current time: "<<this->TChours<<" : "<<this->TCminutes<<" : "<<this->TCseconds;
if(PM=true)cout<<"PM";else cout<<"AM";
cout<<endl;
}
Clock(int dHou,int dMin,int dSec):hours(dHou),minutes(dMin),seconds(dSec){prin tTime();};
Clock(){Clock(0,0,0);}
void SetTime(int a,int b,int c)
{
this->hours=a>23?0:a;
this->minutes=b>59?0:b;
this->seconds=c>59?0:c;
this->printExTime();
}
void AddTime(int a,int b,int c)
{
int temp=a*3600+b*60+c;
temp=(temp+(this->hours*3600+this->minutes*60+this->seconds))%86400;
this->absTime=temp;
this->hours=temp/3600;
this->minutes=(temp%3600)/60;
this->seconds=(temp%60);
if(this->hours>11)
{
this->TChours=this->hours-12;
PM=true;
}
else
{
this->TChours=this->hours-12;
PM=false;
}
this->TCminutes=this->minutes;
this->TCseconds=this->seconds;
this->printExTime();
}
private:
int hours,TChours;
int minutes,TCminutes;
int seconds,TCseconds;
int absTime;
bool PM; // 0= AM 0= PM
};
int main()
{
Clock* clk=new Clock();
//clk->SetTime(21,54,59);
clk->AddTime(742,89,38);
getch();
return 0;
}


Сделал вот такой класс - так думаю даже лучше:)

HellFire
18.12.2009, 13:08
Решение неправильное - как всегда намутил невесть что с этим ООП... Не работает зато со свистом, называется.

всё равно спасибо за уделённое время.

решение скоро выложу(может кому пригодится), там всего в 3-4 строки решается,но я забыл как....