3. Fiscal receipts – solution
TASK TEXT:All fiscal receipts issued by one shop are known. From each receipt you can read the date it was issued and the amount that was charged.
Write a program that finds the maximum daily takings for that shop.
Example 1:
Input:
Enter the number of fiscal receipts: 3
Enter the date and amount of receipt 1 (format: DD.MM.YYYY): 01.05.2024 1500
Enter the date and amount of receipt 2 (format: DD.MM.YYYY): 01.05.2024 2000
Enter the date and amount of receipt 3 (format: DD.MM.YYYY): 02.05.2024 1800
Output
Maximum daily takings: 3500 dinars, date: 01.05.2024
Example 2:
Input:
Enter the number of fiscal receipts: 4
Enter the date and amount of receipt 1 (format: DD.MM.YYYY): 03.05.2024 1200
Enter the date and amount of receipt 2 (format: DD.MM.YYYY): 03.05.2024 1700
Enter the date and amount of receipt 3 (format: DD.MM.YYYY): 04.05.2024 2200
Enter the date and amount of receipt 4 (format: DD.MM.YYYY): 05.05.2024 2500
Output:
Maximum daily takings: 2900 dinars, date: 03.05.2024
Solution
#include <iostream>#include <map>
using namespace std;
int main() {
// Map that stores the daily takings (date -> total amount)
map<string, int> daily_takings;
// Entering the fiscal receipts
int n;
cout << "Enter the number of fiscal receipts: ";
cin >> n;
for (int i = 0; i < n; ++i) {
string date;
int amount;
cout << "Enter the date and amount of receipt " << i+1 << " (format: DD.MM.YYYY): ";
cin >> date >> amount;
// Updating the takings for that date
daily_takings[date] += amount;
}
// Finding the maximum daily takings
int max_takings = 0;
string max_takings_date;
for (const auto& entry : daily_takings) {
if (entry.second > max_takings) {
max_takings = entry.second;
max_takings_date = entry.first;
}
}
// Printing the result
cout << "Maximum daily takings: " << max_takings << " dinars, ";
cout << "date: " << max_takings_date << endl;
return 0;
}
Code analysis:
This solution is correct and uses the map structure efficiently to keep track of the total daily takings for each date. Here are comments step by step:- The map daily_takings:
- It uses map<string, int>, where the string is the date and the int is the total amount of takings for that date. The map makes it efficient to find and update the takings for each date.
- Entering fiscal receipts:
- The program first asks the user to enter the number of fiscal receipts (n). Then, for each receipt, it asks for the date and the amount, and adds that amount to the existing takings for that date in the map daily_takings. If the date is already in the map, the new amount is added to the existing amount.
- Finding the maximum daily takings:
- After all receipts are entered, the program goes through all pairs in the map daily_takings and finds the date with the maximum takings.
- max_takings is first set to 0, and every time a larger amount than the current maximum is found, max_takings and max_takings_date are updated.
- Printing the result:
- Finally, the program prints the maximum daily takings and the date on which they were earned.
- Efficiency: Using a map makes updating and finding data fast, which is useful when there are many different dates.
- Code clarity: The code is simple and easy to understand. The map is used in an intuitive way to track the takings by date.
- Adding checks that the entered date and amount are valid.
- If a very large number of receipts is expected, space optimization could be considered, although the current implementation is efficient enough for typical cases.
