Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************Text Justification**********************/
- /*****************YOU MUST EDIT and SUBMIT THIS FILE*********/
- #include "justify.h"
- #include <cmath>
- unsigned int justify(unsigned int width, string in, vector<string> & out) {
- unsigned int col = 0;
- // first make sure that column width is valid
- if ((width > MAX_COL_WIDTH) || (width < MIN_COL_WIDTH))
- return ERROR_ILLEGAL_WIDTH;
- // make sure that the input is not empty string
- if (in.length() == 0)
- return ERROR_NO_CHARS;
- // write your code here. justify.cpp is currently not doing anything
- string f;
- int amountof_lines = ceil(in.length() / width);
- f.append(in.substr(0, in.length()));
- for (int x = 1; x <= amountof_lines; x++){
- if (f[x*width] == ' '){
- f.erase(x*width, 1);
- }
- else if (f[x*(width)-1] == ','){
- f.erase(x*(width), 1);
- }
- else if (f[x*(width)] == ','){
- f.erase(x*(width)+1, 1);
- }
- else if (f[x*(width)+1] == ','){
- f.erase(x*(width)+2, 1);
- }
- else if (f[x*(width)-1] == '.'){
- f.erase(x*(width), 1);
- }
- else if (f[x*(width)] == '.'){
- f.erase(x*(width)+1, 1);
- }
- else if (f[x*(width)+1] == '.'){
- f.erase(x*(width)+2, 1);
- }
- else if (f[x*(width)-1] != ' ' && f[x*(width)] != ' ' && f[x*(width)-2] != ' '){
- f.insert(x*(width)-1, "-");
- }
- else if (f[x*(width)] != ' ' && f[x*(width)-2] != ' ' && f[x*(width)-1] != ' '){
- f.insert(x*(width)-1, " ");
- }
- }
- for (int L = 0; L< f.length(); L += width){
- if ((((f[(L + width)]) == ',')) && ((L + width + 1) < (f.length()))){
- out.push_back(f.substr(L, width + 1));
- }
- else if (f[L] == ','){
- out.push_back(f.substr(L + 1, width - 1));
- }
- else if ((f[L + width] == '.') && (L + width + 1 < f.length())){
- out.push_back(f.substr(L, width + 1));
- }
- else if (f[L] == '.'){
- out.push_back(f.substr(L + 1, width - 1));
- }
- else {
- out.push_back(f.substr(L, width));
- }
- }
- return ERROR_NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement