Complete and full answer in order to get credit. Thank u. This program is to be
ID: 3807076 • Letter: C
Question
Complete and full answer in order to get credit. Thank u. This program is to be written in C just plain C.
Assignment: Write algorithms and programs to read an input file of dates and generate an output file that contains a list of validated and converted dates. In summary, implement a pair of programs that will interface and exchange data — in this case, a list of dates.
Output: Generate an output file (output.dat) from the second program that contains a converted list of dates in day, abbreviated month & year format (i.e. 1 JAN 1900), followed by the original list of dates. This output file will be the result of appending the input file (dates.dat), which is also accessed by the first program, with the result output file (output.dat), generated by the second program.
Input: Accept input for the first program via the command-line arguments. Command-line input will be the number of valid entries to be redirected from the dates input file (dates.dat) and such input must be validated (>= 0). A zero indicates to input all valid entries from the dates input file.
Your first program should validate dates only in valid month/day/year format (i.e. 1/1/1900), skipping any corrupt dates in the dates.dat file . A valid year is in the range [INT_MIN..INT_MAX] - negative years and year zero are valid. Valid days varies for each month (1-12) and leap years should be considered. The validated dates will then be piped out to the second program.
The second program will accept these validated dates in the month/day/year format and convert each of them to the day, abbreviated month & year format — both exhibited above. The abbreviated month should consist of the first three letters of the month, capitalized. These converted results will be redirected to the output file (output.dat). Upon completion, this output will be followed by a copy of the complete original (dates.dat) data.
Explanation / Answer
readDates.c
// readDates.c
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h> /* for atoi() */
#include <ctype.h> /* for isdigit() */
#include <unistd.h> /* for close() */
#include "dates.h" /* definition for types and function prototype */
/******************************************************************************/
/* START PSEUDO CODE
int main(int argc, char *argv[]) {
if (inputsIsValid(argc == 2)){
convert strings argv[1] to integers to int amountOfData;
if (amountOfData == 0)
getAllData();
else
getSomeData(amountOfData);
}
else
printf(“%s ”, “Some Error”);
return 0;
}
END PSEUDO CODE */
/******************************************************************************/
int main(int argc, char *argv[]) {
int day = 0, month = 0, dToRead = 0;
long year = 0;
char buf[DATALENGTH];
// parameters check and error handling
if(argc == 2) {
dToRead = atoi(argv[1]); // how many data to read
if(dToRead < 0 || !isdigit(*argv[1])) {
// print an error & break the pipe
fprintf(stderr, "You entered an invalid number of data to read. ");
exit(1);
}
}
else {
// print an error & break the pipe
fprintf(stderr, "You did not enter the number of data to read.");
fprintf(stderr, " 0 means all data. ");
exit(2);
}
// handling data
if(dToRead == 0) //read all data
getAllData(month, day, year, buf);
else //read 'dToRead' amount of data
getSomeData(dToRead, month, day, year, buf);
printf("%s ", "");
return NOERRORS;
}
writeDates.c
// writeDates.c
/******************************************************************************/
#include <stdio.h>
#include "dates.h" /* definition for types and function prototype */
int main(int argc, char *argv[]) {
int day = 0, month = 0, year = 0;
char buf[1000];
printf("%s ", "Good dates from input:");
printGoodDates(month, day, year);
printf("%s ", "All dates from input:");
printAllDates(buf, "dates.dat");
return NOERRORS;
}
filterDates.c
#include <stdio.h>
#include <limits.h> /* for INT_MIN and INT_MAX */
#include <stdlib.h> /* for atoi() */
#include <unistd.h> /* for close() */
#include <ctype.h> /* for isdigit() */
#include "dates.h" /* definition for types and function prototype */
/******************************************************************************/
/* Used for days of the month */
const int daytable[MONTHS] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* Used for name of the month */
const char monthName[MONTHS][MONTHS] = {"BAD MONTH", "JAN", "FEB", "MAR", "APR",
"MAY", "JUN","JUL", "AGO", "SET", "OCT", "NOV", "DIC"};
/******************************************************************************/
// FUNCTIONS for weriteDates.c
/******************************************************************************/
/*
* Function: printGoodDates
* ------------------------
* Reads input data in m/d/y format and print in day, abbreviated month & year
* format.
*
* arg: int day, int month, int year.
* return: void
* side sffects: none.
*/
void printGoodDates(int month, int day, int year) {
while(scanf("%d%d%d",&month,&day,&year) != EOF && month != 0) {
printf("%d %s %d " ,day,monthName[month],year);
}
printf("%s ", "");
}
/******************************************************************************/
/*
* Function: printAllDates
* -----------------------
* Reads all data from input file and print it to the output file.
*
* arg: char buf: save data for processing, char file: name of input file.
* return: void
* side sffects: none.
*/
void printAllDates(char buf[], char file[]) {
FILE *fileToRead;
fileToRead = fopen(file,"r");
while (fgets(buf,1000, fileToRead) != NULL) {
printf("%s",buf);
}
printf("%s ", "");
fclose(fileToRead);
}
/******************************************************************************/
// FUNCTIONS for readDates.c
/******************************************************************************/
/*
* Function: getAllData
* ---------------------
* Reads all input data.
*
* arg: int day: of the month, int month: of the year, int year,
* char buf: save data for processing.
* return: void
* side sffects: none.
*/
void getAllData(int month, int day, long year, char buf[]) {
while(scanf("%s",buf) != EOF) {
if(sscanf(buf, "%d/%d/%ld",&month,&day,&year) == DATEFORMAT) {
if(isOkDate(month, day, year)) {
printf("%d %d %ld " ,month,day,year);
}
}
}
}
/******************************************************************************/
/*
* Function: getSomeData
* ----------------------
* Reads the amount of data indicatede by a parameter.
*
* arg: int dToRead: the amount of data to read, int day: of the month, int
* month: of the year, long year, char buf: save data for processing
* return: void
* side sffects: none.
*/
void getSomeData(int dToRead, int month, int day, long year, char buf[]) {
while(scanf("%s",buf) != EOF) {
if(dToRead > 0) {
if(sscanf(buf, "%d/%d/%ld",&month,&day,&year) == DATEFORMAT) {
if(isOkDate(month, day, year)) {
printf("%d %d %ld " ,month,day,year);
dToRead--;
}
}
}
else
return;
}
}
/******************************************************************************/
/*
* Function: isOkDate
* -------------------
* Verify that the dates are in valid format and range.
*
* arg: int day, int month, long year.
* return: FALSE if date is not in valid format & range. Returns TRUE otherwise.
* side sffects: none.
*/
int isOkDate(int month, int day, long year) {
if(!isDay(day)) //validate day
return FALSE;
if(!isMonth(month)) //validate month
return FALSE;
if(!isYear(year)) //validate year
return FALSE;
//validate leap year
if(month == FEB && day == LEAPDAY && year >= FIRSTLEAP) {
if(!isLeap(year))
return FALSE;
}
else if(!(day <= daytable[month])) //validate days of month
return FALSE;
return TRUE;
}
/******************************************************************************/
/*
* Function: isDay
* ----------------
* Helper function to verify that the days are within the range.
*
* arg: int day: of the month.
* return: FALSE if day is not within the range. Returns TRUE otherwise.
* side sffects: none.
*/
int isDay(int day) {
if(day > 0 && day < DAYS)
return TRUE;
return FALSE;
}
/******************************************************************************/
/*
* Function: isMonth
* ------------------
* Helper function to verify that the months are within the range.
*
* arg: int month: of the year.
* return: FALSE if month is not within the range. Returns TRUE otherwise.
* side sffects: none.
*/
int isMonth(int month) {
if(month > 0 && month < MONTHS)
return TRUE;
return FALSE;
}
/******************************************************************************/
/*
* Function: isYear
* -----------------
* Helper function to verify that the year is a valid number between the maximum
* and minimum system integer.
*
* arg: long year.
* return: FALSE if year is not within the range. Returns TRUE otherwise.
* side sffects: none.
*/
int isYear(long year) {
if(year >= INT_MIN && year <= INT_MAX)
return TRUE;
return FALSE;
}
/******************************************************************************/
/*
* Function: isLeap
* -----------------
* Helper function to verify if the years are leap or not.
*
* arg: long year.
* return: FALSE if year is not leap. Returns TRUE otherwise.
* side sffects: none.
*/
int isLeap(long year) {
if(((int)year % 4 == 0 && (int)year % 100 != 0) || (int)year % 400 == 0)
return TRUE;
return FALSE;
}
dates.h
#define FIRSTLEAP 1752 //used to control leap years
#define DATALENGTH 1000 //used to control length of data
#define LEAPDAY 29 //used to check for leap year
#define MONTHS 13 //used to control day of the month
#define DAYS 32 //used to control day of the month
#define DATEFORMAT 3 //used to control date input format
#define FEB 2 //used to check for February
#define TRUE 1 //used to return 1
#define FALSE 0 //used to return 0
#define NOERRORS 0 //no errors found
const int daytable[MONTHS]; //used for day/month control
const char monthName[MONTHS][MONTHS]; //used for name of months
void printGoodDates(int, int, int);
void printAllDates(char[], char[]);
void getAllData(int, int, long, char[]);
void getSomeData(int, int, int, long, char[]);
int isOkDate(int, int, long);
int isDay(int);
int isMonth(int);
int isYear(long);
int isLeap(long);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.