-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
137 lines (123 loc) · 3.09 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
typedef struct resistor{
float value;
struct resistor *next;
} resistor;
resistor* head = NULL;
resistor *add = NULL;
void addResistor(float data) {
resistor* temp;
temp = (resistor*)malloc(sizeof(resistor));
if (temp == NULL) {
fprintf(stderr, "Hafiza dolu!\n");
return;
}
temp->value = data;
temp->next = NULL;
if (head == NULL) {
head = temp;
add = head;
}
else {
add->next = temp;
add = add->next;
}
}
int countResistors(){
int counter=0;
resistor *p;
p=head;
while(p!=NULL){
counter++;
p=p->next;
}
return counter;
}
float calculateEquivalentResistor(resistor *p,int countedResistors,float equivalentResistance,int processCode){
if(processCode==100){
p=head;
countedResistors= countResistors();
if(countedResistors%2==0){
equivalentResistance = p->value + p->next->value;
}
else if(countedResistors==1){
return p->value;
}
else if(countedResistors%2==1){
equivalentResistance = (p->value * p->next->value)/(p->value + p->next->value);
}
countedResistors -=1;
p=p->next->next;
calculateEquivalentResistor(p,countedResistors,equivalentResistance,101);
}
else if(processCode==101){
if(p==NULL){
return equivalentResistance;
}
else{
if(countedResistors%2==0){
equivalentResistance +=p->value;
}
else{
equivalentResistance = (equivalentResistance * p->value)/(equivalentResistance + p->value);
}
countedResistors -=1;
p=p->next;
calculateEquivalentResistor(p,countedResistors,equivalentResistance,101);
}
}
}
void voltageAcrossResistor(resistor *p, float current, int processCode){
float nowResistor,nowEquivalentResistance,nowCurrent;
if(p->next == NULL){
printf("%.2f R uzerine dusen gerilim = %.2f V\n",p->value,p->value*current);
return;
}
if(processCode==100){
resistor *q=head;
p=p->next;
while(p->next !=NULL){
p=p->next;
q=q->next;
}
nowResistor = p->value;
free(p);
q->next=NULL;
printf("%.2f R uzerine dusen gerilim = %.2f V\n",nowResistor,nowResistor*current);
voltageAcrossResistor(head,current,101);
}
else if(processCode==101){
resistor *q=head;
p=p->next;
while(p->next !=NULL){
p=p->next;
q=q->next;
}
nowResistor = p->value;
free(p);
q->next =NULL;
nowEquivalentResistance=calculateEquivalentResistor(head,0,0,100);
nowCurrent= current* (nowEquivalentResistance / (nowEquivalentResistance + nowResistor));
printf("%.2f R uzerine dusen gerilim = %.2f V\n",nowResistor,nowResistor*nowCurrent);
voltageAcrossResistor(head,current-nowCurrent,100);
}
}
int main(){
float data;
float equivalentResistance,voltage,mainFlow;
printf("Gerilimi girin:");
scanf("%f",&voltage);
int counter=0;
printf("İlk direnci girin:");
scanf("%f",&data);
while(data !=0){
addResistor(data);
printf("%.2f, basariyla eklendi. Sonraki direnci girin:",data);
scanf("%f",&data);
}
equivalentResistance=calculateEquivalentResistor(head,0,0,100);
mainFlow = voltage / equivalentResistance;
printf("Esdeger Direnc= %.2f R\nAnakol Akimi = %.2f Amper\n",equivalentResistance, mainFlow);
voltageAcrossResistor(head,mainFlow,100);
}