Ejemplo n.º 1
0
int msglist (void)
{
	int x;
	if (server) {
		x = writemsg (MSGLIST);
		if (x == SCMOK)  x = Tprocess (listT,listone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = writeint ((int)scantime);
		if (x == SCMOK)  x = writemend ();
	} else {
		char *name;
		int mode,flags,mtime;
		TREE *t;
		x = readmsg (MSGLIST);
		if (x == SCMOK)  x = readstring (&name);
		while (x == SCMOK) {
			if (name == NULL)  break;
			x = readint (&mode);
			if (x == SCMOK)  x = readint (&flags);
			if (x == SCMOK)  x = readint (&mtime);
			if (x != SCMOK)  break;
			t = Tinsert (&listT,name,TRUE);
			free (name);
			t->Tmode = mode;
			t->Tflags = flags;
			t->Tmtime = mtime;
			x = readstring (&name);
		}
		if (x == SCMOK)  x = readint ((int *)&scantime);
		if (x == SCMOK)  x = readmend ();
	}
	return (x);
}
void handle_request(void) {
  void *allocs[100] = {0};
  uint32_t alloc_idx;

  sendstr(" **** WELCOME TO THE HEAP SIMULATOR 2001! ****\n\n");
  while (1) {
    sendstr("What would you like to do?\n");
    sendstr(" 1) Allocate memory\n");
    sendstr(" 2) Free memory\n");
    sendstr(" 3) Read some data\n");
    sendstr(" 4) Exit\n\n");

    int cmd = readint();
    uint16_t id;
    switch (cmd) {
        case 1:
            for (alloc_idx = 0; alloc_idx < sizeof(allocs) && allocs[alloc_idx] != NULL; ++alloc_idx) ;

            if (alloc_idx >= sizeof(allocs)) {
                sendstr("Run out of space, free something first!\n");
            } else {
                sendstr("Ok, how much memory do you want (in bytes)?\n");
                uint16_t bytes = readint();
                allocs[alloc_idx] = (void *)alloc(bytes);
                sendstr(tprintf("Allocated with ID = %d\n", alloc_idx));
            }
            dump_heap();
            break;
        case 2:
            sendstr("Please enter the ID of the alloc to free\n");
            id = readint();
            if (id < sizeof(allocs) && allocs[id] != NULL) {
                dealloc(allocs[id]);
                allocs[id] = NULL;
            } else {
                sendstr("Alloc not found\n");
            }
            dump_heap();
            break;
        case 3:
            sendstr("Please enter the ID of the alloc where to read data\n");
            id = readint();
            if (id < sizeof(allocs) && allocs[id] != NULL) {
                sendstr("Ready! Send the data\n");
                recvline(client_fd, allocs[id], 0x100);
            } else {
                sendstr("Alloc not found\n");
            }
            dump_heap();
            break;
        case 4:
            say_bye();
            exit(0);
            break;
        default:
            sendstr("What!?\n");
    };
  }

}
Ejemplo n.º 3
0
int main() {
    for (; scanf("%d%d",&n,&m) != EOF;) {
        totg = n;
        tn = (n*3)/4;
        for (i = 0; i <= n; i++)  g[i].next = -1;
        for (i = 1; i <= n; i++)  addedge(0,i,-1);
        for (i = 1; i <= m; i++) {
            x = readint();
            y = readint();
            z = readint();
            if (x == 1) {
                addedge(y,z,0);
                addedge(z,y,0);
            }
            else if (x == 2)addedge(y,z,-1);
            else if (x == 3)addedge(z,y,0);
            else if (x == 4)addedge(z,y,-1);
            else addedge(y,z,0);
        }
        hasres = SPFA();
        if (hasres == 1) {
            res = 0;
            for (i = 1; i <= n; i++)res += (long long)(-d[i]);
            printf("%lld\n",res);
        }
        else printf("-1\n");
    }
}
Ejemplo n.º 4
0
void comment_add(char **comments, int* length, char *tag, char *val)
{
    char* p=*comments;
    int vendor_length=readint(p, 8);
    int user_comment_list_length=readint(p, 8+4+vendor_length);
    int tag_len=(tag?strlen(tag)+1:0);
    int val_len=strlen(val);
    int len=(*length)+4+tag_len+val_len;

    p=(char*)realloc(p, len);
    if(p==NULL){
        fprintf(stderr, "realloc failed in comment_add()\n");
        exit(1);
    }

    writeint(p, *length, tag_len+val_len);      /* length of comment */
    if(tag){
        memcpy(p+*length+4, tag, tag_len);        /* comment tag */
        (p+*length+4)[tag_len-1] = '=';           /* separator */
    }
    memcpy(p+*length+4+tag_len, val, val_len);  /* comment */
    writeint(p, 8+4+vendor_length, user_comment_list_length+1);
    *comments=p;
    *length=len;
}
Ejemplo n.º 5
0
/** @brief Programa principal para la practica de PRO2: 'Red ferroviaria'. */
int main() {

  // Leer cuantas cocheras
  int M = readint();
  // Inicializar la red, las cocheras y los trenes
  Red red;

  red.leer_red();
  Cjt_Cocheras cocheras(M);
  cocheras.leer_cocheras();

  Cjt_Trenes trenes;
  trenes.leer_trenes();
  // Procesar las opciones
  int opcion = readint();

  while (opcion != -6) {
    if (opcion == -1) cocheras.almacenamiento_nocturno(red, trenes);

    else if (opcion == -2) cocheras.formacion_matinal(trenes);

    else if (opcion == -3) trenes.leer_tren();
    else if (opcion == -4) cocheras.leer_cocheras();

    else if (opcion == -5) cocheras.escribir_cocheras();
    opcion = readint();
  }

  return 0;
}
Ejemplo n.º 6
0
void Servidor::actualizar_pelicula(){
  int x;
  x = readint();
  addPeliculas(x);
  x = readint();
  deletePeliculas(x);
}
Ejemplo n.º 7
0
int main(void) {
    int c, lim, s, t;
    int *n, *p;
    for(readint(t); t--; ) {
        readint(s);
        readint(c);
        for(int i = 0; i < s; ++i) readint(d[0][i]);
        for(lim = s; lim--; ) {
            bool change = false;
            n = d[s - lim];
            p = d[s - lim - 1];
            for(int i = 0; i < lim; ++i) {
                n[i] = p[i + 1] - p[i];
                change |= i && n[i] != n[i - 1];
            }
            if(!change) {
                for(int i = lim; i < lim + c; ++i) n[i] = n[0];
                break;
            }
        }
        while(lim++ < s) {
            n = d[s - lim];
            p = d[s - lim + 1]      ;
            for(int i = lim; i < lim + c; ++i) n[i] = n[i - 1] + p[i - 1];
        }
        if(s > 1) for(int i = s; i < s + c; ++i) buffer(n[i], i==s+c-1?10:32);
        else for(int i = 0; i < c; ++i) buffer(d[0][0], i==c-1?10:32);
    }
    flush_buffer();
    return 0;
}
Ejemplo n.º 8
0
/**
 * Constructor
 * @param ss substream to read from
 */
hpiutil::sqshstream::sqshstream(substream &ss)
{
	valid = false;
	stream = &ss;
	boost::uint32_t magic = readint();
	if (magic != SQSH_MAGIC) {
		std::cerr << "Invalid SQSH header signature: 0x" << std::hex << magic << std::endl;
		return;
	}
	boost::uint8_t unknown = stream->read();
	compress = stream->read();
	encrypt = stream->read();
	compressedsize = readint();
	fullsize = readint();
	checksum = readint();
	boost::uint32_t newcheck = stream->checksum(SQSH_HEADER);
	if (checksum && (newcheck != checksum)) {
		std::cerr << "Chunk checksum " << std::hex << newcheck << " does not match stored checksum " << std::hex << checksum << std::endl;
		return;
	}
	if (decompress())
		valid = true;
	else
		free((void*)data);
}
Ejemplo n.º 9
0
void Servidor::leer_servidor(int i, int n){
  id = i+1;
  lpel = vector<bool>(n,false);
  int x;
  x = readint();
  ancho = x;
  x = readint();
  addPeliculas(x);
}
Ejemplo n.º 10
0
static int readint2( const char** source, int* x, int* y )
{
  if ( readint( source, x ) != 0 )
  {
    return -1;
  }

  return readint( source, y );
}
Ejemplo n.º 11
0
static void print_comments(char *comments, int length)
{
   char *c=comments;
   int len, i, nb_fields, err=0;

   if (length<(8+4+4))
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   if (strncmp(c, "OpusTags", 8) != 0)
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   c += 8;
   fprintf(stderr, "Encoded with ");
   len=readint(c, 0);
   c+=4;
   if (len < 0 || len>(length-16))
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   err&=fwrite(c, 1, len, stderr)!=(unsigned)len;
   c+=len;
   fprintf (stderr, "\n");
   /*The -16 check above makes sure we can read this.*/
   nb_fields=readint(c, 0);
   c+=4;
   length-=16+len;
   if (nb_fields < 0 || nb_fields>(length>>2))
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   for (i=0;i<nb_fields;i++)
   {
      if (length<4)
      {
         fprintf (stderr, "Invalid/corrupted comments\n");
         return;
      }
      len=readint(c, 0);
      c+=4;
      length-=4;
      if (len < 0 || len>length)
      {
         fprintf (stderr, "Invalid/corrupted comments\n");
         return;
      }
      err&=fwrite(c, 1, len, stderr)!=(unsigned)len;
      c+=len;
      length-=len;
      fprintf (stderr, "\n");
   }
}
Ejemplo n.º 12
0
int main(void) {
    int a, b, t;
    readint(&t);
    for(int cnum = 0; cnum++ < t; ) {
        readint(&a);
        readint(&b);
        printf("Case %d: %d\n", cnum, (a + b)/gcd(a, b));
    }
    return 0;
}
Ejemplo n.º 13
0
void readFArr1D(ifstream& stream, FArr1D& v)
{
  int Lo1, Hi1;
  char s[4];
  readint(stream, Lo1);
  readint(stream, Hi1);
  assert( v.L1() == Lo1 && v.H1() == Hi1 );  // Check lim match.
  for(int i=Lo1; i<=Hi1; i++) stream >> v(i);
  stream.getline(s, 4);
//  stream.ignore(80, '\n');
};
/** @brief Se leen los trenes para la formacion matinal del canal estandar de entrada.
 *  \pre cierto
 *  \post Retorna una lista con los trenes.
 */
list<Tren> Cjt_Cocheras::get_trenes_formacion_matinal(Cjt_Trenes &trenes) const {

  list<Tren> trenes_formacion;
  int num_trenes = readint();

  for (int i = 0; i < num_trenes; i++) {

    int id_tren = readint();
    Tren tren = trenes.get_tren(id_tren);

    trenes_formacion.insert(trenes_formacion.end(), tren);
  }
  return trenes_formacion;
}
Ejemplo n.º 15
0
/* }}} end FAST integer input */
int main(void){
	int t; readint(t);
	for(int cnum = 0, n; cnum++ < t; ){
		readint(n);
		int cs = 0, ms = 0;
		for(int i = 0, p = 0, z; i++ < n; p = z){
			readint(z);
			int dist = z - p;
			if(cs == dist) --cs;
			else if(cs < dist) cs = dist > ms ? (ms = dist) - 1 : ++ms;
		}
		printf("Case %d: %d\n", cnum, ms);
	}
	return 0;
}
Ejemplo n.º 16
0
static void print_comments(char *comments, int length)
{
   char *c=comments;
   int len, i, nb_fields;
   char *end;
   
   if (length<8)
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   end = c+length;
   len=readint(c, 0);
   c+=4;
   if (len < 0 || c+len>end)
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   fwrite(c, 1, len, stderr);
   c+=len;
   fprintf (stderr, "\n");
   if (c+4>end)
   {
      fprintf (stderr, "Invalid/corrupted comments\n");
      return;
   }
   nb_fields=readint(c, 0);
   c+=4;
   for (i=0;i<nb_fields;i++)
   {
      if (c+4>end)
      {
         fprintf (stderr, "Invalid/corrupted comments\n");
         return;
      }
      len=readint(c, 0);
      c+=4;
      if (len < 0 || c+len>end)
      {
         fprintf (stderr, "Invalid/corrupted comments\n");
         return;
      }
      fwrite(c, 1, len, stderr);
      c+=len;
      fprintf (stderr, "\n");
   }
}
Ejemplo n.º 17
0
void main_nodisplay() {
  float x, y, z = 0;
  
  // main loop  
   while(1) 
  {  
    led1=led2=led3=led4=0;
    mnu->SetScreen("Wait for file ...");
    while (srv->State() == listen)
        Net::poll();
    GetFile();
    mot->reset();
    plan_get_current_position_xyz(&x, &y, &z);
     printf("%f %f\n", x,y); 
    mnu->SetScreen("Laser BUSY..."); 
    
    char name[32];
    srv->getFilename(name);
    printf("Now processing file: '%s'\n\r", name);
    FILE *in = sd.openfile(name, "r");
    while (!feof(in))
    { 
      while (!mot->ready() );
      mot->write(readint(in));
    }
    fclose(in);
    removefile(name);
    // done
    printf("DONE!...\n");
	while (!mot->ready() );
    mot->moveTo(cfg->xrest, cfg->yrest, cfg->zrest);
  }
}
Ejemplo n.º 18
0
int main() {
	int n;
	n = readint("Please input an integer: ");
	print("Your input number is: %d", n);

	return 0;
}
Ejemplo n.º 19
0
void
emitT(int t)
{
	int i, j, k;

	printf("\n");
	printf("static int T%d[] = {\n", t);

	for (i=k=0; i<maxlen; i++) {
		for (j=0; j<256; j++) {
			int v = readint();
			if (compact && (j < minchar || j > maxchar))
				continue;
			if (k == 0)
				printf("\t");
			printf("%d,", v);
			if (++k == 10) {
				printf("\n");
				k = 0;
			}
			else
				printf(" ");
		}
	}
	if (k)
		printf("\n");
	printf("};\n");
}
Ejemplo n.º 20
0
int main()
{
    char line[MAXL];
    int i;

    eofread = FALSE;

    readint( &ndw );

    for ( i = 0; i < ndw; i++ )
    {
        dlengths[i] = getline( dictionary[i] );
    }

    while ( ( nic = getline( line ) ) != FAIL )
    {
        niw = split( line );

        init();

        if ( backtrack( 0 ) )
            process( line );
        else
            failed( line );
    }

    return 0;
}
Ejemplo n.º 21
0
void Servidor::addPeliculas(int n){
  int x;
  for(int i = 0; i < n; ++i) {
    x = readint();
    lpel[x-1] = true;
  }
}
Ejemplo n.º 22
0
void Servidor::deletePeliculas(int n){
  int x; 
  for(int i = 0; i < n; ++i) {
    x = readint();
    lpel[x-1] = false;
  }
}
Ejemplo n.º 23
0
int msgxpatch (void)
{
	int x;
	int i;

	if (server) {
		x = readmsg (MSGXPATCH);
		if (x != SCMOK)  return (x);
		x = readint (&xargc);
		if (x != SCMOK)  return (x);
		xargc += 2;
		xargv = (char **)calloc (sizeof (char *),(unsigned)xargc+1);
		if (xargv == NULL)
			return (SCMERR);
		for (i = 2; i < xargc; i++) {
			x = readstring (&xargv[i]);
			if (x != SCMOK)  return (x);
		}
		x = readmend ();
	} else {
		x = writemsg (MSGXPATCH);
		if (x != SCMOK)  return (x);
		x = writeint (xargc);
		if (x != SCMOK)  return (x);
		for (i = 0; i < xargc; i++) {
			x = writestring (xargv[i]);
			if (x != SCMOK)  return (x);
		}
		x = writemend ();
	}
	return (x);
}
Ejemplo n.º 24
0
int msgneed (void)
{
	int x;
	if (server) {
		char *name;
		int update;
		TREE *t;
		x = readmsg (MSGNEED);
		if (x == SCMOK)  x = readstring (&name);
		while (x == SCMOK) {
			if (name == NULL)  break;
			x = readint (&update);
			if (x != SCMOK)  break;
			t = Tinsert (&needT,name,TRUE);
			free (name);
			if (update)  t->Tflags |= FUPDATE;
			x = readstring (&name);
		}
		if (x == SCMOK)  x = readmend ();
	} else {
		x = writemsg (MSGNEED);
		if (x == SCMOK)  x = Tprocess (needT,needone);
		if (x == SCMOK)  x = writestring ((char *)NULL);
		if (x == SCMOK)  x = writemend ();
	}
	return (x);
}
Ejemplo n.º 25
0
void readFArr2D(ifstream& stream, FArr2D& v)
{
  int Lo1, Hi1, Lo2, Hi2;
  char s[4];

  readint(stream, Lo1);
  readint(stream, Hi1);
  readint(stream, Lo2);
  readint(stream, Hi2);
  assert( v.L1() == Lo1 && v.H1() == Hi1 && v.L2() == Lo2 && v.H2() == Hi2 );
  for(int i1 = Lo1; i1 <= Hi1; i1++)
  {
    for(int i2 = Lo2; i2 <= Hi2; i2++) stream >> v(i1,i2);
    if (i1 == Hi1) stream.getline(s, 4);
//    if (i1 == Hi1) stream.ignore(80, '\n');
  };
};
Ejemplo n.º 26
0
void Estudiant::llegir_estudiant()
{
  DNI= readint();
  if (DNI<0) throw PRO2Excepcio(ER4);
  double x = readdouble();
  if (x >= 0 and x <= MAX_NOTA)    nota = x;
  else nota=-1;
}
Ejemplo n.º 27
0
int main() {
    n = readint();
    for (int i = 0; i < n; i++) {
        grid[i] = readint();
        scores[i] = std::make_pair(grid[i], i + 1);
    }
    std::sort(scores, scores + n);
    
    for (int i = 0; i < n; i++) grid[scores[i].second] = n - i;

    ans = 0;
    for (int j = 1; j <= 100005; j += j & -j) bit[j]++;
    for (int i = 1; i <= n; i++) {
        for (int j = grid[i]; j > 0; j -= j & -j) ans += bit[j];
        for (int j = grid[i]; j <= 100005; j += j & -j) bit[j]++;
    }
    printf("%.2lf\n", double(ans) / double(n));
}
Ejemplo n.º 28
0
int main(){
  int x= readint();
  vector <Estudiant> V(x);
  leer_vector(V);
  vector <Estudiant> S(x);
  int j;
  S= simplificar(V,j);
  escribir_estudiant(S,j);
}
/** @brief Se leen los datos de las cocheras de la red del canal estandar de entrada.
 *  \pre cierto
 *  \post Los datos del parametro implicito son los leidos de la entrada.
 */
void Cjt_Cocheras::leer_cocheras() {
  for (int i = 0; i < _cocheras.size(); i++) {

    _cocheras[i].leer_cochera();
    int prioridad = readint();

    _prioridades[prioridad - 1] = i;
  }
}
Ejemplo n.º 30
0
static int readint4( const char** source, int* x, int* y, int* z, int* w )
{
  if ( readint3( source, x, y, z ) != 0 )
  {
    return -1;
  }

  return readint( source, w );
}