0

私は多項式の計算を行っていますが、コードを進めるときに助けが必要です。

今のところ、用語といくつかの関数を含むリンクリストとして表現したポリノムクラスのみを作成しました(今のところ、多項式関数のみを読み取り、出力します)。

これは、多項式を読み取って出力するだけのメインプログラムです。

#include "polinom.h"

int main()

{

polinom P1;
bool varStatus = false;
char var = '\0', readStatus = '\0';

cout << "P1 = ";
P1.read(readStatus, var, varStatus); // i don't need readStatus yet as i haven't implemented the reset and quit functions 

cout << "\n\nP = ";
P1.print(var);

getch();
return 0;
}

そしてヘッダファイル polinom.h:

#ifndef _polinom_h
#define _polinom_h

#include <iostream>
#include <list>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <conio.h>


using namespace std;

class polinom 
{
class term
{
    public:
        int coef;
        int pow;

        term() 
        {
            coef = 1;
            pow = 0;
        }    
};

list<term> poly;
list<term>::iterator i;

public:

    bool printable(char c) 
    {

        return (
                  ((int(c) > 42 && int(c) < 123) || isspace(c)) && int(c) != 44 && int(c) != 46 && int(c) != 47 && 
                  int(c) != 58 && int(c) != 59 && 
                  int(c) != 60 && int(c) != 61 && int(c) != 62 && int(c) != 63 && int(c) != 64 && int(c) != 65 && 
                  int(c) != 91 && int(c) != 92 && int(c) != 93 && int(c) != 95 && int(c) != 96
                ); 
    }


    void read(char &readStatus, char &var, bool &varStatus)
    {

        term t; // term variable to push it into the list of terms
        char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient
        int coef, pow; //variables to pass the coef and power to term t
        bool coefRead = false, powRead = false; //reading status of coef and power 

        while (c != '\r') { //we read characters until carriage return
            c = getch(); // get the new imputed char

            if (tolower(c) == 'r' || tolower(c) == 'q') { //if the user inputed r or q we reset the input or quit the program
                    readStatus = c; //pass current char value to readStatus so the program will know what to do next
                    return; //aborting the reading process
            }

            else 
            {
                if (printable(c)) cout << c; //print on screen only the correct characters

                if (!coefRead && !powRead) //we set term coef to the inputed value
                {                    
                    if (isdigit(c)) { 
                        if (isdigit(lc)) coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char
                        else {                                    
                            if (sign == '-')  coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value 
                            else              coef = int(c);   //this means a new term's coef is read
                    }
                    if (!isdigit(c) && isdigit(lc)) coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient
                }

                else if (coefRead && !powRead) //after coefficient is read we get the term's varname and power 
                {
                    if (isdigit(c)) { // just like in the case with coefficient we read the power until the current char is not a digit
                        if (isdigit(lc)) pow = pow * 10 + int(c);
                        else pow = int(c);
                    }

                    else if (isalpha(c) && isdigit(lc) && !varStatus) { //if the last char was a digit and the current not we reached the var name
                    var = c;                                            //also even though the variable is inputed more than once we save it only once
                    varStatus = true; //we mark the var name as read
                    }
                    else {
                        if (isdigit(lc)) powRead = true;
                    }   
                }

            else {
                if (c == '+' || c == '-') { // if a sign was inputed it means a new term is coming and we push the current term to the list and reset 
                    t.coef = coef;          // coefRead and powRead so we can read another term 
                    t.pow = pow;
                    poly.push_back(t);
                    sign = c;
                    coefRead = false;
                    powRead = false;
                }
            }

           lc = c; // we save the last character

            }
        } 
    }

    void print(char var)
    {
        for ( i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them 

            if (i == poly.end() - 1) { // if we reached the last term 
                if (*(i->pow == 0) //if the last term's power is 0 we print only it's coefficient
                    cout << *(i->coef);
                else 
                    cout << *(i->coef) << var << "^" << *(i->pow); //otherwise we print both
            }

            else {
                if (*(i->coef > 0) //if the coef value is positive 
                    cout << *(i->coef) << var << "^" << *(i->pow) << " + "; //we also add the '+' sign
                else 
                    cout << *(i->coef) << var << "^" << *(i->pow) << " - "; // otherwise we add '-' sign
            }
        }
    }

};


#endif                    

編集

JonH のおかげですべてのコンパイル エラーが修正されましたが、入力文字がリストに正しく挿入されていないため、読み取り関数が機能していません。皆さんにとって些細なことかもしれませんが、助けていただければ幸いです。

ありがとう!

4

4 に答える 4

10

コード全体で中括弧と閉じ括弧が多数欠落していることがわかりました。これらのうち少なくとも 10 個を修正するのに数分を費やした後、今夜の夕食に魚を与えるよりも、釣りを学ぶのを手伝ったほうがよいと思いました。

あなたのコードは意識の流れのように書かれています。コードを作成していると、ほかに作成する必要があるものや、作成したばかりのコードによって導入される新しい要件について考えて、心が飛び跳ねます。これらのことを考えると、それらを書きに行って、元の場所に戻ってきます。気が付く前に、あちこち飛び回り、あちこちにビットを書き込んで、何百行ものコードを書いてきました。これに関する問題は、途中で少しの構文ビットを見逃すことなく、このようにコードのセクションをジャグリングし続けることはおそらくできないということです。

コードを記述するには、より反復的なアプローチを取る必要があります。これをどのように正確に行うかは経験に基づいていますが、ここにいくつかのガイダンスがあります。

  1. いくつか (できれば 1 つ) のコア メソッドとメンバー変数を使用してクラス宣言をスタブ化することから始めます。
  2. コンパイル。リンカ エラーなどは発生しますが、括弧やセミコロンの欠落などの構文エラーは発生しないはずです。次に進む前に、見つけたものを修正してください。
  3. スタブ化したばかりのメソッド/関数を実装します。リンカー以外のエラーをコンパイルして修正します。
  4. 上記の手順で出てきたマイナーな要件または依存する要件について考えるときは、「// TODO: Implement bool DoTheThing(int); まだ実装していません」のように、コードにコメントを記述します。
  5. ステップ 1 に戻り、取り組んでいることの範囲をできるだけ限定的かつ基本的なものに保ちます。クリーン コンパイルなしでコンパイル ステップを超えて移動しないでください。

すべてを実装するまで繰り返します。このプロセス中に 50 回以上コンパイルする可能性があります。

于 2010-03-09T21:00:30.680 に答える
2

あなたの根本的な問題は、コードを少しずつテストせずに、考えずに書き留めたことです。初心者として書くときは、一度に少しずつ追加して、コンパイルできることを確認する必要があります。上級プログラマーであっても、モジュール化は設計およびコード作成プロセスの非常に重要な部分です。

とはいえ、特に投稿されたコードに関するいくつかのヒントを次に示します。

  1. あなたの関数printableは罪のように醜いので、デバッグや理解は不可能です。
  2. ネストされたifステートメントの数は、設計上の欠陥を示しています。
  3. if (isdigit(c))ステートメントに中かっこがありません。
  4. 同じ行で複数の変数を宣言 (および特に初期化) することは、悪い形式です。
于 2010-03-09T20:45:59.900 に答える
1

これらのコンパイルエラーには、エラーメッセージに行番号が関連付けられていることは確かです。示された行を見て、何が欠けているかを確認してみましたか?それでも問題が解決しない場合は、コンパイラからの完全なエラー出力を投稿して、エラーが何であるかを確認できるようにしてください。

于 2010-03-09T20:36:44.707 に答える
1

読み取り関数にいくつかの中括弧がありません。

私はここでそれをやり直しました:

 void read(char &readStatus, char &var, bool &varStatus) 
{ 

    term t; // term variable to push it into the list of terms 
    char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient 
    int coef, pow; //variables to pass the coef and power to term t 
    bool coefRead = false, powRead = false; //reading status of coef and power  

    while (c != '\r') { //we read characters until carriage return 
        c = getch(); // get the new imputed char 

        if (tolower(c) == 'r' || tolower(c) == 'q') 
        { //if the user inputed r or q we reset the input or quit the program 
                readStatus = c; //pass current char value to readStatus so the program will know what to do next 
                return; //aborting the reading process 
        } 

        else  
        { 
            if (printable(c)) 
               cout << c; //print on screen only the correct characters 

            if (!coefRead && !powRead) //we set term coef to the inputed value 
            {                     
                if (isdigit(c)) 
                   {  
                    if (isdigit(lc)) 
                       coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char 
                    else 
                      {                                     
                        if (sign == '-')  
                           coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value  
                        else              
                           coef = int(c);   //this means a new term's coef is read 
                      } //end else 
                    }//end if isdigit(c)
                if (!isdigit(c) && isdigit(lc)) 
                   coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient 
            }  //end if

            else if (coefRead && !powRead) //after coefficient is read we get the term's varname and power  
            { 
                if (isdigit(c)) 
                   { // just like in the case with coefficient we read the power until the current char is not a digit 
                    if (isdigit(lc)) 
                       pow = pow * 10 + int(c); 
                    else 
                         pow = int(c); 
                    } 

                else if (isalpha(c) && isdigit(lc) && !varStatus) 
                     { //if the last char was a digit and the current not we reached the var name 
                         var = c;                                            //also even though the variable is inputed more than once we save it only once 
                          varStatus = true; //we mark the var name as read 
                     } 
                else 
                     { 
                     if (isdigit(lc)) 
                        powRead = true; 
                     }    
            } //end else if 

        else 
             { 
             if (c == '+' || c == '-') 
                { // if a sign was inputed it means a new term is coming and we push the current term to the list and reset  
                  t.coef = coef;          // coefRead and powRead so we can read another term  
                  t.pow = pow; 
                  poly.push_back(t); 
                  sign = c; 
                  coefRead = false; 
                  powRead = false; 
                } 
              } 

       lc = c; // we save the last character 

        } //end else
    }  //end while
} //end function

編集

印刷機能も修正しました:

 void print(char var) 
    { 
        for ( i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them  

            if (i == poly.end()) { // if we reached the last term  
                if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient 
                    cout << i->coef;
                else  
                    cout << i->coef << var << "^" << i->pow; //otherwise we print both 
            } 

            else { 
                if (i->coef > 0) //if the coef value is positive  
                    cout << i->coef << var << "^" << i->pow << " + "; //we also add the '+' sign 
                else  
                    cout << i->coef << var << "^" << i->pow << " - "; // otherwise we add '-' sign 
            } 
        } 
    } 
于 2010-03-09T20:48:02.830 に答える