C Programming: Transaction and Vehicle Structs
Section A: Transaction Data Management
Defining the Transaction Structure
A. Create a structure Transaction with the following attributes: ID (integer number), account_number (array of characters), amount (float), and typology (character).
typedef struct {
char plate_number[PLATELENGTH + 1];
char brand[NAMELENGTH];
char model[NAMELENGTH];
int production_year;
} Vehicle;Constants for Transaction Typology
A. Define the max length for the attribute "account" to 20, and two constants to determine the typology of the transaction, such as CREDIT or DEBIT (respectively 'c' and 'd').
#define MAX_ACCOUNT_LEN 20
#define CREDIT 'c'
#define DEBIT 'd'Reading Transaction Data from Input
A. Create a method named readInstance() that reads from the user input an instance of the structure Transaction and returns it.
Transaction readInstance(void) {
Transaction t;
printf("Enter ID: ");
scanf("%d", &t.id);
printf("Enter account number: ");
scanf("%19s", t.account_number);
printf("Enter amount: ");
scanf("%f", &t.amount);
printf("Enter typology: ");
scanf(" %c", &t.typology);
return t;
}Validating Account Numbers in C
A. Create a method named checkAccountNumber() that takes as input a structure of type Transaction and checks that all its characters represent numbers. If it succeeds, it returns 1; otherwise, it returns 0.
int validatePlate(Vehicle v) {
char *p = v.plate_number;
int len = 0;
while (p[len] != '\0') len++;
if (len != PLATELENGTH) return 0;
if (!isalpha((unsigned char)p[0]) || !isalpha((unsigned char)p[1])) return 0;
if (!isdigit((unsigned char)p[2]) || !isdigit((unsigned char)p[3]) ||
!isdigit((unsigned char)p[4])) return 0;
if (!isalpha((unsigned char)p[5]) || !isalpha((unsigned char)p[6])) return 0;
return 1;
}Calculating Incomes from Transaction Lists
A. Create a method named calculateIncomes() that takes as input a list of the structure Transaction and sums only the ones with typology "CREDIT". The sum is returned as output.
Vehicle *filter_by_year(Vehicle list[], int size, int year, int *outSize) {
int count = 0;
for (int i = 0; i < size; i++)
if (list[i].production_year == year) count++;
Vehicle *result = malloc(count * sizeof(Vehicle));
int j = 0;
for (int i = 0; i < size; i++) {
if (list[i].production_year == year) {
result[j] = list[i];
j++;
}
}
*outSize = count;
return result;
}Writing Transaction Data to Files
A. Create a method named writeInstance() that takes as input a structure and a filename, and writes the content of the transaction to the file according to the following template:
- ID:
- Number:
- Amount:
- Typology:
void writeInstance(Transaction t, const char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) return;
fprintf(fp, "ID: %d\n", t.id);
fprintf(fp, "Number: %s\n", t.account_number);
fprintf(fp, "Amount: %.2f\n", t.amount);
fprintf(fp, "Typology: %c\n", t.typology);
fclose(fp);
}Section B: Vehicle Data Management
Defining the Vehicle Structure
B. Create a structure named Vehicle with the following attributes: plate_number (array of characters), brand (array of characters), model (array of characters), and production_year (integer).
typedef struct {
char plate_number[PLATELENGTH + 1];
char brand[NAMELENGTH];
char model[NAMELENGTH];
int production_year;
} Vehicle;Constants for Vehicle Attributes
B. Define a constant for the max length of NAMELENGTH to 20 and PLATELENGTH to 7.
#define NAMELENGTH 20
#define PLATELENGTH 7Reading Vehicle Data from Input
B. Create a method named readInstance() that reads from the user input an instance of the structure Vehicle and returns it.
Vehicle readInstance(void) {
Vehicle v;
printf("Enter plate number: ");
scanf("%7s", v.plate_number);
printf("Enter brand: ");
scanf("%19s", v.brand);
printf("Enter model: ");
scanf("%19s", v.model);
printf("Enter production year: ");
scanf("%d", &v.production_year);
return v;
}Validating Vehicle Plate Numbers
B. Create a method named checkAccountNumber() that takes as input a structure of type Transaction and checks that all its characters represent numbers. If it succeeds, it returns 1; otherwise, it returns 0.
int validatePlate(Vehicle v) {
char *p = v.plate_number;
int len = 0;
while (p[len] != '\0') len++;
if (len != PLATELENGTH) return 0;
if (!isalpha((unsigned char)p[0]) || !isalpha((unsigned char)p[1])) return 0;
if (!isdigit((unsigned char)p[2]) || !isdigit((unsigned char)p[3]) ||
!isdigit((unsigned char)p[4])) return 0;
if (!isalpha((unsigned char)p[5]) || !isalpha((unsigned char)p[6])) return 0;
return 1;
}Filtering Vehicles by Production Year
B. Create a method named calculateIncomes() that takes as input a list of the structure Transaction and sums only the ones with typology "CREDIT". The sum is returned as output.
Vehicle *filter_by_year(Vehicle list[], int size, int year, int *outSize) {
int count = 0;
for (int i = 0; i < size; i++)
if (list[i].production_year == year) count++;
Vehicle *result = malloc(count * sizeof(Vehicle));
int j = 0;
for (int i = 0; i < size; i++) {
if (list[i].production_year == year) {
result[j] = list[i];
j++;
}
}
*outSize = count;
return result;
}Storing Vehicle Data in Files
B. Create a method named writeInstance() that takes as input a structure and a filename, and writes the content of the transaction to the file according to the following template:
- ID:
- Number:
- Amount:
- Typology:
void storeVehicle(Vehicle list[], int size, const char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) return;
for (int i = 0; i < size; i++) {
fprintf(fp, "Plate: %s\n", list[i].plate_number);
fprintf(fp, "Brand: %s\n", list[i].brand);
fprintf(fp, "Model: %s\n", list[i].model);
fprintf(fp, "Year: %d\n", list[i].production_year);
fprintf(fp, "----\n");
}
fclose(fp);
}
English with a size of 6.92 KB