Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int leap(int x){
- if(x%4 == 0 && x%100 != 0 || x%400 == 0)
- return 1;
- else
- return 0;
- }
- int pastDays(int m, int d, int y){
- int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- int i, past = 0;
- if(leap(y)){
- days[1] = 29;
- }
- for(i = 0; i < m-1; i++){
- past = past + days[i];
- }
- return past + d;
- }
- int daysPast(int d, int y) {
- int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- int i, temp = 0;
- if(leap(y)){
- days[1] = 29;
- }
- for(i = 0; i < 11 && d > days[i]; i++){
- d = d - days[i];
- }
- if(d < 0){
- d = days [i] + d;
- }
- printf("\n\t\t%d/%d/%d", i+1,d, y);
- }
- int main()
- {
- int choice, past, mm, dd, yy, temp;
- char more;
- printf("\n\t\tThis program will find days past or date in the year");
- do{
- printf("\n\t\t1) date -> days past");
- printf("\n\t\t2) days past -> date");
- printf("\n\t\tYour choice (1/2)?");
- scanf("%d", &choice);
- if(choice == 1){
- printf("\n\t\tInput date (mm/dd/yyyy)");
- scanf("%d/%d/%d", &mm, &dd, &yy);
- if(mm > 12) {
- printf("\n\t\tThere are not more than 13 months in a year");
- }
- else if(dd > 31){
- printf("\n\t\tCant have more than 31 days in a month");
- }
- else if(mm == 2 && dd > 28 && leap(yy) != 1 || mm == 2 && dd > 29 && leap(yy) == 0){
- printf("\n\t\tThere are not more than 28 day in February %d", yy);
- }
- else{
- printf("\n\t\tThere are %d days past in the year", pastDays(mm, dd, yy));
- }
- }
- else if(choice == 2) {
- printf("\n\t\tInput days past (ddd)");
- scanf("%d", &past);
- printf("\n\t\tInput the year (yyyy)");
- scanf("%d", &yy);
- if(past == 366 && leap(yy) == 0){
- printf("\n\t\tError there's not 366 days in %d", yy);
- }
- else if(past > 365) {
- yy = yy + 1;
- past = past - 365;
- daysPast(past, yy);
- }
- else if(past < 0){
- yy = yy -1;
- past = 365 + past;
- daysPast(past, yy);
- }
- else{
- daysPast(past, yy);
- }
- }
- printf("\n\t\tDo more (Y/N)");
- scanf("%s", &more);
- }while(more == 'y' || more == 'Y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement