如何将数组中的数据用于数学方程和终端显示?C++

ghhaqwfi  于 2023-01-22  发布在  其他
关注(0)|答案(3)|浏览(119)

C++完全初学者
在其他地方使用原始数组中的数据时遇到了麻烦。我可以在数学和显示输出行中把它们当作整数来调用吗?制作一个简单的华氏到摄氏的转换程序。

#include <iostream>
#include <array> 

int main() {
    //Display name of program 
    std::cout << "Farenheit to Celsius Converter"; 
    
    //Temperature array
    int temp[] = {51, 42, 46, 36, 46, 33, 47, 34, 48, 36, 46, 34, 45, 36};
    
    //Math stuff
    int temp1 = temp[1] - 32 * .5556; 
    int temp2 = temp[2] - 32 * .5556;
    int temp3 = temp[3] - 32 * .5556;
    int temp4 = temp[4] - 32 * .5556;
    int temp5 = temp[5] - 32 * .5556;
    int temp6 = temp[6] - 32 * .5556;
    int temp7 = temp[7] - 32 * .5556;
    int temp8 = temp[8] - 32 * .5556;
    int temp9 = temp[9] - 32 * .5556;
    int temp10 = temp[10] - 32 * .5556;
    int temp11 = temp[11] - 32 * .5556;
    int temp12 = temp[12] - 32 * .5556;
    int temp13 = temp[13] - 32 * .5556;
    int temp14 = temp[14] - 32 * .5556;
    
    //Display output
    std::cout << temp1"," << temp2"," << temp3"," << temp4"," << temp5"," << temp6"," << temp7"," << temp8"," << temp9"," << temp10"," << temp11"," << temp12"," << temp13"," << temp14","; 
    return 0;
}
flvlnr44

flvlnr441#

我建议您使用标准的std::transform算法来转换数组,然后创建一个函数或函子(如lambda)将华氏温度转换为摄氏温度,并将其提供给std::transform
示例:

#include <algorithm> // std::transform
#include <iostream>
#include <iterator>  // std::begin, std::end

int main() {
    int temp[] = {51, 42, 46, 36, 46, 33, 47, 34, 48, 36, 46, 34, 45, 36};

    // a lambda for the conversion:
    auto F2C = [](int F) { return (F - 32) * 5 / 9; };

    std::transform(std::begin(temp), std::end(temp), std::begin(temp), F2C);

    for (int v : temp) std::cout << v << '\n';
}

Demo

iqih9akk

iqih9akk2#

在C++中,使用vector表示大小可变的数组。
要获得良好的转换,请使用浮点数。
重复计算应该是函数。
尽可能使用基于范围的for循环,以避免索引问题

#include <iostream>
#include <array> 
#include <vector>
#include <algorithm>

double fahrenheid_to_celcius(const double t)
{
    return (t - 32.0) * .55556;
}

int main()
{
    //Display name of program 
    std::cout << "Farenheit to Celsius Converter\n";

    //Temperature array
    std::vector<double> temperatures{ 51.0, 42.0, 46.0, 36.0, 46.0, 33.0, 47.0, 34.0, 48.0, 36.0, 46.0, 34.0, 45.0, 36.0 };

    // convert temperatures one by one (using a range based for loop)
    // double& is a reference to one of the temperatures in the vector, so the value can be changed within the loop
    for (double& temperature : temperatures)
    {
        temperature = fahrenheid_to_celcius(temperature);
    }

    bool comma = false;
    for (const double& temperature : temperatures)
    {
        if (comma) std::cout << ", ";
        std::cout << temperature;
        comma = true;
    }

    return 0;
}
htzpubme

htzpubme3#

首先:我建议使用std::数组,而不是C数组:

const size_t tempValues = 14;
std::array<int, tempValues> temp = {51, 42, 46, 36, 46, 33, 47,
                                    34, 48, 36, 46, 34, 45, 36};

第二,你可以通过for循环直接对值进行操作,而不必写14个变量:

for (size_t i = 0; i < tempValues; i++) {
  temp[i] = (temp[i]-32) * .5556;
}

打印到输出的过程类似:

for (size_t i = 0; i < tempValues - 1; i++) {
  std::cout << temp[i] << ", ";
}
std::cout << temp[tempValues - 1] << std::endl;

请注意,循环以倒数第二个值结束,因为我们不需要为最后一个值使用逗号分隔符。
这不是最优雅的方式,但可能是最适合初学者的方式。

相关问题