Hilfe benötigt



  • Ich habe folgendes Programm:

    // depth of a tree

    // Grammar:
    // Tree = '(' Branch { Branch } ')'
    // Branch = Label [ Tree ]
    // Label = 'a' | 'b' | ... | 'z'

    #include <iostream>

    // POST: leading whitespace characters are extracted
    // from is, and the first non-whitespace
    // character is returned (0 if there is none)
    char lookahead (std::istream& is);

    // PRE: Tree = '(' Branch { Branch } ')'
    // POST: Extracts tree from is and returns its depth
    int Tree (std::istream& is)
    {
    char c;

    is >> c; // extract '('

    int depth = 0; // int depth = expr1;

    while (lookahead (is) != ')')
    {
    const int bdepth = Branch (is); // const int bdepth = expr2;

    if (bdepth > depth) // if (expr3)
    {
    depth = bdepth; // expr4;
    }
    }
    is >> c; // extract ')'

    return depth;
    }

    // PRE: Branch = Label [ Tree ]
    // POST: Extracts single branch from is and returns
    // its depth
    int Branch (std::istream& is)
    {
    char c;

    is >> c; // extract label

    if (lookahead (is) == '(')
    {
    return 1 + Tree (is); // return expr5;
    }
    return 1;
    }

    int main()
    {
    const int depth = Tree (std::cin);

    std::cout << "Longest stored word has length " << depth;

    return 0;
    }

    Mit folgender Fehlermeldung:

    ./Root/depth_of_a_tree.cpp: In function 'int Tree(std::istream&)':
    ./Root/depth_of_a_tree.cpp:27:37: error: 'Branch' was not declared in this scope
    const int bdepth = Branch(is); // const int bdepth = expr2;
    ^

    Das Problem ist mir bekannt, jedoch weiss ich nicht wie ich es beheben kann.



  • int Branch (std::istream& is);
    

    forward-deklarieren (vor Tree), so wie bei lookahead.



  • // PRE: Branch = Label [ Tree ]
    // POST: Extracts single branch from is and returns
    // its depth
    int Branch (std::istream& is);

    Dies vor int Tree (.....) ?

    Danke schonmal für die schnelle Antwort 😃



  • elgouddi schrieb:

    // PRE: Branch = Label [ Tree ]
    // POST: Extracts single branch from is and returns
    // its depth
    int Branch (std::istream& is);

    Dies vor int Tree (.....) ?

    Danke schonmal für die schnelle Antwort 😃

    Funzt nicht 😞



  • Evtl. hab ichs probiere grad aus. 🙂



  • // depth of a tree

    // Grammar:
    // Tree = '(' Branch { Branch } ')'
    // Branch = Label [ Tree ]
    // Label = 'a' | 'b' | ... | 'z'

    #include <iostream>

    // POST: leading whitespace characters are extracted
    // from is, and the first non-whitespace
    // character is returned (0 if there is none)
    char lookahead (std::istream& is)
    {
    is >> std::ws; // skip whitespaces

    if (is.eof()) // end of stream
    {
    return 0;
    }
    else
    {
    return is.peek(); // next character in is
    }
    }

    // PRE: Branch = Label [ Tree ]
    // POST: Extracts single branch from is and returns
    // its depth
    int Branch (std::istream& is);

    // PRE: Tree = '(' Branch { Branch } ')'
    // POST: Extracts tree from is and returns its depth
    int Tree (std::istream& is)
    {
    char c;

    is >> c; // extract '('

    int depth = 0; // int depth = expr1;

    while (lookahead (is) != ')')
    {
    const int bdepth = Branch (is); // const int bdepth = expr2;

    if (bdepth > depth) // if (expr3)
    {
    depth = bdepth; // expr4;
    }
    }
    is >> c; // extract ')'

    return depth;
    }

    // PRE: Branch = Label [ Tree ]
    // POST: Extracts single branch from is and returns
    // its depth
    int Branch (std::istream& is)
    {
    char c;

    is >> c; // extract label

    if (lookahead (is) == '(')
    {
    return 1 + Tree (is); // return expr5;
    }
    return 1;
    }

    int main()
    {
    const int depth = Tree (std::cin);

    std::cout << "Longest stored word has length " << depth;

    return 0;
    }


Anmelden zum Antworten