当前位置: 代码迷 >> C语言 >> 有关SCANF给DOUBLE数据赋值的问题
  详细解决方案

有关SCANF给DOUBLE数据赋值的问题

热度:292   发布时间:2008-05-04 20:01:16.0
有关SCANF给DOUBLE数据赋值的问题
想通过C语言来实现两个复变函数的加减运算,当全部进行INT数据操作的时候程序运转正常,但是将INT改为DOUBLE后,则结果总是为乱数。
程序如下: #include<stdio.h>
#include<math.h>
typedef struct{
  double real;
  double image;
  }complex_t;
main()
{
void printf_complex(complex_t a);
int  scanf_complex(complex_t *a);
complex_t add_complex(complex_t a,complex_t b);
complex_t sub_complex(complex_t a,complex_t b);
complex_t com1,com2;char c;
printf("please enter your complex\n");
printf("separated by space> ");
scanf_complex(&com1);
printf("the second please ");
scanf_complex(&com2) ;
printf("\n\n");
printf("(");
printf_complex(com1);
printf(")");
printf("+");
printf("(");
printf_complex(com2);
printf(")");
printf("=");
printf_complex(add_complex(com1,com2));
printf("\n\n");
printf("(");
printf_complex(com1);
printf(")");
printf("-");
printf("(");
printf_complex(com2);
printf(")");
printf("=");
printf_complex(sub_complex(com1,com2));
c=getchar();getchar();
}
void scanf_complex(complex_t *a)
{
scanf("%f%f",&a->real,&a->image);问题应该在这里,通过SCANF赋值给DOUBLE出现的问题,但我不确定也解决不好,请高手指教,谢谢
return;
}
void printf_complex(complex_t a)
{
char sign;
if(fabs(a.real)<0.05&&fabs(a.image)<0.05)
   printf("0");
else if(fabs(a.real)<0.05)
        printf("%.2fi",a.image);
      else if(fabs(a.image)<0.05)
             printf("%.2f",a.real);
           else
            if(a.image>0)
            {
            sign='+';printf("%.2f%c%.2fi",a.real,sign,a.image);
            }
            else
            {
            sign='-';printf("%.2f%c%.2fi",a.real,sign,fabs(a.image));
            }
return;
}
complex_t add_complex(complex_t a,complex_t b)
{
  complex_t sum;
  sum.real=a.real+b.real;
  sum.image=a.image+b.image;
  return(sum);
}
complex_t sub_complex(complex_t a,complex_t b)
{
complex_t diff;
diff.real=a.real-b.real;
diff.image=a.image-b.image;
return(diff);
}
搜索更多相关的解决方案: complex  DOUBLE  赋值  SCANF  数据  

----------------解决方案--------------------------------------------------------
scanf("%lf"试试看?
----------------解决方案--------------------------------------------------------
谢谢!
成功测试了,非常感谢楼主。我会常来的
----------------解决方案--------------------------------------------------------
  相关解决方案