所以我的一个朋友在使用具有功能的结构时,让他的代码正确地吐出一个边界(当你在" x y"形式中输入一个点)时会遇到一些问题。
编辑:代码应该占4分,使用欧几里德函数计算每一边。 (你会在print_Perimeter函数中看到尝试)
此外,该程序应该在您点击“&n”之后退出,但他无法弄明白,是否有人可以给出任何建议?
我试图给他一些帮助,但也无法自己解决。很抱歉,如果代码看起来有点被屠杀,我们尝试了一些事情并保存原样。
如果有任何需要更好的说明,我会尽力回复。提前谢谢!
typedef struct Point
{
int xCoord;
int yCoord;
}point;
int point_Print(int x, int y)
{
printf("(X Y): %d %d\n", x, y);
}
double print_TwoPoints(int a, int b, int c, int d)
{
double answer = (sqrt(pow((a - c),2)) + (pow((b - d),2)));
printf("The distance between point (%d,%d) and point (%d,%d) is %.2lf.\n", a, b, c, d, answer);
}
void print_Perimeter(Point *p1, Point *p3, Point *p4)
{
int perimeter = 2 * sqrt(pow((p1->xCoord - p4->xCoord),2) + pow((p1->yCoord - p4->yCoord), 2))
+ sqrt(pow((p1->xCoord - p3->xCoord),2) + pow((p1->yCoord - p3->yCoord), 2))
printf("The perimeter of the rectangle is: %d.\n", perimeter);
}
int main()
{
char quitKey = 'y';
//supposed to make the program quit
while(quitKey != 'n')
{
point xCoord1;
point yCoord1;
printf("Please enter values for first point, X1 Y1: \n");
scanf("%d %d", &(xCoord1.xCoord), &(yCoord1.yCoord));
point xCoord2;
point yCoord2;
printf("Please enter values for second point, X1 Y1: \n");
scanf("%d %d", &(xCoord2.xCoord), &(yCoord2.yCoord));
point xCoord3;
point yCoord3;
printf("Please enter values for third point, X1 Y1: \n");
scanf("%d %d", &(xCoord3.xCoord), &(yCoord3.yCoord));
point xCoord4;
point yCoord4;
printf("Please enter values for forth point, X1 Y1: \n");
scanf("%d %d", &(xCoord4.xCoord), &(yCoord4.yCoord));
point_Print(xCoord1.xCoord, yCoord1.yCoord);
point_Print(xCoord2.xCoord, yCoord2.yCoord);
point_Print(xCoord3.xCoord, yCoord3.yCoord);
point_Print(xCoord4.xCoord, yCoord4.yCoord);
print_TwoPoints(xCoord1.xCoord, yCoord1.yCoord, xCoord2.xCoord, yCoord2.yCoord);
print_TwoPoints(xCoord3.xCoord, yCoord3.yCoord, xCoord4.xCoord, yCoord4.yCoord);
Point p1;
p1.xCoord = xCoord1;
p1.yCoord = yCoord1;
Point p3;
p3.xCoord = xCoord3;
p3.yCoord = yCoord3;
Point p4;
p4.xCoord = xCoord4;
p4.yCoord = yCoord4;
print_Perimeter(&p1, &p3, &p4);
printf("Enter 'y' to enter new coordinates or enter 'n' to quit the program: \n");
scanf(" %c", &quitKey);
}
}
答案 0 :(得分:1)
以下代码:
现在是代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct Point
{
int x;
int y;
} point;
void print_Perimeter( point *point1, point *point2 )
{
int horizontal = point2->x - point1->x;
int vertical = point2->y - point1->y; // EDIT: corrected from '+' to '-'
int perimeter = 2*(horizontal+vertical);
printf("The perimeter of the rectangle is: %d.\n", perimeter);
}
int main( void )
{
int quitKey = 'y';
//supposed to make the program quit
while( quitKey != 'n')
{
point point1;
printf("Please enter values for first point, X1 Y1: \n");
if( 2 != scanf("%d %d", &point1.x, &point1.y) )
{
fprintf( stderr, "scanf for point1 coordinates failed\n");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
point point2;
printf("Please enter values for second point, X2 Y2: \n");
if( 2 != scanf("%d %d", &point2.x, &point2.y) )
{
fprintf( stderr, "scanf for point2 coordinates failed\n");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
print_Perimeter( &point1, &point2 );
// clear stdin
int ch;
while( (ch = getchar()) != EOF && '\n' != ch );
printf("%s\n", "Enter 'y' to enter new coordinates or enter 'n' to quit the program:");
quitKey = getchar();
quitKey = tolower( quitKey );
}
}