Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- typedef struct node {
- char* value;
- node* next;
- } node;
- void print_list(node* head) {
- node* current = head;
- while (current) {
- printf("%s\n", current->value);
- current = current->next;
- }
- printf("\n");
- return;
- }
- void swap(char* m, char* n) {
- char t = *m;
- *m = *n;
- *n = t;
- return;
- }
- node* sort(node* head) {
- struct node* a = NULL;
- struct node* b = NULL;
- struct node* c = NULL;
- struct node* e = NULL;
- struct node* tmp = NULL;
- while (e != head->next)
- {
- c = a = head;
- b = a->next;
- while (a != e)
- {
- if (a->value[0] > b->value[0])
- {
- if (a == head)
- {
- tmp = b->next;
- b->next = a;
- a->next = tmp;
- head = b;
- c = b;
- }
- else
- {
- tmp = b->next;
- b->next = a;
- a->next = tmp;
- c->next = b;
- c = b;
- }
- }
- else
- {
- c = a;
- a = a->next;
- }
- b = a->next;
- if (b == e) e = a;
- }
- }
- return head;
- }
- /*node* add_in_head(node* head, char* value)
- {
- node* temp = (node*)malloc(sizeof(node));
- temp->value = value;
- temp->next = head;
- //temp = sort(temp);
- return temp;
- }*/
- node* add_in_head(node** q, char* value)
- {
- node* tmp = NULL;
- tmp = *q;
- if (*q == NULL)
- {
- *q = (node*)malloc(sizeof(struct node));
- tmp = *q;
- }
- else
- {
- while (tmp->next != NULL)
- tmp = tmp->next;
- tmp->next = (node*)malloc(sizeof(struct node));
- tmp = tmp->next;
- }
- tmp->value = value;
- tmp->next = NULL;
- return tmp;
- }
- int ch_str(char* str, char ch) {
- int i = 0;
- while (str[i]) {
- if (str[i] == ch) {
- return i;
- }
- i++;
- }
- return -1;
- }
- void longest(char* str, int* A) {
- char sep[] = "., ()!?\n\t";
- int i = 0, max_len = 0, curr = 0, in_word = 1, j = 0;
- int pos[50];
- while (str[i]) {
- if (ch_str(sep, str[i]) != -1) {
- if (in_word) {
- if (curr > max_len) {
- pos[j] = i;
- max_len = curr;
- }
- else
- {
- if (curr == max_len) {
- j++;
- pos[j] = i;
- }
- }
- curr = 0;
- in_word = 0;
- }
- }
- else {
- curr++;
- in_word = 1;
- }
- i++;
- }
- if (in_word) {
- if (curr > max_len) {
- max_len = curr;
- pos[j] = i;
- }
- else
- {
- if (curr == max_len) {
- j++;
- pos[j] = i;
- }
- }
- }
- A[0] = (int)max_len;
- for (i = 0; i <= j; i++) {
- A[i + 1] = pos[i];
- //printf("%d\n",pos[i]);
- }
- return;
- }
- char* reverse_sub(char* str, int start, int end) {
- while (start < end) {
- swap(str + start++, str + end--);
- }
- return str;
- }
- int main()
- {
- char str[255];
- int k = 0, h = 0;
- FILE* fp = fopen("c:\\vvv\\test.txt", "r");
- if (fp == NULL) {
- printf("Error!\n");
- exit(1);
- }
- int start = 0;
- int end = 0;
- int A[50];
- node* head = NULL;
- while (fgets(str, 51, fp))
- {
- memset(A, -1, sizeof(A));
- longest(str, A);
- for (k = 1; k < 100; k++)
- {
- if (A[k] == -1) break;
- start = (int)A[k] - (int)A[0];
- end = (int)A[k] - 1;
- reverse_sub(str, start, end);
- }
- //printf(str);
- head = add_in_head(&head, str);
- print_list(head);
- head = sort(head);
- }
- //head = sort(head);
- //print_list(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement