46 lines
1 KiB
C
46 lines
1 KiB
C
|
#include <stdio.h>
|
||
|
|
||
|
/**
|
||
|
* A Complex number consisting of real and imaginary part
|
||
|
*/
|
||
|
struct complex_number {
|
||
|
int real; /**< real part of complex number */
|
||
|
int imag; /**< imaginary part of complex number */
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* read a complex number from command line
|
||
|
*
|
||
|
* @returns the compex number from command line
|
||
|
*/
|
||
|
struct complex_number read_complex_number();
|
||
|
|
||
|
/**
|
||
|
* print a complex number to command line
|
||
|
*
|
||
|
* @param number the complex number to print
|
||
|
*/
|
||
|
void print_complex_number(struct complex_number number);
|
||
|
|
||
|
int main(void) {
|
||
|
struct complex_number number;
|
||
|
number = read_complex_number();
|
||
|
print_complex_number(number);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
struct complex_number read_complex_number() {
|
||
|
struct complex_number number;
|
||
|
printf("please input a complex number in form a+bi: ");
|
||
|
char buf[256] = {0};
|
||
|
fgets(buf, sizeof(buf) / sizeof(buf[0]), stdin);
|
||
|
sscanf(buf, "%d+%di", &number.real, &number.imag);
|
||
|
return number;
|
||
|
}
|
||
|
|
||
|
void print_complex_number(struct complex_number number) {
|
||
|
printf("%d+%di\n", number.real, number.imag);
|
||
|
}
|
||
|
|