Ich habe ein Programm geschrieben, das ein Datum aufnimmt und es dann in einen Wochentag umwandelt. Ich habe eine vorherige Frage gestellt, um es zu reparieren, als ich Probleme hatte, es zu kompilieren.Wie kann ich diesen Code aufräumen, um ein Datum in einen Wochentag umzuwandeln?
Ich habe es mit einigen sehr geschätzten Anregungen behoben und es läuft perfekt.
Allerdings ist mir sehr bewusst, dass es wie ein Haufen Scheiße aussieht. Ich lerne C über ein Buch und habe keine Zieh- oder Zeichenketten behandelt, so dass ich ziemlich begrenzt bin mit dem, was ich verwenden kann, um dieses Programm übersichtlich zu strukturieren.
Ich gehe davon aus, dass das Buch beabsichtigt, diese Übung zu absolvieren OHNE diese Aspekte der Sprache zu verwenden.
Gibt es etwas Großes, das mir fehlt, das es mir ermöglichen würde, dieses Ding kurzzuschließen? Ich bin ein Anfänger also bitte bare mit mir. Hier
ist der 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;
}
genial, danke @Gibstick! – gloopit