Reputation: 437
I've written a program that takes in a date and then converts it into a day of the week. I asked a previous question to fix it when I had trouble compiling it.
I've managed to fix it with some much appreciated suggestions and it runs perfectly.
However, I'm very aware that it looks like a pile of shit. I'm learning C via a book and I've not covered pointers OR character strings so I'm pretty limited with what I can use to structure this program concisely.
I'm assuming the book intends me to complete this exercise WITHOUT using those aspects of the language.
Is there anything huge that I'm missing that would enable me to shorted this thing up?
Here is the code:
#include <stdio.h>
typedef struct
{
int day;
int month;
int year;
}DATE;
int nConvert(DATE N);
int gee(DATE work);
int eff(DATE work);
int dayFinder(int N);
int main (void)
{
int N, day, dayName;
DATE date;
char names[7][3] = {
{'S', 'u', 'n'},
{'M', 'o', 'n'},
{'T', 'u', 'e'},
{'W', 'e', 'd'},
{'T', 'h', 'u'},
{'F', 'r', 'i'},
{'S', 'a', 't'}
};
printf("Okay, choose your date:\n");
scanf("%i/%i/%i", &date.day, &date.month, &date.year); //puts the date and creates a variable withing the DATE struct
N = nConvert(date); //go convert the date into an integer
day = dayFinder(N); //go convert N into an int that represents the day of the week
//now we match the int from dayFinder and match it to the right array in names so it can then be printed below
switch(day)
{
case(0):
dayName = 0;
break;
case(1):
dayName = 1;
break;
case(2):
dayName = 2;
break;
case(3):
dayName = 3;
break;
case(4):
dayName = 4;
break;
case(5):
dayName = 5;
break;
case(6):
dayName = 6;
break;
}
// now we can print out the day of the week that we created in dayFinder
printf("The day of the week of %i/%i/%i is: %c%c%c\n", date.day, date.month, date.year, names[dayName][0], names[dayName][1], names[dayName][2]);
}
int nConvert(DATE N)
{
int f = eff(N); //call the functions so the outputs can be put into the equation for 'result'
int g = gee(N);
int result = (1461 * f) / 4 + (153 * g) / 5 + N.day; //store the result of the equation into a 'result'
return result; //go put result back into main, will be called 'N'
}
int eff(DATE work)
{
if(work.month <= 2)
{
work.year = work.year - 1;
}
else
{
work.year = work.year;
}
return work.year;
}
int gee(DATE work)
{
if(work.month <= 2)
{
work.month = work.month + 13;
}
else
{
work.month = work.month + 1;
}
return work.month;
}
int dayFinder(int n)
{
int convert = (n - 621049) % 7; //convert N sent from main, into integer called converter.
//we will now have a number that conincides with the day of the week
return convert;
}
Upvotes: 2
Views: 75
Reputation: 737
The very first thing I would do is remove that switch statement, because it's essentially doing dayName = day;
Once you do that, you might also notice that you don't even need the dayName
variable at all. You can simply use day
directly.
Upvotes: 2