BUNSU 型を受け取り、値が 0 ならば 0、そうでなければ、負の場合はマイナ ス記号"-"を先頭につけ、分子/分母 という形式で出力する 関数 void printbunsu(BUNSU) を作りなさい。 なお、課題1-1 から 1-3 で定義した関数を呼び出してよい。 という問題です。
課題1-1は、BUNSU 型を受け取り、分数の値を double 型として返す関数 double bkazu(BUNSU) を 作りなさい。です。
作ったプログラムを下に書きます。
※bunsu.hには今回の課題で使う関数のすべてのプロトタイプ 宣言が入っているいます。
----bunsu.h----
typedef struct {
int bunshi;
int bunbo;
} BUNSU;
double bkazu(BUNSU b);
int bsign(BUNSU b);
BUNSU babs(BUNSU b);
void printbunsu(BUNSU b);
----bunsu.c----
#include "bunsu.h"
double bkazu(BUNSU b){
return (b.bunshi /b.bunbo);
}
----main.c----
#include <stdio.h>
#include "bunsu.h"
int main(void){
BUNSU b[] = { { 3, 4 }, { -5, 3 }, { 1, -2 }, { -2, -3 }, { 0, -1 }, { 0, 1 }, { 0, 0 } };
BUNSU *p;
for (p = b; p->bunbo != 0; p++){
printf("%f\n", bkazu(*p));
}
return 0;
}
課題1-2は、BUNSU 型を受け取り、分数の符号が負ならば -1, 0 ならば 0, 正ならば 1 を 返す関数 int bsign(BUNSU) を作りなさい。です。
作ったプログラムを下に書きます。
----bunsu.h----
typedef struct {
int bunshi;
int bunbo;
} BUNSU;
double bkazu(BUNSU b);
int bsign(BUNSU b);
BUNSU babs(BUNSU b);
void printbunsu(BUNSU b);
----bunsu.c----
#include "bunsu.h"
int bsign(BUNSU b){
if (0< ((double)b.bunshi / (double)b.bunbo)){
return 1;
}
if (0> ((double)b.bunshi / (double)b.bunbo)){
return -1;
}
if (0 == ((double)b.bunshi / (double)b.bunbo)){
return 0;
}
else return 0;
}
----main.c----
#include <stdio.h>
#include "bunsu.h"
int main(void){
BUNSU b[] = { { 3, 4 }, { -5, 3 }, { 1, -2 }, { -2, -3 }, { 0, -1 }, { 0, 1 }, { 0, 0 } };
BUNSU *p;
for (p = b; p->bunbo != 0; p++){
printf("%d\n", bsign(*p));
}
return 0;
}
課題1-3は、BUNSU 型を受け取り、分子、分母とも正にした絶対値を返す関数 BUNSU babs(BUNSU) を作りなさい。です。
作ったプログラムを下に書きます。
----bunsu.h----
typedef struct {
int bunshi;
int bunbo;
} BUNSU;
double bkazu(BUNSU b);
int bsign(BUNSU b);
BUNSU babs(BUNSU b);
void printbunsu(BUNSU b);
----bunsu.c----
#include "bunsu.h"
BUNSU babs(BUNSU b){
if (0 > b.bunshi){
b.bunshi *= -1;
}
if (0 > b.bunbo){
b.bunbo *= -1;
}
return b;
}
----main.c----
#include <stdio.h>
#include "bunsu.h"
int main(void){
BUNSU a;
BUNSU b[] = { { 3, 4 }, { -5, 3 }, { 1, -2 }, { -2, -3 }, { 0, -1 }, { 0, 1 }, { 0, 0 } };
BUNSU *p;
for (p = b; p->bunbo != 0; p++){
a = babs(*p);
printf("%d %d\n", a.bunshi, a.bunbo);
}
return 0;
}
前置きが長くなってしまい申し訳ありません。
本題の課題1-4なのですが、bunsu.hとmain.cは先生から指定されており、
----bunsu.h----
typedef struct {
int bunshi;
int bunbo;
} BUNSU;
double bkazu(BUNSU b);
int bsign(BUNSU b);
BUNSU babs(BUNSU b);
void printbunsu(BUNSU b);
----main.c----
#include <stdio.h>
#include "bunsu.h"
int main(void){
BUNSU b[] ={{3,4},{-5,3},{1,-2},{-2,-3},{0,-1},{0,1},{0,0}};
BUNSU *p;
for(p=b;p->bunbo!=0;p++){
printbunsu(*p);
printf("\n");
}
return 0;
}
後は他の部分を書くだけなのですが、さっぱりわからなくて困っています。
どのような方法で書けばいいのかアドバイスがもらいたいと思い書き込みました。
わかる方いらっしゃいましたらお願いします。