예제 #1
0
int main (int argc, char *argv[]) {
  if (argc != 2) {
    std::cout << "Usage: ./numerics inputFileName" << std::endl;
    return EXIT_SUCCESS;
  }
  
  std::string line;
  Functions * functions = new Functions();
  std::fstream f(argv[1]);
  while (std::getline(f, line)) {
    std::size_t pos = 0;
    if ((pos = line.find("define")) != std::string::npos) {
      parse(line, functions);
    }
    else if (((pos = line.find("evaluate")) != std::string::npos)) {
      //compute the value of a function
      double res = evaluate(line, functions);
      printEval(line, res);
    }
    else if (((pos = line.find("numint")) != std::string::npos)) {
      pos += 6;
      skipSpace(line, pos);
      std::string s = line.substr(pos);
      double res = integeral(s, functions);
      printInte(line, res);
    }
    else if (((pos = line.find("mcint")) != std::string::npos)) {
      pos += 5;
      skipSpace(line, pos);
      std::string s = line.substr(pos);
      double res = mcint(s, functions);
      printMcint(line, res);
    }
    else if (((pos = line.find("max")) != std::string::npos)) {
      pos += 3;
      skipSpace(line, pos);
      std::string s = line.substr(pos);
      double res = gradAsc(s, functions, 0);
      printGrad(line, res, 0);
    }
    else if (((pos = line.find("min")) != std::string::npos)) {
      //std::cout << "This is a Gradient Descent" << std::endl;
      pos += 3;
      skipSpace(line, pos);
      std::string s = line.substr(pos);
      double res = gradAsc(s, functions, 1);
      printGrad(line, res, 1);
    }
    else if (((pos = line.find("exit")) != std::string::npos)) {
      delete functions;
      f.close();
      return EXIT_SUCCESS;
    }
  }
  delete functions;
  f.close();
  return EXIT_SUCCESS;
}
예제 #2
0
파일: MCPi.c 프로젝트: jkuchGit/Schoolwork
int main() {
  double pi, x, y;
  int below, trials, mtrial, i, n_experiment = 5;
  double pi_approx[n_experiment];

  printf("TRIALS = \n");
  scanf("%d", &mtrial);

  for(i=0; i<n_experiment; ++i) {
    pi_approx[i] = 4 * mcint(0.0, 1.0, 1.0, mtrial);
    mtrial = 10*mtrial;
  }

  for(i=0; i<n_experiment, ++i) {
    printf("pi trial %d = %f, \n", i, pi_approx[i]);
  }

  return 0;
}