Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. // Cody Scott
  2. // Computer Programming II  (COMP-1431-WA - 63676)
  3. // Assignment 2
  4. // Question 2
  5. // This program accepts user input containing a date and a reminder
  6. // once the user is finished entering dates with reminders it
  7. // displays the reminders sorted by date
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main () {
  14.     // used to compare dates and place the reminders in teh correct array location
  15.     int day1, day2;
  16.     int month1, month2;
  17.     int time1, time2;
  18.    
  19.     char reminders[100][100]; // list of all reminders
  20.     char reminder[100];
  21.  
  22.     //initlizes to all reminders to 32, will not add a date greater than 31 into the reminders list
  23.     //also accounts for the more than 2 digit output
  24.     for (int i = 0; i < 100; i++)
  25.         strcpy(reminders[i], "13/32/2500");
  26.  
  27.     for(;;){
  28.         printf("Enter 0 to view all reminders and exit.\nEnter month/day/time and reminder.\nExample Input: 12/1/1800 Party\n");
  29.  
  30.         fgets(reminder, 101, stdin);
  31.         //used to get the date from the string entered by the user
  32.         sscanf(reminder, "%d/%d/%d", &month1, &day1, &time1);
  33.        
  34.         //if they want to display and exit
  35.         if (reminder[0] == '0')
  36.             break;
  37.  
  38.         //goes through all elements
  39.         for (int i=0;i<100;i++){
  40.             //gets date to compare with
  41.             sscanf(reminders[i], "%d/%d/%d", &month2, &day2, &time2);
  42.             //if it is less than the date in that location
  43.             //ie it should be in spot i
  44.             if ((month1<month2) || (month1 == month2 && day1<day2) || (month1 == month2 && day1 == day1 && time1<time2)){
  45.                 //moves all elements from the end back to i
  46.                 for (int h = 99; h > i-1;h--){
  47.                     strcpy(reminders[h],reminders[h-1]); //shift them all down one element
  48.                 }//keep repeating and move each element down one more location
  49.                 //place the new reiminder in the correct location
  50.                 strcpy(reminders[i], reminder);
  51.                 break;
  52.             }
  53.         }
  54.  
  55.        
  56.     }
  57.  
  58.     //Output
  59.    
  60.     char *ch; //used to fomat teh current reminder
  61.     for (int i = 0; i < 100;i++){
  62.         sscanf(reminders[i], "%d/%d/%d", &month1, &day1, &time1);
  63.         //if it is not 32 which it was initialized to be
  64.         if (month1 != 13){
  65.             //outputs the date right justified
  66.             printf("%2d/%2d/%d", month1, day1, time1);
  67.             //then the date is taken out of the string since it was already printed
  68.             ch = strtok(reminders[i], " ");
  69.             ch = strtok (NULL, "\0");
  70.             // outputs the message of the reminder
  71.             printf(" - %s", ch);
  72.             //adds the message back to the date concatenating reminders[i] back together
  73.             strcat(reminders[i], ch);
  74.         }
  75.     }
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement