Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 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.         if (day1<0 || day1>31)
  34.             printf("\nERROR - invalid day, please enter a day from 0 to 31\n\n");
  35.  
  36.         //if they want to display and exit
  37.         if (reminder[0] == '0')
  38.             break;
  39.  
  40.         //goes through all elements
  41.         for (int i=0;i<100;i++){
  42.             //gets date to compare with
  43.             sscanf(reminders[i], "%d/%d/%d", &month2, &day2, &time2);
  44.             //if it is less than the date in that location
  45.             //ie it should be in spot i
  46.             if ((day1 > 0) ||(month1<month2) || (month1 == month2 && day1<day2) || (month1 == month2 && day1 == day1 && time1<time2)){
  47.                 //moves all elements from the end back to i
  48.                 for (int h = 99; h > i-1;h--){
  49.                     strcpy(reminders[h],reminders[h-1]); //shift them all down one element
  50.                 }//keep repeating and move each element down one more location
  51.                 //place the new reiminder in the correct location
  52.                 strcpy(reminders[i], reminder);
  53.                 break;
  54.             }
  55.         }
  56.  
  57.        
  58.     }
  59.  
  60.     //Output
  61.    
  62.     char *ch; //used to fomat teh current reminder
  63.     for (int i = 0; i < 100;i++){
  64.         sscanf(reminders[i], "%d/%d/%d", &month1, &day1, &time1);
  65.         //if it is not 32 which it was initialized to be
  66.         if (month1 != 13){
  67.             //outputs the date right justified
  68.             printf("%2d%2d%d", month1, day1, time1);
  69.             //then the date is taken out of the string since it was already printed
  70.             ch = strtok(reminders[i], " ");
  71.             ch = strtok (NULL, "\0");
  72.             // outputs the message of the reminder
  73.             printf(" - %s", ch);
  74.             //adds the message back to the date concatenating reminders[i] back together
  75.             //strcat(reminders[i], ch);
  76.         }
  77.     }
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement