Multiple Questions, Stuck Here
15 hours ago, dhannemon13 said:<snip>
First, research how the compilation and linking process works. Once you've done that you can put yourself in the compiler's shoes. Each source file is compiled separately. So when main.c is compiled, it includes complex.h and the function prototypes therein. At no point in time will the compiler see the contents of complex.c while it's compiling main.c. Thus, you should define struct `complex` in the header file complex.h, so the compiler can see it when it's compiling main.c. There is also no need for the `input` char array to be in the struct.
Furthermore, what is this doing in complex.h ?
mag(complex a); phase(complex a);
First of all, they don't have a return type, your compiler should warn for that. In modern C, the canonical way to write things is to explicitly write `void` as a return type when a function returns nothing. In this case, there are no functions `mag` and `phase` in complex.c so remove these prototypes to non-existing functions.
Then, in complex.c:
struct complex a,b,c;
When you've typedef'd the struct there's no need for the struct keyword. These global variables are also never used and should be removed.
complex getcomplex(char *input){ char input[20]; complex a,b; scanf("%s",&input); sscanf(input, "%lf+%lf", &real, &img); }
The function `getcomplex` takes a string (pointer to char) as argument, presumably the string to be parsed? Why do you declare a new array, also called input and read user input with scanf? Also, there's only a single complex number to be parsed, so no need for a `a` and a `b`. You should explicitly point sscanf to the struct members. And you have to actually return the parsed complex.
complex sum(double real, double img){ complex c; c.real = a.real + b.real; c.img = a.img + b.img; printf("Hasil operasi penjumlahan bilangan kompleks = %d + %di", c.real, c.img); return (c); }
This function is supposed to sum 2 complex numbers `a` and `b`. Why does it take 2 doubles `real` and `im` as arguments?
The printf specifier to print doubles is `%lf`, not `%d`.
Similar problems plague the other functions in complex.c
In main.c
char ch,p,q,r; p = argv[1]; q = argv[2]; r = argv[3];
`argv` is a array of pointers to char, not char.
switch(ch){ case 'sum': sum(getcomplex(q),getcomplex(r)); break; case 'substract': substract(getcomplexA(q),getcomplexB(r)); break; case 'multiply': multiply(getcomplexA(q),getcomplexB(r)); break; case 'divide': divide(getcomplexA(q),getcomplexB(r)); break; }
You cannot switch on an entire string. Comparing strings has to be done with a function like `strcmp`. Also, strings require double quotes.
Final modified program, complex.h:
#ifndef COMPLEX_H_INCLUDED #define COMPLEX_H_INCLUDED typedef struct { double real; double img; } complex; complex getcomplex(const char *input); complex sum(complex a, complex b); complex subtract(complex a, complex b); complex multiply(complex a, complex b); #endif // COMPLEX_H_INCLUDED
complex.c:
#include <stdio.h> #include "complex.h" complex getcomplex(const char *input) { complex a; sscanf(input, "%lf+%lf", &a.real, &a.img); return a; } complex sum(complex a, complex b) { complex c; c.real = a.real + b.real; c.img = a.img + b.img; printf("Hasil operasi penjumlahan bilangan kompleks = %lf + %lfi\n", c.real, c.img); return c; } complex subtract(complex a, complex b) { complex c; c.real = a.real - b.real; c.img = a.img - b.img; printf("Hasil operasi pengurangan bilangan kompleks = %lf - %lfi\n", c.real, c.img); return c; } complex multiply(complex a, complex b) { complex c; c.real = a.real*b.real - a.img*b.img; c.img = a.img*b.real + a.real*b.img; printf("Hasil operasi perkalian bilangan kompleks = %lf + %lfi\n", c.real, c.img); return c; }
main.c:
#include <stdio.h> #include <string.h> #include "complex.h" int main(int argc, char *argv[]) { if (argc < 4 || argc > 5) { printf("Tolong masukan minimal 4 argumen. //ex. namafile arg1 arg2\n"); return 0; } const char operator = argv[1][0]; const char* q = argv[2]; const char* r = argv[3]; switch(operator) { case '+': sum(getcomplex(q),getcomplex(r)); break; case '-': subtract(getcomplex(q),getcomplex(r)); break; case 'x': multiply(getcomplex(q),getcomplex(r)); break; } }
The `divide` function was removed and implementing it is left as an exercise.
Example command line:
./test_complex + 5+3i 7+2i
output:
Hasil operasi penjumlahan bilangan kompleks = 12.000000 + 5.000000i
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now