#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#define M_PI 3.14159265359
#define DISTRICT_NUM 50
struct location{
char name[100];
double lats;
double longs;
};
char username[50];
char* get_current_time() {
static char time_string[50];
time_t current_time;
struct tm *time_info;
strftime(time_string
, sizeof(time_string
), "%Y-%m-%d %H:%M:%S", time_info
);
return time_string;
}
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
double R = 6371.0;
double dlat = (lat2 - lat1) * M_PI / 180.0;
double dlon = (lon2 - lon1) * M_PI / 180.0;
double a
= sin(dlat
/2) * sin(dlat
/2) + cos(lat1
* M_PI
/ 180.0) * cos(lat2
* M_PI
/ 180.0) * sin(dlon
/2) * sin(dlon
/2); double d = R * c;
return d;
}
double calculateMoney(double km){
double money = 35;
if(km < 1) return 0;
if(km > 80){
money += (km - 80) * 10.50;
km = 80;
}
if(km > 60){
money += (km - 60) * 9.0;
km = 60;
}
if(km > 40){
money += (km - 40) * 8.5;
km = 40;
}
if(km > 20){
money += (km - 20) * 8;
km = 20;
}
if(km > 10){
money += (km - 10) * 7;
km = 10;
}
km -= 1;
money += km * 6.5;
return money;
}
void loadData(int mode , struct location *loc_arr){
char str[100];
if(mode
== 0)fp
= fopen("district.txt", "r"); else if (mode
== 1)fp
= fopen("lats.txt", "r"); else if (mode
== 2)fp
= fopen("longs.txt", "r"); if (fp == NULL) {
printf("Error opening the file!\n"); return;
}
int i = 0;
if(mode == 0){
strcpy
(loc_arr
[i
].name
, strtok(str
, "\n")); }else{
char *eptr;
double data = strtod(str , &eptr);
if(mode == 1)loc_arr[i].lats = data;
if(mode == 2)loc_arr[i].longs = data;
}
i++;
}
}
void printTitle(){
printf("\033[0;33m=======================================================\n"); printf("\033[0;31m _____ _ _ _ _ \n"); printf("\033[0;31m |__ __| | | \\/ | | | \n"); _ ____| | \\ / | ___| |
_ _ __ \n");
printf("\033[0;32m | | '__/ _` \\ \\ / / _ \\ | |\\/| |/ _ \\ __/ _ \\ '__|\n"); printf("\033[0;31m | | | | (_| |\\ V / __/ | | | | __/ || __/ | \n"); printf("\033[0;32m |_|_| \\__,_| \\_/ \\___|_|_| |_|\\___|\\__\\___|_| \n"); printf("\033[0;33m\n=======================================================\033[0;37m\n\n"); }
void printMenu(){
printf("[1]: Calculate travel expense.\n"); printf("[2]: Show district list.\n"); }
void printDistricList(struct location *loc_arr){
printf("\033[0;33m====================================================================\033[0;37m\n\n"); printf("\033[0;36m8888888b. d8b 888 d8b 888 \n"); printf("888 \"Y88b Y8P 888 Y8P 888 \n"); printf("888 888 888 .d8888b 888888 888d888 888 .d8888b 888888 .d8888b \n"); printf("888 888 888 88K 888 888P\" 888 d88P\" 888 88K \n"); printf("888 888 888 \"Y8888b. 888 888 888 888 888 \"Y8888b. \n"); printf("888 .d88P 888 X88 Y88b. 888 888 Y88b. Y88b. X88 \n"); printf("8888888P\" 888 88888P' \"Y888 888 888 \"Y8888P \"Y888 88888P' \n\n"); printf("\033[0;33m====================================================================\033[0;37m\n\n"); int i = 0;
for(i = 0; i < DISTRICT_NUM ; i++){
printf("\033[0;32m%d.\033[0;37m%s\n" , i
+1 , loc_arr
[i
].name
); }
printf("\n\033[0;31mPress enter to continue..... \033[0;37m"); getchar();
getchar();
}
int searchLoc(char *name, struct location *loc_arr){
int i = 0;
for(i = 0; i < DISTRICT_NUM ; i++){
if(!strcmp(loc_arr
[i
].name
, name
))return i
; }
return -1;
}
void travelExpense(struct location *loc_arr){
printf("\033[0;33m====================================================================\033[0;37m\n\n"); printf("\033[0;36m8888888b. d8b 888 d8b 888 \n"); printf("888 \"Y88b Y8P 888 Y8P 888 \n"); printf("888 888 888 .d8888b 888888 888d888 888 .d8888b 888888 .d8888b \n"); printf("888 888 888 88K 888 888P\" 888 d88P\" 888 88K \n"); printf("888 888 888 \"Y8888b. 888 888 888 888 888 \"Y8888b. \n"); printf("888 .d88P 888 X88 Y88b. 888 888 Y88b. Y88b. X88 \n"); printf("8888888P\" 888 88888P' \"Y888 888 888 \"Y8888P \"Y888 88888P' \n\n"); printf("\033[0;33m====================================================================\033[0;37m\n\n"); int i = 0;
for(i = 0; i < DISTRICT_NUM ; i++){
printf("\033[0;32m%d.\033[0;37m%s\n" , i
+1 , loc_arr
[i
].name
); }
printf("\n\033[0;33m===============================\n"); printf(" __/__\\___________| \\_\n"); printf("| / \\ |___/ / \\ `-.\n"); printf("|_| (O) |________| (O) |____|\n"); printf("\n\033[0;33m===============================\n\n\n\033[0;37m"); printf("Please enter estimate idle time(min):"); printf("Please enter the district in string(lower).\n"); printf("From where (-1 to exit)? :"); char input[50];
gets(input);
int first;
int index_1 , index_2;
double distance = 0;
while(1){
gets(input);
if(!strcmp("-1" , input
))return; index_1 = searchLoc(&input ,loc_arr );
if(index_1 != -1)break;
printf("\n\033[0;31mInvalid input!\n\033[0;37m\nTry again:"); }
while(1){
gets(input);
if(!strcmp("-1" , input
))return; index_2 = searchLoc(&input ,loc_arr );
if(index_2 != -1)break;
printf("\n\033[0;31mInvalid input!\n\033[0;37m\nTry again:"); }
first = index_1;
distance += calculateDistance(loc_arr[index_1].lats , loc_arr[index_1].longs ,loc_arr[index_2].lats , loc_arr[index_2].longs );
while(1){
index_1 = index_2;
gets(input);
while(1){
gets(input);
if(!strcmp("-1" , input
))return; index_2 = searchLoc(&input ,loc_arr );
if(index_2 != -1)break;
printf("\n\033[0;31mInvalid input!\n\033[0;37m\nTry again:"); }
distance += calculateDistance(loc_arr[index_1].lats , loc_arr[index_1].longs ,loc_arr[index_2].lats , loc_arr[index_2].longs );
}
printf("\nFrom \033[0;32m%s \033[0;37mto \033[0;32m%s \033[0;37mis \033[0;32m%.2f \033[0;37mkm\n" , loc_arr
[first
].name
, loc_arr
[index_2
].name
, distance
); double money
= calculateMoney
(distance
) + (time * 3); printf("Estimated Price:\033[0;33m %.0f \033[0;37mbaht\n" , money
);
char str[100];
fp
= fopen("logs.txt" , "a"); if (fp == NULL) {
printf("Error opening file!\n"); return 1;
}
fprintf(fp
, "\n\nAt %s \n%s travel from %s to %s\nIn a distance of %.2fkm\nEstimated Price of %.0f baht" , get_current_time
() , username
, loc_arr
[first
].name
, loc_arr
[index_2
].name
, distance
,money
);
printf("\n\033[0;31mPress enter to continue..... \033[0;37m"); getchar();
}
void printHistory(){
printf("================\033[0;33m History \033[0;37m===============\n"); char str[100];
fp
= fopen("logs.txt", "r"); if (fp == NULL) {
printf("Error opening the file!\n"); return;
}
}
printf("\n\033[0;31mPress enter to continue..... \033[0;37m"); getchar();
getchar();
}
int main(){
printf("Enter your username: "); scanf("%s" , &username);
struct location *loc_arr;
loc_arr
= (struct location
*)malloc
(50 * sizeof(struct location
)); loadData(0 , loc_arr);
loadData(1 , loc_arr);
loadData(2 , loc_arr);
printTitle();
while(1){
printMenu();
int command;
scanf("%d" , &command);
if(command == 1){
travelExpense(loc_arr);
printTitle();
}else if(command == 2){
printDistricList(loc_arr);
printTitle();
}else if(command == 3){
printHistory();
printTitle();
}else if(command == 4){
break;
}else{
printf("\033[0;31mInvalid Command!\n\n\033[0;37m"); }
}
return 0;
}
I2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3lzL3N0YXQuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgTV9QSSAzLjE0MTU5MjY1MzU5CiNkZWZpbmUgRElTVFJJQ1RfTlVNIDUwCgpzdHJ1Y3QgbG9jYXRpb257CiAgICBjaGFyIG5hbWVbMTAwXTsKICAgIGRvdWJsZSBsYXRzOwogICAgZG91YmxlIGxvbmdzOwp9OwoKY2hhciB1c2VybmFtZVs1MF07CgpjaGFyKiBnZXRfY3VycmVudF90aW1lKCkgewogICBzdGF0aWMgY2hhciB0aW1lX3N0cmluZ1s1MF07CiAgIHRpbWVfdCBjdXJyZW50X3RpbWU7CiAgIHN0cnVjdCB0bSAqdGltZV9pbmZvOwoKICAgdGltZSgmY3VycmVudF90aW1lKTsKICAgdGltZV9pbmZvID0gbG9jYWx0aW1lKCZjdXJyZW50X3RpbWUpOwogICBzdHJmdGltZSh0aW1lX3N0cmluZywgc2l6ZW9mKHRpbWVfc3RyaW5nKSwgIiVZLSVtLSVkICVIOiVNOiVTIiwgdGltZV9pbmZvKTsKCiAgIHJldHVybiB0aW1lX3N0cmluZzsKfQoKCmRvdWJsZSBjYWxjdWxhdGVEaXN0YW5jZShkb3VibGUgbGF0MSwgZG91YmxlIGxvbjEsIGRvdWJsZSBsYXQyLCBkb3VibGUgbG9uMikgewogICAgZG91YmxlIFIgPSA2MzcxLjA7ICAKICAgIGRvdWJsZSBkbGF0ID0gKGxhdDIgLSBsYXQxKSAqIE1fUEkgLyAxODAuMDsKICAgIGRvdWJsZSBkbG9uID0gKGxvbjIgLSBsb24xKSAqIE1fUEkgLyAxODAuMDsKICAgIGRvdWJsZSBhID0gc2luKGRsYXQvMikgKiBzaW4oZGxhdC8yKSArIGNvcyhsYXQxICogTV9QSSAvIDE4MC4wKSAqIGNvcyhsYXQyICogTV9QSSAvIDE4MC4wKSAqIHNpbihkbG9uLzIpICogc2luKGRsb24vMik7CiAgICBkb3VibGUgYyA9IDIgKiBhdGFuMihzcXJ0KGEpLCBzcXJ0KDEtYSkpOwogICAgZG91YmxlIGQgPSBSICogYzsKICAgIHJldHVybiBkOwp9Cgpkb3VibGUgY2FsY3VsYXRlTW9uZXkoZG91YmxlIGttKXsKICAgIGRvdWJsZSBtb25leSA9IDM1OwogICAgaWYoa20gPCAxKSByZXR1cm4gMDsKICAgIGlmKGttID4gODApewogICAgICAgIG1vbmV5ICs9IChrbSAtIDgwKSAqIDEwLjUwOwogICAgICAgIGttID0gODA7CiAgICB9CiAgICBpZihrbSA+IDYwKXsKICAgICAgICBtb25leSArPSAoa20gLSA2MCkgKiA5LjA7CiAgICAgICAga20gPSA2MDsKICAgIH0KICAgIGlmKGttID4gNDApewogICAgICAgIG1vbmV5ICs9IChrbSAtIDQwKSAqIDguNTsKICAgICAgICBrbSA9IDQwOwogICAgfQogICAgaWYoa20gPiAyMCl7CiAgICAgICAgbW9uZXkgKz0gKGttIC0gMjApICogODsKICAgICAgICBrbSA9IDIwOwogICAgfQogICAgaWYoa20gPiAxMCl7CiAgICAgICAgbW9uZXkgKz0gKGttIC0gMTApICogNzsKICAgICAgICBrbSA9IDEwOwogICAgfQogICAga20gLT0gMTsKICAgIG1vbmV5ICs9IGttICogNi41OwoKICAgIHJldHVybiBtb25leTsKICAgIAp9Cgp2b2lkIGxvYWREYXRhKGludCBtb2RlICwgc3RydWN0IGxvY2F0aW9uICpsb2NfYXJyKXsKICAgRklMRSAqZnA7CiAgIGNoYXIgc3RyWzEwMF07CiAgIGlmKG1vZGUgPT0gMClmcCA9IGZvcGVuKCJkaXN0cmljdC50eHQiLCAiciIpOwogICBlbHNlIGlmIChtb2RlID09IDEpZnAgPSBmb3BlbigibGF0cy50eHQiLCAiciIpOwogICBlbHNlIGlmIChtb2RlID09IDIpZnAgPSBmb3BlbigibG9uZ3MudHh0IiwgInIiKTsKICAgaWYgKGZwID09IE5VTEwpIHsKICAgICAgcHJpbnRmKCJFcnJvciBvcGVuaW5nIHRoZSBmaWxlIVxuIik7CiAgICAgIHJldHVybjsKICAgfQogICBpbnQgaSA9IDA7CiAgIHdoaWxlIChmZ2V0cyhzdHIsIHNpemVvZihzdHIpLCBmcCkgIT0gTlVMTCkgewogICAgICBpZihtb2RlID09IDApewogICAgICAgIHN0cmNweShsb2NfYXJyW2ldLm5hbWUgLCBzdHJ0b2soc3RyICwgIlxuIikpOwogICAgICB9ZWxzZXsKICAgICAgICBjaGFyICplcHRyOwogICAgICAgIGRvdWJsZSBkYXRhID0gc3RydG9kKHN0ciAsICZlcHRyKTsKICAgICAgICBpZihtb2RlID09IDEpbG9jX2FycltpXS5sYXRzID0gZGF0YTsKICAgICAgICBpZihtb2RlID09IDIpbG9jX2FycltpXS5sb25ncyA9IGRhdGE7CiAgICAgIH0KICAgICAgIGkrKzsKICAgfQogICBmY2xvc2UoZnApOwp9Cgp2b2lkIHByaW50VGl0bGUoKXsKICAgIHN5c3RlbSgiY2xzIik7CiAgICBwcmludGYoIlwwMzNbMDszM209PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09XG4iKTsKICAgIHByaW50ZigiXDAzM1swOzMxbSAgX19fX18gICAgICAgICAgICAgICAgICBfIF8gIF8gICAgICBfICAgICAgICAgICAgXG4iKTsKICAgIHByaW50ZigiXDAzM1swOzMxbSB8X18gICBfX3wgICAgICAgICAgICAgICAgfCB8ICBcXC8gIHwgICAgfCB8ICAgICAgICAgICBcbiIpOwogICAgcHJpbnRmKCJcMDMzWzA7MzJtICAgIHwgfF8gXyBfCl8gICBfX19ffCB8IFxcICAvIHwgX19ffCB8Cl8gXyBfXyBcbiIpOwogICAgcHJpbnRmKCJcMDMzWzA7MzJtICAgIHwgfCAnX18vIF9gIFxcIFxcIC8gLyBfIFxcIHwgfFxcL3wgfC8gXyBcXCBfXy8gXyBcXCAnX198XG4iKTsKICAgIHByaW50ZigiXDAzM1swOzMxbSAgICB8IHwgfCB8IChffCB8XFwgViAvICBfXy8gfCB8ICB8IHwgIF9fLyB8fCAgX18vIHwgICBcbiIpOwogICAgcHJpbnRmKCJcMDMzWzA7MzJtICAgIHxffF98ICBcXF9fLF98IFxcXy8gXFxfX198X3xffCAgfF98XFxfX198XFxfX1xcX19ffF98ICAgXG4iKTsKICAgIHByaW50ZigiXDAzM1swOzMzbVxuPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PVwwMzNbMDszN21cblxuIik7Cn0KCnZvaWQgcHJpbnRNZW51KCl7CiAgICBwcmludGYoIlsxXTogQ2FsY3VsYXRlIHRyYXZlbCBleHBlbnNlLlxuIik7CiAgICBwcmludGYoIlsyXTogU2hvdyBkaXN0cmljdCBsaXN0LlxuIik7CiAgICBwcmludGYoIlszXTogSGlzdG9yeVxuIik7CiAgICBwcmludGYoIls0XTogRXhpdFxuXG4iKTsKICAgIHByaW50ZigiU2VsZWN0IGNvbW1hbmQ6ICIpOwp9Cgp2b2lkIHByaW50RGlzdHJpY0xpc3Qoc3RydWN0IGxvY2F0aW9uICpsb2NfYXJyKXsKICAgIHN5c3RlbSgiY2xzIik7CiAgICBwcmludGYoIlwwMzNbMDszM209PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PVwwMzNbMDszN21cblxuIik7CiAgICBwcmludGYoIlwwMzNbMDszNm04ODg4ODg4Yi4gIGQ4YiAgICAgICAgICA4ODggICAgICAgICAgICBkOGIgICAgICAgICAgODg4ICAgICAgICAgICAgIFxuIik7CiAgICBwcmludGYoIjg4OCAgXCJZODhiIFk4UCAgICAgICAgICA4ODggICAgICAgICAgICBZOFAgICAgICAgICAgODg4ICAgICAgICAgICAgIFxuIik7CiAgICBwcmludGYoIjg4OCAgICA4ODggICAgICAgICAgICAgIDg4OCAgICAgICAgICAgICAgICAgICAgICAgICA4ODggICAgICAgICAgICAgXG4iKTsKICAgIHByaW50ZigiODg4ICAgIDg4OCA4ODggLmQ4ODg4YiAgODg4ODg4IDg4OGQ4ODggODg4ICAuZDg4ODhiIDg4ODg4OCAuZDg4ODhiICBcbiIpOwogICAgcHJpbnRmKCI4ODggICAgODg4IDg4OCA4OEsgICAgICA4ODggICAgODg4UFwiICAgODg4IGQ4OFBcIiAgICA4ODggICAgODhLICAgICAgXG4iKTsKICAgIHByaW50ZigiODg4ICAgIDg4OCA4ODggXCJZODg4OGIuIDg4OCAgICA4ODggICAgIDg4OCA4ODggICAgICA4ODggICAgXCJZODg4OGIuIFxuIik7CiAgICBwcmludGYoIjg4OCAgLmQ4OFAgODg4ICAgICAgWDg4IFk4OGIuICA4ODggICAgIDg4OCBZODhiLiAgICBZODhiLiAgICAgICBYODggXG4iKTsKICAgIHByaW50ZigiODg4ODg4OFBcIiAgODg4ICA4ODg4OFAnICBcIlk4ODggODg4ICAgICA4ODggIFwiWTg4ODhQICBcIlk4ODggIDg4ODg4UCcgXG5cbiIpOwogICAgcHJpbnRmKCJcMDMzWzA7MzNtPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT1cMDMzWzA7MzdtXG5cbiIpOwogICAgaW50IGkgPSAwOwogICAgZm9yKGkgPSAwOyBpIDwgRElTVFJJQ1RfTlVNIDsgaSsrKXsKICAgICAgICBwcmludGYoIlwwMzNbMDszMm0lZC5cMDMzWzA7MzdtJXNcbiIgLCBpKzEgLCBsb2NfYXJyW2ldLm5hbWUpOwogICAgfQogICAgcHJpbnRmKCJcblwwMzNbMDszMW1QcmVzcyBlbnRlciB0byBjb250aW51ZS4uLi4uIFwwMzNbMDszN20iKTsKICAgIGdldGNoYXIoKTsKICAgIGdldGNoYXIoKTsKfQoKaW50IHNlYXJjaExvYyhjaGFyICpuYW1lLCBzdHJ1Y3QgbG9jYXRpb24gKmxvY19hcnIpewogICAgaW50IGkgPSAwOwogICAgZm9yKGkgPSAwOyBpIDwgRElTVFJJQ1RfTlVNIDsgaSsrKXsKICAgICAgICBpZighc3RyY21wKGxvY19hcnJbaV0ubmFtZSAsIG5hbWUpKXJldHVybiBpOwogICAgfQogICAgcmV0dXJuIC0xOwp9Cgp2b2lkIHRyYXZlbEV4cGVuc2Uoc3RydWN0IGxvY2F0aW9uICpsb2NfYXJyKXsKICAgIHN5c3RlbSgiY2xzIik7CgogICAgcHJpbnRmKCJcMDMzWzA7MzNtPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT1cMDMzWzA7MzdtXG5cbiIpOwogICAgcHJpbnRmKCJcMDMzWzA7MzZtODg4ODg4OGIuICBkOGIgICAgICAgICAgODg4ICAgICAgICAgICAgZDhiICAgICAgICAgIDg4OCAgICAgICAgICAgICBcbiIpOwogICAgcHJpbnRmKCI4ODggIFwiWTg4YiBZOFAgICAgICAgICAgODg4ICAgICAgICAgICAgWThQICAgICAgICAgIDg4OCAgICAgICAgICAgICBcbiIpOwogICAgcHJpbnRmKCI4ODggICAgODg4ICAgICAgICAgICAgICA4ODggICAgICAgICAgICAgICAgICAgICAgICAgODg4ICAgICAgICAgICAgIFxuIik7CiAgICBwcmludGYoIjg4OCAgICA4ODggODg4IC5kODg4OGIgIDg4ODg4OCA4ODhkODg4IDg4OCAgLmQ4ODg4YiA4ODg4ODggLmQ4ODg4YiAgXG4iKTsKICAgIHByaW50ZigiODg4ICAgIDg4OCA4ODggODhLICAgICAgODg4ICAgIDg4OFBcIiAgIDg4OCBkODhQXCIgICAgODg4ICAgIDg4SyAgICAgIFxuIik7CiAgICBwcmludGYoIjg4OCAgICA4ODggODg4IFwiWTg4ODhiLiA4ODggICAgODg4ICAgICA4ODggODg4ICAgICAgODg4ICAgIFwiWTg4ODhiLiBcbiIpOwogICAgcHJpbnRmKCI4ODggIC5kODhQIDg4OCAgICAgIFg4OCBZODhiLiAgODg4ICAgICA4ODggWTg4Yi4gICAgWTg4Yi4gICAgICAgWDg4IFxuIik7CiAgICBwcmludGYoIjg4ODg4ODhQXCIgIDg4OCAgODg4ODhQJyAgXCJZODg4IDg4OCAgICAgODg4ICBcIlk4ODg4UCAgXCJZODg4ICA4ODg4OFAnIFxuXG4iKTsKICAgIHByaW50ZigiXDAzM1swOzMzbT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09XDAzM1swOzM3bVxuXG4iKTsKICAgIGludCBpID0gMDsKICAgIGZvcihpID0gMDsgaSA8IERJU1RSSUNUX05VTSA7IGkrKyl7CiAgICAgICAgcHJpbnRmKCJcMDMzWzA7MzJtJWQuXDAzM1swOzM3bSVzXG4iICwgaSsxICwgbG9jX2FycltpXS5uYW1lKTsKICAgIH0KICAgIAogICAgcHJpbnRmKCJcblwwMzNbMDszM209PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09XG4iKTsKICAgIHByaW50ZigiXG5cMDMzWzA7MzJtICAgICAgICAgICAgICAgICAgLlxuIik7CiAgICBwcmludGYoIiAgICBfXyAgICAgICAgICAgIHxcXFxuIik7CiAgICBwcmludGYoIiBfXy9fX1xcX19fX19fX19fX198IFxcX1xuIik7CiAgICBwcmludGYoInwgICBfICAgIHwgICx8ICAgX19fYC0uXG4iKTsKICAgIHByaW50ZigifCAgLyAgIFxcICAgfF9fXy8gIC8gICBcXCAgYC0uXG4iKTsKICAgIHByaW50ZigifF98IChPKSB8X19fX19fX198IChPKSB8X19fX3xcbiIpOwogICAgcHJpbnRmKCIgICBcXF9fXy8gICAgICAgICAgXFxfX18vXG5cbiIpOwogICAgcHJpbnRmKCJcblwwMzNbMDszM209PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09XG5cblxuXDAzM1swOzM3bSIpOwogICAgaW50IHRpbWU7CiAgICBwcmludGYoIlBsZWFzZSBlbnRlciBlc3RpbWF0ZSBpZGxlIHRpbWUobWluKToiKTsKICAgIHNjYW5mKCIlZCIgLCZ0aW1lKTsKICAgIHByaW50ZigiUGxlYXNlIGVudGVyIHRoZSBkaXN0cmljdCBpbiBzdHJpbmcobG93ZXIpLlxuIik7CiAgICBwcmludGYoIkZyb20gd2hlcmUgKC0xIHRvIGV4aXQpPyA6Iik7CiAgICBjaGFyIGlucHV0WzUwXTsKICAgIGdldHMoaW5wdXQpOwogICAgaW50IGZpcnN0OwogICAgaW50IGluZGV4XzEgLCBpbmRleF8yOwogICAgZG91YmxlIGRpc3RhbmNlID0gMDsKICAgIHdoaWxlKDEpewogICAgICAgIGdldHMoaW5wdXQpOwogICAgICAgIGlmKCFzdHJjbXAoIi0xIiAsIGlucHV0KSlyZXR1cm47CiAgICAgICAgaW5kZXhfMSA9IHNlYXJjaExvYygmaW5wdXQgLGxvY19hcnIgKTsKICAgICAgICBpZihpbmRleF8xICE9IC0xKWJyZWFrOwogICAgICAgIHByaW50ZigiXG5cMDMzWzA7MzFtSW52YWxpZCBpbnB1dCFcblwwMzNbMDszN21cblRyeSBhZ2FpbjoiKTsKICAgIH0KICAgIHByaW50ZigiVG8gd2hlcmU/OiIpOwogICAgd2hpbGUoMSl7CiAgICAgICAgZ2V0cyhpbnB1dCk7CiAgICAgICAgaWYoIXN0cmNtcCgiLTEiICwgaW5wdXQpKXJldHVybjsKICAgICAgICBpbmRleF8yID0gc2VhcmNoTG9jKCZpbnB1dCAsbG9jX2FyciApOwogICAgICAgIGlmKGluZGV4XzIgIT0gLTEpYnJlYWs7CiAgICAgICAgcHJpbnRmKCJcblwwMzNbMDszMW1JbnZhbGlkIGlucHV0IVxuXDAzM1swOzM3bVxuVHJ5IGFnYWluOiIpOwogICAgfQogICAgZmlyc3QgPSBpbmRleF8xOwogICAgZGlzdGFuY2UgKz0gY2FsY3VsYXRlRGlzdGFuY2UobG9jX2FycltpbmRleF8xXS5sYXRzICwgbG9jX2FycltpbmRleF8xXS5sb25ncyAsbG9jX2FycltpbmRleF8yXS5sYXRzICwgbG9jX2FycltpbmRleF8yXS5sb25ncyApOwogICAgd2hpbGUoMSl7CiAgICAgICAgaW5kZXhfMSA9IGluZGV4XzI7CiAgICAgICAgcHJpbnRmKCJDb250aW51ZT8gKHkvbik6Iik7CiAgICAgICAgZ2V0cyhpbnB1dCk7CiAgICAgICAgaWYoIXN0cmNtcCgibiIsIGlucHV0KSlicmVhazsKICAgICAgICAgICAgcHJpbnRmKCJUbyB3aGVyZT86Iik7CiAgICAgICAgd2hpbGUoMSl7CiAgICAgICAgICAgIGdldHMoaW5wdXQpOwogICAgICAgICAgICBpZighc3RyY21wKCItMSIgLCBpbnB1dCkpcmV0dXJuOwogICAgICAgICAgICBpbmRleF8yID0gc2VhcmNoTG9jKCZpbnB1dCAsbG9jX2FyciApOwogICAgICAgICAgICBpZihpbmRleF8yICE9IC0xKWJyZWFrOwogICAgICAgICAgICBwc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ICwgbG9jX2Fycik7CiAgICBsb2FkRGF0YSgyICwgbG9jX2Fycik7CiAgICAKICAgIHByaW50VGl0bGUoKTsKICAgIHdoaWxlKDEpewogICAgICAgIHByaW50TWVudSgpOwogICAgICAgIGludCBjb21tYW5kOwogICAgICAgIHNjYW5mKCIlZCIgLCAmY29tbWFuZCk7CiAgICAgICAgaWYoY29tbWFuZCA9PSAxKXsKICAgICAgICAgICAgdHJhdmVsRXhwZW5zZShsb2NfYXJyKTsKICAgICAgICAgICAgcHJpbnRUaXRsZSgpOwogICAgICAgIH1lbHNlIGlmKGNvbW1hbmQgPT0gMil7CiAgICAgICAgICAgIHByaW50RGlzdHJpY0xpc3QobG9jX2Fycik7CiAgICAgICAgICAgIHByaW50VGl0bGUoKTsKICAgICAgICAgICAgCiAgICAgICAgfWVsc2UgaWYoY29tbWFuZCA9PSAzKXsKICAgICAgICAgICAgcHJpbnRIaXN0b3J5KCk7CiAgICAgICAgICAgIHByaW50VGl0bGUoKTsKICAgICAgICB9ZWxzZSBpZihjb21tYW5kID09IDQpewogICAgICAgICAgICBicmVhazsKICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgcHJpbnRmKCJcMDMzWzA7MzFtSW52YWxpZCBDb21tYW5kIVxuXG5cMDMzWzA7MzdtIik7CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gMDsKfQ==
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#define M_PI 3.14159265359
#define DISTRICT_NUM 50
struct location{
char name[100];
double lats;
double longs;
};
char username[50];
char* get_current_time() {
static char time_string[50];
time_t current_time;
struct tm *time_info;
time(¤t_time);
time_info = localtime(¤t_time);
strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", time_info);
return time_string;
}
double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
double R = 6371.0;
double dlat = (lat2 - lat1) * M_PI / 180.0;
double dlon = (lon2 - lon1) * M_PI / 180.0;
double a = sin(dlat/2) * sin(dlat/2) + cos(lat1 * M_PI / 180.0) * cos(lat2 * M_PI / 180.0) * sin(dlon/2) * sin(dlon/2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = R * c;
return d;
}
double calculateMoney(double km){
double money = 35;
if(km < 1) return 0;
if(km > 80){
money += (km - 80) * 10.50;
km = 80;
}
if(km > 60){
money += (km - 60) * 9.0;
km = 60;
}
if(km > 40){
money += (km - 40) * 8.5;
km = 40;
}
if(km > 20){
money += (km - 20) * 8;
km = 20;
}
if(km > 10){
money += (km - 10) * 7;
km = 10;
}
km -= 1;
money += km * 6.5;
return money;
}
void loadData(int mode , struct location *loc_arr){
FILE *fp;
char str[100];
if(mode == 0)fp = fopen("district.txt", "r");
else if (mode == 1)fp = fopen("lats.txt", "r");
else if (mode == 2)fp = fopen("longs.txt", "r");
if (fp == NULL) {
printf("Error opening the file!\n");
return;
}
int i = 0;
while (fgets(str, sizeof(str), fp) != NULL) {
if(mode == 0){
strcpy(loc_arr[i].name , strtok(str , "\n"));
}else{
char *eptr;
double data = strtod(str , &eptr);
if(mode == 1)loc_arr[i].lats = data;
if(mode == 2)loc_arr[i].longs = data;
}
i++;
}
fclose(fp);
}
void printTitle(){
system("cls");
printf("\033[0;33m=======================================================\n");
printf("\033[0;31m _____ _ _ _ _ \n");
printf("\033[0;31m |__ __| | | \\/ | | | \n");
printf("\033[0;32m | |_ _ _
_ ____| | \\ / | ___| |
_ _ __ \n");
printf("\033[0;32m | | '__/ _` \\ \\ / / _ \\ | |\\/| |/ _ \\ __/ _ \\ '__|\n");
printf("\033[0;31m | | | | (_| |\\ V / __/ | | | | __/ || __/ | \n");
printf("\033[0;32m |_|_| \\__,_| \\_/ \\___|_|_| |_|\\___|\\__\\___|_| \n");
printf("\033[0;33m\n=======================================================\033[0;37m\n\n");
}
void printMenu(){
printf("[1]: Calculate travel expense.\n");
printf("[2]: Show district list.\n");
printf("[3]: History\n");
printf("[4]: Exit\n\n");
printf("Select command: ");
}
void printDistricList(struct location *loc_arr){
system("cls");
printf("\033[0;33m====================================================================\033[0;37m\n\n");
printf("\033[0;36m8888888b. d8b 888 d8b 888 \n");
printf("888 \"Y88b Y8P 888 Y8P 888 \n");
printf("888 888 888 888 \n");
printf("888 888 888 .d8888b 888888 888d888 888 .d8888b 888888 .d8888b \n");
printf("888 888 888 88K 888 888P\" 888 d88P\" 888 88K \n");
printf("888 888 888 \"Y8888b. 888 888 888 888 888 \"Y8888b. \n");
printf("888 .d88P 888 X88 Y88b. 888 888 Y88b. Y88b. X88 \n");
printf("8888888P\" 888 88888P' \"Y888 888 888 \"Y8888P \"Y888 88888P' \n\n");
printf("\033[0;33m====================================================================\033[0;37m\n\n");
int i = 0;
for(i = 0; i < DISTRICT_NUM ; i++){
printf("\033[0;32m%d.\033[0;37m%s\n" , i+1 , loc_arr[i].name);
}
printf("\n\033[0;31mPress enter to continue..... \033[0;37m");
getchar();
getchar();
}
int searchLoc(char *name, struct location *loc_arr){
int i = 0;
for(i = 0; i < DISTRICT_NUM ; i++){
if(!strcmp(loc_arr[i].name , name))return i;
}
return -1;
}
void travelExpense(struct location *loc_arr){
system("cls");
printf("\033[0;33m====================================================================\033[0;37m\n\n");
printf("\033[0;36m8888888b. d8b 888 d8b 888 \n");
printf("888 \"Y88b Y8P 888 Y8P 888 \n");
printf("888 888 888 888 \n");
printf("888 888 888 .d8888b 888888 888d888 888 .d8888b 888888 .d8888b \n");
printf("888 888 888 88K 888 888P\" 888 d88P\" 888 88K \n");
printf("888 888 888 \"Y8888b. 888 888 888 888 888 \"Y8888b. \n");
printf("888 .d88P 888 X88 Y88b. 888 888 Y88b. Y88b. X88 \n");
printf("8888888P\" 888 88888P' \"Y888 888 888 \"Y8888P \"Y888 88888P' \n\n");
printf("\033[0;33m====================================================================\033[0;37m\n\n");
int i = 0;
for(i = 0; i < DISTRICT_NUM ; i++){
printf("\033[0;32m%d.\033[0;37m%s\n" , i+1 , loc_arr[i].name);
}
printf("\n\033[0;33m===============================\n");
printf("\n\033[0;32m .\n");
printf(" __ |\\\n");
printf(" __/__\\___________| \\_\n");
printf("| _ | ,| ___`-.\n");
printf("| / \\ |___/ / \\ `-.\n");
printf("|_| (O) |________| (O) |____|\n");
printf(" \\___/ \\___/\n\n");
printf("\n\033[0;33m===============================\n\n\n\033[0;37m");
int time;
printf("Please enter estimate idle time(min):");
scanf("%d" ,&time);
printf("Please enter the district in string(lower).\n");
printf("From where (-1 to exit)? :");
char input[50];
gets(input);
int first;
int index_1 , index_2;
double distance = 0;
while(1){
gets(input);
if(!strcmp("-1" , input))return;
index_1 = searchLoc(&input ,loc_arr );
if(index_1 != -1)break;
printf("\n\033[0;31mInvalid input!\n\033[0;37m\nTry again:");
}
printf("To where?:");
while(1){
gets(input);
if(!strcmp("-1" , input))return;
index_2 = searchLoc(&input ,loc_arr );
if(index_2 != -1)break;
printf("\n\033[0;31mInvalid input!\n\033[0;37m\nTry again:");
}
first = index_1;
distance += calculateDistance(loc_arr[index_1].lats , loc_arr[index_1].longs ,loc_arr[index_2].lats , loc_arr[index_2].longs );
while(1){
index_1 = index_2;
printf("Continue? (y/n):");
gets(input);
if(!strcmp("n", input))break;
printf("To where?:");
while(1){
gets(input);
if(!strcmp("-1" , input))return;
index_2 = searchLoc(&input ,loc_arr );
if(index_2 != -1)break;
printf("\n\033[0;31mInvalid input!\n\033[0;37m\nTry again:");
}
distance += calculateDistance(loc_arr[index_1].lats , loc_arr[index_1].longs ,loc_arr[index_2].lats , loc_arr[index_2].longs );
}
printf("\nFrom \033[0;32m%s \033[0;37mto \033[0;32m%s \033[0;37mis \033[0;32m%.2f \033[0;37mkm\n" , loc_arr[first].name , loc_arr[index_2].name , distance);
double money = calculateMoney(distance) + (time * 3);
printf("Estimated Price:\033[0;33m %.0f \033[0;37mbaht\n" , money);
FILE *fp;
char str[100];
fp = fopen("logs.txt" , "a");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fp , "\n\nAt %s \n%s travel from %s to %s\nIn a distance of %.2fkm\nEstimated Price of %.0f baht" , get_current_time() , username , loc_arr[first].name , loc_arr[index_2].name , distance ,money );
fclose(fp);
printf("\n\033[0;31mPress enter to continue..... \033[0;37m");
getchar();
}
void printHistory(){
system("cls");
printf("================\033[0;33m History \033[0;37m===============\n");
FILE *fp;
char str[100];
fp = fopen("logs.txt", "r");
if (fp == NULL) {
printf("Error opening the file!\n");
return;
}
while (fgets(str, sizeof(str), fp) != NULL) {
printf("%s",str);
}
fclose(fp);
printf("\n\033[0;31mPress enter to continue..... \033[0;37m");
getchar();
getchar();
}
int main(){
printf("Enter your username: ");
scanf("%s" , &username);
struct location *loc_arr;
loc_arr = (struct location*)malloc(50 * sizeof(struct location));
loadData(0 , loc_arr);
loadData(1 , loc_arr);
loadData(2 , loc_arr);
printTitle();
while(1){
printMenu();
int command;
scanf("%d" , &command);
if(command == 1){
travelExpense(loc_arr);
printTitle();
}else if(command == 2){
printDistricList(loc_arr);
printTitle();
}else if(command == 3){
printHistory();
printTitle();
}else if(command == 4){
break;
}else{
printf("\033[0;31mInvalid Command!\n\n\033[0;37m");
}
}
return 0;
}