![]() |
|
|
| 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 |
|
| There is no answer at this time. |
|
| 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();
} |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |