私のソフトウェアは最終結果をユーザーに表示する必要があり、データは UART ポートを介して受信され、データをフォーマットして丸める必要があります。
私はそれを行うアルゴリズムを書きましたが、より良い解決策が必要だと感じています。
私の出力には複数の範囲が含まれている可能性があり、正しく丸めてフォーマットする必要があります。
例:
多くの結果のうちの 1 つは次の場所から ->
0.000オーム~2.000オーム
2.01オームから20.00オーム
20.1Ω~100.0Ω
100Ω~200Ω
フォーマット用の基本データを保持するための単純な構造を作成しました
struct _range
{
float from; //0.000 for first example
float to; //2.000
int decimal; //3, decimal places I need
int unit; //Unit to format, Ohm in my example
float div; //sometimes I need to divide the result at some range
//example, when I reach 1000VA I need to divide by 1000 to get 1kVA
};
AddUnitToResult という静的関数があります。これは結果に単位を追加します。
ここで、結果を文字列に正しくフォーマットし、それを double に丸める関数を作成するのを手伝ってくれるようお願いしています (後で比較するには double が必要です)。
正しいフォーマットとは、結果が 0 の場合でも、小数点以下 3 桁にフォーマットする必要があることを意味します。
皆さんが私を助けてくれることを願っています
編集:
これは、現在、丸めと除算を処理する必要があるものです。
void ResultBox::SetResult(float res)
{
this->measureCounter++;
this->valueAVG +=res;
if (res > this->valueMax)
this->valueMax = res;
if (res < this->valueMin)
this->valueMin = res;
float tttmp;
if (this->RangeCount > 0 && this->ranges[0].decimal >= 0)
{
tttmp = Converter::cutDecimal(res,this->ranges[0].decimal);
}
int decimal = GetDecimalPlaces(res,0);
float div = GetDivision(res);
int unit = GetUnit(res);
tttmp=tttmp/div;
this->result = tttmp;
float tmpRes = res /div;
this->isValid =true;
WCHAR resText[20];
WCHAR finalText[20];
WCHAR maxText[20];
WCHAR minText[20];
char resTEXT[20];
tmpRes = res/div;;
std::ostringstream ss;
ss << std::fixed << std::setprecision(decimal) << tttmp;
std::string s = ss.str();
if (decimal > 0 && s[s.find_last_not_of('0')] == '.')
{
s.erase(s.size()-decimal+1);
}
Converter::dtoa(resTEXT,tmpRes);
switch(decimal)
{
case 0:
if (floor(tttmp) == tttmp)
{
swprintf(resText,L"%.0f",tttmp);
}else
{
swprintf(resText,L"%S",s.c_str());
}
swprintf(maxText,L"%.0f",this->GetMax());
swprintf(minText,L"%.0f",this->GetMin());
break;
case 1:
if (floor(tttmp) == tttmp)
{
swprintf(resText,L"%.1f",tttmp);
}else
{
swprintf(resText,L"%S",s.c_str());
}
swprintf(maxText,L"%.1f",this->GetMax());
swprintf(minText,L"%.1f",this->GetMin());
break;
case 2:
if (floor(tttmp) == tttmp)
{
swprintf(resText,L"%.2f",tttmp);
}else
{
swprintf(resText,L"%S",s.c_str());
}
swprintf(maxText,L"%.2f",this->GetMax());
swprintf(minText,L"%.2f",this->GetMin());
break;
case 3:
if (floor(tttmp) == tttmp)
{
swprintf(resText,L"%.3f",tttmp);
}else
{
swprintf(resText,L"%S",s.c_str());
}
swprintf(maxText,L"%.3f",this->GetMax());
swprintf(minText,L"%.3f",this->GetMin());
break;
case 4:
if (floor(tttmp) == tttmp)
{
swprintf(resText,L"%.4f",tttmp);
}else
{
swprintf(resText,L"%S",s.c_str());
}
swprintf(maxText,L"%.4f",this->GetMax());
swprintf(minText,L"%.4f",this->GetMin());
break;
case 5:
if (floor(tttmp) == tttmp)
{
swprintf(resText,L"%.5f",tttmp);
}else
{
swprintf(resText,L"%S",s.c_str());
}
swprintf(maxText,L"%.5f",this->GetMax());
swprintf(minText,L"%.5f",this->GetMin());
break;
}
//pogledamo če je majni
if (res < this->GetMin())
{
if (LowerEnabled == true)
{
wcscpy(finalText,L"<");
}
else
{
wcscpy(finalText,L"");
}
wcscat(finalText,minText);
}
else if (res > this->GetMax())
{
wcscpy(finalText,L">");
wcscat(finalText,maxText);
}
else
{
wcscpy(finalText,resText);
}
if (res == this->GetMin())
{
wcscpy(finalText,minText);
}
if (res == this->GetMax())
{
wcscpy(finalText,maxText);
}
if (this->unitBox)
{
WCHAR mm[10];
wcscpy(mm,L"");
TABSGuiProxy::MargeResultAndUnit(mm,unit);
if (mm[0] == ' ')
this->unitBox->SetText(&mm[1]);
else
this->unitBox->SetText(mm);
}
this->m_ptextBlock->SetText(finalText);
if (this->resultLimit)
{
if (unit == MEASRUEMENT_UNITS::kVA)
{
float fff = this->resultLimit->GetValue()*1000;
std::wostringstream ss1;
ss1 << std::fixed << std::setprecision(decimal) << fff;
std::wstring s1 = ss1.str();
if (decimal > 0 && s1[s1.find_last_not_of('0')] == '.')
{
s1.erase(s1.size()-decimal+1);
}
if (wcscmp(resText,s1.c_str()) == 0)
{
this->SetGoodResult();
}else
{
float mmm = fabs(_wtof(s1.c_str()) - this->resultLimit->GetValue()*1000) ;
//else if (fabs(IDelResoult - IDeltaLim) <= 0.001 || IDelResoult < IDeltaLim)
if (mmm<= 0.001 || tttmp < (_wtof(s1.c_str())))
{
this->SetGoodResult();
}
else
{
this->SetBadResult();
}
}
}
else
{
float fff = this->resultLimit->GetValue();
std::wostringstream ss1;
ss1 << std::fixed << std::setprecision(decimal) << fff;
std::wstring s1 = ss1.str();
if (decimal > 0 && s1[s1.find_last_not_of('0')] == '.')
{
s1.erase(s1.size()-decimal+1);
}
if (wcscmp(resText,s1.c_str()) == 0)
{
this->SetGoodResult();
}
else
{
float mmm = fabs(_wtof(resText) - this->resultLimit->GetValue()) ;
//else if (fabs(IDelResoult - IDeltaLim) <= 0.001 || IDelResoult < IDeltaLim)
if (mmm<= 0.001 || tttmp < (_wtof(s1.c_str())))
{
this->SetGoodResult();
}
else
{
this->SetBadResult();
}
}
}
}
}
もう少し説明してみましょう。
struct _range を使用して、出力の有効な範囲を設定します。結果を初期化すると、範囲型の複数の構造体が作成されます。
私が持っているとしましょう:
レンジ 1 -> 0.000 ~ 2.000 オーム レンジ 2 -> 2.01 ~ 20.00 オーム レンジ 3 -> 20.1 ~ 100.0 オーム レンジ 4 -> 101 ~ 200 オーム
結果を初期化すると、それぞれがこのデータを含む 4 つの構造体の配列を作成します
Range1 ->
struct _range
{
float from = 0.000
float to = 2.000
int decimal =3;
int unit = Ohm; //Unit to format, Ohm in my example
float div =1;
};
Range2 ->
struct _range
{
float from = 2.01
float to = 20.00
int decimal =2;
int unit = Ohm; //Unit to format, Ohm in my example
float div =1;
};
Range3 ->
struct _range
{
float from = 20.1
float to = 100.0
int decimal =1;
int unit = Ohm; //Unit to format, Ohm in my example
float div =1;
};
Range4 ->
struct _range
{
float from = 101
float to = 200
int decimal =0;
int unit = Ohm; //Unit to format, Ohm in my example
float div =1;
};
これで、関数への入力は 0 から 200 までになり、構造内の範囲に従ってテキストをフォーマットし、フロートを丸める必要があります。
これでもう少し説明がつくことを願っています