Program 1)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define NUM_DAYS_IN_WEEK 7
int i;

// Structure to represent a day


typedef struct
{
char *acDayName; // Dynamically allocated string for the day name
int iDate; // Date of the day
char *acActivity; // Dynamically allocated string for the activity description
}DAYTYPE;

//Prototypes
void FreeCal(DAYTYPE *);
void DispCal(DAYTYPE *);
void ReadCal(DAYTYPE *);
DAYTYPE *CreateCal();

//Main function
int main()
{
// Create the calendar
DAYTYPE *weeklyCalendar = CreateCal();

// Read data from the keyboard


ReadCal(weeklyCalendar);

// Display the week's activity details


DispCal(weeklyCalendar);
// Free allocated memory
FreeCal(weeklyCalendar);

return 0;
}

// Createcalender function definition


DAYTYPE *CreateCal()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK *
sizeof(DAYTYPE));

for( i = 0; i < NUM_DAYS_IN_WEEK; i++)


{
calendar[i].acDayName = NULL;
calendar[i].iDate = 0;
calendar[i].acActivity = NULL;
}

return calendar;
}
// Read Calender function definition
void ReadCal(DAYTYPE *calendar)
{
char Choice;
for( i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Do you want to enter details for day %d [Y/N]: ", i + 1);
scanf("%c", &Choice);
getchar();

if(tolower(Choice) == 'n')
continue;
printf("Day Name: ");
char nameBuffer[50];
scanf("%s", nameBuffer);
calendar[i].acDayName = strdup(nameBuffer); // Dynamically allocate and copy
the string

printf("Date: ");
scanf("%d", &calendar[i].iDate);

printf("Activity: ");
char activityBuffer[100];
scanf(" %[^\n]", activityBuffer); // Read the entire line, including spaces
calendar[i].acActivity = strdup(activityBuffer);

printf("\n");
getchar(); //remove trailing enter character in input buffer
}
}

// DisplayCalender function definition


void DispCal(DAYTYPE *calendar)
{
printf("\nWeek's Activity Details:\n");
for(i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Day %d:\n", i + 1);
if(calendar[i].iDate == 0)
{
printf("No Activity\n\n");
continue;
}
printf(" Day Name: %s\n", calendar[i].acDayName);
printf(" Date: %d\n", calendar[i].iDate);
printf(" Activity: %s\n\n", calendar[i].acActivity);
}
}

// FreeCalender function definition


void FreeCal(DAYTYPE *calendar)
{
for( i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
free(calendar[i].acDayName);
free(calendar[i].acActivity);
}
free(calendar);
}

/* Output

[root@localhost 2022batchDSLab]# cc 1_Calender.c


[root@localhost 2022batchDSLab]# ./a.out
Do you want to enter details for day 1 [Y/N]: y
Day Name: Monday
Date: 27102023
Activity: CSI work

Do you want to enter details for day 2 [Y/N]: y


Day Name: Tuesday
Date: 28102023
Activity: FDP conduction

Do you want to enter details for day 3 [Y/N]: y


Day Name: Wednesday
Date: 29102023
Activity: Proposal Writeup

Do you want to enter details for day 4 [Y/N]: y


Day Name: Thursday
Date: 30102023
Activity: Data Analysis

Do you want to enter details for day 5 [Y/N]: y


Day Name: Friday
Date: 31102023
Activity: Article Review

Do you want to enter details for day 6 [Y/N]: y


Day Name: Saturday
Date: 01112023
Activity: Week Off Enjoy

Do you want to enter details for day 7 [Y/N]: y


Day Name: Sunday
Date: 02112023
Activity: Enjoy

Week's Activity Details:


Day 1:
Day Name: Monday
Date: 27102023
Activity: CSI work

Day 2:
Day Name: Tuesday
Date: 28102023
Activity: FDP conduction
Day 3:
Day Name: Wednesday
Date: 29102023
Activity: Proposal Writeup

Day 4:
Day Name: Thursday
Date: 30102023
Activity: Data Analysis

Day 5:
Day Name: Friday
Date: 31102023
Activity: Article Review

Day 6:
Day Name: Saturday
Date: 1112023
Activity: Week Off Enjoy

Day 7:
Day Name: Sunday
Date: 2112023
Activity: Enjoy

[root@localhost 2022batchDSLab]# ./a.out


Do you want to enter details for day 1 [Y/N]: y
Day Name: Wednesday
Date: 01112023
Activity: Article Review

Do you want to enter details for day 2 [Y/N]: n


Do you want to enter details for day 3 [Y/N]: n
Do you want to enter details for day 4 [Y/N]: n
Do you want to enter details for day 5 [Y/N]: n
Do you want to enter details for day 6 [Y/N]: n
Do you want to enter details for day 7 [Y/N]: y
Day Name: sunday
Date: 07112023
Activity: Enjoy

Week's Activity Details:


Day 1:
Day Name: Wednesday
Date: 1112023
Activity: Article Review

Day 2:
No Activity

Day 3:
No Activity

Day 4:
No Activity

Day 5:
No Activity

Day 6:
No Activity

Day 7:
Day Name: sunday
Date: 7112023
Activity: Enjoy

You might also like