構造体へのポインタの受け渡し方をよく理解していないため、このような書き方しかできませんでした。ド素人質問で申し訳ないのですが、下記のプログラムを手直ししていただけないでしょうか。
答えが表示すらされない状況です。
returnとprintV3とprintfをちょこちょこいじったのでさらにわからなくなりました。
エラーコード
??.c: In function ‘v3k’:
??.c:40:9: warning: ‘return’ with a value, in function returning void
40 | return ( r);
| ^
??.c:34:6: note: declared here
34 | void v3k( double k, V3 *p, V3 *r)
| ^~~
??.c: In function ‘v2k’:
??.c:45:18: warning: implicit declaration of function ‘v3abs’ [-Wimplicit-function-declaration]
45 | r->wx = p->wx / v3abs( p);
| ^~~~~
??.c: At top level:
??.c:50:8: error: conflicting types for ‘v3abs’
50 | double v3abs( V3 *p)
| ^~~~~
??.c:45:18: note: previous implicit declaration of ‘v3abs’ was here
45 | r->wx = p->wx / v3abs( p);
| ^~~~~
??.c: In function ‘v3t’:
??.c:57:16: warning: implicit declaration of function ‘v3ip’ [-Wimplicit-function-declaration]
57 | return( acos( v3ip( p, q) / v3abs( p) * v3abs( q)));
| ^~~~
??.c: In function ‘v3cp’:
??.c:66:9: warning: ‘return’ with a value, in function returning void
66 | return ( r);
| ^
??.c:60:6: note: declared here
60 | void v3cp( V3 *p, V3 *q, V3 *r)
| ^~~~
??.c: At top level:
??.c:69:8: error: conflicting types for ‘v3ip’
69 | double v3ip( V3 *p, V3 *q)
| ^~~~
??.c:57:16: note: previous implicit declaration of ‘v3ip’ was here
57 | return( acos( v3ip( p, q) / v3abs( p) * v3abs( q)));
| ^~~~
??.c: In function ‘main’:
??.c:93:10: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’
-Wformat=]
93 | scanf("%d", &k);
| ~^ ~~
| | |
| | double *
| int *
| %le
??.c:94:9: warning: too many arguments for format [-Wformat-extra-args]
94 | printf("定数倍 k * a", &c);
| ^~~~~~~~~~~~~~~~~
??.c:96:2: warning: implicit declaration of function ‘v3u’; did you mean ‘v3t’? [-Wimplicit-function-declaration]
96 | v3u(&a, &c);
| ^~~
| v3t
??.c:106:19: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘struct _v3 *’ [-Wformat=]
106 | printf("外積%.2f", &c);
| ~~~^ ~~
| | |
| | struct _v3 *
| double
「三次元ベクトルを表す構造体を定義して、この構造体の演算のための関数を作る」
//------- 3次元ベクトルを扱うプログラム #include <stdio.h> #include<math.h> // 構造体の定義 struct _v3{ double wx; double wy; double wz; }; #define V3 struct _v3 // 関数の型の定義(プロトライプ宣言) void printV3( char *s, V3 *p); // 二次元ベクトルの名前付き表示 void v3add( V3 *p, V3 *q, V3 *r); // 3次元ベクトルの足し算 r = p + q void v3sub( V3 *p, V3 *q, V3 *r); // 3次元ベクトルの引き算 r = p - q void v3k( double k, V3 *p, V3 *r); // 3次元ベクトルの定数倍 r = k * p void v3u( V3 *p, V3 *r); // 3次元ベクトルを単位ベクトルに r <- p/|p| double v3abs( V3 *p); // 3次元ベクトルの絶対値 double v3t( V3 *p, V3 *q); // 3つのベクトルのなす角 double v3cp( V3 *p, V3 *r); // 3次元ベクトルの外積 double v3ip( V3 *p, V3 *q); // 3次元ベクトルの内積 // 3次元ベクトルの表示 void printV3( char *s, V3 *p) { printf("%s = (%.2f, %.2f, %.2f)\n", s, p->wx, p->wy, p->wz); } // 3次元ベクトルの和 void v3add( V3 *p, V3 *q, V3 *r) { r->wx = p->wx + q->wx; r->wy = p->wy + q->wy; r->wz = p->wz + q->wz; } // 3次元ベクトルの差 void v3sub( V3 *p, V3 *q, V3 *r) { r->wx = p->wx - q->wx; r->wy = p->wy - q->wy; r->wz = p->wz - q->wz; } // 3次元ベクトルの定数倍 void v3k( double k, V3 *p, V3 *r) { r->wx = k * p->wx; r->wy = k * p->wy; r->wz = k * p->wz; return ( r); } // 3次元ベクトルの単位ベクトル Unit vector void v2k( V3 *p, V3 *r) { r->wx = p->wx / v3abs( p); r->wy = p->wy / v3abs( p); r->wz = p->wz / v3abs( p); } // 3次元ベクトルの絶対値 double v3abs( V3 *p) { return( sqrt( p->wx * p->wx + p->wy * p->wy + p->wz * p->wz)); } // 3次元ベクトルのなす角 double v3t( V3 *p, V3 *q) { return( acos( v3ip( p, q) / v3abs( p) / v3abs( q))); } // 3次元ベクトルの外積 Cross product void v3cp( V3 *p, V3 *q, V3 *r) { r->wx = (p->wy * p->wz) - (p->wz * p->wy); r->wy = (p->wz * p->wx) - (p->wx * p->wz); r->wz = (p->wx * p->wy) - (p->wy * p->wx); return ( r); } // 3次元ベクトルの内積 Inner product double v3ip( V3 *p, V3 *q) { return( p->wx * q->wx * q->wz + p->wy * q->wy * q->wz); } int main( void) { double k, theta; // 3次元ベクトルの表示 V3 a = { 1.0, 3.0, 5.0}, b = { 4.0, 5.0, 6.0}, c; printV3( "a", &a); printV3( "b", &b); // 3次元ベクトルの和 v3add( &a, &b, &c); printV3( "和 a+b", &c); // 3次元ベクトルの差 v3sub( &a, &b, &c); printV3( "差 a-b", &c); // 3次元ベクトルの定数倍 v3k(k, &a, &c); printf("定数倍 k * a", &c); // 3次元ベクトルの単位ベクトル Unit vector v3u(&a, &c); printV3("単位ベクトル", &c); // 3次元ベクトルの絶対値 v3abs( &a); printf("絶対値%.2f", v3abs( &a)); // 3次元ベクトルのなす角 Theta v3t( &a, &b); printf("なす角", &c); // 3次元ベクトルの外積 Cross product v3cp( &a, &b, &c); printf("外積", &c); // 3次元ベクトルの内積 Inner product v3ip( &a, &b); printf("内積", &c); }