Define a structure type auto_t to represent an automobile. Include components fo
ID: 3767921 • Letter: D
Question
Define a structure type auto_t to represent an automobile. Include components for the make and model ( strings), the odometer reading, the manufacture, and purchase dates (use another user-defined type called data_t), and the gas tank ( use a user-defined hype tank_t with componetns for tank capacity and current fuel level, giving both ingallons). Write I/O functions scan_data, scan_tank, scan_auto, print_date, print_tank, and print_auto, and also write a driver function therepeatedly fills and displays an auto structure variable until EOF is encountered in the input file.
**C not C++
Data set to try:
Mercury Sable 99842 1 18 2001 5 30 1991 16 12.5
Explanation / Answer
#include <stdio.h>
#include <string.h>
#define BUFSIZE 1000
struct auto_t scan_auto(char *);
struct date_t {
char day[2];
char month[2];
char year[4];
};
struct tank_t {
char tankCapacity[10];
char currentFuelLevel[10];
};
struct auto_t {
char make[50];
char model[50];
char odometerReading[10];
struct date_t manufactureDate;
struct date_t purchaseDate;
struct tank_t gasTank;
};
int main(int argc, char *argv[]) {
/* the first command-line parameter is in argv[1]
(arg[0] is the name of the program) */
FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */
char buff[BUFSIZE]; /* a buffer to hold what you read in */
struct auto_t newAuto;
/* read in one line, up to BUFSIZE-1 in length */
while(fgets(buff, BUFSIZE - 1, fp) != NULL)
{
/* buff has one line of the file, do with it what you will... */
newAuto = scan_auto(buff);
printf("%s ", newAuto.make);
}
fclose(fp); /* close the file */
}
struct auto_t scan_auto(char *line) {
int spacesCount = 0;
int i, endOfMake, endOfModel, endOfOdometer;
for (i = 0; i < sizeof(line); i++) {
if (line[i] == ' ') {
spacesCount++;
if (spacesCount == 1) {
endOfMake = i;
}
else if (spacesCount == 2) {
endOfModel = i;
}
else if (spacesCount == 3) {
endOfOdometer = i;
}
}
}
struct auto_t newAuto;
int count = 0;
for (i = 0; i < endOfMake; i++) {
newAuto.make[count++] = line[i];
}
newAuto.make[count] = '';
count = 0;
for (i = endOfMake+1; i < endOfModel; i++) {
newAuto.model[count++] = line[i];
}
newAuto.model[count] = '';
count = 0;
for (i = endOfModel+1; i < endOfOdometer; i++) {
newAuto.odometerReading[count++] = line[i];
}
newAuto.odometerReading[count] = '';
return newAuto;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.