Jump to content

Nautical bell: Difference between revisions

m (→‎{{header|C}}: remove attribution)
Line 144:
=={{header|C}}==
 
Implementation corrected, sounds the system bell as per nautical standards.
{{incorrect|C|A nautical bell ''never'' sounds more than eight times. The number of chimes indicate how many half-hour increments have passed on a four hour 'watch'.}}
 
<lang C>
Sounds the bell once every half hour (12:30, 1:30 etc.) and for as many times on the hour (12 times on 12 PM/AM, 3 times on 3 AM/PM etc.)
/*Abhishek Ghosh, Original : 20th September 2017, Corrected : 25th September 2017*/
<lang C>#include<unistd.h>
 
<lang C>#include<unistd.h>
#include<stdio.h>
#include<time.h>
 
#define DELAYSHORTLAG 30001000
#define LONGLAG 2000
 
int main(){
int i,times,hour,min,sec,min1,min2;
time_t t;
Line 163 ⟶ 166:
currentTime = localtime(&t);
timeshour = (currentTime->tm_hour%12==0)?12:currentTime->tm_hour%12;
min = currentTime->tm_min;
if(currentTime->tm_minsec ==0 && currentTime->tm_sec==0){;
hour = 12;
if(currentTime->tm_min==0 && currentTime->tm_sec==0){
min = 0;
printf("\nIt is now %d:00 %s. Sounding the bell %d times.",times,(currentTime->tm_hour>11)?"PM":"AM",times);
sec = 0;
if((min==0 || min==30) && sec==0)
for(i=0;i<times;i++){
times = ((hour*60 + min)%240)%8;
printf("\a");
if(times==0){
sleep(DELAY);
}times = 8;
}
 
if(min==0){
min1 = 0;
min2 = 0;
}
else{
else if(currentTime->tm_min==30 && currentTime->tm_sec==0){
min1 = 3;
printf("\nIt is now %d:30 %s. Sounding the bell once.",times,(currentTime->tm_hour>11)?"PM":"AM");
printf("\a")min2 = 0;
}
if((min==0 || min==30) && sec==0){
printf("\nIt is now %d:00%d%d %s. Sounding the bell %d times.",timeshour,min1,min2,(currentTime->tm_hourhour>11)?"PM":"AM",times);
for(i=01;i<=times;i++){
printf("\a");
(i%2==0)?sleep(LONGLAG):sleep(SHORTLAG);
}
}
}
return 0;
}
 
</lang>
 
503

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.