Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct History {
- char date[11];
- char chat_client[22];
- char protocol[20];
- char userName[36];
- char messages[103];
- };
- typedef struct History History;
- struct History_Node {
- History history;
- struct History_Node *next;
- };
- typedef struct History_Node History_Node;
- struct User{
- char name[40];
- char messages[100];
- int lenght_messages;
- };
- typedef struct User User ;
- struct Users_Node{
- User user;
- struct Users_Node *next;
- };
- typedef struct Users_Node Users_Node ;
- int get_last(Users_Node * root)
- {
- Users_Node *current = root;
- while(current->next != NULL)
- {
- current = current->next;
- }
- printf("%s\n", current->user.name);
- return 0;
- }
- int load_histories(History_Node **head, Users_Node **root);
- int get_history_record(FILE *fp, History *history);
- void push_history_node(History_Node **head, History *history);
- void push_users_node(Users_Node **root, User *user);
- void menu(History_Node *head, Users_Node *root);
- History_Node *delete_all_mess(History_Node *head);
- void mood(History_Node *head);
- double find_word(char*str, int mood);
- Users_Node *find_user(Users_Node *root, const char *name);
- //void max_mess(History_Node *head);
- int get_users_record(FILE *fp, User *user);
- int load_users(Users_Node *root);
- void print(History_Node* head);
- User extract_userdata(History * history);
- int main()
- {
- History_Node *head ;
- Users_Node *root;
- if(load_histories(&head,&root))
- return 1;
- menu(head,root);
- return 0;
- }
- void menu(History_Node *head, Users_Node *root)
- {
- int choice;
- printf("\nEnter what you want to do : \n\n");
- printf("1. Delete all messages from given user : \n\n");
- printf("2. Mood of user : \n\n");
- printf("3. User with maximum messages : \n\n");
- printf("4. User with maximum characters : \n\n");
- printf("5. To save and Exit \n\n");
- do
- {
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- delete_all_mess(head);
- break;
- case 2:
- mood(head);
- break;
- case 3:
- print(head);
- break;
- case 4:
- get_last(root);
- break;
- case 5:
- system("cls");
- printf("Goodbye\n");
- break;
- default:
- printf("Try again!\n");
- }
- } while (choice != 5);
- }
- int load_histories(History_Node **head, Users_Node **root)
- {
- History history;
- User user;
- FILE *fp;
- if((fp = fopen("test.txt", "r")) == NULL)
- {
- printf("Error in reading!\n");
- return 1;
- }
- while(get_history_record(fp, &history)) // tova se zavurta 7 puti, a trbav 6
- {
- push_history_node(head, &history);
- }
- fclose(fp);
- return 0;
- }
- int get_history_record(FILE *fp, History *history)
- {
- char hour[12];
- if((fscanf(fp, "[%[^ ] %[^]]] [%[^.].%[^]]] %[^:]: %[^\n]\n" , history->date, hour ,history->chat_client,
- history->protocol, history->userName, history->messages)) > 0)
- return 1;
- else
- return 0;
- }
- void push_history_node(History_Node **head, History *history) //za dobavqne na element sled posledniq
- {
- History_Node *new_node;
- new_node = malloc(sizeof (History_Node));
- new_node->history = *history;
- new_node->next = *head;
- *head = new_node;
- }
- void push_users_node(Users_Node **root, User *user) //za dobavqne na element sled posledniq
- {
- Users_Node *new_node;
- new_node = malloc(sizeof (Users_Node));
- new_node->user = *user;
- new_node->next = *root;
- *root = new_node;
- }
- History_Node *delete_all_mess(History_Node *head)
- {
- History_Node *prev = head;
- History_Node *curr = head;
- int br=0;
- char name[30];
- printf("Enter user name :");
- scanf("%29s",name);
- while(curr!=NULL)
- {
- if(!strcmp(name,curr->history.userName))
- {
- if(curr == head)
- {
- head = head->next;
- prev = head;
- br=1;
- }
- else
- prev->next = curr->next;
- free(curr);
- curr = prev ;
- }
- prev = curr;
- if(br==0)
- curr = curr->next;
- br=0;
- }
- printf("All messages from %s are deleted\n",name);
- return head;
- }
- void mood(History_Node *head)
- {
- History_Node *curr =head;
- double good=0.0, bad=0.0, mood = 0.0;
- char name[30];
- printf("Enter user name :");
- scanf("%29s",name);
- while(1)
- {
- if(!strcmp(name,curr->history.userName))
- {
- good+=find_word(curr->history.messages, 1);
- bad+=find_word(curr->history.messages, 2);
- }
- if(!curr->next)
- break;
- curr = curr->next;
- }
- mood = good/bad;
- if(mood>=1.5)
- printf("\nThe user %s is happy.\n",name);
- else if(mood<=0.5)
- printf("\nThe user %s is sad.\n",name);
- else if(mood>0.5&&mood<1.5)
- printf("\nThe user %s is neutral.\n",name);
- else
- printf("\nThis user has not used emoticons\n");
- }
- double find_word(char*str, int mood)
- {
- int i=0;
- double count=0.0;
- for(i=0;str[i]!='\0';i++)
- {
- if(mood==1&&((str[i]==':'&&str[i+1]=='D')||(str[i]==':'&&str[i+1]==')')||(str[i]=='='&&str[i+1]==']')))
- {
- count++;
- }
- if(mood==2&&((str[i]==':'&&str[i+1]=='(')||(str[i]==':'&&str[i+1]=='\''&&str[i+2]=='(')||(str[i]=='='&&str[i+1]=='(')))
- {
- count++;
- }
- }
- return count;
- }
- Users_Node *create_update_user(Users_Node **root, User *user)
- {
- Users_Node* curr_item = *root;
- while(curr_item != NULL)
- {
- if(!strcmp(user, curr_item->user.name))
- {
- return curr_item;
- }
- curr_item=curr_item->next;
- }
- return NULL;
- }
- User extract_userdata(History * history)
- {
- User user;
- user.lenght_messages = (int)strlen(history->messages);
- strcpy(user.name, history->userName);
- return user;
- }
- void print(History_Node* head)
- {
- History_Node* curr_item = head;
- while(curr_item != NULL)
- {
- printf("Item has value %s\n",curr_item->history.messages);
- curr_item=curr_item->next;
- }
- }
- /*void max_mess(Node *head)
- {
- Names *users = (Names *) malloc (sizeof(Names));
- Node *curr = head;
- char * new_user = (char*) calloc (16,1);
- int i=0, broi=0, max=0;
- while(1)
- {
- i=0;
- while(1)
- {
- i++;
- if(!strcmp(users[i-1].name,curr->history.userName))
- {
- users[i-1].count++;
- break;
- }
- else if(i>broi)
- {
- unsigned int j = (unsigned int)i;
- users = (Names*) realloc (users,j*sizeof(Names));
- strcpy(users[i-1].name,curr->history.userName);
- users[i-1].count=1;
- broi++;
- break;
- }
- }
- if(!curr->next)
- break;
- curr = curr->next;
- }
- for(i=0;i<broi;i++)
- {
- if(users[i].count>max)
- {
- strcpy(new_user,users[i].name);
- max = users[i].count;
- }
- }
- free(users);
- printf("\nThe user with the most messages is %s(%d)\n",new_user,max);
- free(new_user);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement