将Char转换为Double C ++

时间:2018-03-22 04:45:13

标签: c++ string-parsing

更新 我传递一个字符串变量,并逐个字符地迭代字符串。每当我遇到一个小数时,我想把字符串上的前一个位置(即2)和字符串中的下一个位置(即5)组合成一个double。那么我将如何制作char 2,char。 ,char 5成一整个双值(2.5)?不使用STL类或向量。我接下来尝试的是以下内容。但是,每当我在var行中执行这些操作时,它都不会将值2.5保存为字符串。它包含:"•"。我有什么不对劲吗?

2 个答案:

答案 0 :(得分:2)

如果您要解析字符串为双精度数,我会采用不同的方法来迭代字符串。首先,我将创建一个按空格分割的迭代器,以避免检查是否存在'。'。然后我会迭代这个新的迭代器。像这样的东西

<table>
  <tr id="1">
    <td style="color : blue;"><b>President Staff</b></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan1" value="green" onClick="green('1')" required></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan1" value="red" onClick="red('1')" required></td>
  </tr>

  <tr id="2">
    <td>Corporate Planning & Program Office</td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan2" value="green" onClick="green('2')" required></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan2" value="red" onClick="red('2')" required></td>
  </tr>

  <tr id="3">
    <td>Acquisitiom & Aircraft Management</td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan3" value="green" onClick="green('3')" required></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan3" value="red" onClick="red('3')" required></td>
  </tr>

  <tr id="4">
    <td>Corporate Quality, Safety & Environmentak Management</td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan4" value="green" onClick="green('4')" required></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan4" value="red" onClick="red('4')" required></td>
  </tr>

  <tr id="5">
    <td>Corporate Secretary</td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan5" value="green" onClick="green('5')" required></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan5" value="red" onClick="red('5')" required></td>
  </tr>

  <tr id="6">
    <td>Internal Audit</td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan6" value="green" onClick="green('6')" required></td>
    <td class="text-center" style="padding-left:40px;"><input type="radio" class="form-check-input" name="jabatan6" value="red" onClick="red('7')" required></td>
  </tr>
</table>
<script>
  function green(i) {}

  function red(i) {}
</script>

这种方法的优点是,无论小数点前后有多少个字符,它都能正常工作。

答案 1 :(得分:0)

我会做这样的事情:

double x;
std::string temp = "";
std::string variableName = "4 5 7 2.5";
std::string::size_type sz;     // alias of size_t
for (int i = 0; i < variableName.length(); i++) // iterates through variableName
{
    if (variableName[i] == '.') {
        temp += variableName[i - 1];
        temp += variableName[i];
        temp += variableName[i + 1];
        x = stod(temp, &sz);
    }
}
return x;