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 Users{
- char name[36];
- char messages[100];
- unsigned long long lenght_messages;
- };
- typedef struct Users Users ;
- struct Users_Node{
- Users users;
- 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->users.name);
- return 0;
- }
- History_Node * start_history_node()
- {
- History_Node *rev;
- rev=(History_Node*)malloc(sizeof(History_Node));
- rev->next = NULL;
- return rev;
- }
- Users_Node * start_users_node()
- {
- Users_Node *rev;
- rev=(Users_Node*)malloc(sizeof(Users_Node));
- rev->next = NULL;
- return rev;
- }
- 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, Users *users);
- 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, Users *users);
- int load_users(Users_Node *root);
- void print(History_Node* head);
- int main()
- {
- History_Node *head = start_history_node();
- Users_Node *root = start_users_node();
- 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;
- Users users;
- FILE *fp;
- if((fp = fopen("test.txt", "r")) == NULL)
- {
- printf("Error in reading!\n");
- return 1;
- }
- while(get_history_record(fp, &history))
- {
- push_history_node(head, &history);
- if(!find_user(root, head->history.userName ))
- push_users_node(root,&users);
- }
- 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 *current = head;
- while(current->next != NULL)
- {
- current = current->next;
- }
- current->next = malloc(sizeof(History_Node));
- current->next->history = *history;
- current->next->next = NULL;
- }
- void push_users_node(Users_Node *root, Users *users) //za dobavqne na element sled posledniq
- {
- Users_Node *current = root;
- while(current->next != NULL)
- {
- current = current->next;
- }
- current->next = malloc(sizeof(Users_Node));
- current->next->users = *users;
- current->next->next = NULL;
- }
- 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 *find_user(Users_Node *root, const char *name)
- {
- Users_Node* curr_item = root;
- while(curr_item != NULL)
- {
- if(!strcmp(name, curr_item->users.name))
- {
- return curr_item;
- }
- curr_item=curr_item->next;
- }
- return NULL;
- }
- void print(History_Node* head)
- {
- History_Node* curr_item = head;
- while(curr_item != NULL)
- {
- printf("Item has value %s\n",curr_item->history.userName);
- 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