I need to implement a simple version of schedule for monthly tasks. For example payment of electricity bills, subscription fees for communications, etc. I want to implement a set of the following operations:
ADD(i,s) - assign a case with name s to day i of the current month.
DUMP(i) - Display all tasks scheduled for day i of the current month.
NEXT - Go to the to-do list for the new month. When this command is executed, instead of the current (old) to-do list for the current month, a (new) to-do list for the next month is created and becomes active: all tasks from the old to-do list are copied to the new list. After executing this command, the new to-do list and the next month become the current month, and work with the old to-do list is stopped. When moving to a new month, you need to pay attention to the different number of days in months:
if the next month has more days than the current one, the "additional" days must be left empty (not containing cases);
if the next month has fewer days than the current one, cases from all "extra" days must be moved to the last day of the next month.
Here is my code:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
//q - number of operations.
//day - on which day to plan operation.
//to_do - what exactly to do on some day.
//operation - which kind of operation to perform.
const vector<int>day_mon = {31,28,31,30,31,30,31,31,30,31,30,31};
//m_ind - month index
int c_ind = 0;
int n_ind = 0;
int q,day;
int days_diff;
string operation;
string to_do;
//vector of the current month and next month
vector<vector<string> > current_month(31);
vector<vector<string> > next_month;
cin >> q;
//for q operations:
for(int i = 0;i< q;i ){
cin >> operation;
if(operation == "NEXT"){
//change next_month index
if(c_ind == 11){
n_ind = 0;
} else{
n_ind =1;
}
next_month.resize(day_mon[n_ind]);
copy(current_month.begin(),current_month.begin() day_mon[n_ind],next_month.begin());
days_diff = day_mon[c_ind] - day_mon[n_ind];
//if next month has less days as current month, write days into the last day
//of the next months
if(days_diff > 0){
for(int i = 0; i < days_diff;i ){
next_month[day_mon[n_ind]-1].insert(end(next_month[day_mon[n_ind]-1]), begin(current_month[day_mon[n_ind] i]), end(current_month[day_mon[n_ind] i]));
}
}
current_month.clear();
c_ind =1;
current_month.resize(next_month.size());
current_month = next_month;
next_month.clear();
} else if(operation == "ADD"){
cin >> day >> to_do;
current_month[day-1].push_back(to_do);
} else if(operation == "DUMP"){
cin >> day;
cout << current_month[day-1].size() << ' ';
for(int i = 0; i < current_month[day-1].size(); i ){
cout << current_month[day-1][i] << ' ';
}
cout << endl;
}
}
return 0;
}
I have tested program by following inputs:
12
ADD 5 Salary
ADD 31 Walk
ADD 30 WalkPreparations
NEXT
DUMP 5
DUMP 28
NEXT
DUMP 31
DUMP 30
DUMP 28
ADD 28 Payment
DUMP 28
and on my compiler I got correct result:
1 Salary
2 WalkPreparations Walk
0
0
2 WalkPreparations Walk
3 WalkPreparations Walk Payment
but on other compiler I hot incorrect results:
1 Salary
2 WalkPreparations Walk
1 Salary
1
2 WalkPreparations Walk
3 WalkPreparations Walk Payment
Can you help me to find my mistake?
CodePudding user response:
Look here
copy(current_month.begin(), current_month.begin() day_mon[n_ind], next_month.begin());
What happens when current_month has 28 days and the next month has 31 days - ie day_mon[n_ind] = 31
CodePudding user response:
As mentioned, this line:
copy(current_month.begin(), current_month.begin() day_mon[n_ind], next_month.begin());
assumes that the current_month
has at least day_mon[n_ind]
days, when that is not the case when, for example, current_month
has 28 days (February), but the next month (March) has 31 days (the day_mon[n_ind]
will be 31).
One way to fix this is to limit the copy
to the minimum of the days in the month of the current month and next month:
size_t numElementsToCopy = std::min(current_month.size(), static_cast<size_t>(day_mon[n_ind]));
std::copy_n(current_month.begin(), numElementsToCopy, next_month.begin());
Note: Usage of std::copy_n
is a lot less wordy than using std::copy
in this instance.