joannakr

Untitled

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