time_money/nctime_money.c

131 lines
4.3 KiB
C

#include <stdio.h>
#include <ncurses.h>
#include <time.h>
#include <unistd.h> // For the sleep function
int main() {
while (1) {
// Describe the program
printf("This program will calculate who much you earn per second\nat the time you execute, as well as a date you specify.\n");
// Enter your annual salary
double annualSalary;
printf("Enter your annual salary: $");
scanf("%lf", &annualSalary);
// Calculate your earnings per second
double earningsPerSecond = annualSalary / (365.25 * 24 * 60 * 60);
// Initialize the total earned
double earned = 0.0;
double earnedSincetarget = 0.0;
// Get the current time when the program starts
time_t startTime;
time(&startTime);
// Get the retire date from the user
struct tm retireDate;
printf("Enter the date you want to retire (YYYY-MM-DD): ");
scanf("%d-%d-%d", &retireDate.tm_year, &retireDate.tm_mon, &retireDate.tm_mday);
// Get the retire date from the user
struct tm targetDate;
printf("Enter a date you want to count from (YYYY-MM-DD): ");
scanf("%d-%d-%d", &targetDate.tm_year, &targetDate.tm_mon, &targetDate.tm_mday);
// Adjust month and year values
retireDate.tm_year -= 1900; // Years since 1900
retireDate.tm_mon -= 1; // Months are 0-based (0 = January)
targetDate.tm_year -= 1900; // Years since 1900
targetDate.tm_mon -= 1; // Months are 0-based (0 = January)
// Convert the dates to a time_t value
time_t retireTime = mktime(&retireDate);
time_t targetTime = mktime(&targetDate);
if (retireTime == -1) { // Prompt for a valid date
printf("Invalid date entered.\n");
refresh();
continue;
}
if (targetTime == -1) {
printf("Invalid date entered.\n");
refresh();
continue;
}
// Initialize ncurses
initscr();
cbreak(); // Disable line buffering
noecho(); // Don't display user input
curs_set(0); // Hide the cursor
while (1) {
// Get the current time
time_t currentTime;
time(&currentTime);
// Calculate the time difference in seconds
time_t timeDifference = retireTime - currentTime;
time_t timeDifferencetarget = currentTime - targetTime;
// Calculate the time elapsed since the program started
double timeElapsed = difftime(currentTime, startTime);
double timeElapsedtarget = difftime(currentTime, targetTime);
// Calculate the earnings based on the time elapsed
double earnings = earningsPerSecond * timeElapsed;
double earningsTarget = earningsPerSecond * timeElapsedtarget;
// Update the total earned
earned = earnings;
earnedSincetarget = earningsTarget;
// Clear the screen
clear();
// Print the current earnings
printw("Earnings since program start: $%.2lf\n", earned);
printw("Earnings since target date: $%.2lf\n", earnedSincetarget);
if (timeDifference < 0) {
printw("The retire date has already passed.\n");
refresh();
break; // Exit the countdown loop
} else {
// Calculate seconds, minutes, hours, days, months, and years
int seconds = timeDifference % 60;
int minutes = (timeDifference / 60) % 60;
int hours = (timeDifference / (60 * 60)) % 24;
int days = timeDifference / (24 * 60 * 60);
// Calculate the number of years, months, and days
int years = days / 365;
int remainingDays = days % 365;
int months = remainingDays / 30; // Approximate number of days in a month
remainingDays %= 30; // Remaining days after subtracting months
// Print the countdown
printw("Time remaining: %d years, %d months, %d days\n%d hours, %d minutes, %d seconds",
years, months % 12, days % 7, hours, minutes, seconds);
}
// Refresh the screen to display changes
refresh();
// Sleep for 1 second before the next update
sleep(1);
}
}
// End ncurses
endwin();
return 0;
}