Skip to content

Instantly share code, notes, and snippets.

@k14i
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k14i/f18159db36e096874159 to your computer and use it in GitHub Desktop.
Save k14i/f18159db36e096874159 to your computer and use it in GitHub Desktop.
「あそこにベンツが停まってますね」 参考: https://twitter.com/syusui_s/status/534548412889178112
#include <stdio.h>
#include <string.h>
#define BUGGY
#define PRICE_BENZ 6000000
#define PRICE_CIGARETTE 42
#define SEQ_1 "「一日何本くらい煙草をお吸いに?」: "
#define SEQ_2 "「%d本ですね」\n"
#define SEQ_3 "「喫煙年数は?」: "
#define SEQ_4 "「%d年です」\n"
#define SEQ_5 "「あそこにベンツが停まってますね」: (y/n) "
#define SEQ_6Y "「はい」\n"
#define SEQ_6N "「いいえ」\n"
#define SEQ_7 "「もし煙草を吸っていなければ、"
#define SEQ_7YP "%d年であのベンツが買えました。」\n"
#define SEQ_7NP "%d年でベンツが買えました。」\n"
#define SEQ_7YM "あれくらい買えました。」\n"
#define SEQ_7NM "ベンツくらい買えました。」\n"
int get_cigarettes_per_day() {
char buf[BUFSIZ];
printf(SEQ_1);
fgets(buf, sizeof(buf), stdin);
int per_day = atoi(&buf);
printf(SEQ_2, per_day);
return per_day;
}
int get_cigarettes_per_year(int per_day) {
return per_day * 365;
}
int get_years() {
char buf[BUFSIZ];
printf(SEQ_3);
fgets(buf, sizeof(buf), stdin);
int years = atoi(&buf);
printf(SEQ_4, years);
return years;
}
int get_years_to_acquire(int per_year, int years) {
int cost = per_year * years * PRICE_CIGARETTE;
return (PRICE_BENZ - cost) * years / cost;
}
int benz_exists() {
printf(SEQ_5);
char ch = getchar();
if (strncmp(&ch, "y", 1) == 0) {
return 1;
} else {
return 0;
}
}
void get_result(int num, int benz) {
if (benz == 1) {
printf(SEQ_6Y);
printf(SEQ_7);
if (num > 0) {
printf(SEQ_7YP, num);
} else {
printf(SEQ_7YM);
}
} else {
printf(SEQ_6N);
printf(SEQ_7);
if (num > 0) {
printf(SEQ_7NP, num);
} else {
printf(SEQ_7NM);
}
}
}
int main(int argc, char *argv[]) {
int per_day = get_cigarettes_per_day();
#ifndef BUGGY
if (per_day <= 0) return 1;
#endif
int per_year = get_cigarettes_per_year(per_day);
int years = get_years();
#ifndef BUGGY
if (years <= 0) return 1;
#endif
get_result(get_years_to_acquire(per_year, years), benz_exists());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment