C/C++の宿題を片付けます 117代目at TECH
C/C++の宿題を片付けます 117代目 - 暇つぶし2ch843:デフォルトの名無しさん
08/11/15 13:12:50
[1] 授業単元: アルゴリズムデータ構造Ⅱ
[2] 問題文(含コード&リンク):
複素数a,bを標準入力から入力し、それぞれの絶対値を求め、2つの複素数の和、差、積、商を求め
標準出力に出力するプログラムを作りたい。これらを求める関数として、複素数の絶対値を求める関数
および和、差、積、商を求める関数が
double cabs(struct mycomplex)
struct mycomplex wa(struct mycomplex,struct mycomplex)
struct mycomplex sa(struct mycomplex,struct mycomplex)
struct mycomplex seki(struct mycomplex,struct mycomplex)
struct mycomplex shou(struct mycomplex,struct mycomplex)
として作ることにした。これらの絶対値、和、差、積と商を求める関数を解答せよ
なおこの構造体は予めCの処理系には用意されているが、それを利用せず独立に考えよ。
[3] 環境
 [3.1] OS: Windows
 [3.2]
 [3.3] 言語: C

[4] 期限: 11月16日まで

[5] その他の制限:構造体の問題です 問題文に沿ってお願いします。




844:デフォルトの名無しさん
08/11/15 13:18:37
843の問題です
#include<stdio.h>
#include<math.h>
struct mycomplex {
double re;
double im;
};

int main()
{
struct mycomplex a,b;
struct mycomplex wa(struct mycomplex,struct mycomplex);
struct mycomplex sa(struct mycomplex,struct mycomplex);
struct mycomplex seki(struct mycomplex,struct mycomplex);
struct mycomplex shou(struct mycomplex,struct mycomplex);
double cabs(struct mycomplex);
struct mycomplex cinput(void);
void cprint(struct mycomplex);
a=cinput(); b=cinput();
cprint(wa(a,b)); cprint(sa(a,b)); cprint(seki(a,b)); cprint(shou(a,b));
printf("絶対値 cabs(a)=%f\n",carbs(a)); printf("絶対値 cabs(b)=%f\n",cabs(b));}



845:デフォルトの名無しさん
08/11/15 13:20:11
844の問題の続きです
struct mycomplex cinput(void)
{
struct mycomplex z;
scanf("%f %f",&z.re,&z.im);
return z;
}

void cprint(struct mycomplex z)
{
printf("複素数 = %f + i*%f\n",z.re,z.im);
}
誰かお願いします

846:デフォルトの名無しさん
08/11/15 14:11:56
>>843
struct mycomplex wa(struct mycomplex a, struct mycomplex b)
{
struct mycomplex z = { a.re+b.re, a.im+b.im };
return z;
}

struct mycomplex sa(struct mycomplex a, struct mycomplex b)
{
struct mycomplex z = { a.re-b.re, a.im-b.im };
return z;
}

struct mycomplex seki(struct mycomplex a, struct mycomplex b)
{
struct mycomplex z = { a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re };
return z;
}

struct mycomplex shou(struct mycomplex a, struct mycomplex b)
{
struct mycomplex z = {
(a.re*b.re + a.im*b.im) / (b.re*b.re + b.im*b.im),
(a.im*b.re - a.re*b.im) / (b.re*b.re + b.im*b.im)
};
return z;
}

double cabs(struct mycomplex a)
{
return hypot(a.re, a.im);
}

847:デフォルトの名無しさん
08/11/15 17:18:06
全然わかりません。よろしくお願いします。

[1] 授業単元:応用プログラミング論
[2] 問題文(含コード&リンク):
ある任意の点X(a,b)、Y(c,d)を乱数で求め、
X,Yからなる線分に1辺が平行であり、四角形の中心とその線分との距離が30pixelになるような四角形を描写せよ。
四角形は20×40のものを使用。
[3] 環境
 [3.1] OS:WindowsXP
 [3.2] コンパイラ名とバージョン:Visual C++ 2008
 [3.3] 言語:C++
[4] 期限: 2008年11月18日13時まで
[5] その他の制限:とくになし。

848:デフォルトの名無しさん
08/11/15 17:43:41
四角形が20*40なのに中心から線分までの距離が30かあ。

849:デフォルトの名無しさん
08/11/15 17:49:46
いいんじゃないか?仮に20の方が平行な辺だとしてもね。

850:デフォルトの名無しさん
08/11/15 18:03:00
あ。読み間違えてた。
線分XYを一辺とするのかと思いこんでた。
     X
┌─┐│
└─┘│
     Y
こういうことね。

851:デフォルトの名無しさん
08/11/15 18:06:09
┌─┤
└─┤
じゃないんだねww?

852:デフォルトの名無しさん
08/11/15 18:45:07
846のかた
ありがとうございます


853: ◆hXvyVozAPo
08/11/15 19:11:10
[1] 授業単元:プログラミングC
[2] 問題文(含コード&リンク):
以下のようなプログラムを作成しなさい.

int型変数xを宣言し標準入力で適当な数値を代入する。
その変数xのアドレスを引数として、アドレスと値を表示する関数を作成せよ。

実行例
./a
? 5
変数xのアドレスは 0x00 です
変数xの値は 5 です
[3] 環境
 [3.1] OS: WindowsXP
 [3.2] コンパイラ名 Cygwin
 [3.3] 言語:C
[4] 期限: 11月16日 17時まで
[5] その他の制限: 授業はまだまだ初歩レベルみたいです。

854:デフォルトの名無しさん
08/11/15 19:24:18
#include<stdio.h>
#include<math.h>
struct mycomplex {
double re;
double im;
};
int main()
{
struct mycomplex a,b;
struct mycomplex wa(struct mycomplex,struct mycomplex);
struct mycomplex sa(struct mycomplex,struct mycomplex);
struct mycomplex seki(struct mycomplex,struct mycomplex);
struct mycomplex shou(struct mycomplex,struct mycomplex);
double cabs(struct mycomplex);
struct mycomplex cinput(void);
void cprint(struct mycomplex);
a=cinput(); b=cinput();
cprint(wa(a,b)); cprint(sa(a,b)); cprint(seki(a,b)); cprint(shou(a,b));
printf("絶対値 cabs(a)=%f\n",carbs(a)); printf("絶対値 cabs(b)=%f\n",cabs(b));
}
struct mycomplex cinput(void)
{
struct mycomplex z;
scanf("%f %f",&z.re,&z.im);
return z;


855:デフォルトの名無しさん
08/11/15 19:29:32
}
struct mycomplex cinput(void) {
struct mycomplex z;
scanf("%f %f",&z.re,&z.im);
return z; }
void cprint(struct mycomplex z) {
printf("複素数 = %f + i*%f\n",z.re,z.im); }
struct mycomplex wa(struct mycomplex a, struct mycomplex b) {
struct mycomplex z = { a.re+b.re, a.im+b.im };
return z; }
struct mycomplex sa(struct mycomplex a, struct mycomplex b) {
struct mycomplex z = { a.re-b.re, a.im-b.im };
return z; }
struct mycomplex seki(struct mycomplex a, struct mycomplex b) {
struct mycomplex z = { a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re };
return z; }
struct mycomplex shou(struct mycomplex a, struct mycomplex b) {
struct mycomplex z = {
(a.re*b.re + a.im*b.im) / (b.re*b.re + b.im*b.im),
(a.im*b.re - a.re*b.im) / (b.re*b.re + b.im*b.im)
};
return z; }
double cabs(struct mycomplex a) {
return hypot(a.re, a.im); }
すいません、動かないんで何処が違うか教えてください
854の続きです



856:デフォルトの名無しさん
08/11/15 19:32:14
>>853
#include<stdio.h>
void pp(int *x)
{
printf("変数xのアドレスは 0x%p です\n", x);
printf("変数xの値は %d です\n", *x);
}
int main(void)
{
int x;
fputs("? ", stdout);
scanf("%d", &x);
pp(&x);
return 0;
}

857:デフォルトの名無しさん
08/11/15 19:35:21
>>855
見づらいし
インデントしなおすのもめんどくさいから
素直にろだにあげろカス

858:デフォルトの名無しさん
08/11/15 19:37:24
>>855
carbs → acabs

859:デフォルトの名無しさん
08/11/15 19:49:51
>>854
関数宣言を main の外へ出せ

860:デフォルトの名無しさん
08/11/15 20:07:38
859の方
わからないので、8036でアップいましたので修正お願いします
ほかの方も教えてくれるとありがたいです

861:デフォルトの名無しさん
08/11/15 20:13:17
C++です。
二つの整数値を読み込んで、小さい方の数以上で大きい方の数以下の整数を全て加えた値を表示するプログラムを作りたいんです
 
 
整数1は37
整数2は28
28~37までの全整数の和は325
for文は使わずにお願いします。
 
 
よろしくお願いしますm(_ _)m



862:デフォルトの名無しさん
08/11/15 20:19:13
>>860
関数宣言を main 関数の外へ
scanf のフォーマットが違った。 %f -> %lf
main 関数の最後に return 0; を追加
URLリンク(kansai2channeler.hp.infoseek.co.jp)

863:デフォルトの名無しさん
08/11/15 20:26:58
>>859
別に関数宣言は問題ではないと思うが

標準ライブラリのcabs()と被るのでacabs()に名前変更
main()には、return 0;でもつけてね
cinput()内のscanf()は、%fではなくて%lfにする
shou()内の(b.re * b.re + b.im * b.im) != 0 を保証すべき
acabs()でhypot()呼んでいるので、必要に応じてリンクする


最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch