Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct node{
  5. char* string;
  6. struct node* next;
  7. }Node_t;
  8.  
  9. //reading and creating
  10. void readFile(FILE* fp, Node_t* currentPtr);
  11. void printList(Node_t* head);
  12.  
  13. //creating the nodes
  14. //freeing nodes
  15. Node_t* createNode(char* string);
  16. void freeMemory(Node_t* head);
  17. Node_t* insert(Node_t* currentPtr, char* string);
  18. int calculateSpaces(Node_t* head);
  19. void printUpdatedList(Node_t* head, int justify, int spaces, int numWords);
  20. //maybe we can do snazzy stuff if this is done quick enough
  21. void removeNewLines(Node_t* head);
  22.  
  23. int main()
  24. {
  25.  
  26. char fileName[100];
  27. printf("Please enter the file to open (no spaces please): ");
  28. scanf("%s", fileName);
  29. printf("\n");
  30.  
  31. /* generate new file pointer */
  32. FILE* fp = NULL;
  33. if((fp = fopen(fileName, "r")) == NULL){
  34. printf("Error, could not open file\n");
  35. exit(1);
  36. }
  37.  
  38. Node_t* head = NULL;
  39. readFile(fp, head);
  40.  
  41. return 0;
  42. }
  43.  
  44. void readFile(FILE* fp, Node_t* head){
  45.  
  46. /*allocate memory for a new string pointer */
  47. char* word = malloc(100*sizeof(char));
  48. /* fgets to get each line with max buffer size of 100 chars */
  49. while(fgets(word, 100, fp)){
  50. /* tokenize each word */
  51. char* token = strtok(word, " ");
  52. while(token != NULL){
  53. /* insert each token into linked list */
  54. head = insert(head, token);
  55. token = strtok(NULL, " ");
  56. }
  57. }
  58.  
  59. removeNewLines(head);
  60. int spaces = calculateSpaces(head);
  61. // printList(head);
  62. }
  63.  
  64. Node_t* insert(Node_t* head, char* string){
  65. /* allocate memory for new node */
  66. Node_t* temp = malloc(sizeof(Node_t));
  67. /* if new pointer cannot be created */
  68. if(temp == NULL) {
  69. printf("Error could not allocate memory....");
  70. return NULL;
  71. }
  72. /* allocate memory for new data to be added */
  73. temp->string = malloc(100*sizeof(char));
  74. /* if head is empty create new stringcpy */
  75. if(head == NULL){
  76. strcpy(temp->string, string);
  77. temp->next = NULL;
  78. head = temp;
  79. } else {
  80. /* save the head */
  81. Node_t* currPtr = head;
  82. /* move to the last node */
  83. while(currPtr->next != NULL){
  84. currPtr = currPtr->next;
  85. }
  86. strcpy(temp->string, string);
  87. temp->next = NULL;
  88. currPtr->next = temp;
  89. }
  90. /* return starting node */
  91. return head;
  92. }
  93.  
  94. void printList(Node_t* head){
  95. while(head->next != NULL){
  96. printf("%s ", head->string);
  97. head = head->next;
  98. }
  99. }
  100.  
  101. void removeNewLines(Node_t* head){
  102. int ln = 0;
  103. Node_t* currPtr = head;
  104.  
  105. while(currPtr->next != NULL){
  106.  
  107. ln = strlen(currPtr->string) - 1;
  108. if(currPtr->string[ln] == '\n'){
  109. currPtr->string[ln] = '\0';
  110. }
  111.  
  112. currPtr = currPtr->next;
  113. }
  114. }
  115.  
  116. int calculateSpaces(Node_t* head){
  117.  
  118. int justifyAmount;
  119.  
  120. printf("Enter a jusitfy amount: (between 40 and 100) ");
  121. scanf("%d", &justifyAmount);
  122.  
  123. int totalStrLen = 0;
  124. int tempVal = 0;
  125. Node_t* currPtr = head;
  126. Node_t* tempList = NULL;
  127. int spaces = 0;
  128. int words = 0;
  129. int tempJust = 0;
  130.  
  131. while(currPtr->next != NULL){
  132.  
  133. tempVal = totalStrLen;
  134.  
  135. if((tempVal += strlen(currPtr->string)) >= justifyAmount){ //sentence is too big
  136.  
  137. spaces = words - 1;
  138. tempJust = justifyAmount - totalStrLen;
  139.  
  140. printUpdatedList(tempList, tempJust, spaces, words);
  141.  
  142. totalStrLen = 0;
  143. tempJust = 0;
  144. spaces = 0;
  145. words = 0;
  146. free(tempList);
  147. tempVal = 0;
  148. tempList = NULL;
  149.  
  150. }else{
  151.  
  152. tempList = insert(tempList, currPtr->string);
  153. totalStrLen += strlen(currPtr->string);
  154. words++;
  155.  
  156. }
  157.  
  158. currPtr = currPtr->next;
  159. }
  160.  
  161. }
  162.  
  163. void printUpdatedList(Node_t* head, int justify, int spaces, int numWords){
  164.  
  165. int i = 0;
  166. int j = 0;
  167.  
  168. int tempLen = 0;
  169.  
  170. //printf("justify amount: %d\n", justify);
  171.  
  172. int justifyPerSpace;
  173. Node_t* currPtr = head;
  174.  
  175. if(spaces != 0) {
  176. justifyPerSpace = justify / spaces;
  177. } else { }
  178. //do nothing (this is the empty line)
  179.  
  180. while(i < numWords && currPtr != NULL) {
  181.  
  182. tempLen += strlen(currPtr->string);
  183.  
  184. printf("%s", currPtr->string);
  185.  
  186. for(j = 0; j < justifyPerSpace; j++) {
  187. printf(" ");
  188. }
  189. currPtr = currPtr->next;
  190. i++;
  191. }
  192. printf("\n");
  193. }
  194.  
  195. void freeMemory(Node_t* head){
  196. while(head->next != NULL){
  197. Node_t* temp = head;
  198. head = head->next;
  199. free(temp);
  200. }
  201. free(head);
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement