Lösningsförslag till datorövning 4 i Programmeringsmetodik /* Uppg4a.c */ /* Impedans.c */ #include "komplex.h" int main() { komplex z1, z2, z; las_komplex(&z1); las_komplex(&z2); /* parallellkoppling, produkten dividerat med summan */ z = div(mul(z1, z2), add(z1, z2)); skriv_komplex(z); return 0; } /* Komplex.h */ typedef struct { float re, im ; } komplex; void las_komplex(komplex *kp); void skriv_komplex(komplex k); komplex add(komplex k1, komplex k2); komplex sub(komplex k1, komplex k2); komplex mul(komplex k1, komplex k2); komplex div(komplex k1, komplex k2); /* Komplex.c */ #include "komplex.h" #include void las_komplex(komplex *kp) { printf("Komplext tal på formen 2.5+3.1j : "); scanf("%f%fj", &kp->re, &kp->im); } void skriv_komplex(komplex k) { printf("Komplexa talet : %.2f%+.2fj", k.re, k.im); } komplex add(komplex k1, komplex k2) { komplex k; k.re = k1.re + k2.re; k.im = k1.im + k2.im; return k; } komplex sub(komplex k1, komplex k2) { komplex k; k.re = k1.re - k2.re; k.im = k1.im - k2.im; return k; } komplex mul(komplex k1, komplex k2) { komplex k; k.re = k1.re*k2.re - k1.im*k2.im; k.im = k1.re*k2.im + k1.im*k2.re; return k; } komplex div(komplex k1, komplex k2) { komplex k; float n = k2.re*k2.re + k2.im*k2.im; k.re = (k1.re*k2.re + k1.im*k2.im) / n; k.im = (k1.im*k2.re - k1.re*k2.im) / n; return k; } /* Uppg4b.c */ /* Stackmai.c */ #include #include "vekstack.h" int main() { stack vs; char text[81]; int i; printf("Skriv text : "); gets(text); /* stacka tecken för tecken */ clear(&vs); for (i = 0; text[i] != '\0'; i++) push(&vs, text[i]); /* töm stacken och skriv ut */ while (!is_empty(vs)) putchar(pop(&vs)); putchar('\n'); return 0; } /* Vekstack.h */ typedef struct { int top; char vek[100]; } stack; void clear(stack *sp); void push(stack *sp, char ch); char pop(stack *sp); int is_empty(stack s); int is_full(stack s); /* Vekstack.c */ #include "vekstack.h" void clear(stack *sp) { sp->top = -1; } void push(stack *sp, char ch) { sp->top++; sp->vek[sp->top] = ch; } char pop(stack *sp) { return sp->vek[sp->top--]; } int is_empty(stack s) { return s.top == -1; } int is_full(stack s) { return s.top == 99; } /* Uppg4c.c */ /* Stackmai.c */ #include #include "vekstack.h" int main() { stack vs; komplex z; /* stacka komplexa tal */ clear(&vs); printf("Avsluta med talet 0+0j!\n"); las_komplex(&z); while (!(z.re == 0.0 && z.im == 0.0)) { push(&vs, z); las_komplex(&z); } /* töm stacken och skriv ut */ while (!is_empty(vs)) skriv_komplex(pop(&vs)); return 0; } /* Vekstack.h */ #include "komplex.h" typedef komplex datatyp; typedef struct { int top; datatyp vek[100]; } stack; void clear(stack *sp); void push(stack *sp, datatyp data); datatyp pop(stack *sp); int is_empty(stack s); int is_full(stack s); /* Vekstack.c */ #include "vekstack.h" void clear(stack *sp) { sp->top = -1; } void push(stack *sp, datatyp data) { sp->top++; sp->vek[sp->top] = data; } datatyp pop(stack *sp) { return sp->vek[sp->top--]; } int is_empty(stack s) { return s.top == -1; } int is_full(stack s) { return s.top == 99; }