我写的一个C语言作业 欢迎各位批评。。
编写程序读入两个复数并输出这两个复数的乘积。输入输出如下图。直接放代码啦
程序代码:
#include<stdio.h> #include<math.h> int main(void) { /*init 0.0*/ double cpl1_real = 0.0; double cpl1_virtl = 0.0; double cpl2_real = 0.0; double cpl2_virtl = 0.0; double realpart = 0.0; double virtualpart = 0.0; char *szRPart = "\tReal part:"; char *szVPart = "\tVirtual part:"; char *szYouInput = "You Input:"; printf("\t\t********** Complexs Product **********\n"); printf("Please input the first complex:\n"); printf(szRPart); /*must be %lf beacuse we use double*/ scanf("%lf",&cpl1_real); printf(szVPart); scanf("%lf",&cpl1_virtl); printf(szYouInput); /*diff format*/ if (fabs(cpl1_virtl - 0.0) < 0.0000001) /*compare with a small num*/ { printf("%.2f%.2fi",cpl1_real,cpl1_virtl); } else { printf("%.2f+%.2fi",cpl1_real,cpl1_virtl); } printf("\nPlease input the second complex:\n"); printf(szRPart); scanf("%lf",&cpl2_real); printf(szVPart); scanf("%lf",&cpl2_virtl); printf(szYouInput); if (fabs(cpl2_virtl - 0.0) < 0.0000001) { printf("%.2f%.2fi",cpl2_real,cpl2_virtl); } else { printf("%.2f+%.2fi",cpl2_real,cpl2_virtl); } realpart = cpl1_real*cpl2_real - cpl1_virtl*cpl2_virtl; virtualpart = cpl1_real*cpl2_virtl + cpl1_virtl*cpl2_real; printf("\nThe product is "); if (fabs(virtualpart - 0.0) < 0.0000001) { printf("%.3f%.3fi",realpart,virtualpart); } else { printf("%.3f+%.3fi",realpart,virtualpart); } return 0; }
[ 本帖最后由 zklhp 于 2011-3-28 19:13 编辑 ]