3

問題 -> 固定長文字列を std::string* に返す。
ターゲット マシン -> Fedora 11 。

整数値を受け入れ、固定長の文字列を文字列ポインターに返す関数を派生させる必要があります。
例 -> int 値は 0 から -127 の範囲です

したがって、int 値 0 の場合 ->
値 -9 に対して 000 を表示する必要があり
ます -> 値 -50 に対して -009 を返す必要があり
ます -> 値 -110 に対して -050 ​​を返す必要があります -> -110 を返す必要があります

要するに、長さはすべての場合で同じでなければなりません。

私がやったこと:以下に示す要件に従って関数を定義しました。

助けが必要な場合:関数を導出しましたが、これが正しいアプローチかどうかわかりません。Windows側のスタンドアロンシステムでテストすると、exeが時々動作を停止しましたが、Linuxマシンのプロジェクト全体にこの機能を含めると、問題なく動作します.

    /* function(s)to implement fixed Length Rssi */
 std::string convertString( const int numberRssi, std::string addedPrecison="" )
 {
     const std::string         delimiter   =   "-";
                stringstream   ss;

     ss  << numberRssi ;
     std::string tempString = ss.str();
     std::string::size_type found = tempString.find( delimiter );
     if( found == std::string::npos )// not found
     {
         tempString = "000";
     }
     else
     {
         tempString = tempString.substr( found+1 );
         tempString = "-" +addedPrecison+tempString ;
     }
     return  tempString;

 }

 std::string stringFixedLenght( const int number )
 {
     std::string str;
     if( (number <= 0) && (number >= -9) )
       {
           str = convertString( number, "00");
       }
       else if( (number <= -10) && (number >= -99) )
       {
           str = convertString( number, "0");
       }
       else
       {
           str= convertString(number, "");
       }
     return str;
 }
// somewhere in the project calling the function
ErrorCode A::GetNowString( std::string macAddress, std::string *pString )
{
    ErrorCode result = ok;
    int lvalue;
    //some more code like iopening file and reading file 
    //..bla
    // ..bla     
    // already got the value in lvalue ;

    if( result == ok )
    {
         *pString = stringFixedLenght( lValue );
    }

    // some more code

    return result;

}
4

5 に答える 5

12

I/O マニピュレータを使用して、必要な幅を設定し、ゼロで埋めることができます。たとえば、このプログラムは次のように出力し00123ます。

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    cout << setfill('0') << setw(5) << 123 << endl;
    return 0;
}

cout << setfill('0') << setw(5) << -123 << endlただし、負の値は自分で処理する必要があり0-123ます-0123。値が負かどうかを確認し、幅を に設定しN-1、前にマイナスを追加します。

于 2012-07-17T11:11:23.713 に答える
12

std::ostringstream標準出力の書式設定マニピュレータを使用するのはどうですか?

std::string makeFixedLength(const int i, const int length)
{
    std::ostringstream ostr;

    if (i < 0)
        ostr << '-';

    ostr << std::setfill('0') << std::setw(length) << (i < 0 ? -i : i);

    return ostr.str();
}
于 2012-07-17T11:15:12.843 に答える
2

あなたの例はあなたの説明と矛盾していることに注意してください:値が-9で固定長が3の場合、出力は「-009」(あなたの例のように)または「-09」(あなたが説明したように)ですか? 前者の場合、明らかな解決策は、フォーマット フラグ on を使用することstd::ostringstreamです。

std::string
fixedWidth( int value, int width )
{
    std::ostringstream results;
    results.fill( '0' );
    results.setf( std::ios_base::internal, std::ios_base::adjustfield );
    results << std::setw( value < 0 ? width + 1 : width ) << value;
    return results.str();
}

後者の場合は、条件を にドロップしてstd::setw、 を渡す だけwidthです。

記録のために、私はそれを避けますが、これprintfostream. 使用snprintf:

std::string
fixedWidth( int value, int width )
{
    char buffer[100];
    snprintf( buffer, sizeof(buffer), "%.*d", width, value );
    return buffer;
}

念のため、 の戻り値を取得し、snprintfその後にエラー処理を追加することをお勧めします (ただし、char現在のほとんどのマシンでは 100 秒で十分です)。

于 2012-07-17T11:42:17.950 に答える
0

ストリームを使用するバージョンに反対するものは何もありませんが、コードよりも簡単にすべてを自分で行うことができます。

std::string fixedLength(int value, int digits = 3) {
    unsigned int uvalue = value;
    if (value < 0) {
        uvalue = -uvalue;
    }
    std::string result;
    while (digits-- > 0) {
        result += ('0' + uvalue % 10);
        uvalue /= 10;
    }
    if (value < 0) {
        result += '-';
    }
    std::reverse(result.begin(), result.end());
    return result;
}
于 2012-07-17T11:54:08.507 に答える
0

このような?

#include <cstdlib>
#include <string>

template <typename T>
std::string meh (T x)
{
    const char* sign = x < 0 ? "-" : "";
    const auto mag = std::abs (x);
    if (mag < 10)  return sign + std::string ("00" + std::to_string(mag));
    if (mag < 100) return sign + std::string ("0" + std::to_string(mag));
    return std::to_string(x);
}


#include <iostream>
int main () {
    std::cout << meh(4) << ' '
              << meh(40) << ' '
              << meh(400) << ' '
              << meh(4000) << '\n';
    std::cout << meh(-4) << ' '
              << meh(-40) << ' '
              << meh(-400) << ' '
              << meh(-4000) << '\n';
}

004 040 400 4000

-004 -040 -400 -4000

于 2012-07-17T11:08:07.203 に答える