Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. /** sms archiver **/
  2. #include <stdio.h>
  3.  
  4. #define MAXMESSAGES 10
  5.  
  6. /** data type declaration **/
  7.  
  8. typedef struct date {
  9.   unsigned year, month, day, hour, min, sec;
  10. } DATE;
  11.  
  12. typedef struct shortmessage {
  13.   char text[160], sender[30];
  14.   DATE date;
  15. } SHORTMESSAGE;
  16.  
  17. SHORTMESSAGE smArchive[MAXMESSAGES] = {};
  18. unsigned smAEntries = 0;
  19.  
  20. unsigned isBefore (DATE t1, DATE t2)
  21. {
  22.   if ((t1.year < t2.year)
  23.     || (t1.year == t2.year && t1.month < t2.month)
  24.     || (t1.year == t2.year && t1.month == t2.month && t1.day < t2.day)
  25.     || (t1.year == t2.year && t1.month == t2.month && t1.day == t2.day
  26.     && t1.hour < t2.hour)
  27.     || (t1.year == t2.year && t1.month == t2.month && t1.day == t2.day
  28.     && t1.hour == t2.hour && t1.min < t2.min)
  29.     || (t1.year == t2.year && t1.month == t2.month && t1.day == t2.day
  30.     && t1.hour == t2.hour && t1.min == t2.min && t1.sec == t2.sec))
  31.     return 1;
  32.   return 0;
  33. }
  34.  
  35. unsigned isEqual (DATE t1, DATE t2)
  36. {
  37.   if (t1.year == t2.year && t1.month == t2.month && t1.day == t2.day
  38.      && t1.hour == t2.hour && t1.min == t2.min && t1.sec == t2.sec)
  39.     return 1;
  40.   return 0;
  41. }
  42.  
  43. void addMessage (SHORTMESSAGE msg)
  44. /** sorted by date **/
  45. {
  46.   if (smAEntries >= 10) {
  47.     printf("There are over 10 messages in the archive, you cannot add more.");
  48.   } else {
  49.     if (smAEntries == 0) {
  50.         smArchive[0]=msg;
  51.     } else {
  52.         int i = smAEntries;
  53.         while (i >= 0 && isBefore(msg.date, smArchive[i-1].date)) {
  54.             smArchive[i] = smArchive[i-1];
  55.             i = i-1;
  56.         }
  57.         smArchive[i] = msg;
  58.     }
  59.     smAEntries = smAEntries+1;
  60.   }
  61. }
  62.  
  63. int findMessage (DATE msgDate)
  64. /** use binary search by date **/
  65. {
  66.     int left=0, right=smAEntries-1;
  67.     while (left<=right){
  68.         int middle=(left+right)/2;
  69.         if(isEqual(msgDate, smArchive[middle].date)) {
  70.            return middle;
  71.         }
  72.         if(isBefore(msgDate, smArchive[middle].date)) {
  73.             right = middle-1;
  74.         } else {
  75.             left = middle+1;
  76.         }
  77.     }
  78.     return -1;
  79. }
  80.  
  81. void printMessage (SHORTMESSAGE msg)
  82. /** don't forget to print the date of the message **/
  83. {
  84.  printf("From: %s \nMessage: %s\nDate: %u:%u:%u, %u.%u.%u\n\n", msg.sender, msg.text, msg.date.hour, msg.date.min, msg.date.sec, msg.date.day, msg.date.month, msg.date.year);
  85. }
  86.  
  87. DATE getDate()
  88. {
  89.     DATE msgDate;
  90.  
  91.     printf("Please enter year: ");
  92.     scanf("%u", &msgDate.year);
  93.     printf("Please enter month: ");
  94.     scanf("%u", &msgDate.month);
  95.     printf("Please enter day: ");
  96.     scanf("%u", &msgDate.day);
  97.     printf("Please enter hour: ");
  98.     scanf("%u", &msgDate.hour);
  99.     printf("Please enter minute: ");
  100.     scanf("%u", &msgDate.min);
  101.     printf("Please enter second: ");
  102.     scanf("%u", &msgDate.sec);
  103.  
  104.     return msgDate;
  105. }
  106.  
  107. SHORTMESSAGE getMessage()
  108. {
  109.     SHORTMESSAGE msg;
  110.     if(smAEntries > 0) {
  111.         fgets(msg.text, 160, stdin);
  112.     }
  113.     printf("\nPlease enter your message: ");
  114.     fgets(msg.text, 160, stdin);
  115.     strtok(msg.text, "\n");
  116.     printf("Please enter your name: ");
  117.     fgets(msg.sender, 30, stdin);
  118.     strtok(msg.sender, "\n");
  119.  
  120.     msg.date = getDate();
  121.  
  122.     return msg;
  123. }
  124.  
  125. int main ()
  126. {
  127.   unsigned i;
  128.   int foundAt;
  129.  
  130.   addMessage(getMessage());
  131.   addMessage(getMessage());    /**ask user for 3 messages!**/
  132.   addMessage(getMessage());
  133.  
  134.   printf("\nList of all messages:\n\n");
  135.  
  136.   for (i=0; i<smAEntries; i++){
  137.     printMessage(smArchive[i]);
  138.   }
  139.  
  140.   printf("\nEnter date to search for!\n\n");
  141.   foundAt = findMessage (getDate());
  142.  
  143.   if (foundAt != -1)
  144.   {
  145.     printf("\n\nHere is your message:\n\n");
  146.     printMessage(smArchive[foundAt]);
  147.   }
  148.   else
  149.   {
  150.     printf("\n\nMessage not found.");
  151.   }
  152.  
  153.   printf("\n\n\nList of all messages:\n\n");
  154.  
  155.   for (i=0; i<smAEntries; i++){
  156.     printMessage(smArchive[i]);
  157.   }
  158.  
  159.   return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement