这个代码怎么才能多个输入?
之前用scanf()!=-1就可以多个输入,这个怎么不行?只能输入一个样例。
题目描述
给出平行四边形中两条相邻边的端点的(x, y)坐标,请找到第四个点的(x, y)坐标。
输入输出格式
输入格式
输入的每行给出8个浮点数:首先,给出第一条边的一个端点和另一个端点的(x, y)坐标;然后,给出第二条边的一个端点和另一个端点的(x, y)坐标;所有的坐标均以米为单位,精确到毫米。所有的坐标的值在-10000和+10000之间。输入以EOF终止。
输出格式
对于每行输入,输出平行四边形的第四个点的(x, y)坐标,以米为单位,精确到毫米,用一个空格隔开x和y。
输入
1.000 0.000 3.500 3.500 3.500 3.500 0.000 1.000
1.866 0.000 3.127 3.543 3.127 3.543 1.412 3.145
输出
-2.500 -2.500
0.151 -0.398
#include <stdio.h>
#include <stdlib.h>
void swap(double *a,double *b,double *c,double *d);//交换函数
int main()
{
double x1=0,x2=0,x3=0,x4=0,x5=0,y1=0,y2=0,y3=0,y4=0,y5=0;
if(scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=-1)
{
//固定点位
if(x2==x3&&y2==y3)
{
swap(&x3,&x4,&y3,&y4);swap(&x1,&x2,&y1,&y2);
}
if(x2==x4&&y2==y4)
{
swap(&x1,&x2,&y1,&y2);
}
if(x1==x3&&y1==y3)
{
swap(&x3,&y4,&y3,&y4);
}
//计算第四个点
x5=x3+x2-x1;
y5=y3+y2-y1;
printf("%.3lf %.3lf\n",x5,y5);
}
return 0;
}
void swap(double *a,double *b,double *c,double *d)
{
double t=0,p=0;
t=*a;*a=*b;*b=t;
p=*c;*c=*d;*d=p;
}
#include <stdio.h>
#include <stdlib.h>
void swap(double *a, double *b, double *c, double *d); // 交换函数
int main()
{
double x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, y1 = 0, y2 = 0, y3 = 0, y4 = 0, y5 = 0;
while (scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) != EOF)
{
// 固定点位
if (x2 == x3 && y2 == y3)
{
swap(&x3, &x4, &y3, &y4);
swap(&x1, &x2, &y1, &y2);
}
if (x2 == x4 && y2 == y4)
{
swap(&x1, &x2, &y1, &y2);
}
if (x1 == x3 && y1 == y3)
{
swap(&x3, &y4, &y3, &y4);
}
// 计算第四个点
x5 = x3 + x2 - x1;
y5 = y3 + y2 - y1;
printf("%.3lf %.3lf\n", x5, y5);
}
return 0;
}
void swap(double *a, double *b, double *c, double *d)
{
double t = 0, p = 0;
t = *a;
*a = *b;
*b = t;
p = *c;
*c = *d;
*d = p;
}
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
【以下回答由 GPT 生成】
问题
需要一个C语言程序来计算平行四边形的第四个点,并支持多个输入样例。
问题描述
给定平行四边形的两个相邻边的端点坐标(x, y),需要找出第四个点的坐标(x, y)。希望能够支持多个输入样例。
输入格式
输入的每行包含8个浮点数,表示第一条边的两个端点和第二条边的两个端点的坐标。
输出格式
对于每行输入,输出平行四边形的第四个点的坐标(x, y)。
样例输入
1.000 0.000 3.500 3.500 3.500 3.500 0.000 1.000
1.866 0.000 3.127 3.543 3.127 3.543 1.412 3.145
样例输出
-2.500 -2.500
0.151 -0.398
代码
#include <stdio.h>
#include <stdlib.h>
void swap(double *a, double *b, double *c, double *d); // 交换函数
int main()
{
double x1, x2, x3, x4, x5, y1, y2, y3, y4, y5;
while (scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) != -1)
{
// 固定点位
if (x2 == x3 && y2 == y3)
{
swap(&x3, &x4, &y3, &y4);
swap(&x1, &x2, &y1, &y2);
}
if (x2 == x4 && y2 == y4)
{
swap(&x1, &x2, &y1, &y2);
}
if (x1 == x3 && y1 == y3)
{
swap(&y3, &y4);
}
// 计算第四个点
x5 = x3 + x2 - x1;
y5 = y3 + y2 - y1;
printf("%.3lf %.3lf\n", x5, y5);
}
return 0;
}
void swap(double *a, double *b, double *c, double *d)
{
double t = *a;
*a = *b;
*b = t;
double p = *c;
*c = *d;
*d = p;
}
这个问题可以通过编写一个C语言程序来解决。下面是解决该问题的具体步骤和代码实现。
swap
的函数,接受四个指针作为参数,两个指向坐标的x值,两个指向坐标的y值。在函数内部,使用一个临时变量实现交换操作。void swap(double *a, double *b, double *c, double *d)
{
double t = *a;
*a = *b;
*b = t;
double p = *c;
*c = *d;
*d = p;
}
main
函数中,我们使用一个无限循环来接收输入,并计算每个输入样例的第四个点的坐标。我们定义一些变量来存储输入的坐标和计算后的第四个点的坐标。double x1, x2, x3, x4, x5, y1, y2, y3, y4, y5;
scanf
函数来逐行读取输入,并保存到对应的变量中。while (scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) != -1)
if
语句来检查这两种情况,并在必要时进行点的交换。// 固定点位
if (x2 == x3 && y2 == y3)
{
swap(&x3, &x4, &y3, &y4);
swap(&x1, &x2, &y1, &y2);
}
if (x2 == x4 && y2 == y4)
{
swap(&x1, &x2, &y1, &y2);
}
if (x1 == x3 && y1 == y3)
{
swap(&y3, &y4);
}
printf
函数将结果输出。// 计算第四个点
x5 = x3 + x2 - x1;
y5 = y3 + y2 - y1;
printf("%.3lf %.3lf\n", x5, y5);
while (scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) != -1)
完整的代码如下:
#include <stdio.h>
#include <stdlib.h>
void swap(double *a, double *b, double *c, double *d); // 交换函数
int main()
{
double x1, x2, x3, x4, x5, y1, y2, y3, y4, y5;
while (scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) != -1)
{
// 固定点位
if (x2 == x3 && y2 == y3)
{
swap(&x3, &x4, &y3, &y4);
swap(&x1, &x2, &y1, &y2);
}
if (x2 == x4 && y2 == y4)
{
swap(&x1, &x2, &y1, &y2);
}
if (x1 == x3 && y1 == y3)
{
swap(&y3, &y4);
}
// 计算第四个点
x5 = x3 + x2 - x1;
y5 = y3 + y2 - y1;
printf("%.3lf %.3lf\n", x5, y5);
}
return 0;
}
void swap(double *a, double *b, double *c, double *d)
{
double t = *a;
*a = *b;
*b = t;
double p = *c;
*c = *d;
*d = p;
}
希望这个解决方案对你有帮助!如果你有任何问题,请随时提问。
【相关推荐】