Sunteți pe pagina 1din 3

Computation 5EIA0/5AIA0

Exercise 6: Database

In this exercise you will develop a database program that a user can use to keep track of the specifications of
his/her cars (i.e., model, brand, year, value). The user will be able to add and remove cars from the database.
Of course, the user should also be able to print the complete database to the terminal.
Task 1. Create a new project in Visual C++ with the name 06-database and add a C++ file to the project.
As always you should assign a meaningful name to the file (e.g., database.cpp).
Task 2. Create a struct of type struct carinfo t that can be used to store the following information:
The brand of the car (a string)
The model (a string)
The year the car was built (an int)
The value of the car (a float)
Task 3. Create inside the main function an array carbase with room for 100 pointers to structs of type
struct carinfo t and initialize all elements with the NULL value.
Task 4.
Write a function struct carinfo t *createCarinfo(char *brand, char *model, int
year, float value) that creates a new carinfo t instance. The function has 4 input parameters (2
strings, an int and a float). The function will have to implement the following steps:
Allocate space for a new carinfo t struct.
Allocate space for the brand and model strings.
Copy the brand and the model strings from the input arguments into the memory allocated in the
previous step.
Set the remaining struct fields (year and value). No allocation is required here, you can just copy them.
Return the pointer to the struct you created.
If all went well, you should be able to call you function from you main function in the following manner:
car_info_t* car = create("Opel", "Manta",

1988, 350.25);

Task 5.
Write a function void addCarinfo(struct carinfo t **carbase, struct carinfo t
*carinfo) that adds the variable carinfo to carbase. When the database is already full, the car information should not be added. Instead an error message should be printed.
Task 6. Write a function void freeCarinfo(struct carinfo t *carinfo) that releases all memory
allocated inside the createCarinfo function for the struct carinfo.
Hint: You will have to call the function free three times inside the function freeCarinfo.

1/3

Task 7. Write a function void printCarbase(struct carinfo t **carbase) that outputs all information about all cars stored in the database. When the database is empty, the function should print a message
that the database contains no cars.
When the database contains information on two cars you should see an output similar to:
Car:
- brand:
- model:
- year:
- value:
Car:
- brand:
- model:
- year:
- value:

Opel
Manta
1980
350.25
Audi
A3
2011
35000

When the database is empty, you should see the following output:
The database is empty.

Task 8. Your database program already supports multiple operations (i.e., add cars and print the database).
The user should be able to select the operation he/she wants to perform and subsequently the program should
perform this operation. Obviously, the user should also have the option to quit the program. Adapt your
main function such that the user can select the desired operation. For now, your program should support the
following operations:
command
a
p
q

operation
add car
print database
quit program

The output of your program should now look as follows:


Command (a/p/q): p
The database is empty.
Command (a/p/q): a
Add a car:
brand: Opel
model: Manta
year: 1980
value: 350.25
Command (a/p/q): p
Car:
- brand: Opel
- model: Manta
- year: 1980
- value: 350.25
Command (a/p/q): q

2/3

Task 9. Write a function void removeCarinfo(struct carinfo t **carbase, struct carinfo t


*carinfo) that removes all occurrences of cars with the supplied information inside the variable carinfo
from the database contained in the array carbase. When removing cars, you should ignore the value of the car.
Of course you will have to extend the set of operations supported by your program. You can use the command
r for the operation remove a car. The output of your program should then look as follows:
Command (a/p/r/q):
brand: Opel
model: Manta
year: 1980
value: 350.25
Command (a/p/r/q):
brand: Opel
model: Manta
year: 1980
value: 250.00
Command (a/p/r/q):
brand: Audi
model: A3
year: 2011
value: 350000
Command (a/p/r/q):
Car:
- brand: Opel
- model: Manta
- year: 1980
- value: 350.25
Car:
- brand: Opel
- model: Manta
- year: 1980
- value: 250.00
Car:
- brand: Audi
- model: A3
- year: 2011
- value: 35000
Command (a/p/r/q):
Remove a car:
brand: Opel
model: Manta
year: 1980
Command (a/p/r/q):
Car:
- brand: Audi
- model: A3
- year: 2011
- value: 35000
Command (a/p/r/q):

3/3

S-ar putea să vă placă și