jw_cad 外部変形 - (484) gccで円を描く -

外部変形は データのやり取りをテキストファイルで行うので プログラム言語は 自由に選ぶことができます。図形は機能的かつシンプルなため、数多くのユーザーに受け入れられています。

 

gccで円を描く

:gccで円を描く
@echo off
if not exist %~dpn0.exe (
  for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
    more +%%n %0 > %~dpn0.c
    gcc -Os %~dpn0.c -o %~dpn0.exe -s
  )
)
%~dpn0
goto:eof

REM #jww
REM #1-%d 円の中心を指示してください
REM #2%d 円周上の点を指示してください
REM #e

#!この次の行からプログラムを書いてください
#include "jw.h"
int main(void)
{
  FILE *f;
  char S_[256], *F[20];
  double x1, y1, x2, y2;
  if ( (f = fopen("jwc_temp.txt", "r") ) != NULL) {
    while (fgets(S_, 256, f) != NULL) {
      split(chomp(S_), F);
      if (strncmp(S_, "hp1", 3) == 0) { x1 = atof(F[1]); y1 = atof(F[2]); }
      if (strncmp(S_, "hp2", 3) == 0) { x2 = atof(F[1]); y2 = atof(F[2]); }
    }
  } else {
    exit(MISSING_JWC_TEMP_TXT);
  }
  fclose(f);
  f = fopen("jwc_temp.txt", "w");
  fprintf(f, "ci %.15g %.15g %.15g\n", x1, y1, hypot(x2 - x1, y2 - y1));
  fclose(f);
  return 0;
}

 

○jw.h

/*  jw.h : JW_CAD 外部変形 例題用ヘッダファイル

    動作確認
    MinGW + gcc / g++ 4.5.0
    Borland C++ 5.5.1 for Win32
    Visual C++ 2005 Express Edition
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795
#define MISSING_JWC_TEMP_TXT printf("jwc_temp.txt が 見当たりません\n")
#define NO_OPERATION printf("処理できませんでした\n")

int split(char *S_, char **F)
{   /* char S_[256], *F[20]; */
    int i, NF;
    for (i = 0; i < 20; i++) {
        if ( (F[i] = strtok(S_, " ") ) == NULL) { NF = i; break; }
        S_ = NULL;
    }
    return NF;
}

char *substr(char *src, int off, int len, char *buf)
{
    return strncpy(buf, src+off, len);
}

char *chomp(char *str)
{
    int l = strlen(str);
    if( l > 0 && str[l-1] == '\n' )
    {
        str[l-1] = '\0';
    }
    return str;
}

double hypot(double x, double y)
{
    return sqrt(x * x + y * y);
}

 

 

はじめてバッチファイルを実行した際にバッチファイル名.exeで実行形式のファイルを作成します。