Code:
#include <stdio.h>
void instructions (void); //print intructions
struct resist{char relate; double value;}add; //structure for additional resistors
void draw_v (double v); //function for voltage line
void draw_lines(struct resist add, int n); //lines for additional resistors
void initial(double r1);
//calc functions
double totr (struct resist add, int n, double r1);
double toti (double r, double v);
double power (double i, double v);
void display (double r, double i, double p);//display funtion
int
main (void)
{
int n; //number of resistors
char decide; //check printed curcuit
double v; //voltage
double r1; //initial resistor
instructions ();
printf ("What is your source voltage? \n");
scanf ("%lf.3", &v);
while (v <1){
printf ("Try again... \n");
scanf ("%lf.3", &v);
}
printf ("How many resistors are in your circuit? \n");
scanf ("%d", &n);
while (n < 1) {
printf ("Entry invalid, please try again... \n");
scanf ("%d", &n);
}
printf ("What is the value of the initial resistor? \n");
scanf ("%lf", &r1);
while ( r1 < 0) {
printf ("What is the value of the initial resistor, now? \n");
scanf ("%lf", &r1);
}
double more[n-1]; //array variable in struct
char and [n-1]; //array variable in struct
int i; //initiator variable
do {
printf ("What is the value of your next resistor? \n");
scanf("%lf", more[i]);
add.value = more[i];
printf ("And what is this resistor's relationship to the previous resistor? %.3f\n");
scanf ("%c", and[i]);
add.relate = and[n-1];}
while (i = n -1, i < n && i > 0, i -- );
draw_v (v);
draw_lines(add, n);
initial (r1);
printf ("\n Is this the circuit you would like solved? %c \n", decide);
scanf ("%c", &decide);
while (decide != 'y' || 'n'){
printf ("Please answer y for yes or n for no... %c \n", decide);
scanf ("%c", &decide);
}
if (decide == 'n')
printf ("Sorry, please reload program.");
else
printf ("Your circuit calculations are....\n");
totr(add, n, r1);
//values not passed from main
toti;
power;
display;
return (0); //main/////////////////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////////////////////////////////
void instructions (void)
{
printf ("This program will only work if you can make one continuous line through your circuit. \n");
printf ("This program will only work if your circuit has only one voltage source. \n");
printf ("This program requires lower case letters for verifiers. \n");
printf ("Enter the value of source voltage. \n");
printf ("Name the number of resistors. Then, starting with the outer most resistor, give the values of each resistor. \n");
printf ("For every additional resistor also give it's relation, series - (s) or parallel - (p), to the previous resistor. \n");
printf ("Next the program will print a simplyfied image of the circuit based on the resistor values that were entered. \n");
printf ("If the circuit is not to your liking type 'n' and start over. \n");
printf ("If the circuit meets your approval type 'y' to recieve your calculations. \n");
printf ("The program will calculate your total resistance, total current, and total power.\n");
}
//////////////////////////////////////////////////////////////////////////////////////
void draw_v ( double v)
{
printf ("*");
printf ("*");
printf ("*");
printf ("*");
printf("%lf ",&v);
printf ("*");
printf ("*");
printf ("*");
printf ("*");
}
////////////////////////////////////////////////////////////////////////////
void draw_lines(struct resist add, int n)
{
if (add.relate == 's' )
printf("***********%lf ***********",add .value );
else if (add.relate == 'p')
printf("**************************");
printf("* ");
printf("* ");
printf("* ");
printf("%lf ",add. value);
printf("* ");
printf("* ");
printf("* ");
printf("* ");
}
void initial(double r1)
{
printf ("*");
printf ("*");
printf ("*");
printf ("*");
printf("%lf", &r1);
printf ("*");
printf ("*");
printf ("*");
printf ("*");
}
double totr (struct resist add, int n, double r1)
{
double r;
int x;
if (add.relate == 'p')
r = (add.value*r1)/(add.value+r1);
else
r = add.value+r1;
if (add.relate == 'p') {
do ( r=(add.value*r)/(add.value+r));
while (x = n-2, x > 0, --x);
}
else {
do (r=add.value+r);
while (x = n-2, x > 0, --x);
}
return (r);
}
double toti (double v,double r)
{
double i;
i = v/r;
return (i);
}
double power (double i,double v)
{
double p;
p = i*v;
return (p);
}
void display (double r,double i,double p)
{
printf("The total resistance is .3%lf ohms", r);
printf("The total current is .3%lf amperes", i);
printf("The total power is .3%lf watts", p);
}