ccc chaplja <3
•
19 Oct 2010, 13:55
•
Journals
So last week we got a task from our lecturer to make a small program in c giving you how many hours, seconds & minuts coming out of a decided amount of seconds.
"#include <stdio.h>
int main(void) {
int Hora,Miner,Secundus,Plaga;
printf("Penetro amplitudo of secundus: ");
scanf("%d", &Secundus);
Hora = Secundus/3600;
Plaga = Secundus%3600;
Miner = Plaga/60;
Plaga = Plaga%60;
Secundus = Plaga;
printf("Hora: %d Miner: %d Secundus: %d\n", Hora,Miner,Secundus);
return 0;
}
"
After he saw that I've made it in latin he asked me to make it a bit more special.
He want's me to make a similar program just this time when writing a special amount of seconds it only has to write the correct variables used, if you know what I mean.
Like 60 secs is printed ONLY as "1 min" or in this case "0 Mine" instead of "1 Hora, 1 Miner 0 Secundus"
I mean it has to remove the plural form of the variables and the not used variables.
Messy I know, someone got a idea? I would really hate starting using 1000000000000000 elseif commands...
Seems like chaplja is still the king.
Simple is simple
#include <stdio.h>
int main(void) {
int Hora,Miner,Secundus,Plaga;
printf("Penetro amplitudo of secundus: ");
scanf("%d", &Secundus);
Hora = Secundus/3600;
Plaga = Secundus%3600;
Miner = Plaga/60;
Plaga = Plaga%60;
Secundus = Plaga;
if (Hora) printf("%d %s ", Hora, Hora == 1 ? "time" : "timer");
if (Miner) printf("%d %s ", Miner, Miner == 1 ? "minut" : "minutter");
if (Secundus) printf("%d %s", Secundus, Secundus == 1 ? "sekund" : "sekunder");
printf("\n");
return 0;
}
"#include <stdio.h>
int main(void) {
int Hora,Miner,Secundus,Plaga;
printf("Penetro amplitudo of secundus: ");
scanf("%d", &Secundus);
Hora = Secundus/3600;
Plaga = Secundus%3600;
Miner = Plaga/60;
Plaga = Plaga%60;
Secundus = Plaga;
printf("Hora: %d Miner: %d Secundus: %d\n", Hora,Miner,Secundus);
return 0;
}
"
After he saw that I've made it in latin he asked me to make it a bit more special.
He want's me to make a similar program just this time when writing a special amount of seconds it only has to write the correct variables used, if you know what I mean.
Like 60 secs is printed ONLY as "1 min" or in this case "0 Mine" instead of "1 Hora, 1 Miner 0 Secundus"
I mean it has to remove the plural form of the variables and the not used variables.
Messy I know, someone got a idea? I would really hate starting using 1000000000000000 elseif commands...
Seems like chaplja is still the king.
Simple is simple
#include <stdio.h>
int main(void) {
int Hora,Miner,Secundus,Plaga;
printf("Penetro amplitudo of secundus: ");
scanf("%d", &Secundus);
Hora = Secundus/3600;
Plaga = Secundus%3600;
Miner = Plaga/60;
Plaga = Plaga%60;
Secundus = Plaga;
if (Hora) printf("%d %s ", Hora, Hora == 1 ? "time" : "timer");
if (Miner) printf("%d %s ", Miner, Miner == 1 ? "minut" : "minutter");
if (Secundus) printf("%d %s", Secundus, Secundus == 1 ? "sekund" : "sekunder");
printf("\n");
return 0;
}
but why dont you use elseif, just copypaste and edit the code a bit. wont take long
arovca10 at student.aau.dk
Can you send it there? :(
if (timer > 1 && minutter > 1 && sekunder > 1){
printf("%d timer %d minutter %d sekunder", timer, minutter, sekunder);
} else if (timer > 1 && minutter == 1 && sekunder > 1){
printf("%d timer %d minut %d sekunder", timer, minutter, sekunder);
} else if (timer > 1 && minutter > 1 && sekunder == 1){
printf("%d timer %d minutter %d sekundt", timer, minutter, sekunder);
} else if (timer > 1 && minutter > 1 && sekunder == 1){
printf("%d timer %d minutter %d sekundt", timer, minutter, sekunder);
} else if (timer > 1 && minutter > 1 && sekunder < 1){
printf("%d timer %d minutter", timer, minutter);
} else if (timer > 1 && minutter == 1 && sekunder < 1){
printf("%d timer %d minut", timer, minutter);
} else if (timer == 1 && minutter == 1 && sekunder == 1){
printf("%d time %d minut %d sekund", timer, minutter, sekunder);
} else if (timer == 1 && minutter == 1 && sekunder > 1){
printf("%d time %d minut %d sekunder", timer, minutter, sekunder);
} else if (timer == 1 && minutter > 1 && sekunder == 1){
printf("%d time %d minutter %d sekundt", timer, minutter, sekunder);
} else if (timer == 1 && minutter > 1 && sekunder > 1){
printf("%d time %d minutter %d sekunder", timer, minutter, sekunder);
} else if (timer < 1 && minutter > 1 && sekunder > 1) {
printf(" %d minutter %d sekunder", minutter, sekunder);
} else if (timer < 1 && minutter == 1 && sekunder > 1) {
printf(" %d minut %d sekunder", minutter, sekunder);
} else if (timer < 1 && minutter == 1 && sekunder == 1) {
printf(" %d minut %d sekundt", minutter, sekunder);
} else if (timer < 1 && minutter > 1 && sekunder == 1) {
printf(" %d minutter %d sekundt", minutter, sekunder);
} else if (timer < 1 && minutter == 1 && sekunder == 1) {
printf(" %d minut %d sekundt", minutter, sekunder);
} else if (timer < 1 && minutter < 1 && sekunder > 1) {
printf("%d sekunder", sekunder);
} else if (timer < 1 && minutter < 1 && sekunder == 1) {
printf("%d sekundt", sekunder);
} else if (timer > 1 && minutter < 1 && sekunder > 1) {
printf(" %d timer %d sekunder", timer, sekunder);
} else if (timer > 1 && minutter == 1 && sekunder == 1) {
printf(" %d timer %d minut %d sekundt", timer, minutter, sekunder);
} else if (timer == 1 && minutter < 1 && sekunder == 1) {
printf(" %d time %d sekundt", timer, sekunder);
} else if (timer > 1 && minutter < 1 && sekunder < 1) {
printf("%d timer", timer);
} else if (timer == 1 && minutter == 1 && sekunder < 1) {
printf("%d time %d minut", timer, minutter);
} else if (timer == 1 && minutter > 1 && sekunder < 1) {
printf("%d time %d minutter", timer, minutter);
} else if (timer == 1 && minutter < 1 && sekunder < 1) {
printf("%d time", timer);
} else if (timer < 1 && minutter > 1 && sekunder < 1) {
printf("%d minutter", minutter);
} else if (timer < 1 && minutter == 1 && sekunder < 1) {
printf("%d minut", minutter);
}
return 0;
"
This is danish this time, but isn't there a way to shorten it up! or some other commands doing the trick aswell?
no,actually I was never doing anything in C, tho..I would do it a bit differently. instead of putting "Hora" or "Hores" (no latin user) into printf, I would put there any variable, which would be "Hora" or "Hores" depending on wether it is singular or plural, therefore you would save many lines. hope you got my point
e: doing it in english that is ;D
But looking at what he's writing and asking I think he's a beginner and doesnt know methods or concatenation yet
oh wait?
now he's a smart arse!