2012-11-19 2 views
-1

사용자로부터 입력을 읽고이를 구조에 저장하는 함수를 작성해야합니다. c.구조 함수

typedef struct { 
    float real, imag; 
} complex_t; 


complex_t read_complex(void) 
{ 

} 

어떻게 입력을 스캔해야합니까?

+6

서식있는 코드. 그러나 무엇을 시도 했습니까? –

답변

0
#include <stdio.h> 

typedef struct 
{ 
    int re;// real part 
    int im;//imaginary part 
} complex; 

void add(complex a, complex b, complex * c) 
{ 
    c->re = a.re + b.re; 
    c->im = a.im + b.im; 
} 

void multiply(complex a, complex b, complex * c) 
{ 
    c->re = a.re * b.re - a.im * b.im; 
    c->im = a.re * b.im + a.im * b.re; 
} 

main() 
{ 
    complex x, y, z; 
    char Opr[2]; 

    printf(" Input first operand. \n"); 
    scanf("%d %d", &x.re, &x.im); 
    printf(" Input second operand. \n"); 
    scanf("%d %d", &y.re, &y.im); 
    printf(" Select operator.(+/x) \n"); 
    scanf("%1s", Opr); 

    switch(Opr[0]){ 
    case '+': 
      add(x, y, &z); 
      break; 
    case 'x': 
      multiply(x, y, &z); 
      break; 
    default: 
     printf("Bad operator selection.\n"); 
     break; 
    } 
    printf("[%d + (%d)i]", x.re,x.im); 
    printf(" %s ", Opr); 
    printf("[%d + (%d)i] = ", y.re, y.im); 
    printf("%d +(%d)i\n", z.re, z.im); 
} 
0

쉽게 할 수 있습니다. 함수 서명에 대해 언급 했으므로 다음을 시도하십시오.

complex_t read_complex(void) 
{ 
    complex_t c; 
    float a,b; 
    printf("Enter real and imaginary values of complex :"); 
    scanf("%f",&a); 
    scanf("%f",&b); 
    c.real=a; 
    c.imag=b; 
    return c; 
} 

int main() 
{ 
complex_t cobj; 
cobj=read_complex(void); 
printf("real : %f imag : %f",cobj.real,cobj.imag); 
return 0; 
} 
+0

내가 이걸 갖기를 바랍니다. – Omkant