2014-07-21 48 views
0

我想讀取一些整數到一個結構。我讓用戶輸入兩個三維向量並返回兩個交叉產品和點積。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 ,然後進入:5 6 2

使用的兩個向量:[3 2 1]和[5 2 0]

我試圖在scanf%d個左右的空間,和周圍&斧等

由於沒有括號的前瞻性和任何幫助表示讚賞。只是爲了全面披露,這是針對我參加的C編程課程。

回答

2

你讀入b.y兩次:

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

最後一個應該是b.z,否則b.y設置爲6,然後被覆蓋到2,而b.z從未設置(與發生是0)。

+0

你知道我通過這4行代碼試圖弄清楚發生了什麼嗎? DOH !!!!那麼,希望這是我的荷馬辛普森一週的動作範圍。快樂的星期一每個人! –

+0

每個人都會犯你認識的錯誤...一個小小的錯誤就足以導致痛苦的頭痛! :) –