0
好傢伙我有這樣的代碼:分配+零個
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1,y1,x2,y2,x3,y3;
float a,b,a2,b2,xf3,yf3,xa,ya;
printf("please find the line1 formula: y = ax+b\n");
printf("please input any point (x1,y1):\n");
printf("input x1:");
scanf("%d",&x1);
printf("input y1:");
scanf("%d",&y1);
printf("please input any point (x2,y2):\n");
printf("input x2:");
scanf("%d",&x2);
printf("input y2:");
scanf("%d",&y2);
a=(y2-y1)/(x2-x1);
b=y1-(a*x1);
b=(b);
printf("this formula: y = %fx %f\n\n",a,b);
printf("please input any point(x3,y3):\n");
printf("input x3:");
scanf("%d",&x3);
printf("input y3:");
scanf("%d",&y3);
xf3=x3;
yf3=y3;
a2 = -1/a;
b2=y3-(a2*x3);
xa=(b2-b)/(a-a2);
ya=a2*xa+b2;
printf("the line2 which contains (%.3f,%.3f) perpendicular to the line1.",xf3,yf3);
printf("please find the intersection point of line1 and line2: \n");
printf("(%f,%f) ",xa,ya);
return 0;
}
它的工作原理,但打印第一線時,我有一個問題。 我應該將其打印爲y=ax+b
。如果b
是正數或負數,那麼就沒有問題了。但是,如果b
爲零,則會打印(示例)y=2.000000x0.000000
我需要它爲y=2.000000x+0.000000
。
我該如何解決這個問題?
爲什麼?零不是正數也不是負數 –
只是不打印0 –
@EdHeal,因爲它是一個數學表達式。 「3 + 0」和「30」不是一回事。 – hobbs