#include <stdio.h>
#include <ctype.h>
#include <string.h>
int days(char *p);
struct month{
char name[10];
char abbrev[4];
int days;
int monumb;
};
struct month months[12] = {
{"janury", "jan", 31, 1},
{"february", "feb", 28, 2},
{"march", "mar", 31, 3},
{"april", "apr", 30, 4},
{"may", "may", 31, 5},
{"june", "jun", 30, 6},
{"july", "jul", 31, 7},
{"august", "aug", 31, 8},
{"september", "sep", 30, 9},
{"october", "oct", 31, 10},
{"november", "nov", 30, 11},
{"december", "dec", 31, 12}
};
int main(void)
{
char input[10];
int daytotal;
printf("Enter the name of a month: ");
while(gets(input) != NULL && input[0] != '\0')
{
daytotal = days(input);
if(daytotal > 0)
printf("There are 5d days through %s.\n", daytotal, input);
else
printf("%s is not valid through %s.\n", daytotal, input);
printf("Next month (empty line to quit):");
}
puts("bye");
return 0;
}
int days(char *p)
{
int i = 0, total = 0;
while(p[i] != '\0')
{
p[i] = tolower(p[i]);
i++;
}
for(i = 0; i<12; i++)
{
total += months[i].days;
if(strcmp(p, months[i].name) == 0)
return total;
}
return -1;
}
2.
#include <stdio.h>
int days(int day, int month, int year);
int leapyear(int year);
struct month{
char name[10];
char abbrev[4];
int days;
int monumb;
};
struct month months[12] = {
{"janury", "jan", 31, 1},
{"february", "feb", 28, 2},
{"march", "mar", 31, 3},
{"april", "apr", 30, 4},
{"may", "may", 31, 5},
{"june", "jun", 30, 6},
{"july", "jul", 31, 7},
{"august", "aug", 31, 8},
{"september", "sep", 30, 9},
{"october", "oct", 31, 10},
{"november", "nov", 30, 11},
{"december", "dec", 31, 12}
};
int main(void)
{
int day = 1, mon = 12, year = 1, daytotal;
printf("Enter the number of day,month,year: ");
while(scanf("%d%d%d", &day, &mon, &year) == 3)
{
daytotal = days(day, mon, year);
if(daytotal > 0)
printf("There are %d days through day %d, month %d, year%d.\n", daytotal, day, mon, year);
else
printf("day %d, month %d, year %d is not valid input.\n", day, mon, year);
printf("Next month(input q to quit): ") ;
}
puts("bye");
return 0;
}
int days(int day, int mon, int year)
{
int i, total;
if(leapyear(year))
months[1].days = 29;
else
months[1].days = 28;
if(mon <1 || mon > 12 || day < 1 || day > months[mon -1].days)
return (-1);
else
{
for(i = 0, total = 0; i< mon-1;i++)
total += months[i].days;
return (total+day);
}
}
int leapyear(int year)
{
if(year %400 == 0)
return 1;
else if(year % 100 != 0 && year %4 == 0)
return 1;
else
return 0;
}
3.
#include <stdio.h>
#include <string.h>
void sort_title(struct book *p, int count);
void sort_value(struct book *p, int count);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
struct book{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
int count = 0;
int index;
printf("Please enter the book title.\n");
printf("Press [enter] at the start of a line to stop.\n");
while(count < MAXBKS && gets(library[count].title) != NULL &&
library[count].title[0] != '\0')
{
printf("Now enter the author.\n");
gets(library[count].author);
printf("No enter the value.\n");
scanf("%f", &library[count++].value);
while(getchar() != '\n')
continue;
if(count < MAXBKS)
printf("Enter the next title.\n");
}
if(count > 0)
{
printf("Here is the list of your books:\n");
for(index = 0; index <count; index++)
printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
printf("\nHere is the list of your books by title:\n");
sort_title(&library[0], count);
for(index = 0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
}
else
printf("No books?Too bad.\n");
return 0;
}
void sort_title(struct book *p, int count)
{
int i,j;
struct book temp;
for(i = 0; j < count-1; i++)
for(j = 0; j < count-1-i; j++)
if(strcmp(p[j].title,p[j+1].title) > 0)
{
temp = p[j];
p[j] = p[j+1];
p[j + 1] = temp;
}
}
void sort_value(struct book *p, int count)
{
int i, j;
struct book temp;
for(i = 0; i < count -1; i++)
for(j = 0; j <count -1-i; j++)
if(p[j].value > p[j+1].value)
{
temp = p[j];
p[j]= p[j+1];
p[j+1] = temp;
}
}