Как реализовать расчет падения напряжения? - PullRequest
0 голосов
/ 17 июня 2019

Я довольно новичок в этом, поэтому постараюсь делать заметки. Заранее спасибо. Напишите программу для расчета параллельных и последовательных цепей для цепи до 5 резисторов. Программа должна: Подскажите по количеству резисторов в цепи Предложите пользователю выбрать серию или параллель Для последовательной цепи на выходе будет полное сопротивление и падение напряжения на каждом резисторе. Для параллельной цепи выходом будет полное сопротивление и ток через каждый резистор.

series circuit calculation:
total resistance= r1 +r2+rn
I=v/r_total
voltage drop in r1 = r1*I and so on until r5.
i'm trying to do the calculations for voltage drop using an array but its not working and i'm not sure why.

i'm getting an error and i'm not sure why



    #include <iostream>
    #include <string>
    #include <cmath>

    using namespace std;

    string circuitType(string x)
    {
        string P, S; //Parallel circuit (p) and series circuit (s)

        while (x!="P" && x!="S")
        {

        if (x=="P")
            {
            cout<<"     Parallel Circuit Calculator\n\n";
            }
        else if(x=="S")
            {
            cout<<"     Series Circuit Calculator\n\n";
            }
        else 
            {
            cout << "Please Enter the type of circuit (P/S):";
            cin >>x;
            }
        }
        return(x);
    }

    int main()
    {

        string userChoice = "Yes";
        string userans ;
        string P, S; //Parallel circuit (p) and series circuit (s)

        while (userChoice == "Yes")
        {
        double R [5];//[5] = {1, 2,3,4,5};//an array of Resistors 1 to 5

        double V, I; //Voltage and Current

        double e_R= 0.0; //Equivalent Resistance

        double r_T= 0.0; //Total resistance

        double v_D= 0.0; //Voltage drop

        cout <<"                Welcome to Circuit Calculator \n";
        cout <<"\n";

        cout <<"\n (Main) Please Enter the type of circuit (P/S):";
        cin >>userans;
        userans = circuitType(userans);

    if (userans == "S")
        {

    // a fuction for the prompt the user to enter resistance values

        cout<<"Enter the values for Resistors 1 to 5: \n";
        for(int i=0; i<=5;i++) 

    //implementing that i has a value more than 0and less than or equal                     
 to 5

            {
           cout<<"Enter R"<<i+1<<" = ";//output of Resistance values 
           from 1 to 5
           cin>>R[i];   
            }       
    //calculating Equivalent Resistance

       for (int i=0; i<=5;i++)
       r_T+= R[i];
       cout<< "The Total Resistance is "<<r_T<<"ohms.\n"; 

    // prompting the user to enter voltage value

        cout<< "enter V: ";
        cin >> V;

    //calculating current

        I = V/(r_T);
        cout<<"\n current = "<<I;

    // voltage drop at Resistors.

        for (int i=0; i<=5;i++) 

    //error 1 [Error] invalid types 'double[int]' for array subscript

        v_D[i] *= (R[i],I);

//  2[Error] name lookup of 'i' changed for ISO 'for' scoping [- 
    fpermissive]
//  3[Note] (if you use '-fpermissive' G++ will accept your code)
//  4[Error] invalid types 'double[int]' for array subscript

    cout << "The voltage drop at R"<<i <<"=" <<v_D[i]++<<"V\n";

        }

    //if resistance connected in parallel       
    //calculating equivalent resistance
    //eR=1/resistance;
    //printing current accross all resistance
    //I=voltage/resistance;

    //when circuit is connected in series   

        cout <<"Do you want to run calculator again?(Yes/No)\n";
        cin >>userChoice;
        cout <<"\n\n";
    }
        return 0;
    }

1 Ответ

0 голосов
/ 17 июня 2019

Я полагаю, вы забыли объявить v_D как массив.

Вы объявляете переменную как double (не массив): double v_D= 0.0;

Тем не менее, в коде вы рассматриваете его как массив: v_D[i] и v_D[i]++.

Для просмотра, к объявлению может понадобиться:

static const unsigned int MAX_RESISTORS = 5;
double Resistors[MAX_RESISTORS];
double voltage_drops[MAX_RESISTORS];

Я рекомендую отойти от вашего кода на некоторое время, а затем вернуться для объективной проверки. : -)

...