Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. /********************Text Justification**********************/
  2. /*****************YOU MUST EDIT and SUBMIT THIS FILE*********/
  3. #include "justify.h"
  4. #include <cmath>
  5. unsigned int justify(unsigned int width, string in, vector<string> & out) {
  6. unsigned int col = 0;
  7. // first make sure that column width is valid
  8. if ((width > MAX_COL_WIDTH) || (width < MIN_COL_WIDTH))
  9. return ERROR_ILLEGAL_WIDTH;
  10. // make sure that the input is not empty string
  11. if (in.length() == 0)
  12. return ERROR_NO_CHARS;
  13. // write your code here. justify.cpp is currently not doing anything
  14. string f;
  15. int amountof_lines = ceil(in.length() / width);
  16.  
  17. f.append(in.substr(0, in.length()));
  18. for (int x = 1; x <= amountof_lines; x++){
  19. if (f[x*width] == ' '){
  20. f.erase(x*width, 1);
  21. }
  22. else if (f[x*(width)-1] == ','){
  23. f.erase(x*(width), 1);
  24. }
  25. else if (f[x*(width)] == ','){
  26. f.erase(x*(width)+1, 1);
  27. }
  28. else if (f[x*(width)+1] == ','){
  29. f.erase(x*(width)+2, 1);
  30. }
  31.  
  32. else if (f[x*(width)-1] == '.'){
  33. f.erase(x*(width), 1);
  34. }
  35. else if (f[x*(width)] == '.'){
  36. f.erase(x*(width)+1, 1);
  37. }
  38. else if (f[x*(width)+1] == '.'){
  39. f.erase(x*(width)+2, 1);
  40. }
  41. else if (f[x*(width)-1] != ' ' && f[x*(width)] != ' ' && f[x*(width)-2] != ' '){
  42. f.insert(x*(width)-1, "-");
  43. }
  44. else if (f[x*(width)] != ' ' && f[x*(width)-2] != ' ' && f[x*(width)-1] != ' '){
  45. f.insert(x*(width)-1, " ");
  46. }
  47. }
  48. for (int L = 0; L< f.length(); L += width){
  49. if ((((f[(L + width)]) == ',')) && ((L + width + 1) < (f.length()))){
  50. out.push_back(f.substr(L, width + 1));
  51. }
  52. else if (f[L] == ','){
  53. out.push_back(f.substr(L + 1, width - 1));
  54. }
  55. else if ((f[L + width] == '.') && (L + width + 1 < f.length())){
  56. out.push_back(f.substr(L, width + 1));
  57. }
  58. else if (f[L] == '.'){
  59. out.push_back(f.substr(L + 1, width - 1));
  60. }
  61. else {
  62. out.push_back(f.substr(L, width));
  63. }
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. return ERROR_NONE;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement