サブ io.js   Last modified: Mar 25, 2004
var newline = (navigator.appVersion.lastIndexOf('Win') != -1) ? "\r\n" : "\n"

function whichCRLF()
{
  if (navigator.appVersion.lastIndexOf('Win') == 5) { // Windows Netscape 6.1
    return "\n"
  }
  else if (navigator.appVersion.lastIndexOf('Win') != -1) { // Windows Netscape Communicator 4.78
    return "\r\n"
  }
  else if (navigator.appVersion.lastIndexOf('5.0') == 0) { // Macintosh Netscape 6.1
    return "\n"
  }
  else { // Macintosh Netscape Communicator 4.78
    return "\r"
  }
}

function getdata(data_string, checkflag)
{
  var lines, vars, nv, nc, j0, nv0, i, j
  var delimiter = ","
  var data_matrix = new Array()
  var newline2

  if (data_string.indexOf(delimiter) == -1) {
    if (data_string.indexOf("\t") != -1) {
      delimiter = "\t"
    }
    else {
      delimiter = " "
    }
  }
  newline2 = whichCRLF()
  lines = data_string.split(newline2)
  nc = 0
  for (i = 0; i < lines.length; i++) {
    if (lines[i] != "") {
      vars = lines[i].split(delimiter)
      nv = vars.length
      data_matrix[nc] = new Array()
      j0 = 0
      for (j = 0; j < nv; j++) {
        if (vars[j] == "") {
          continue
        }
        data_matrix[nc][j0] = parseFloat(vars[j])
        if (isNaN(data_matrix[nc][j0++])) {
          printf("Input line #%i: '%s' is invalid.\n", i+1, vars[j])
          return false
        }
      }
      nc++
      if (checkflag >= 0) {
        if (nc == 1) {
          nv0 = j0
        }
        else if (checkflag == 0 && j0 != nv0) {
          printf("The value described in line %i differs from the value described in the line before it.\n", i+1)
          return false
        }
        else if (checkflag > 0 && j0 != checkflag) {
          printf("The number of the values described in line %i is not %i.\n", i+1, checkflag)
          return false
        }
      }
    }
  }
  if (nc == 0) {
    printf("Data is not inputted.\n")
    return false
  }
  return data_matrix
}

function makeVector(n)
{
  return new Array(n)
}

function makeMatrix(n, m)
{
  var i
  var a = makeVector(n)
  for (i = 0; i < n; i++) {
    a[i] = makeVector(m)
  }
  return a
}

function sep(n)
{
  while(n--) {
    printf("-")
  }
  printf("\n")
}

function sep2(n)
{
  while(n--) {
    printf("=")
  }
  printf("\n")
}

function err100(string)
{
  alert(string)
}

function isDigit(c)
{
  return "0123456789.".indexOf(c) == -1 ? 0 : 1
}

var string_constant_blank = "                                  "
var string_constant_zero  = "0000000000000000000000000000000000"
var _line = ""

function out_one_line()
{
  document.Result.result.value +=_line+newline
  _line = ""
}

function printf(arg)
{
  var i, c, c2, c3, ww, pointer = 1, w = 0, d = 0, left, piriod
  var format = printf.arguments[0]
  for (i = 0; i < format.length; i++) {
    c = format.charAt(i)
    if (c == "\n") {
      if (_line == "") _line = " "
      out_one_line()
    }
    else if (c == "%") {
      left = 0
      i++
      c2 = format.charAt(i)
      if (c2 == "%") {
        _line += c2
        continue
      }
      else if (c2 == "-") {
        left = 1
        c2 = format.charAt(++i)
      }
      if (isDigit(c2)) {
        piriod = 0
        w = 0
        d = 0
        while ((c3 = format.charAt(i)) != "s" && c3 != "f" && c3 != "g" && c3 != "i" && c3 != "d") {
          if (c3 == ".") {
            piriod = 1
          }
          else if (isDigit(c3) == 0) {
            err100("non digit after % ("+c3+")")
          }
          else if (piriod == 0) {
            w = w*10+eval(c3)
          }
          else if (piriod == 1) {
            d = d*10+eval(c3)
          }
          i++
        }
        if (c3 == "s") {
          print_string(left, w, printf.arguments[pointer++])
        }
        else if (c3 == "g" || c3 == "f") {
          print_number(c3, w, d, printf.arguments[pointer++])
        }
        else if (c3 == "i" || c3 == "d") {
          print_number(c3, w, 0, printf.arguments[pointer++])
        }
      }
      else if (c2 == "s") {
        print_string(left, 0, printf.arguments[pointer++])
      }
      else if (c2 == "f") {
        print_number(c2, 0, 3, printf.arguments[pointer++])
      }
      else if (c2 == "g") {
        print_number(c2, 0, 6, printf.arguments[pointer++])
      }
      else if (c2 == "i" || c2 == "d") {
        print_number(c2, 0, 0, printf.arguments[pointer++])
      }
    }
    else {
      _line += c
    }
  }
}

function my_length(str)
{
  var i, c
  var retv = 0
//  var constantstring = "  "
//  if (constantstring.length == 4) {
//    return str.length
//  }
  for (i = 0; i < 200; i++) {
    if ((c = str.charAt(i)) == "") {
      break
    }
    else if (c > "~") {
      retv += 2
    }
    else {
      retv++
    }
  }
  return retv
}

function print_string(left, w, arg)
{
  var ww, temp
  if (w != 0) {
    ww = my_length(""+arg)
    if (left == 1) {
      if (ww > w) {
        _line += arg
      }
      else {
        _line += arg+string_constant_blank.substring(0, w-ww)
      }
    }
    else {
      if (ww > w) {
        _line += arg
      }
      else {
        _line += string_constant_blank.substring(0, w-ww)+arg //@@
      }
    }
  }
  else {
    _line += arg
  }
}

function print_number(gf, w, d, number)
{
  if (gf == "g") {
    _line += print_number_g(w, d, number)
  }
  else if (gf == "f") {
    _line += print_number_f(w, d, number)
  }
  else if (gf == "i" || gf == "d") {
    _line += print_number_i(w, d, number)
  }
}

function print_number_i(w, d, number)
{
  var s, p, ww
  s = fixed2(number, d)
  ww = (""+s).length
  if (ww < w) {
    s = string_constant_blank.substring(0, w-ww)+s
  }
  return s
}

function print_number_f(w, d, number)
{
  var s, p, ww
  s = fixed2(number, d)
  if ((""+s).charAt(0) == ".") {
    s = "0"+s
  }
  else if ((""+s).indexOf("-.") != -1) {
    s = "-0"+(""+s).substring(1, (""+s).length)

  }
  if ((""+s).indexOf(".") == -1) {
    s += "."
  }
  ww = (""+s).length-1
  p = (""+s).indexOf(".")

  if (ww-p < d) {
    s += string_constant_zero.substring(0, d-(ww-p))
  }

  ww = (""+s).length
  if (ww < w) {
    s = string_constant_blank.substring(0, w-ww)+s
  }
  return s
}

function print_number_g(w, d, number)
{
  var abs = Math.abs(number)
  if (number == 0 || (0.001 <= abs && abs < Math.pow(10, d))) {
    return print_number_g2(w, d, number)
  }
  else {
    return print_number_e(w, d, number)
  }
}

function print_number_g2(w, d, number)
{
  var s, p, ww, abs, temp
  abs = Math.abs(number)
  if (number == 0) {
    temp = 1
  }
  else if (abs >= 1) {
    temp = d-(""+Math.floor(abs)).length
  }
  else {
    temp = d-1
    while (abs < 1) {
      temp++
      abs *= 10
    }
  }
  s = fixed2(number, temp)
  if ((""+s).charAt(0) == ".") {
    s = "0"+s
  }
  else if ((""+s).indexOf("-.") != -1) {
    s = "-0"+(""+s).substring(1, (""+s).length)

  }
  if ((""+s).indexOf(".") == -1) {
    s += "."
  }
  ww = (""+s).length
  ww -= (number < 0 ? 1 : 0)+(Math.abs(number) < 1 ? 1 : 0)+1
  if (ww < d) {
    s += string_constant_zero.substring(0, d-ww)
  }

  ww = (""+s).length
  if (ww < w) {
    s = string_constant_blank.substring(0, w-ww)+s
  }
  return s
}

function print_number_e(w, d, number)
{
  var s, p, ww, exponent, sign, abs, temp
  w -= 4
  exponent = 0
  abs = Math.abs(number)
  while (abs < 1) {
    exponent--
    abs *= 10
  }
  while (abs >= 10) {
    exponent++
    abs /= 10
  }
  temp = (number < 0 ? -1 : 1)*abs
  return print_number_f(w, d-1, temp)+"e"+(exponent < 0 ? "-" : "+")+rec(exponent)
}

function rec(expo)
{
  expo = Math.abs(expo)
  return (expo < 10) ? "0"+expo : ""+expo
}
function fixed2(x, d)
{
  return Math.round(x*Math.pow(10,d))/Math.pow(10,d)
}


・ 直前のページへ戻る  ・ E-mail to Shigenobu AOKI

Made with Macintosh