コード例 #1
0
ファイル: q4.c プロジェクト: bfosterscripps/learning
main()
{
  Queue q;
  Stack s;
  Jval v;
  IS is;
  int i;

  q = new_queue();
  s = new_stack();
  i = 0;
  is = new_inputstruct(NULL);

  while (get_line(is) >= 0) {
    if (i % 2 == 0) {
      queue_enqueue(q, new_jval_s(strdup(is->fields[0])));
    } else {
      stack_push(s, new_jval_s(strdup(is->fields[0])));
    }
    i++;
  }
  while (!queue_empty(q)) { 
    v = queue_dequeue(q);
    printf("%s\n", v.s);
  }
  while (!stack_empty(s)) { 
    v = stack_pop(s);
    printf("%s\n", v.s);
  }
}
コード例 #2
0
ファイル: q4.c プロジェクト: bfosterscripps/learning
main()
{
  IS is;
  Queue q;
  Stack s;
  Dllist l;
  int i;
  Jval j;

  is = new_inputstruct(NULL);

  while (get_line(is) > 0) {
    q = new_queue();
    s = new_stack();
    l = new_dllist();
    for (i = 0; i < strlen(is->fields[0]); i++) {
      queue_enqueue(q, new_jval_c(is->fields[0][i]));
      stack_push(s, new_jval_c(is->fields[0][i]));
      dll_append(l, new_jval_c(is->fields[0][i]));
      dll_prepend(l, new_jval_c(is->fields[0][i]));
    }
    while (!queue_empty(q)) {
      j = queue_dequeue(q); printf("%c", j.c);
      j = stack_pop(s); printf("%c", j.c);
      printf("%c", l->flink->val.c); dll_delete_node(l->flink);
      printf("%c", l->flink->val.c); dll_delete_node(l->flink);
      printf(" ");
    }
    printf("\n");
    free_queue(q);
    free_stack(s);
    free_dllist(l);
  }
}
コード例 #3
0
ファイル: dlltail.c プロジェクト: C24IO/Chatak_Megh_SamVad
main(int argc, char **argv)
{
  IS is;
  int n;
  Dllist l;
  Dllist tmp;

  if (argc != 2) {
    fprintf(stderr, "usage: dlltail n\n");
    exit(1);
  }
  n = atoi(argv[1]);
  if (n < 0) {
    fprintf(stderr, "usage: dlltail n  -- n must be >= 0\n");
    exit(1);
  }

  is = new_inputstruct(NULL);
  l = new_dllist();

  while (get_line(is) >= 0) {
    dll_append(l, new_jval_s(strdup(is->text1)));
    if (is->line > n) {
      tmp = dll_first(l);
      free(jval_s(dll_val(tmp)));
      dll_delete_node(tmp);
    }
  }

  dll_traverse(tmp, l) printf("%s", jval_s(tmp->val));
}
コード例 #4
0
ファイル: Jtar.c プロジェクト: StoneyPhenix/Tar_Malloc
	jrb_traverse(JRBnode, paths){
		t = (tar*) JRBnode->val.v;
		if(verb){
			if(S_ISDIR(t->istat.st_mode)) fprintf(stderr,"Directory: %s\n",t->name);
			if(S_ISREG(t->istat.st_mode)) fprintf(stderr,"File: %s\n",t->name);
		}
		fwrite(t->name,strlen(t->name),1,stdout);
		fprintf(stdout,"\n");
		fwrite(&t->istat,sizeof(struct stat),1,stdout);

		if(S_ISREG(t->istat.st_mode)){
	//		printf("name is %s\n", t->name);
			is = new_inputstruct(t->name);
			if(is == NULL) {
				fprintf(stderr,"ERROR: invalid file/directory name provided\n");
				exit(1);
			}
			buf = (void*) malloc(sizeof(char)*8192);
			size = t->istat.st_size;

			while(size > 0){
//				printf("\nsize : %d\n", size);
				if(size < 8192) i = fread(buf,sizeof(char),size,is->f);
				else i = fread(buf,sizeof(char),8192,is->f);
				size -= fwrite(buf,sizeof(char),i,stdout);
			}

			jettison_inputstruct(is);
			free(buf);
		}
		
	}
コード例 #5
0
ファイル: q16.c プロジェクト: bfosterscripps/learning
main(int argc, char **argv)
{
    /* Make variable declarations */

    FILE *f;       /* For reading in the file */
    int n;         /* Number of lines */
    char **array;  /* Array for holding n lines */
    IS is;         /* Inputstruct for reading input */
    int i;         /* Induction variable for the for loops */

    /* Make sure you have the right number of command line arguments */

    if (argc != 3) {
      fprintf(stderr, "usage: revn n filename\n");
      exit(1);
    }

    /* Make sure n is a legal value */

    n = atoi(argv[1]);  /* You could also do a sscanf here */
    if (n <= 0) {
      fprintf(stderr, "Bad value of n\n");
      exit(1);
    }

   /* Open the file, allocate your array to hold the lines */

    f = fopen(argv[2], "w");
    if (f == NULL) { perror(argv[2]); exit(1); }
    array = (char **) malloc(sizeof(char *) * n);
   
   /* Read in the first n lines (or the entire file if it has < n lines */
   /* There are other ways to do this, but this is pretty clean */

    is = new_inputstruct(NULL);
    for (i = 0; i < n; i++) {
      if (get_line(is) < 0) {
        n = i;
      } else {
        array[i] = strdup(is->text1);
      }
    }

   /* Print out the first n lines to the file in reverse order */

   for (i = n-1; i >= 0; i--) {
     fprintf(f, "%s", array[i]);
   }

}
コード例 #6
0
ファイル: nsort.c プロジェクト: leaderwing/test_repo
main()
{
  JRB b;
  JRB bn;
  IS is;

  is = new_inputstruct(NULL);
  b = make_jrb();

  while (get_line(is) >= 0) {
    (void) jrb_insert_int(b, atoi(is->text1), new_jval_s(strdup(is->text1)));
  }

  jrb_traverse(bn, b) {
    printf("%s", jval_s(bn->val));
  }
コード例 #7
0
ファイル: powerj.c プロジェクト: jacksontcollier/PowerJ
/* ========= BEGINNING OF MAIN ================= */
int main(int argc, char **argv){
    int is_pipe_command;    /* Bool: 1 if command contains pipes, 0 otherwise */
    char *shell_prompt;     /* The shell prompt */
    char buf[1000];         /* For grabbing shell prompt from command line */
    char **arg_array;       /* Arguments to command to be executed */
    IS mis;                 /* My input struct */
    Command *com;           /* Arbitrary command */
    Pipe_Command *pip_com;  /* Arbitrary pipe command */

    /* Get shell prompt */
    if (argc == 1) shell_prompt = strdup("PowerJ >> ");
    else if (argc == 2){
        sscanf(argv[1], "%s", buf);
        if (strcmp(buf, "-") == 0) shell_prompt = NULL;
        else shell_prompt = strdup(buf);
    }
    else{
        printf("Usage: ./jsh prompt\n");
        exit(1);
    }
        
    /* Initialize input struct and print shell prompt */
    mis = new_inputstruct(NULL);
    if (shell_prompt != NULL) printf("%s", shell_prompt);

    /* Wait on user input */
    while (get_line(mis) >= 0){
        if (mis->NF > 0){   /* Use fields library to parse command */
            is_pipe_command = check_for_pipes(mis);
            if (is_pipe_command){
                pip_com = create_pip_com(mis);
                exec_pip_com(pip_com);
            } else{
                com = grab_com(mis);
                execute_com(com);
            }
        }
        if (shell_prompt != NULL) printf("%s", shell_prompt);
    }
    return 0;
}
コード例 #8
0
ファイル: creatbtdic.c プロジェクト: leaderwing/test_repo
void creat_btree(BTA* btact){
  IS is;
  dict* dic;
  int i;
  btinit();
  is=new_inputstruct("dictionary.txt");
  if(is==NULL){
    printf("Not exsit this file\n");return ;
  } 
  while((get_line(is)>=0)){
    dic=(dict*)malloc(sizeof(dict)); 
    strcpy(dic->word,is->fields[0]);
    for(i=1;i<is->NF;i++){
      strcat(dic->mean,is->fields[i]);
      if(i!=is->NF-1)
	strcat(dic->mean," ");
    }            
    btins(btact,dic->word,(char*)dic,sizeof(dict));
	  //printf("%s\n",dic->word);
  }  
  jettison_inputstruct(is); 
}
コード例 #9
0
ファイル: user.c プロジェクト: leaderwing/test_repo
void creatBtreeUser(BTA* btUser){
  IS is;
  USER* user;
  int i = 0;
  btinit();
  is = new_inputstruct("user.txt");
  if(is == NULL){
    printf("Not exsit this file\n");
    return;
  } 
  while((get_line(is) >= 0)){
    user = (USER*)malloc(sizeof(USER)); 
    strcpy(user->userName,is->fields[0]);
    strcpy(user->passWord,is->fields[1]);
    user->rights = atoi(is->fields[2]);
    btins(btUser,user->userName,(char*)user,sizeof(USER)); 
    if(i == 0)
      root = user;
    else i++;
  }
  jettison_inputstruct(is);
}
コード例 #10
0
ファイル: q-coderead.c プロジェクト: bfosterscripps/learning
main()
{
  IS is;
  JRB t, tmp;
  int i;
  int j;
  int x;

  j = 0;
  t = make_jrb();
  is = new_inputstruct(NULL);

  while (get_line(is) >= 0) {
    for (i = 0; i < is->NF; i++) {
      jrb_insert_str(t, strdup(is->fields[i]), new_jval_i(j));
      j++;
    }
  }

  jrb_traverse(tmp, t) {
    x = strlen(tmp->key.s);
    printf("%c", tmp->key.s[tmp->val.i%x]);
  }
コード例 #11
0
ファイル: nsort3.c プロジェクト: leaderwing/test_repo
main()
{
  JRB level_1, level_2;
  JRB bn, bn2;
  IS is;

  is = new_inputstruct(NULL);
  level_1 = make_jrb();

  while (get_line(is) >= 0) {
    bn = jrb_find_int(level_1, atoi(is->text1));
    if (bn == NULL) {
      bn = jrb_insert_int(level_1, atoi(is->text1), 
                               new_jval_v((void *)make_jrb()));
    }
    level_2 = (JRB ) jval_v(bn->val);
    jrb_insert_str(level_2, strdup(is->text1), new_jval_v(NULL));
  }

  jrb_traverse(bn, level_1) {
    level_2 = (JRB ) jval_v(bn->val);
    jrb_traverse(bn2, level_2) {
      printf("%s", bn2->key.s);
    }
コード例 #12
0
main (int argc, char *argv[]) {
   int i, j, k, start, size;
   unsigned char *buffer;
   char row_file[512];
   double ssi;
   FILE *fp, *matrix;
   IS is;

   if (argc != 3) {
      printf("usage: bov2csv ssi_matrix.dat size\n");
      exit(1);
   }
   matrix = fopen(argv[1], "rb");
   if (matrix == NULL) {
      perror(argv[1]);
      exit(1);
   }
   if (sscanf(argv[2], "%d", &size) == 0 || size < 0) {
      printf("Error:: not a valid size\n");
      exit(1);
   }

   buffer = (unsigned char *)malloc(sizeof(unsigned char) * size * size);
   // read matrix into buffer
   fread(buffer, sizeof(unsigned char), size*size, matrix);
   fclose(matrix);

   // read species from stdin
   //  for each one, make separate file with appropriate row out of matrix
   i = 0;
   is = new_inputstruct(NULL);
   while(get_line(is) >= 0) {
      sprintf(row_file, "for_web/ssi_rows/%s.txt", is->fields[0]);
      fp = fopen(row_file, "w");
      start = i * size;
      for(j = 0; j < size; j++) {
         fprintf(fp, "%d\n", buffer[start+j]);
      }
      
      //printf("field 0 is %s\n", is->fields[0]);
      fclose(fp);

      // print out header line incrementally
      if(i == 0) {
         printf("%s", is->fields[0]);
      }
      else printf(",%s", is->fields[0]);

      i++;
   }
   printf("\n");
   jettison_inputstruct(is);

   // write out csv file of whole matrix
   for (i = 0; i < size; i++) {
      for (j = 0; j < size; j++) {
         k = buffer[i*size+j];
         k = 255 - k;
         // ADDING THIS TO TAKE CARE OF ZEROS
         if(k == 0 && i != j) k = 1;
         //if(k==0) ssi = 0.001;
         //else ssi = (double)255.0/k;
         //else ssi = k / (double)255.0;
         ssi = k / (double)255.0;
         if(j==0) {
            printf("%lf",ssi);
         }
         else printf(",%lf",ssi);
      } //end reading each field per line
      printf("\n");
   } //end checking rows
   free(buffer);
} //end main
コード例 #13
0
ファイル: double-check.c プロジェクト: Kourpa/Spring2016
main(int argc, char **argv)
{
  IS is;
  JRB people, tmp;
  JRB elevators;
  double t;
  Elevator *e;
  char name[100];
  Person *p;

  elevators = make_jrb();
  people = make_jrb();
  is = new_inputstruct(NULL);

  while (get_line(is) > 0) {
    sscanf(is->fields[0], "%lf", &t);
    if (strcmp(is->fields[1], "Elevator") == 0) {
      e = get_elevator(atoi(is->fields[2]), elevators);
      if (strcmp(is->fields[3], "opening") == 0) {
        if (e->door != 0) {
          printf("Line %d: Elevator %d opening a door that's already open\n", is->line, e->id);
          exit(1);
        }
        if (e->state == 'O') {
          printf("Line %d: Elevator %d opening a door twice\n", is->line, e->id);
          exit(1);
        }
        if (e->state == 'C') {
          printf("Line %d: Elevator %d opening a door that is closing\n", is->line, e->id);
          exit(1);
        }
        e->state = 'O';
        e->time = t;
      } else if (strcmp(is->fields[3], "closing") == 0) {
        if (e->door != 1) {
          printf("Line %d: Elevator %d closing a door that's already closed\n", is->line, e->id);
          exit(1);
        }
        if (e->state == 'C') {
          printf("Line %d: Elevator %d closing a door twice\n", is->line, e->id);
          exit(1);
        }
        if (e->state == 'O') {
          printf("Line %d: Elevator %d closing a door that is opening\n", is->line, e->id);
          exit(1);
        }
        e->state = 'C';
        e->time = t;
      } else if (strcmp(is->fields[5], "closed.") == 0) {
        if (e->state != 'C') {
          printf("Line %d: Elevator %d closed a door that was not closing.\n", is->line, e->id);
          exit(1);
        }
        e->state = 'R';
        e->door = 0;
        e->time = t;
      } else if (strcmp(is->fields[5], "open.") == 0) {
        if (e->state != 'O') {
          printf("Line %d: Elevator %d opened a door that was not opening.\n", is->line, e->id);
          exit(1);
        }
        e->state = 'R';
        e->door = 1;
        e->time = t;
      } else if (strcmp(is->fields[3], "moving") == 0) {
        if (e->state != 'R') {
          printf("Line %d: Elevator %d moving from a non-rest state.\n", is->line, e->id);
          exit(1);
        }
        if (e->door != 0) {
          printf("Line %d: Elevator %d moving when the door is open.\n", is->line, e->id);
          exit(1);
        }
        if (atoi(is->fields[6]) != e->floor) {
          printf("Line %d: Elevator %d moving from a bad floor.\n", is->line, e->id);
          exit(1);
        }
        e->floor = atoi(is->fields[9]);
        e->state = 'M';
        e->time = t;
      } else if (strcmp(is->fields[3], "arrives") == 0) {
        if (e->state != 'M') {
          printf("Line %d: Elevator %d arriving from a non-moving state.\n", is->line, e->id);
          exit(1);
        }
        if (e->door != 0) {
          printf("Line %d: Elevator %d arriving when the door is open.\n", is->line, e->id);
          exit(1);
        }
        if (atoi(is->fields[6]) != e->floor) {
          printf("Line %d: Elevator %d arriving at the wrong floor (%d).\n", is->line, e->id, e->floor);
          exit(1);
        }
        e->state = 'R';
        e->time = t;
      }
    } else if (strcmp(is->fields[1], "Simulation") == 0) {
      exit(0);
    } else {
      sprintf(name, "%s %s", is->fields[1], is->fields[2]);
      if (strcmp(is->fields[3], "arrives") == 0) {
        if (jrb_find_str(people, name) != NULL) {
          printf("%d: Duplicate person %s\n", is->line, name);
          exit(1);
        }
        p = talloc(Person, 1);
        p->name = strdup(name);
        jrb_insert_str(people, p->name, new_jval_v((void *) p));
        p->from = atoi(is->fields[6]);
        p->to = atoi(is->fields[12]);
        p->state = 'A';
      } else {
        tmp = jrb_find_str(people, name);
        if (tmp == NULL) {
          printf("Line %d: Person %s doesn't exist\n", is->line, name);
          exit(1);
        }
        p = (Person *) tmp->val.v;
        if (strcmp(is->fields[4], "on") == 0) {
          if (p->state != 'A') {
            printf("Line %d: Person %s not in arriving state when getting on an elevator\n", is->line, p->name);
            exit(1);
          }
          if (atoi(is->fields[9]) != p->from) {
            printf("Line %d: Person %s not getting on the proper floor\n", is->line, p->name);
            exit(1);
          }
          e = get_elevator(atoi(is->fields[6]), elevators);
          if (e->floor != p->from) {
            printf("Line %d: Person %s getting on an elevator not on the right floor\n", is->line, p->name);
            exit(1);
          }
          if (e->door != 1) {
            printf("Line %d: Person %s getting on an elevator whose door isn't open.\n", is->line, p->name);
            exit(1);
          }
          if (e->state != 'R') {
            printf("Line %d: Person %s getting on an elevator who is not at rest.\n", is->line, p->name);
            exit(1);
          }
          p->state = 'O';
          p->e = e;
        } else if (strcmp(is->fields[4], "off") == 0) {
          if (p->state != 'O') {
            printf("Line %d: Person %s not on the elevator when getting off\n", is->line, p->name);
            exit(1);
          }
          if (atoi(is->fields[9]) != p->to) {
            printf("Line %d: Person %s not getting off on the proper floor\n", is->line, p->name);
            exit(1);
          }
          e = get_elevator(atoi(is->fields[6]), elevators);
          if (e != p->e) {
            printf("Line %d: Person %s getting off the wrong elevator\n", is->line, p->name);
            exit(1);
          }
          if (e->floor != p->to) {
            printf("Line %d: Person %s getting off an elevator not on the right floor\n", is->line, p->name);
            exit(1);
          }
          if (e->door != 1) {
            printf("Line %d: Person %s getting off an elevator whose door isn't open.\n", is->line, p->name);
            exit(1);
          }
          if (e->state != 'R') {
            printf("Line %d: Person %s getting off an elevator who is not at rest.\n", is->line, p->name);
            exit(1);
          }
          p->state = 'F';
        } else if (strcmp(is->fields[4], "done.") == 0) {
          if (p->state != 'F') {
            printf("Line %d: Person %s done before getting off the elevator\n", is->line, p->name);
            exit(1);
          }
          jrb_delete_node(tmp);
          free(p->name);          
          free(p);
        }
      }
    }
  }
  exit(0);
}