無題

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
blue

無題

#1

投稿記事 by blue » 15年前

前期に大学でやった問題を復習しています。ただしくソースを書いたつもりなのになぜかエラーや警告文がたくさん出ます。どこを直せばよいかわかる方おしえてください。
上のフロッピーの画像を押せば実行するのに必要なフォルダがダウンロードができます。
やり方は、作ったC言語のソースファイルをritspen/workspaceに入れる。
コンパイルは gcc -o (実行ファイル名)(作ったC言語のファイル名) RitsPen.o と入力する。
新しく端末を開いて
cd ritspen
./canvas.sh
と入力してcanvasを立ち上げる。
実行ファイルを実行させる。


重要: RitsPenへの命令を解釈するインタプリタ

重要課題 13..1
以下のような構造体instructionを考える。この構造体は、指定された長さ, 幅, 角度, 色, 速度で線を引くための RisPen への命令を表している。

 #define MAX_NAME (64)
 
 struct instruction {
  int length;
 int width;
  int angle;
 char color[MAX_NAME];
  int speed;
 };
この構造体を引数として与えられたときに、RitsPenを使って指定された仕様の線 を引く関数

interpret_program(PEN, struct instruction)
を作れ。 CreatePEN()の引数は interpreter とする。
作成した関数interprete_program()に以下のように設定された構造体instructionを引数に与える main()関数を作成し、開始点から垂直上向きに太さ 20、長さ 200の青色線がもっとも速い速度で描線されていることを確かめよ。

長さ 200
回転角 -90
幅 20
色 ''blue''
速度 1
ヒント: 関数interpret_program()は以下のような手続きに従う。

void
interpret_program(PEN pen, struct instruction instruction)
{

ChangeWidth()で幅を設定
ChangeSpeed()で速度を設定
ChangeColor()もしくはChangeColor2()で色を設定
UsePEN()で長さと回転角を指定して描線

return;
}
ヒント: 色の指定には、構造体と RitsPen で指定方法が異なっているので、 注意が必要である。構造体のメンバでは色名が文字列で指定されているが、 RitsPenのChangeColor()では色番号で、RitsPenのChangeColor2()ではRGBのそれ ぞれの強度で色が指定される。RGBのそれぞれの強度で指定される。構造体内のメ ンバ color と色名を指定した文字列を、教科書 p.252, p.266 で説明されている str_comp()か、ライブラリ関数 strcmp() を用いて比較し、構造体内のメンバ colorが何色を指定しているかを判断した上で色の設定を行う。具体的には、ライ ブラリ関数 strcmp() を用いると、以下のようなプログラムとなる。

#define BLACK (0)
#define RED (1)
.......
#define GRAY (7)

if(strcmp(構造体内のメンバ color, "black") == 0) {
ChangeColor(pen, BLACK);
} else if(strcmp(構造体内のメンバ color, "red") == 0) {
ChangeColor(pen, RED);
} else if(
.......
} else if(strcmp(instruction.color, "violet") == 0) {
ChangeColor2(pen, 90, 90, 150);
} else if(strcmp(instruction.color, "brown") == 0) {
ChangeColor2(pen, 0, 90, 30);
} else {
// その他の色はとりあえずグレー
ChangeColor(pen, GRAY);
}
文字列の比較には教科書 p.253 に説明さているように
if( 構造体内のメンバ color == "black") {
ChangeColor(pen, BLACK);
}
と書いてはいけない。教科書を読んで、理由をよく理解しておくこと。



自分が書いたソースコード
#include <stdio.h>
#include <string.h>
#include "RitsPen.h"
#define MAX_NAME 64
#define Black 0
#define RED 1
#define BLUE 2
#define GREEN 3
#define ORANGE 4
#define PINK 5
#define YELLOW 6
#define GRAY 7
//プロトタイプ宣言


struct instruction{
int length;
int width;
int angele;
char color[MAX_NAME];
int speed;
};
void interpret_program(PEN pen, struct instruction instruction){
ChangeWidth(pen,instruction.width);
ChangeSpeed(pen,instruction.speed);

if(strcmp(instruction.color, "black") == 0){
ChangeColor(pen, BLACK);
}
else if(strcmp(instruction.color, "red") == 0){
ChangeColor(pen, RED);
}
else if(strcmp(instruction.color, "blue") == 0){
ChangeColor(pen, BLUE);
}
else if(strcmp(instruction.color, "green") == 0){
ChangeColor(pen, GREEN);
}
else if(strcmp(instruction.color, "orange") == 0){
ChangeColor(pen, ORANGE);
}
else if(strcmp(instruction.color, "pink") == 0){
ChangeColor(pen, PINK);
}
else if(strcmp(instruction.color, "yellow") == 0){
ChangeColor(pen, YELLOW);
}
else{
ChangeColor(pen, GRAY);
}
UsePEN(pen, instruction.length, instruction.angle);/*線の描画*/
}

int main(void){
//設定
PEN pen;
struct instruction = {200,-90,20,"blue",1};

pen = CreatePEN("interpreter");
interpret_program(pen,a);
ClosePEN(pen);
return 0;
}

fatens

Re:無題

#2

投稿記事 by fatens » 15年前

#define Black 0 → #define BLACK 0
int angele; → int angle;
struct instruction = {200,-90,20,"blue",1}; → struct instruction a = {200,20,-90,"blue",1};

これでどうでしょうか。

blue

Re:無題

#3

投稿記事 by blue » 15年前

うまくいきました。fatensさんありがとうございます。

blue

Re:無題

#4

投稿記事 by blue » 15年前


閉鎖

“C言語何でも質問掲示板” へ戻る