Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cody Scott
- // Computer Programming II (COMP-1431-WA - 63676)
- // Assignment 2
- // Question 2
- // This program accepts user input containing a date and a reminder
- // once the user is finished entering dates with reminders it
- // displays the reminders sorted by date
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main () {
- // used to compare dates and place the reminders in teh correct array location
- int day1, day2;
- int month1, month2;
- int time1, time2;
- char reminders[100][100]; // list of all reminders
- char reminder[100];
- //initlizes to all reminders to 32, will not add a date greater than 31 into the reminders list
- //also accounts for the more than 2 digit output
- for (int i = 0; i < 100; i++)
- strcpy(reminders[i], "13/32/2500");
- for(;;){
- printf("Enter 0 to view all reminders and exit.\nEnter month/day/time and reminder.\nExample Input: 12/1/1800 Party\n");
- fgets(reminder, 101, stdin);
- //used to get the date from the string entered by the user
- sscanf(reminder, "%d/%d/%d", &month1, &day1, &time1);
- //if they want to display and exit
- if (reminder[0] == '0')
- break;
- //goes through all elements
- for (int i=0;i<100;i++){
- //gets date to compare with
- sscanf(reminders[i], "%d/%d/%d", &month2, &day2, &time2);
- //if it is less than the date in that location
- //ie it should be in spot i
- if ((month1<month2) || (month1 == month2 && day1<day2) || (month1 == month2 && day1 == day1 && time1<time2)){
- //moves all elements from the end back to i
- for (int h = 99; h > i-1;h--){
- strcpy(reminders[h],reminders[h-1]); //shift them all down one element
- }//keep repeating and move each element down one more location
- //place the new reiminder in the correct location
- strcpy(reminders[i], reminder);
- break;
- }
- }
- }
- //Output
- char *ch; //used to fomat teh current reminder
- for (int i = 0; i < 100;i++){
- sscanf(reminders[i], "%d/%d/%d", &month1, &day1, &time1);
- //if it is not 32 which it was initialized to be
- if (month1 != 13){
- //outputs the date right justified
- printf("%2d/%2d/%d", month1, day1, time1);
- //then the date is taken out of the string since it was already printed
- ch = strtok(reminders[i], " ");
- ch = strtok (NULL, "\0");
- // outputs the message of the reminder
- printf(" - %s", ch);
- //adds the message back to the date concatenating reminders[i] back together
- strcat(reminders[i], ch);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement