Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 并联电路_C++_Visual Studio 2010 - Fatal编程技术网

C++ 并联电路

C++ 并联电路,c++,visual-studio-2010,C++,Visual Studio 2010,我在这个编程任务上遇到了问题。我需要计算并联和串联电路的总电阻值。我有串联电路的功能,但我的问题是,当我试图计算并联电路的总电阻时,我的返回值是1。#INF。有什么建议可以解决这个问题吗 // project.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <mat

我在这个编程任务上遇到了问题。我需要计算并联和串联电路的总电阻值。我有串联电路的功能,但我的问题是,当我试图计算并联电路的总电阻时,我的返回值是1。#INF。有什么建议可以解决这个问题吗

    // project.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <math.h>

    using namespace std;

    void menu()
    {
       cout <<"\t\t\tLab 2 Menu Driven Algorithms" << endl;
       cout <<"\t\t  Calculating Parallel and Series Resistance" << endl;
       cout <<"1)\tSeries" << endl;
       cout <<"2)\tParallel" << endl;
       cout <<"3)\tQuit" << endl;   
    }

    int series(int& num, int& sum)
    {
int answer;
num = 0;
sum = 0;

do
{
    cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
    cin >> answer;
    cout << endl;

    sum = sum + answer;

    num++;
}
while(answer != 0);     

return sum;
    }

    double parallel (int& num, double& sum)
    {
double answer;
num = 0;
sum = 0.0;
int counter = 0;

do
{
    cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
    cin >> answer;
    cout << endl;
    counter++;

    sum = (1/answer) + sum;
    cout << sum << endl;

    num++;
}
while(answer != 0);

return sum;
    }




    int main()
    {
char choice;
int num = 0;
int sum = 0;
double sum2 = 0.0;

menu();

cout <<"\n\nPlease enter a value from the menu: ";
cin >> choice;
cout << endl;

while (choice != '3' && choice != 'q' && choice != 'Q')
{


switch(choice)
{
    case '1': cout << "Calculating Series Resistance" << endl;
              cout << "The series resistance for the " << num-1 << " resistors is: " << series(num, sum) << " Ohms\n";
              system("pause");
              break;
    case '2': cout << "Calculating Parallel Resistance" << endl;
              cout << "The parallel resistance for the " << num-1 << " resistors is: " << parallel(num, sum2) << " Ohms\n";
              system("pause");
              break;
    default: break;

}

system("cls");

menu();

cout <<"\n\nPlease enter a value from the menu: ";
cin >> choice;
cout << endl;


} 
system("pause");

return 0;
    }
//project.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
无效菜单()
{

cout您可能希望以这种方式重写
parallel()
函数中的循环,这样您就永远不会处理
0
的值(在这种情况下,它会导致被零除):


cout你期望什么?当你以
answer
的形式输入
0
,然后将
1/answer
添加到
sum
,当然它将是无穷大的。请去掉。我几乎在我的一台机器上运行了你的程序来测试它,而那台机器上的
pause
命令会暂停我核反应堆的冷却系统tor.@DavidSchwartz噢,糟糕,对我来说太晚了。太感谢了……我完全忽略了这个等式。也许下次你可以在代码格式方面做得更好一点?+1-我想你还是应该保留原来的公式:)。@us2012:Whoops。我从错误的函数复制了它:-)修复了,谢谢。
cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
cin >> answer;
cout << endl;

while (answer != 0);     
{
    counter++; // NOTICE: This is never used for computation...

    sum = (1/answer) + sum;
    cout << sum << endl;

    num++;

    cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
    cin >> answer;
    cout << endl;
}