#ifndef CIRCUIT_H
#define CIRCUIT_H
#include <iostream>
using namespace std;
class Resistor
{
public:
void setResistance(double);
double getResistance();
double GenerateRandomCircuit();
double calctotalResistance();
void GenerateQuiz();
int op[7],res[8];
private:
double r;
};
#endif // CIRCUIT_H
#include "Circuit.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
void Resistor::setResistance(double r1)
{
r=r1;
}
double Resistor::getResistance()
{
return r;
}
double Resistor::GenerateRandomCircuit()
{
int j=0,k=0,x;
double y,z;
srand(time(NULL));
int random=rand()%6+3;
for(int i=1;i<=random;i++)
{
x=1+rand()%9;
y=(1+rand()%9)*0.1;
z=x+y;
int sign=rand()%2+1;
if(sign==1)
{
op[x]='+';
cout<<"+";
}else if(sign==2)
{
op[x]='/';
cout<<"/";
}
cout<<fixed<<setprecision(1)<<z;
}
return z;
}
double Resistor::calctotalResistance()
{
int x=0,y=0;
double tempres[99]={};
double temptotal,total;
for(int i=0;i<4;i++)
{
if(op[x]=='/') //If operator of symbol "/", it will convert temporary resistor to 1/(value of resistor)
{
temptotal =((tempres[i]*tempres[i+1])/(tempres[i]+tempres[i+1]));
i++;
}else if(op[x]=='+')
{
total=tempres[x]+tempres[x+1];
}else if(i==3)
{
total=tempres[x]+tempres[x+1];
}
}
return total;
}
void Resistor::GenerateQuiz()
{
Resistor a;
int mark=0;
double answer,resistance;
string formula;
for(int i=1;i<=5;i++)
{
cout<<"Circuit "<<i<<" formula is "; a.GenerateRandomCircuit(); cout<<endl;
cout<<"Enter circuit "<<i<<" total resistance:";
cin>>answer;
resistance=calctotalResistance();
if(resistance==answer)
{
mark++;
}
}
cout<<endl;
cout<<"Your mark for this quiz is "<<mark<<"/5"<<endl;
}
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "Circuit.h"
using namespace std;
int main()
{
int choice,quiz;
string formula;
double formula1,totalres;
Resistor b;
cout <<"*** Welcome to Total Resistance Calculator Program ***"<<endl;
cout<<"1. Enter circuit formula."<<endl;
cout<<"2. Generate circuit randomly."<<endl;
cout<<"3. Show circuit formula."<<endl;
cout<<"4. Calculate total resistance."<<endl;
cout<<"5. Play a quiz."<<endl;
while(choice!=-1)
{
cout<<"Your choice [1-5] : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter circuit formula: ";
cin>>formula;
break;
case 2:
cout<<"Random circuit is generated."<<endl;
break;
case 3:
cout<<"Circuit formula is ";
b.GenerateRandomCircuit();
cout<<endl;
break;
case 4:
cout<<"Circuit total resistance is "<<setprecision(1)<<b.calctotalResistance()<<" ohm"<<endl;
break;
case 5:
cout<<"Quiz is selected."<<endl;
b.GenerateQuiz();
break;
}
}
return 0;
}
I had write the coding to calculate total resistance of circuit by using series and parallel formula, but it keeps prompt out total resistance is 0 ohm. Can anyone help me to solve it?