complex_pointer.c |
---|
#include<stdio.h> #include<stdlib.h> struct Complex{ double Re, Im; }; struct Complex Complex(double re, double im); void print_Complex(struct Complex *arg); struct Complex add_Complex(struct Complex *lhs, struct Complex *rhs); struct Complex sub_Complex(struct Complex *lhs, struct Complex *rhs); struct Complex mul_Complex(struct Complex *lhs, struct Complex *rhs); struct Complex div_Complex(struct Complex *lhs, struct Complex *rhs); int main(void){ struct Complex x=Complex(2.0, 3.1), y=Complex(-1.2, 3.4), z=Complex(5.6, 7.8), u=Complex(-9.1, 2.3), result; printf("x="); print_Complex(&x); printf("y="); print_Complex(&y); result=add_Complex(&x, &y); printf("result=x+y="); print_Complex(&result); result=sub_Complex(&x, &y); printf("result=x-y="); print_Complex(&result); result=mul_Complex(&x, &y); printf("result=x*y="); print_Complex(&result); result=div_Complex(&x, &y); printf("result=x/y="); print_Complex(&result); /*** ポインタ変数を引数にとるように 関数を書き換えたので、 もはや↓のような書き方は不可能なので 混合演算をしたい場合には 中間変数を用意して 一つ一つ行わなければならない***/ /*** result=sub_Complex(add_Complex(x, div_Complex(mul_Complex(y, x), z)), u); printf("result=x+y*x/z-u=(%.3e)+(%.3e)j\n", result.Re, result.Im);***/ return 0; } struct Complex Complex(double re, double im){ struct Complex result; result.Re=re; result.Im=im; return result; } void print_Complex(struct Complex *arg){ printf("(%.3e)+i(%.3e)\n", arg->Re, arg->Im); return; } struct Complex add_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; result.Re=lhs->Re+rhs->Re; result.Im=lhs->Im+rhs->Im; return result; } struct Complex sub_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; result.Re=lhs->Re-rhs->Re; result.Im=lhs->Re-rhs->Im; return result; } struct Complex mul_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; result.Re=lhs->Re*rhs->Re-lhs->Im*rhs->Im; result.Im=lhs->Re*rhs->Im+lhs->Im*rhs->Re; return result; } struct Complex div_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; if(rhs->Re==0.0 && rhs->Im==0.0){ printf("Division by 0\n"); exit(1); } result.Re=(lhs->Re*rhs->Re+lhs->Im*rhs->Im)/(rhs->Re*rhs->Re+rhs->Im*rhs->Im); result.Im=(lhs->Im*rhs->Re-lhs->Re*rhs->Im)/(rhs->Re*rhs->Re+rhs->Im*rhs->Im); return result; } |
fraction_pointer.c |
---|
#include<stdio.h> struct Fraction{ int Denominator, Numerator; }; struct Fraction Fraction(int numerator, int denominator); void print_Fraction(struct Fraction *arg); struct Fraction add_Fraction(struct Fraction *lhs, struct Fraction *rhs); struct Fraction sub_Fraction(struct Fraction *lhs, struct Fraction *rhs); struct Fraction mul_Fraction(struct Fraction *lhs, struct Fraction *rhs); struct Fraction div_Fraction(struct Fraction *lhs, struct Fraction *rhs); int main(void){ struct Fraction x=Fraction(2, 3), y=Fraction(-1, 3), z; printf("x="); print_Fraction(&x); printf("y="); print_Fraction(&y); z=add_Fraction(&x, &y); printf("z=x+y="); print_Fraction(&z); z=sub_Fraction(&x, &y); printf("z=x-y="); print_Fraction(&z); z=mul_Fraction(&x, &y); printf("z=x*y="); print_Fraction(&z); z=div_Fraction(&x, &y); printf("z=x/y="); print_Fraction(&z); return 0; } struct Fraction Fraction(int numerator, int denominator){ struct Fraction result; result.Denominator=denominator; result.Numerator=numerator; return result; } void print_Fraction(struct Fraction *arg){ printf("(%d)/(%d)\n", arg->Numerator, arg->Denominator); return; } struct Fraction add_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Denominator; result.Numerator=lhs->Denominator*rhs->Numerator+lhs->Numerator*rhs->Denominator; return result; } struct Fraction sub_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Denominator; result.Numerator=rhs->Denominator*lhs->Numerator-rhs->Numerator*lhs->Denominator; return result; } struct Fraction mul_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Denominator; result.Numerator=lhs->Numerator*rhs->Numerator; return result; } struct Fraction div_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Numerator; result.Numerator=lhs->Numerator*rhs->Denominator; return result; } |
complex/complex.h |
---|
struct Complex{ double Re, Im; }; struct Complex Complex(double re, double im); void print_Complex(struct Complex *arg); struct Complex add_Complex(struct Complex *lhs, struct Complex *rhs); struct Complex sub_Complex(struct Complex *lhs, struct Complex *rhs); struct Complex mul_Complex(struct Complex *lhs, struct Complex *rhs); struct Complex div_Complex(struct Complex *lhs, struct Complex *rhs); |
complex/complex.c |
---|
#include <stdio.h> #include "complex.h" struct Complex Complex(double re, double im){ struct Complex result; result.Re=re; result.Im=im; return result; } void print_Complex(struct Complex *arg){ printf("(%.3e)+i(%.3e)\n", arg->Re, arg->Im); return; } struct Complex add_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; result.Re=lhs->Re+rhs->Re; result.Im=lhs->Im+rhs->Im; return result; } struct Complex sub_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; result.Re=lhs->Re-rhs->Re; result.Im=lhs->Im-rhs->Im; return result; } struct Complex mul_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; result.Re=lhs->Re*rhs->Re-lhs->Im*rhs->Im; result.Im=lhs->Re*rhs->Im+lhs->Im*rhs->Re; return result; } struct Complex div_Complex(struct Complex *lhs, struct Complex *rhs){ struct Complex result; if(rhs->Re==0.0 && rhs->Im==0.0){ printf("Division by 0\n"); exit(1); } result.Re=(lhs->Re*rhs->Re+lhs->Im*rhs->Im)/(rhs->Re*rhs->Re+rhs->Im*rhs->Im); result.Im=(lhs->Im*rhs->Re-lhs->Re*rhs->Im)/(rhs->Re*rhs->Re+rhs->Im*rhs->Im); return result; } |
complex/main_complex.c |
---|
#include<stdio.h> #include<stdlib.h> #include"complex.h" int main(void){ struct Complex x=Complex(2.0, 3.1), y=Complex(-1.2, 3.4), z=Complex(5.6, 7.8), u=Complex(-9.1, 2.3), result; printf("x="); print_Complex(&x); printf("y="); print_Complex(&y); result=add_Complex(&x, &y); printf("result=x+y="); print_Complex(&result); result=sub_Complex(&x, &y); printf("result=x-y="); print_Complex(&result); result=mul_Complex(&x, &y); printf("result=x*y="); print_Complex(&result); result=div_Complex(&x, &y); printf("result=x/y="); print_Complex(&result); /*** ポインタ変数を引数にとるように 関数を書き換えたので、 もはや↓のような書き方は不可能なので 混合演算をしたい場合には 中間変数を用意して 一つ一つ行わなければならない***/ /*** result=sub_Complex(add_Complex(x, div_Complex(mul_Complex(y, x), z)), u); printf("result=x+y*x/z-u=(%.3e)+(%.3e)j\n", result.Re, result.Im);***/ return 0; } |
fraction/fraction.h |
---|
struct Fraction{ int Denominator, Numerator; }; struct Fraction Fraction(int numerator, int denominator); void print_Fraction(struct Fraction *arg); struct Fraction add_Fraction(struct Fraction *lhs, struct Fraction *rhs); struct Fraction sub_Fraction(struct Fraction *lhs, struct Fraction *rhs); struct Fraction mul_Fraction(struct Fraction *lhs, struct Fraction *rhs); struct Fraction div_Fraction(struct Fraction *lhs, struct Fraction *rhs); |
fraction/fraction.c |
---|
#include<stdio.h> #include"fraction.h" struct Fraction Fraction(int numerator, int denominator){ struct Fraction result; result.Denominator=denominator; result.Numerator=numerator; return result; } void print_Fraction(struct Fraction *arg){ printf("(%d)/(%d)\n", arg->Numerator, arg->Denominator); return; } struct Fraction add_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Denominator; result.Numerator=lhs->Denominator*rhs->Numerator+lhs->Numerator*rhs->Denominator; return result; } struct Fraction sub_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Denominator; result.Numerator=rhs->Denominator*lhs->Numerator-rhs->Numerator*lhs->Denominator; return result; } struct Fraction mul_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Denominator; result.Numerator=lhs->Numerator*rhs->Numerator; return result; } struct Fraction div_Fraction(struct Fraction *lhs, struct Fraction *rhs){ struct Fraction result; result.Denominator=lhs->Denominator*rhs->Numerator; result.Numerator=lhs->Numerator*rhs->Denominator; return result; } |
fraction/main_fraction.c |
---|
#include<stdio.h> #include"fraction.h" int main(void){ struct Fraction x=Fraction(2, 3), y=Fraction(-1, 3), z; printf("x="); print_Fraction(&x); printf("y="); print_Fraction(&y); z=add_Fraction(&x, &y); printf("z=x+y="); print_Fraction(&z); z=sub_Fraction(&x, &y); printf("z=x-y="); print_Fraction(&z); z=mul_Fraction(&x, &y); printf("z=x*y="); print_Fraction(&z); z=div_Fraction(&x, &y); printf("z=x/y="); print_Fraction(&z); return 0; } |