Thread: Help me with the code=]

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    17

    Help me with the code=]

    Hi! I Downloaded and modified this opensource.

    It basically, read a txt file and tell how much characters it has and it also tell how much of each letter (from A to Z) it has.

    But... I Cant understand most of the code, from the "///////////OUTPUT////////" line to the end...

    Someone can help me? Maybe commenting the code would help a lot!

    Thanks!!

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    using std::ifstream;
    
    char String[1024];
    double Length = 0;
    int letterCount[36] = {0};
    
    void sortCharacter(char cLetter);
    
    int main()
    {
    char nome[1024];
    
    cout << "Coloque o nome do arquivo a ser examinado (deve ser um .txt)." << endl;
    cout << "O arquivo tem que estar na mesma pasta do programa." << endl;
    
    cin >> nome;
    
    ifstream inFile(nome, ios::in);
    
    if (!inFile) 
      {
      cout << "O Arquivo " << nome << " nao foi encontrado." << endl;
      cout << "Por favor execulte o programa novamente." << endl;
    
      cin.get();
      cin.get();
    
      return 0;
      }
    
    while (!inFile.eof())
      {
      inFile >> String;
    
      for (int x = 0; x < strlen(String); x++)
        {
        sortCharacter(String[x]);
        Length++;
        }
      }
    
    ///////////////////OUTPUT//////////////////////////
    cout << "Caracteres: " << Length << endl;
    
    for (int w = 0; w < 36; w++)
      {
      if (w <= 9)
        {
        if (letterCount[w] != 0)
          {
          cout << w << " aparece " << letterCount[w] << " vezes. \t";
          cout << ((double)letterCount[w] / Length) * 100 << "%" << endl;
          }
        }
      else
        {
          if (letterCount[w] != 0)
          {
          cout << char(w + 55) << " aparece " << letterCount[w] << " vezes. \t";
          cout << ((double)letterCount[w] / Length) * 100 << "%" << endl;
          }
        }
      }
    
    cout << endl << "(Caracteres com acento, assim como os de pontuacao, nao sao listados, porem sao contados)" << endl << endl;
    cout << "Processo completo!" << endl;
    
    cin.get();
    cin.get();
    
    return(0);
    }
    
    void sortCharacter(char cLetter)
    {
    if (cLetter <= '9' && cLetter >= '0')
      {
      letterCount[cLetter - 48]++;
      }
    else if ((cLetter <= 'z' && cLetter >= 'a') || (cLetter <= 'Z' && cLetter >= 'A'))
      {
      if (cLetter <= 'Z' && cLetter >= 'A')
        {
        letterCount[cLetter - 55]++;
        }
      else
        {
        letterCount[cLetter - 87]++;
        }
      }
    }
    Last edited by Ken Fitlike; 09-16-2006 at 04:15 AM. Reason: code indented courtesy of the Joy of Whitesmiths, the style of choice for handsome guys everywhere. Original code attached for comparison.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No one is going to read that unless you indent.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    What you mean by ident?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Moved to c++.

    edit:

    >>What you mean by ident?<<

    Indentation styles.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That's a disaster. Hurts my eyes.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Probose
    What you mean by ident?
    It's difficult to figure out what the code does, unless the writer or poster, gives it the right kind of indentation.

    Consider this:
    (No indentation)
    Code:
    void sortCharacter(char cLetter)
    {
    if (cLetter <= '9' && cLetter >= '0')
    letterCount[cLetter - 48]++;
    else if ((cLetter <= 'z' && cLetter >= 'a') || (cLetter <= 'Z' && cLetter >= 'A'))
    {
    if (cLetter <= 'Z' && cLetter >= 'A')
    letterCount[cLetter - 55]++;
    else
    letterCount[cLetter - 87]++;
    }
    }
    and compare it to this:
    Code:
    void sortCharacter(char cLetter)
    {
       if (cLetter <= '9' && cLetter >= '0')
          letterCount[cLetter - 48]++;
       else if ((cLetter <= 'z' && cLetter >= 'a') || (cLetter <= 'Z' && cLetter >= 'A'))
       {
          if (cLetter <= 'Z' && cLetter >= 'A')
             letterCount[cLetter - 55]++;
          else
             letterCount[cLetter - 87]++;
       }
    }
    If you're used to programming, your eye see's how the flow of the program will proceed, and the logic that it is defining MUCH easier, in the second example.

    Even without indentation, this is easy code to understand. What is it about it that leaves you with questions? Is there some specific block or lines of code that confuse you?

    If you could cut that out from your original post, give it some indentation, and post it back up with your specific questions, I'm very certain you'll have a quick answer.

    Hope that helps.

    Adak

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    er................dont u think that the problem can be solved far more easily using this code:
    <<off-topic, unindented and untagged void main code deleted by salem>>

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Archie please use code tags... and that code doesn't do all of what the code the OP posted does. Regardless of how much easier it could be done, the point is s/he's trying to understand what is in front of him/her. Alternatives are not what s/he needs.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    Quote Originally Posted by Adak
    It's difficult to figure out what the code does, unless the writer or poster, gives it the right kind of indentation.

    Consider this:
    (No indentation)
    Code:
    void sortCharacter(char cLetter)
    {
    if (cLetter <= '9' && cLetter >= '0')
    letterCount[cLetter - 48]++;
    else if ((cLetter <= 'z' && cLetter >= 'a') || (cLetter <= 'Z' && cLetter >= 'A'))
    {
    if (cLetter <= 'Z' && cLetter >= 'A')
    letterCount[cLetter - 55]++;
    else
    letterCount[cLetter - 87]++;
    }
    }
    and compare it to this:
    Code:
    void sortCharacter(char cLetter)
    {
       if (cLetter <= '9' && cLetter >= '0')
          letterCount[cLetter - 48]++;
       else if ((cLetter <= 'z' && cLetter >= 'a') || (cLetter <= 'Z' && cLetter >= 'A'))
       {
          if (cLetter <= 'Z' && cLetter >= 'A')
             letterCount[cLetter - 55]++;
          else
             letterCount[cLetter - 87]++;
       }
    }
    If you're used to programming, your eye see's how the flow of the program will proceed, and the logic that it is defining MUCH easier, in the second example.

    Even without indentation, this is easy code to understand. What is it about it that leaves you with questions? Is there some specific block or lines of code that confuse you?

    If you could cut that out from your original post, give it some indentation, and post it back up with your specific questions, I'm very certain you'll have a quick answer.

    Hope that helps.

    Adak
    Well. I Dont understand how the code count how much of each letter we have in the examined text file =]

Popular pages Recent additions subscribe to a feed