2014-07-21 3 views
0

구조체에 정수를 읽으려고합니다. 사용자가 두 개의 3 차원 벡터를 입력하고 두 개의 교차 제품과 점 제품을 반환하도록하는 중입니다.구조체에 C scanf int

두 번째 벡터의 두 번째 값을 건너 뛰는 것처럼 보입니다. 여기 내 코드는 원경 같습니다

/** Write a program to calculate cross-products and dot products of 
** a 3-dimensional vector: 
** 
** 1. Uses a type definition 
** 2. Accepts User input of qty(2) 3-dimensional vectors 
** 3. Calculate the cross-product of A x B and B x A       
** 4. Calculate the dot product A * B 
** 
******************************************************************/ 


/************* Preprocessor Functions  **********************/ 
#include <stdio.h> 
#include <stdlib.h> 


/************ Structured Data Types ***************************/ 

typedef struct vector 
{ 
    int x; 
    int y; 
    int z; 
} Vector; 


/************* Declare User Functions **********************/ 

int dot_product(Vector a, Vector b); 
Vector cross_product(Vector a, Vector b); 

/************  Begin MAIN LOOP  *************************/ 

int main(void) 
{ 
/**  Declare variables  **/ 
    Vector a, b, c; 

    printf("Enter the 3 integer components of the first vector: "); 
    scanf("%d%d%d", &(a.x), &(a.y), &(a.z)); 
    printf("Enter the 3 integer components of the second vector: "); 
    scanf("%d%d%d", &(b.x), &(b.y), &(b.y)); 
    c = cross_product(a, b); 
    printf("\n\t(%d %d %d) x (%d %d %d) = (%d %d %d)", a.x,a.y,a.z,b.x,b.y,b.z,c.x,c.y,c.z); 
    c = cross_product(b, a); 
    printf("\n\t(%d %d %d) x (%d %d %d) = (%d %d %d)", b.x,b.y,b.z,a.x,a.y,a.z,c.x,c.y,c.z); 
    printf("\n\t(%d %d %d) * (%d %d %d) = %d\n", a.x,a.y,a.z,b.x, b.y,b.z,dot_product(a, b)); 

/*********** AND CUT! It's a wrap folks! Take 5!  ***********/  
    return 0; 
} 

/********** User Functions to perform the calculations ****/ 

int dot_product(Vector a, Vector b) 
{ 
    return((a.x*b.x)+(a.y*b.y)+(a.z*b.z)); 
} 

Vector cross_product(Vector a, Vector b) 
{ 
Vector c; 
c.x = (a.y*b.z)-(a.z*b.y); 
c.y = (a.z*b.x)-(a.x*b.z); 
c.z = (a.x*b.y)-(a.y*b.x); 

return(c); 

} 

사용자가 입력하는 경우 : 3 2 1 을 그리고 입사 6 2

사용 두 벡터 (5)는 다음과 같다 : 1 2 3] 및 [5 2 0]

나는 scanf와의 % d의 주위에 공간을 시도하고,보고를위한 & 도끼 등

덕분에 주위에 괄호 어떤 도움을 주시면 감사하지 않습니다. 전체 내용을 공개하기 위해, 이것은 내가 다니고있는 C 프로그래밍 수업을위한 것입니다.

답변

2

당신은 두 번 b.y로 읽어 : b.z가 설정되지 (그리고 어떻게됩니까 않습니다 동안

scanf("%d%d%d", &(b.x), &(b.y), &(b.y));

이 마지막 하나는, 다음 2에 덮어 쓰기됩니다, 그렇지 않으면 b.y6로 설정되어 b.z 있어야하는데 0).

+0

무슨 일이 일어나고 있는지 알아 내려고 4 줄의 코드를 몇 번 읽었는지 아십니까? DOH !!!! 글쎄, 잘하면 그 주 동안 내 호머 심슨의 움직임의 정도입니다. 매일 행복한 월요일 !!!! –

+0

모두들 당신이 알고있는 실수를합니다 ... 하나의 작은 실수로 고통스러운 두통을 일으킬 수 있습니다! :) –