LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2008, 09:03 PM   #1
fs11
Member
 
Registered: Aug 2006
Posts: 79

Rep: Reputation: 15
data with same number of rows and coloumns C/C++


Hello All,

I have been working on a problem, which requires the data to be aligned.In other words have same number of rows and columns. If this is not the case, the remaining 'spaces' are filled with an '-'.

Forexample:

File contains..
Code:
>hha
fhahahdhaa
>hhb
kskksk
>hhc
jfkdjfdk
>hhd
jjkkj
Then after processing the file contains

file 2:

Code:
>hha
fhahahdhaa
>hhb
kskksk----
>hhc
jfkdjfdk--
>hhd
jjkkj-----

I am able to solve this problem.Now the problem is that the code wont work, if the number of rows are different and I am not sure how to do it.

For example the code wont work if the file contains this:

Code:
>hha
fhahahdhaa
>hhb
kskksk
>hhc
jfkdjfdk
jiojoijo
>hhd
jjkkj
hdjhfdk

Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>




using namespace std;


int main(int argc, char *argv[])
{
    int j,i,a,b,max;
    a=0;
    b=0;
    max=0;
    char s[255];
    char t[255];
    ifstream inFile;
    ifstream inFile1;
    
     
//      cout << "Running program " << argv[0] << endl;
           if (argc != 2)
            {
            printf("%s [File Name]\n", argv[0]);
            return -1;
            }
                                                
     
     
     
     
     inFile.open(argv[1]);
     inFile1.open(argv[1]);
     
        if (!inFile) {
           cout << "Unable to open file";
           exit(1); // terminate with error
           }
                                 
    
    
    
    
    
    
   while (inFile >> s) {
                                          
                                
         if(max < a){
            max=a;
             }
       a=0;                             
    
    for (i=0;i<=strlen(s)-1;i++){
        
        if (s[i]=='a' || s[i]=='A')
         a=a+1;
        else if (s[i]=='-' || s[i]=='_')
         a=a+1;
        else if (s[i]=='b' || s[i]=='B')
         a=a+1;
         else if (s[i]=='c' || s[i]=='C')
         a=a+1;
         else if (s[i]=='d' || s[i]=='D')
         a=a+1;
         else if (s[i]=='e' || s[i]=='E')
         a=a+1;
         else if (s[i]=='f' || s[i]=='F')
         a=a+1;
         else if (s[i]=='g' || s[i]=='G')
         a=a+1;
         else if (s[i]=='h' || s[i]=='H')
         a=a+1;
         else if (s[i]=='i' || s[i]=='I')
         a=a+1;
         else if (s[i]=='j' || s[i]=='J')
         a=a+1;
         else if (s[i]=='k' || s[i]=='K')
         a=a+1;
         else if (s[i]=='l' || s[i]=='L')
         a=a+1;
         else if (s[i]=='m' || s[i]=='M')
         a=a+1;
         else if (s[i]=='n' || s[i]=='N')
         a=a+1;
         else if (s[i]=='o' || s[i]=='O')
         a=a+1;
         else if (s[i]=='p' || s[i]=='P')
         a=a+1;
         else if (s[i]=='q' || s[i]=='Q')
         a=a+1;
         else if (s[i]=='r' || s[i]=='R')
         a=a+1;
         else if (s[i]=='s' || s[i]=='S')
         a=a+1;
         else if (s[i]=='t' || s[i]=='T')
         a=a+1;
         else if (s[i]=='u' || s[i]=='U')
         a=a+1;
         else if (s[i]=='v' || s[i]=='V')
         a=a+1;
         else if (s[i]=='w' || s[i]=='W')
         a=a+1;
         else if (s[i]=='x' || s[i]=='X')
         a=a+1;
         else if (s[i]=='y' || s[i]=='Y')
         a=a+1;
         else if (s[i]=='z' || s[i]=='Z')
          a=a+1;
                      
               
        }//end of for
        
        
//        cout<<"There are "<<a<<" letters 'a' in the string";

}//END OF WHILE


// we know the max length of the sequence in consideration
// what we want is that to pad the sequences with "-" as to 
// make the lenght equal for the sequences

//cout<<"The max length is "<< max<<" in the sequence file"<<endl;

 while (inFile1 >> t) {
   b=0;
   cout << t;
     for (j=0;j<=strlen(t)-1;j++){

        if (t[j]=='a' || t[j]=='A')
           b=b+1;
        else if (t[j]=='-' || t[j]=='_')
           b=b+1;
        else if (t[j]=='b' || t[j]=='B')
           b=b+1;
        else if (t[j]=='c' || t[j]=='C')
          b=b+1;
        else if (t[j]=='d' || t[j]=='D')
          b=b+1;
        else if (t[j]=='e' || t[j]=='E')
          b=b+1;
        else if (t[j]=='f' || t[j]=='F')
          b=b+1;
        else if (t[j]=='g' || t[j]=='G')
          b=b+1;
        else if (t[j]=='h' || t[j]=='H')
          b=b+1;
        else if (t[j]=='i' || t[j]=='I')
          b=b+1;
        else if (t[j]=='j' || t[j]=='J')
          b=b+1;
        else if (t[j]=='k' || t[j]=='K')
          b=b+1;
        else if (t[j]=='l' || t[j]=='L')
          b=b+1;
        else if (t[j]=='m' || t[j]=='M')
          b=b+1;
        else if (t[j]=='n' || t[j]=='N')
          b=b+1;
         else if (t[j]=='o' || t[j]=='O')
          b=b+1;
         else if (t[j]=='p' || t[j]=='P')
          b=b+1;
         else if (t[j]=='q' || t[j]=='Q')
          b=b+1;
         else if (t[j]=='r' || t[j]=='R')
          b=b+1;
         else if (t[j]=='s' || t[j]=='S')
          b=b+1;
         else if (t[j]=='t' || t[j]=='T')
          b=b+1;
         else if (t[j]=='u' || t[j]=='U')
          b=b+1;
         else if (t[j]=='v' || t[j]=='V')
          b=b+1;
         else if (t[j]=='w' || t[j]=='W')
          b=b+1;
         else if (t[j]=='x' || t[j]=='X')
          b=b+1;
         else if (t[j]=='y' || t[j]=='Y')
          b=b+1;
         else if (t[j]=='z' || t[j]=='Z')
          b=b+1;

        }//end of for

//  cout << "This is b before while" <<b << endl;                                                                                                                                                                                                                                                        
   if(t[0]=='>'){
    cout<<endl;
    }
   
   if(t[0]!='>'){
        while(b<max){    
        cout << "-";
        b++;
        }//while(b<max)
   cout << endl; 
    }//end of if

//    cout << "This is b after while" <<b <<endl; 
    }//end of while





return 0;

}

Any help would be appreciable.
Thanks
 
Old 04-29-2008, 09:05 PM   #2
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
The result for this file would be

Code:
>hha
fhahahdhaa
>hhb
kskksk
>hhc
jfkdjfdk
jiojoijo
>hhd
jjkkj
hdjhfdk
Code:
>hha
fhahahdhaa
----------
>hhb
kskksk----
----------
>hhc
jfkdjfdk--
jiojoijo--
>hhd
jjkkj-----
hdjhfdk---
 
Old 04-29-2008, 10:17 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
First observation you have a couple of very long if else blocks looking, as I understand it, to see if you have a letter or hyphen or underscore. There is a C function available to C++ called isalpha() which returns true if the character is a letter, you will need to #include <cctype>
Using that may at least simplify your code a little.

Last edited by graemef; 04-29-2008 at 10:19 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which data copy tool is the best if number of files are more. ssrameez Linux - General 2 02-05-2008 10:01 PM
Shell Script that sorts data with number beginning on each line. sunksullen Programming 12 05-09-2007 03:35 PM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM
text data conversion: rows into columns frankie_DJ Programming 6 06-03-2006 06:43 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration