0

これは、2 つの 5 桁の数字を文字列として取得し、2 つの数字を '+' のオーバーロード演算子を使用して加算する基本的なプログラムです。

#include <iostream>
#include <limits>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;


class IntStr
{
   int InputNum;
   public:
     //IntStr();
     IntStr::IntStr(int num);
     IntStr operator+ (const IntStr &);
     //~IntStr();
     void Display();
};

IntStr::IntStr(int num)
{
  InputNum = num;
}

void IntStr::Display()
{
   cout << "Number is (via Display) : " << InputNum <<endl;
}


IntStr IntStr::operator+ (const IntStr & second) {
        int add_result = InputNum + second.InputNum;
        return IntStr(add_result);
        }



int main()
{
    string str;
    bool option = true;
    bool option2 = true;
    while (option)
    {
    cout << "Enter the number : " ;
    if (!getline(cin, str)) 
    {
       cerr << "Something went seriously wrong...\n";
    }

    istringstream iss(str);
    int i;
    iss >> i;    // Extract an integer value from the stream that wraps str

    if (!iss) 
    {
       // Extraction failed (or a more serious problem like EOF reached)
       cerr << "Enter a number dammit!\n";
    } 
    else if (i < 10000 || i > 99999) 
    {
    cerr << "Out of range!\n";
    } 
    else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposesc only
      cout << "Number is : " << i << endl;
      option = false;
      IntStr obj1 = IntStr(i);
      obj1.Display();
    }
    }//while


    while (option2)
    {
    cout << "Enter the second number : " ;
    if (!getline(cin, str)) 
    {
       cerr << "Something went seriously wrong...\n";
    }

    istringstream iss(str);
    int i;
    iss >> i;    // Extract an integer value from the stream that wraps str

    if (!iss)  //------------------------------------------> (i)
    {
       // Extraction failed (or a more serious problem like EOF reached)
       cerr << "Enter a number dammit!\n";
    } 
    else if (i < 10000 || i > 99999) 
    {
    cerr << "Out of range!\n";
    } 
    else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposes only
      cout << "Number is : " << i << endl;
      option2 = false;
      IntStr obj2 = IntStr(i);
      obj2.Display();
      //obj1->Display();
    }
    }//while

    //IntStr Result = obj1 + obj2; // --------------------> (ii)
    //Result.Display();

    cin.get();
}

上記のコードのポイント (i) と (ii) について説明が必要です ...

(1) (i) は実際に何をしますか?

(2) (ii) → 「obj1 が宣言されていません (最初にこの関数を使用します)」というエラーが出て、コンパイルできません。これは、obj1 と obj2 が while ループ内でのみ宣言されているためですか? それらにグローバルにアクセスするにはどうすればよいですか?

4

3 に答える 3

1

1) http://www.cplusplus.com/reference/iostream/ios/operatornot/から:

ブール演算子 ! ( ) 定数; ストリーム オブジェクトを評価する

エラー フラグ (failbit または badbit) のいずれかがストリームに設定されている場合、true を返します。それ以外の場合は false を返します。

http://www.cplusplus.com/reference/iostream/ios/fail/から:

通常、failbit はエラーが操作自体の内部ロジックに関連している場合に入力操作によって設定されますが、badbit は通常、エラーがストリームの完全性の喪失を伴う場合に設定されます。ストリームで実行されます。

2) 2 つのオブジェクトはスコープ内になく、前の括弧内にのみ存在します。

于 2009-04-20T08:36:25.420 に答える
0
if (!iss) 

ストリームが悪い状態にあるかどうかをテストします。これは、変換が失敗した場合、またはストリームの最後にいる場合に当てはまります。

obj1 は次のように定義されています。

else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposesc only
      cout << "Number is : " << i << endl;
      option = false;
      IntStr obj1 = IntStr(i);
      obj1.Display();
    }

したがって、else ブロックに対してローカルであり、その外部からアクセスすることはできません。スコープを拡大したい場合は、ブロックの外で定義を変更します。ただし、すべてのブロックの外に移動する (つまり、グローバルにする) のは得策ではありません。

于 2009-04-20T08:31:57.600 に答える
0
  1. ブールコンテキストでストリームを評価するオーバーロードされた演算子を呼び出します。iこれにより、ストリームの状態がチェックされ、前の操作が失敗したかどうかが確認されます。失敗した場合は、ストリームへの入力が整数ではないため、整数変数の値が有効であることに依存できません。

  2. 変数はobj1obj2while ループのスコープ内で定義されます。スコープ外では使用できません。while のスコープ外でそれらを宣言できます。その場合、変数は while ループで最後に保持した値を保持します。

于 2009-04-20T08:34:30.183 に答える