File: main.cpp | Size: 2,862 bytes | Download file | Back to directory listing | BWPOW's homepage
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include "ts.h"
 
using namespace std;
 
bool operator<(const INSTR &a,const INSTR &b)
{
  if(a.s==b.s) return a.p<b.p;
  return a.s<b.s;
}  
 
map<INSTR,INSTR> instr;
vector<string> state;
vector<char> pr,pl;
INFO info;
char chars[256];
 
static char *license[]={
"This program is free software; you can redistribute it and/or modify",
"it under the terms of the GNU General Public License as published by",
"the Free Software Foundation; either version 2 of the License, or",
"(at your option) any later version.",
NULL
};  
 
void main_help(void)
{
  printf("Turing Machine Simulator %s\n",VERSION);
  printf("Copyright (C) 2005 Samuel BWPOW Kupka, bwpow@ideaz.sk, http://bwpow.ideaz.sk\n");
  printf("Created for Judge@srobarka project (http://judge.srobarka.sk)\n");
  printf("============================================================================\n\n");
  printf("Usage: judge_ts.exe tsfile inputfile outputfile maxlength maxsteps [options]\n\n");
  printf("Options:\n");
  printf("-t0 : (default) Machine without constraints\n");
  printf("-t1 : Left constraint\n");
  printf("-t2 : Left and right constraint (maximal length is used as length)\n");
  printf("-ot : Output tape (default)\n");
  printf("-os : Output state\n");
  printf("-oc : Output count of ones (second character in input file)\n");
  printf("These options can be placed at the beginning of the input file.\n");
  printf("\n");
  for(int i=0;license[i];i++) printf("%s\n",license[i]);
  exit(1);
}  
 
void main_exit(char *text)
{
  if(strlen(text)>0) printf("%s\n",text);
  exit(1);
}  
 
int main(int argc,char *argv[])
{
  int r;
  if(argc<6) main_help();
  for(int i=6;i<argc;i++){
    if(!stricmp(argv[i],"-t0")){ info.cl=0;info.cr=0; }
    if(!stricmp(argv[i],"-t1")){ info.cl=1;info.cr=0; }
    if(!stricmp(argv[i],"-t2")){ info.cl=1;info.cr=1; }
    if(!stricmp(argv[i],"-ot")){ info.output=0; }
    if(!stricmp(argv[i],"-os")){ info.output=1; }
    if(!stricmp(argv[i],"-oc")){ info.output=2; }
  }  
  strcpy(info.tsfile,argv[1]);
  strcpy(info.infile,argv[2]);
  strcpy(info.outfile,argv[3]);
  if(sscanf(argv[4],"%d",&info.maxmemory)!=1) main_help();
  if(sscanf(argv[5],"%d",&info.maxsteps)!=1) main_help();
 
  if(load_input()!=0) main_exit("Error loading input file!");
  r=load_ts();
  if(r==-1) main_exit("Error opening TS file!");
  if(r==-2) main_exit("Too many states.");
  if(r==-3) main_exit("Too many instructions.");
  if(r==-4) main_exit("Invalid character.");
  if(r==-5) main_exit("Non-deterministic machine.");
  if(r==-6) main_exit("Unknown movement.");
 
  r=run();
  if(r==-1) main_exit("Time limit exceeded.");
  if(r==-2) main_exit("Memory limit exceeded.");
 
  if(save_output()!=0) main_exit("Can't write to output file!");
 
  print_output();
  return 0; 
}