xcode 我试图创建一个程序,输出到屏幕和文件,但当输出到屏幕我一直得到一个(-)数字,而不是十进制?[关闭]

hmtdttj4  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(106)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
3天前关闭。
Improve this question
我如何让这个程序从文本文件中读取值,并将值的总和除以1,当值为:100 200 500 10000 330 330?

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
    ifstream in_file;
    double resistor1, resistor2, resistor3, resistor4, resistor5, resistor6;
    double resistors_in_series;
    int resistors_in_parallel;
    
    in_file>> resistor1>>resistor2>>resistor3>>resistor4>>resistor5>>resistor6;
    resistors_in_parallel = 1.0/(resistor1+resistor2+resistor3+resistor4+resistor5+resistor6);
    cout << "The total equivalent resistance is " << fixed <<setprecision(1) << resistors_in_parallel << " ohms if resistors are connected in parallel." << endl;
    ofstream out_file;
        
    in_file.open("resistors.txt");
    out_file.open("total_resistance.txt");
    
  
    in_file.close();
    out_file.close();
}
dly7yett

dly7yett1#

您没有创建任何函数,只是向main添加了几行代码。创建函数和数据结构对于保持代码的可读性和可维护性非常重要。
对于SO来说,代码编译和任何文件I/O模拟也很重要,这样我们就可以在我们最喜欢的(在线)编译器中测试代码。如本例所示:https://onlinegdb.com/_KJLAq6mK

#include <iostream>
#include <fstream>
#include <numeric> // for accumulate
#include <sstream>
#include <vector>  // for vector a dynamically resizable array

// using namespace std; <== no don't use this.

// simulate an input file
// by providing a load function that accepts an std::istream&
// you can use both a real input file or any other input stream
// to load from.
std::istringstream simulated_file
{
    "4\n"
    "100.0\n"
    "2000.0\n"
    "3500.0\n"
    "10000.0\n"
};

// when you have data that is related 
// like it needs to be read from file
// declare a class or struct;
struct Circuit
{
    // make small functions
    double average_resistance()
    {
        if (resistances.empty()) return 0.0;
        auto total_resistance = std::accumulate(resistances.begin(), resistances.end(), 0.0);
        return total_resistance / static_cast<double>(resistances.size());
    }

    // when you have more then 1 thing it is an array or vector
    //double resistor1, resistor2, resistor3, resistor4, resistor5, resistor6;
    std::vector<double> resistances;
};

Circuit load(std::istream& is)
{
    Circuit circuit{};
    std::size_t number_of_resistors;
    double resistance;

    is >> number_of_resistors;
    for (std::size_t n{0ul}; n < number_of_resistors; ++n)
    {
        is >> resistance;
        circuit.resistances.push_back(resistance);
    }

    return circuit;
}

int main() 
{
    //int resistors_in_parallel; should have been a double
    // by making functions your code becomes a lot more readable
    auto circuit = load(simulated_file);

    std::cout << std::format("The resistance is {} ohms when connected in parallel.", circuit.average_resistance());

    return 0;
}

相关问题