Google Answers Logo
View Question
 
Q: algorithm problems: paragraph formatting problem using dynamic programming ( No Answer,   1 Comment )
Question  
Subject: algorithm problems: paragraph formatting problem using dynamic programming
Category: Computers > Algorithms
Asked by: levymoshe-ga
List Price: $25.00
Posted: 22 Mar 2006 07:34 PST
Expires: 02 Apr 2006 13:39 PDT
Question ID: 710536
I need to Write a program to solve the paragraph formatting problem
using dynamic programming. The input will come from a text file that
contains one paragraph, and the output will go to standard out. The
user should enter the input file name and W, the maximum number of
characters allowed per line. These can either be specified on the
command line or entered upon prompting.

The cost of a line is the square of (W - number of characters on the
line), and the cost of the paragraph is the sum of the cost of each
line except the last.

The W that the user gives will not accomodate a space after the last
word on a line. So in your output, there should be one space after
each word except the last word on each line. The input file might have
more than one white space character (space, tab, or newline) after
some words, but your output should have only one space after each
word.

Clarification of Question by levymoshe-ga on 22 Mar 2006 07:39 PST
EVERYTHING IN C++
Answer  
There is no answer at this time.

Comments  
Subject: Re: algorithm problems: paragraph formatting problem using dynamic programming
From: ashish_o4u-ga on 28 Mar 2006 20:26 PST
 
try the code given below, it may help you

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

#include<process.h>

void main()

{

clrscr();

char *path; //Filename or the path.

int w; //Max. number of characters allowed in a line.

char ch,word[50];

unsigned long int cost=0,line_cost;

int length_diff;

cout<<"\n NOTE:: Enter only the filename if in current dir. \

or the filename along with its path if in other location in the format
d:\\abc\\xyz\\asd.txt\

\n\nEnter the file name :: ";

gets(path);

ifstream in(path); //Open the input file.

if(!in)

{

cout<<"\n\n Unable to open file";

getch();

exit(0);

}

cout<<"\n\nEnter the max. no. of char allowed per line :: ";

cin>>w;

cout<<endl<<endl;

int length=0; //Actual number of characters in the line.

int len=0; //Length of each word

int flag=1;

while(in)

{

in.get(ch);

len=0;

while(in&&(ch=='\0'||ch==' '||ch=='\t'||ch=='\n'||ch=='\f'||ch=='\v'))

{

in.get(ch);

}

while(in&&(!(ch=='\0'||ch==' '||ch=='\t'||ch=='\n'||ch=='\f'||ch=='\v')))

{

word[len]=ch;

len++;

in.get(ch);

if(ch==','||ch==';'||ch=='.'||ch==':')

{

word[len]=ch;

len++;

break;

}

if((len+1)>=w)

{

break;

}

}

word[len]='\0';

if(flag==1)

{

cout<<word;

length+=len;

flag=0;

}

else

{

if(w>=(length+len+1))

{

cout<<" "<<word;

length+=len+1;

}

else

{

cout<<endl;

length_diff=w-length; //Difference between char. allowed and char. present.

line_cost=length_diff*length_diff;//Cost of each line is the square of
the difference.

cost+=line_cost; //Calculating total cost.

length=0;

cout<<word;

length+=len;

}

}

}

cout<<"\n\nTotal cost for formating the file entered is :: "<<cost;

in.close();

getch();

}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy