package wordwrap; import java.io.*; import java.util.*; import java.math.*; /** * * @author ahangari */ public class WordWrap { String strtmp=null; String delimiter =" "; String[] words=new String[100]; int linewidth=0; int i,j,k; int stage; int sum=0; int space=0; int totalspace=0; int min=0; int cost=-1;//baraye inke shartemun ine ke cost>=0 bashe int[][] fcost=new int[100][100];//an array for storing final cost int[] check=new int[100]; int p=0; int finalsum1=0; int[] finalsum2=new int[1000]; //********************************************* public void Init(){ for(i=0;i<fcost.length;i++){ for(j=0;j<fcost.length;j++){ fcost[i][j]=-1; } } } public void inputfile() throws FileNotFoundException { try{ Scanner wr=new Scanner(new FileInputStream("wordwrap.txt")); strtmp = wr.nextLine(); words = strtmp.split(delimiter); wr.close(); } catch (Exception e){ System.out.println(e+"read exception"); } } //********************************************* public void entries(){ Scanner scan=new Scanner(System.in); System.out.println("Please Enter the line width :"); linewidth=scan.nextInt(); //for(i=0;i<words.length;i++){ //if(linewidth<words[i].length()) // System.out.printf("The text can't be wraped into %d column.",linewidth); //break; // } } //********************************************* public void cost(){ for(i=0 ;i<words.length; i++){ for(j=i;j<words.length;j++){ space=j-i; sum=words[i].length(); if(i==j) { totalspace=linewidth; cost=(totalspace-sum); } else{ totalspace=linewidth-space;//chon fazaye beyne kalame ha 1space cost=0; if(totalspace>0){ //ast pas miad tedade space ha ro az kole linewidth kam mikone sum+=words[j].length(); cost=(totalspace-sum); } } if(cost>=0) { fcost[i][j]=(cost)*(cost); System.out.printf("The cost of word %d to %d is :%d \n ",i,j,fcost[i][j]); } cost=0; } sum=0;// baraye inke maghadire ghabli ruye stage baadi asar nazare } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)