#01

// triarea --- area of a triangle
#include <stdio.h>
double triarea(double w, double h) {
  double s = (w * h) / 2.0;
  return s;
}
int main(void) {
  double w, h;
  printf("w> "); scanf("%lf", &w);
  printf("h> "); scanf("%lf", &h);
  double x = triarea(w, h);
  printf("area of triangle = %g\n", x);
  return 0;
}
// prime.c --- see if an integer is a prime.
#include <stdio.h>
#include <math.h>      // sqrtを使う場合必要
#include <stdbool.h>   // true, false, boolを使う場合必要
bool isprime(int n) {
  int limit = (int)sqrt(n);
  for(int i = 2; i <= limit; ++i) {
    if(n % i == 0) { return false; }
  }
  return true;
}
int main(void) {
  int n;
  printf("n> "); scanf("%d", &n);
  if(isprime(n)) { printf("%d is a prime.\n", n); }
  else           { printf("%d is not a prime.\n", n); }
  return 0;
}
// eps.h --- eps library API
void eps_open(char *fname, int w, int h);
void eps_close(void);
void eps_cmd(char *cmd);
void eps_num(double val);
void eps_drawline(double x0, double y0, double x1, double y1);
void eps_drawrect(double x, double y, double w, double h);
void eps_fillrect(double x, double y, double w, double h);
void eps_drawcircle(double x, double y, double r);
void eps_fillcircle(double x, double y, double r);
int eps_newfont(char *font, double size);
void eps_puts(int id, double x, double y, char *s);
// eps_demo.c --- demonstration of eps library.
#include <stdio.h>
#include "eps.h"

int main(void) {
  eps_open("out.ps", 480, 480);   
  eps_cmd("240 240 translate");   
  eps_drawline(-200, 0, 200, 0);  
  eps_drawline(0, 200, 0, -200);  
  for(int i = 1; i <= 8; ++i) {
    eps_num(i*0.1); eps_cmd("setgray");
    eps_cmd("4 setlinewidth");         
    eps_drawrect(i*20, i*20, 30, 30);  
    eps_num(i*0.1); eps_cmd("1.0 1.0 sethsbcolor");
    eps_fillcircle(-i*20, -i*20, 15);   
  }
  int f1 = eps_newfont("Courier", 20);      
  eps_puts(f1, -180, 50, "This is a pen."); 
  int f2 = eps_newfont("Helvetica", 30);    
  eps_puts(f2, 20, -50, "How are you?");    
  eps_close();                          
  return 0;
}
// eps.c --- eps library implementation
#include <stdio.h>
#include "eps.h"
static FILE *fd = NULL;
static int fontid = 0;

void eps_open(char *fname, int w, int h){
  fd = fopen(fname, "wb");
  fprintf(fd, "%%!PS-Adobe-2.0\n%%%%BoundingBox: 0 0 %d %d\n", w, h);
}
void eps_close(void) {
  fprintf(fd, "showpage\n"); fclose(fd); fd = NULL;
}
void eps_cmd(char *cmd) {
  fprintf(fd, "%s\n", cmd);
}
void eps_num(double val) {
  fprintf(fd, "%.2f ", val);
}
void eps_drawline(double x0, double y0, double x1, double y1) {
  fprintf(fd, "newpath %.2f %.2f moveto %.2f %.2f lineto stroke\n",
          x0, y0, x1, y1);
}
static void rect(double x, double y, double w, double h) {
  fprintf(fd, "newpath %.2f %.2f moveto %.2f 0 rlineto\n", x, y, w);
  fprintf(fd, " 0 %.2f rlineto %.2f 0 rlineto closepath\n", h, -w, -h);
}
void eps_drawrect(double x, double y, double w, double h) {
  rect(x, y, w, h); fprintf(fd, "stroke\n");
}
void eps_fillrect(double x, double y, double w, double h) {
  rect(x, y, w, h); fprintf(fd, "fill\n");
}
static void circle(double x, double y, double r) {
  fprintf(fd, "%.2f %.2f %.2f 0 360 arc closepath\n", x, y, r);
}
void eps_drawcircle(double x, double y, double r) {
  circle(x, y, r); fprintf(fd, "stroke\n");
}
void eps_fillcircle(double x, double y, double r) {
  circle(x, y, r); fprintf(fd, "fill\n");
}
int eps_newfont(char *font, double size) {
  fprintf(fd, "/%s findfont %.2f scalefont /font%d exch def\n",
          font, size, ++fontid);
  return fontid;
}
void eps_puts(int id, double x, double y, char *s) {
  fprintf(fd, "font%d setfont %.2f %.2f moveto (%s) show\n",
          id, x, y, s);
}