1

学校で完了する必要のある運動があり、混乱しています。私には意味がないので、これを実行するための正しい方向へのヒントが必要です。

Employeeクラスにメンバー関数を追加します。

void Employee::format(char buffer[], int buffer_maxlength)

メンバー関数は、従業員の名前と給与でバッファーを埋める必要があります。バッファをオーバーランしないように注意してください。'\ 0'ターミネータをカウントせずに、buffer_maxlength文字を保持できます。

私が得られないのは、関数に渡されるパラメーターが何であるかです。名前を渡してからバッファにフィードするべきではありませんか?または、関数がパラメーターを受け取ってバッファーにデータを入力するべきではありませんか?バッファがパラメータの場合、どのように設定しますか?

ここで混乱しています。

演習がまだわからないため、コーディングを開始していません。

私のためにプログラムを書くのに誰かを必要としないでください、ただここで何が起こっているかについてのヒントが必要です。

ありがとう。

編集:これは私がこれまでに持っているコードです、それはうまくいくようです。よくわからないのは、バッファオーバーランです。バッファのサイズを変更できないので、既存のデータでオーバーランできないことがわかっているサイズにする必要がありますか?これは非効率に思えますが、他に何をすべきかわかりません。

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string.h>
#pragma warning(disable : 4996) // had to include this for the strcpy function, not sure why

using namespace std;

/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
   /**
   Constructs an employee with empty name and no salary.
   */
Employee();
/**
  Constructs an employee with a given name and salary.
  @param employee_name the employee name
  @param initial_salary the initial salary
*/
Employee(string employee_name, double initial_salary);
/**
  Sets the salary of this employee.
  @param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
  Gets the salary of this employee.
  @return the current salary
*/
double get_salary() const;
/**
  Gets the name of this employee.
  @return the employee name
*/
string get_name() const;

void format(char buffer[], int buffer_maxlength);

private:
   string name;
   double salary;
   char buffer;
};

Employee::Employee()
{  
   salary = 0;
}

Employee::Employee(string employee_name, double initial_salary)
{  
   name = employee_name;
   salary = initial_salary;
}

void Employee::set_salary(double new_salary)
{  
   salary = new_salary;
}

double Employee::get_salary() const
{  
   return salary;
}

string Employee::get_name() const
{  
   return name;
}

void Employee::format(char buffer[], int buffer_maxlength)
{   
string temp_name;
//string space = " ";
char terminator = '\0';
double input_salary = salary;   
string s;   
stringstream output_salary;   
output_salary << input_salary;   
s = output_salary.str();
temp_name = name.c_str() + s + terminator;
strcpy(buffer, temp_name.c_str());
cout << buffer << endl; 
}


int main()
{
const int BUFFER_SIZE = 100;
char input_buffer[BUFFER_SIZE];

string temp_string;
string space = " ";

Employee bob_buffer("Buffer, Bob", 100000);
bob_buffer.format(input_buffer, BUFFER_SIZE);   

system("pause");

return 0;
}

編集:オーバーランから保護するためにstrcpyの代わりにstrncpyを使用

4

3 に答える 3

1

名前給与がクラスメンバーの場合、メンバー関数でそれらにアクセスできます。これらを渡す必要はありません。buffer渡されるパラメーターは、関数に入力するものです。Employeeクラスのユーザーは、関数、結果で満たされるバッファを渡します。

したがって、割り当ては、EmployeeクラスAPIの一部を定義し、クラス内部の特定の表現を提供し、バッファー境界/文字列処理について学習することです。

于 2012-08-26T03:27:40.603 に答える
1

インストラクターが求めているのは、次のように使用できる関数です。

  1. 呼び出し元は、選択したサイズのバッファーを割り当てます。
  2. 呼び出し元は を呼び出しEmployee::format、バッファとバッファ サイズの両方を渡します。
  3. Employee::formatバッファーに従業員名と給与を入力し、バッファーの末尾を超えて書き込まないようにします。
  4. 呼び出し元は Employee の文字列表現を取得し、これを必要なこと (コンソールへの出力、ファイルへの保存、GUI ウィジェットでの表示など) に使用できます。

基本的に、バッファ引数は戻り値です。 Employee::formatの唯一の責任は、呼び出し元が使用できるように名前と給与をバッファーに書き込むことです。それを念頭に置いて、以下にいくつかの変更を加えることができます。

  • に のbufferメンバがありEmployee、使用するために に追加された可能性がありformatます。呼び出し元がバッファーの割り当てを担当するため、これは不要です。さらに、 のbuffer引数formatは同じ名前のメンバーを非表示にするため、メンバーが使用されることはありません。
  • cout << buffer << endl;最後の行formatは従業員情報をコンソールに出力しますが、発信者はそれを望まない場合があります。 format従業員情報のみをバッファに書き込む必要があります。
于 2012-08-26T06:00:03.130 に答える
0

あなたの先生はあなたに名前と給与情報をバッファにフォーマットするように要求していると思います、そしてあなたのフォーマットされたテキストがバッファサイズを超えないことを確認してください。

名前と給与は、従業員クラスのメンバー変数である必要があります。

于 2012-08-26T03:24:13.557 に答える