Advertisement
joannakr

Untitled

May 20th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct History {
  6. char date[11];
  7. char chat_client[22];
  8. char protocol[20];
  9. char userName[36];
  10. char messages[103];
  11. };
  12.  
  13. typedef struct History History;
  14.  
  15. struct History_Node {
  16. History history;
  17. struct History_Node *next;
  18. };
  19. typedef struct History_Node History_Node;
  20.  
  21. struct User{
  22. char name[40];
  23. char messages[100];
  24. int lenght_messages;
  25. };
  26. typedef struct User User ;
  27.  
  28. struct Users_Node{
  29. User user;
  30. struct Users_Node *next;
  31. };
  32. typedef struct Users_Node Users_Node ;
  33.  
  34. int get_last(Users_Node * root)
  35. {
  36. Users_Node *current = root;
  37. while(current->next != NULL)
  38. {
  39. current = current->next;
  40. }
  41. printf("%s\n", current->user.name);
  42. return 0;
  43. }
  44.  
  45. int load_histories(History_Node **head, Users_Node **root);
  46. int get_history_record(FILE *fp, History *history);
  47. void push_history_node(History_Node **head, History *history);
  48. void push_users_node(Users_Node **root, User *user);
  49. void menu(History_Node *head, Users_Node *root);
  50. History_Node *delete_all_mess(History_Node *head);
  51. void mood(History_Node *head);
  52. double find_word(char*str, int mood);
  53. Users_Node *find_user(Users_Node *root, const char *name);
  54. //void max_mess(History_Node *head);
  55. int get_users_record(FILE *fp, User *user);
  56. int load_users(Users_Node *root);
  57. void print(History_Node* head);
  58. User extract_userdata(History * history);
  59.  
  60. int main()
  61. {
  62. History_Node *head ;
  63. Users_Node *root;
  64. if(load_histories(&head,&root))
  65. return 1;
  66. menu(head,root);
  67.  
  68. return 0;
  69. }
  70.  
  71. void menu(History_Node *head, Users_Node *root)
  72. {
  73. int choice;
  74. printf("\nEnter what you want to do : \n\n");
  75. printf("1. Delete all messages from given user : \n\n");
  76. printf("2. Mood of user : \n\n");
  77. printf("3. User with maximum messages : \n\n");
  78. printf("4. User with maximum characters : \n\n");
  79. printf("5. To save and Exit \n\n");
  80. do
  81. {
  82. scanf("%d", &choice);
  83. switch (choice)
  84. {
  85. case 1:
  86. delete_all_mess(head);
  87. break;
  88. case 2:
  89. mood(head);
  90. break;
  91. case 3:
  92. print(head);
  93. break;
  94. case 4:
  95. get_last(root);
  96. break;
  97. case 5:
  98. system("cls");
  99. printf("Goodbye\n");
  100. break;
  101. default:
  102. printf("Try again!\n");
  103. }
  104. } while (choice != 5);
  105. }
  106.  
  107. int load_histories(History_Node **head, Users_Node **root)
  108. {
  109. History history;
  110. User user;
  111. FILE *fp;
  112. if((fp = fopen("test.txt", "r")) == NULL)
  113. {
  114. printf("Error in reading!\n");
  115. return 1;
  116. }
  117. while(get_history_record(fp, &history)) // tova se zavurta 7 puti, a trbav 6
  118. {
  119. push_history_node(head, &history);
  120.  
  121. }
  122. fclose(fp);
  123. return 0;
  124. }
  125.  
  126. int get_history_record(FILE *fp, History *history)
  127. {
  128. char hour[12];
  129. if((fscanf(fp, "[%[^ ] %[^]]] [%[^.].%[^]]] %[^:]: %[^\n]\n" , history->date, hour ,history->chat_client,
  130. history->protocol, history->userName, history->messages)) > 0)
  131. return 1;
  132. else
  133. return 0;
  134. }
  135.  
  136. void push_history_node(History_Node **head, History *history) //za dobavqne na element sled posledniq
  137. {
  138. History_Node *new_node;
  139. new_node = malloc(sizeof (History_Node));
  140. new_node->history = *history;
  141. new_node->next = *head;
  142. *head = new_node;
  143. }
  144.  
  145. void push_users_node(Users_Node **root, User *user) //za dobavqne na element sled posledniq
  146. {
  147. Users_Node *new_node;
  148. new_node = malloc(sizeof (Users_Node));
  149. new_node->user = *user;
  150. new_node->next = *root;
  151. *root = new_node;
  152. }
  153.  
  154. History_Node *delete_all_mess(History_Node *head)
  155. {
  156. History_Node *prev = head;
  157. History_Node *curr = head;
  158. int br=0;
  159. char name[30];
  160. printf("Enter user name :");
  161. scanf("%29s",name);
  162. while(curr!=NULL)
  163. {
  164. if(!strcmp(name,curr->history.userName))
  165. {
  166. if(curr == head)
  167. {
  168. head = head->next;
  169. prev = head;
  170. br=1;
  171. }
  172. else
  173. prev->next = curr->next;
  174. free(curr);
  175. curr = prev ;
  176. }
  177. prev = curr;
  178. if(br==0)
  179. curr = curr->next;
  180. br=0;
  181. }
  182. printf("All messages from %s are deleted\n",name);
  183. return head;
  184. }
  185.  
  186. void mood(History_Node *head)
  187. {
  188. History_Node *curr =head;
  189. double good=0.0, bad=0.0, mood = 0.0;
  190. char name[30];
  191. printf("Enter user name :");
  192. scanf("%29s",name);
  193. while(1)
  194. {
  195. if(!strcmp(name,curr->history.userName))
  196. {
  197. good+=find_word(curr->history.messages, 1);
  198. bad+=find_word(curr->history.messages, 2);
  199. }
  200. if(!curr->next)
  201. break;
  202. curr = curr->next;
  203. }
  204. mood = good/bad;
  205. if(mood>=1.5)
  206. printf("\nThe user %s is happy.\n",name);
  207. else if(mood<=0.5)
  208. printf("\nThe user %s is sad.\n",name);
  209. else if(mood>0.5&&mood<1.5)
  210. printf("\nThe user %s is neutral.\n",name);
  211. else
  212. printf("\nThis user has not used emoticons\n");
  213. }
  214.  
  215. double find_word(char*str, int mood)
  216. {
  217. int i=0;
  218. double count=0.0;
  219. for(i=0;str[i]!='\0';i++)
  220. {
  221. if(mood==1&&((str[i]==':'&&str[i+1]=='D')||(str[i]==':'&&str[i+1]==')')||(str[i]=='='&&str[i+1]==']')))
  222. {
  223. count++;
  224. }
  225. if(mood==2&&((str[i]==':'&&str[i+1]=='(')||(str[i]==':'&&str[i+1]=='\''&&str[i+2]=='(')||(str[i]=='='&&str[i+1]=='(')))
  226. {
  227. count++;
  228. }
  229. }
  230. return count;
  231. }
  232.  
  233. Users_Node *create_update_user(Users_Node **root, User *user)
  234. {
  235. Users_Node* curr_item = *root;
  236. while(curr_item != NULL)
  237. {
  238. if(!strcmp(user, curr_item->user.name))
  239. {
  240. return curr_item;
  241. }
  242. curr_item=curr_item->next;
  243. }
  244. return NULL;
  245. }
  246.  
  247. User extract_userdata(History * history)
  248. {
  249. User user;
  250. user.lenght_messages = (int)strlen(history->messages);
  251. strcpy(user.name, history->userName);
  252. return user;
  253. }
  254. void print(History_Node* head)
  255. {
  256. History_Node* curr_item = head;
  257. while(curr_item != NULL)
  258. {
  259. printf("Item has value %s\n",curr_item->history.messages);
  260. curr_item=curr_item->next;
  261. }
  262. }
  263. /*void max_mess(Node *head)
  264. {
  265. Names *users = (Names *) malloc (sizeof(Names));
  266. Node *curr = head;
  267. char * new_user = (char*) calloc (16,1);
  268. int i=0, broi=0, max=0;
  269.  
  270. while(1)
  271. {
  272. i=0;
  273. while(1)
  274. {
  275. i++;
  276. if(!strcmp(users[i-1].name,curr->history.userName))
  277. {
  278. users[i-1].count++;
  279. break;
  280. }
  281. else if(i>broi)
  282. {
  283. unsigned int j = (unsigned int)i;
  284. users = (Names*) realloc (users,j*sizeof(Names));
  285. strcpy(users[i-1].name,curr->history.userName);
  286. users[i-1].count=1;
  287. broi++;
  288. break;
  289. }
  290. }
  291. if(!curr->next)
  292. break;
  293. curr = curr->next;
  294. }
  295. for(i=0;i<broi;i++)
  296. {
  297. if(users[i].count>max)
  298. {
  299. strcpy(new_user,users[i].name);
  300. max = users[i].count;
  301. }
  302. }
  303. free(users);
  304. printf("\nThe user with the most messages is %s(%d)\n",new_user,max);
  305. free(new_user);
  306. }
  307. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement