为什么我的 Struct 没有通过 C 中的点符号检索正确的信息?
Why isn't my Struct retrieving the right information via dot notation in C?
我正在尝试编写一个程序,它从用户那里获取 2 个圆的 X-Y 坐标和半径。然后,它会判断2个圆是否相交或包含在另一个圆中。
对于 contain() 函数,当我尝试打印出 c1.x 和 c2.x 的值时,只有 c1.x 似乎给我用户输入,而 c2.x 打印出一个对我没有意义的非常大的数字。为什么我无法检索 c2.x 的用户输入?谢谢
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct circle {
double radius;
double x;
double y;
};
// Returns 1 if the Circle intersects
int intersect(struct circle c1, struct circle c2);
int contain(struct circle *c1, struct circle *c2);
int main(){
struct circle c1, c2;
int answer, selection;
do{
printf("Please choose a Function to be used!!!\n");
printf("1: Enter the Parameters of the 2 Circles:\n");
printf("2: intersect():\n");
printf("3: contain():\n");
printf("4: Quit!!\n");
scanf("%d", &selection);
switch(selection){
case 1:{
// First Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the First Circle\n");
printf("Radius:\n");
scanf("%d", &c1.radius);
printf("X-Coordinate:\n");
scanf("%d", &c1.x);
printf("Y-Coordinate:\n");
scanf("%d", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%d", &c2.radius);
printf("X-Coordinate:\n");
scanf("%d", &c2.x);
printf("Y-Coordinate:\n");
scanf("%d", &c2.y);
break;
}
case 2:{
int choice = intersect(c1, c2);
switch(choice){
case 1:{
printf("The 2 Circles intersects!\n");
break;
}
case 2:{
printf("The 2 Circles does not intersects!!\n");
}
}
break;
}
case 3:{
answer = contain(&c1,&c2);
if(answer == 1){
printf("Circle C1 contains Circle C2!!\n");
}
else{
printf("Circle C1 does not contain Circle C2!!\n");
}
}
}
} while (selection < 4);
return 0;
}
int intersect(struct circle c1, struct circle c2){
// Calculate the Distance between the Centers of 2 Circles
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1.x,2) - pow(c2.x,2);
printf("X Coordinate of C1 and C2: %d, %d\n", c1.x,c2.x);
printf("%d", xDiff);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1.y,2) - pow(c2.y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
dRadii = c1.radius + c2.radius;
if((dist == dRadii) || (dist < dRadii)){
return 1;
}
else{
return 0;
}
}
int contain(struct circle *c1, struct circle *c2){
double specialValue;
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
specialValue = dist + c2->radius;
// Checking
if((c1->radius > specialValue) || (c1->radius == specialValue)){
return 1;
}
else{
return 0;
}
}
intersect
函数的参数传递存在一些问题,printf
.
的格式说明符存在一些其他问题
尝试这样的事情:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct circle {
double radius;
double x;
double y;
};
// Returns 1 if the Circle intersects
int intersect(struct circle *c1, struct circle *c2);
int contain(struct circle *c1, struct circle *c2);
int main(){
struct circle c1, c2;
int answer, selection;
do{
printf("Please choose a Function to be used!!!\n");
printf("1: Enter the Parameters of the 2 Circles:\n");
printf("2: intersect():\n");
printf("3: contain():\n");
printf("4: Quit!!\n");
scanf("%d", &selection);
switch(selection){
case 1:{
// First Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the First Circle\n");
printf("Radius:\n");
scanf("%lf", &c1.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c1.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%lf", &c2.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c2.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c2.y);
break;
}
case 2:{
int choice = intersect(&c1, &c2);
switch(choice){
case 1:{
printf("The 2 Circles intersects!\n");
break;
}
case 2:{
printf("The 2 Circles does not intersects!!\n");
}
}
break;
}
case 3:{
answer = contain(&c1,&c2);
if(answer == 1){
printf("Circle C1 contains Circle C2!!\n");
}
else{
printf("Circle C1 does not contain Circle C2!!\n");
}
}
}
} while (selection < 4);
return 0;
}
int intersect(struct circle *c1, struct circle *c2){
// Calculate the Distance between the Centers of 2 Circles
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
printf("X Coordinate of C1 and C2: %lf, %lf\n", c1->x,c2->x);
printf("%lf\n", xDiff);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
dRadii = c1->radius + c2->radius;
if((dist == dRadii) || (dist < dRadii)){
return 1;
}
else{
return 0;
}
}
int contain(struct circle *c1, struct circle *c2){
double specialValue;
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
specialValue = dist + c2->radius;
// Checking
if((c1->radius > specialValue) || (c1->radius == specialValue)){
return 1;
}
else{
return 0;
}
}
如有其他问题,请添加演示用户输入和预期输出。
struct circle {
double radius;
double x;
double y;
};
这里,结构成员是double数据类型。所以你必须使用 %lf 而不是 %d 这是整数的格式说明符。
在 main() 中执行此操作:
scanf("%lf", &c1.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c1.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%lf", &c2.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c2.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c2.y);
在相交()中:
printf("%lf", xDiff);
我正在尝试编写一个程序,它从用户那里获取 2 个圆的 X-Y 坐标和半径。然后,它会判断2个圆是否相交或包含在另一个圆中。
对于 contain() 函数,当我尝试打印出 c1.x 和 c2.x 的值时,只有 c1.x 似乎给我用户输入,而 c2.x 打印出一个对我没有意义的非常大的数字。为什么我无法检索 c2.x 的用户输入?谢谢
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct circle {
double radius;
double x;
double y;
};
// Returns 1 if the Circle intersects
int intersect(struct circle c1, struct circle c2);
int contain(struct circle *c1, struct circle *c2);
int main(){
struct circle c1, c2;
int answer, selection;
do{
printf("Please choose a Function to be used!!!\n");
printf("1: Enter the Parameters of the 2 Circles:\n");
printf("2: intersect():\n");
printf("3: contain():\n");
printf("4: Quit!!\n");
scanf("%d", &selection);
switch(selection){
case 1:{
// First Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the First Circle\n");
printf("Radius:\n");
scanf("%d", &c1.radius);
printf("X-Coordinate:\n");
scanf("%d", &c1.x);
printf("Y-Coordinate:\n");
scanf("%d", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%d", &c2.radius);
printf("X-Coordinate:\n");
scanf("%d", &c2.x);
printf("Y-Coordinate:\n");
scanf("%d", &c2.y);
break;
}
case 2:{
int choice = intersect(c1, c2);
switch(choice){
case 1:{
printf("The 2 Circles intersects!\n");
break;
}
case 2:{
printf("The 2 Circles does not intersects!!\n");
}
}
break;
}
case 3:{
answer = contain(&c1,&c2);
if(answer == 1){
printf("Circle C1 contains Circle C2!!\n");
}
else{
printf("Circle C1 does not contain Circle C2!!\n");
}
}
}
} while (selection < 4);
return 0;
}
int intersect(struct circle c1, struct circle c2){
// Calculate the Distance between the Centers of 2 Circles
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1.x,2) - pow(c2.x,2);
printf("X Coordinate of C1 and C2: %d, %d\n", c1.x,c2.x);
printf("%d", xDiff);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1.y,2) - pow(c2.y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
dRadii = c1.radius + c2.radius;
if((dist == dRadii) || (dist < dRadii)){
return 1;
}
else{
return 0;
}
}
int contain(struct circle *c1, struct circle *c2){
double specialValue;
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
specialValue = dist + c2->radius;
// Checking
if((c1->radius > specialValue) || (c1->radius == specialValue)){
return 1;
}
else{
return 0;
}
}
intersect
函数的参数传递存在一些问题,printf
.
尝试这样的事情:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct circle {
double radius;
double x;
double y;
};
// Returns 1 if the Circle intersects
int intersect(struct circle *c1, struct circle *c2);
int contain(struct circle *c1, struct circle *c2);
int main(){
struct circle c1, c2;
int answer, selection;
do{
printf("Please choose a Function to be used!!!\n");
printf("1: Enter the Parameters of the 2 Circles:\n");
printf("2: intersect():\n");
printf("3: contain():\n");
printf("4: Quit!!\n");
scanf("%d", &selection);
switch(selection){
case 1:{
// First Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the First Circle\n");
printf("Radius:\n");
scanf("%lf", &c1.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c1.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%lf", &c2.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c2.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c2.y);
break;
}
case 2:{
int choice = intersect(&c1, &c2);
switch(choice){
case 1:{
printf("The 2 Circles intersects!\n");
break;
}
case 2:{
printf("The 2 Circles does not intersects!!\n");
}
}
break;
}
case 3:{
answer = contain(&c1,&c2);
if(answer == 1){
printf("Circle C1 contains Circle C2!!\n");
}
else{
printf("Circle C1 does not contain Circle C2!!\n");
}
}
}
} while (selection < 4);
return 0;
}
int intersect(struct circle *c1, struct circle *c2){
// Calculate the Distance between the Centers of 2 Circles
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
printf("X Coordinate of C1 and C2: %lf, %lf\n", c1->x,c2->x);
printf("%lf\n", xDiff);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
dRadii = c1->radius + c2->radius;
if((dist == dRadii) || (dist < dRadii)){
return 1;
}
else{
return 0;
}
}
int contain(struct circle *c1, struct circle *c2){
double specialValue;
double xDiff, yDiff, dist, dRadii;
xDiff = pow(c1->x,2) - pow(c2->x,2);
if(xDiff < 0){
xDiff = -xDiff;
}
yDiff = pow(c1->y,2) - pow(c2->y,2);
if(yDiff < 0){
yDiff = -yDiff;
}
dist = sqrt(xDiff + yDiff);
specialValue = dist + c2->radius;
// Checking
if((c1->radius > specialValue) || (c1->radius == specialValue)){
return 1;
}
else{
return 0;
}
}
如有其他问题,请添加演示用户输入和预期输出。
struct circle {
double radius;
double x;
double y;
};
这里,结构成员是double数据类型。所以你必须使用 %lf 而不是 %d 这是整数的格式说明符。
在 main() 中执行此操作:
scanf("%lf", &c1.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c1.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c1.y);
// Second Circle
printf("Please Enter the Radius, X-Coordinate and Y-Coordinate of the Second Circle\n");
printf("Radius:\n");
scanf("%lf", &c2.radius);
printf("X-Coordinate:\n");
scanf("%lf", &c2.x);
printf("Y-Coordinate:\n");
scanf("%lf", &c2.y);
在相交()中:
printf("%lf", xDiff);