Ejemplo n.º 1
0
// Parse an identifier from the input stream
void parseid(void) {
	char c = *idbuf = tolower(inchar);
	byte idbuflen = 1;
	fetchc();
	while (isalnum(inchar) || (inchar == '.') || (inchar == '_')) {
		if (idbuflen >= IDLEN) overflow(M_id);
		idbuf[idbuflen++] = tolower(inchar);
		fetchc();
	}
	idbuf[idbuflen] = 0;

	// do we have a one-char alpha nvar identifier?
	if ((idbuflen == 1) && isalpha(c)) {
		sym = s_nvar;
		symval = c - 'a';
	}
	
	// a pin identifier 'a'digit* or 'd'digit*?
	else if ((idbuflen <= 3) &&
		((c == 'a') || (c == 'd')) && 
		isdigit(idbuf[1]) && (
#if !defined(TINY85)
		isdigit(idbuf[2]) || 
#endif
		(idbuf[2] == 0))) {
		sym = (c == 'a') ? s_apin : s_dpin;
		symval = pinnum(idbuf);
	}

	// reserved word?
	else if (findindex(idbuf, (const prog_char *) reservedwords, 1)) {
		sym = pgm_read_byte(reservedwordtypes + symval);	// e.g., s_if or s_while
	}

	// function?
	else if (findindex(idbuf, (const prog_char *) functiondict, 1)) sym = s_nfunct;
#ifdef LONG_ALIASES
	else if (findindex(idbuf, (const prog_char *) aliasdict, 0)) sym = s_nfunct;
#endif

#ifdef PIN_ALIASES
	else if (findpinname(idbuf)) {;}		// sym and symval are set in findpinname
#endif

#ifdef USER_FUNCTIONS
	else if (find_user_function(idbuf)) sym = s_nfunct;
#endif

	else findscript(idbuf);
}
Ejemplo n.º 2
0
byte findpinname(char *alias) {
	if (!findindex(alias, (const prog_char *) pinnames, 0)) return 0;		// sets symval
	byte pin = pgm_read_byte(pinvalues + symval);
	sym = (pin & PV_ANALOG) ? s_apin : s_dpin;
	symval = pin & PV_MASK;
	return 1;
}
Ejemplo n.º 3
0
PetscInt calcPeriodicInterpFactor(PetscInt n,PetscScalar t,PetscScalar tparr[],PetscInt *itf1,PetscInt *itf2,PetscScalar *al1,PetscScalar *al2)
/* Compute interpolation factor and indices for interpolation on a periodic grid */
/* tparr is a periodic array of length n+2 */

{
  PetscInt it1, it2;

  /* 0<=t<=Tc, tparr[n] < Tc < tparr[n+1] */  
  it1=findindex(tparr,n+2,t);   
  if (it1<0) SETERRQ(1,"Error in findindex: time out of bound");

  it1=MIN(it1,n); /* it1=n, if t==tparr[n+1]. Not really necessary. */  
  it2=it1+1;  
  /* it1,it2 are referenced to tparr (tparr[0] tparr[1] ... tparr[n] tparr[n+1]) */
  /* 0<=it1<=n, 1<=it2<=n+1 */
  /* Data are given at tparr[1] ... tparr[n] */
  *al1=(tparr[it2]-t)/(tparr[it2]-tparr[it1]);
  *al2=1.0-(*al1);
  

  /* now index to actual data arrays */
  it1=it1-1;
  it2=it2-1;
  
  if (it1==-1) {
    it1=n-1;
    it2=0;
  }
  if (it1==n-1) it2=0;

  *itf1=it1;
  *itf2=it2;
   
  return 0;
}
Ejemplo n.º 4
0
bool DJI_Pro_Hw::Pro_Hw_Setup(QString port_name,int baudrate)
{
    QString str = "\\\\.\\";
    int index=findindex(baudrate);
      if (-1 == index)
    {
        index=baudrate;
    }
    bool ret = false;
    str += port_name;
    port->setPortName(str);
    ret = port->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
    port->setBaudRate((BaudRateType)index);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    //set timeouts to 20 ms
    port->setTimeout(20);
   // port->setQueryMode(QextSerialBase::EventDriven);

    if(ret == true)
    {
        printf("OPEN SUCCESS\n");
        port->flush();
    }
    return ret;
}
Ejemplo n.º 5
0
bool shouldReport(uint8_t sensorType, int offset, int *index) {

	bool rc = false;

	if (findindex(sensorType, offset, index)) { rc = true;	}

	if (rc==false) { debug_print_ok_to_dont_care(sensorType, offset); }

	return rc;
}
void main()
{
 int a[MAX]={0,0},k=0,i=0;
 printf("An inifinite series is created......");
 for(i=0;i<300;i++)
   a[i]=i;
 for(i=300;i<400;i++)
   a[i]='$';
 i=0;
 while(i<400)
 {
     printf("%d ",a[i++]);
 }
 printf("\nEnter the element you are searching for = ");
 scanf("%d",&k);
 int n=findinfiniteseries(a,k);
 if(findindex(a,0,n-1,k)!=-1)
   printf("\nThe element you are looking exists at index=%d",findindex(a,0,n-1,k));
 else
    printf("\nElement doesnot exists !!!");

}
Ejemplo n.º 7
0
PetscErrorCode calcInterpFactor( PetscInt n, PetscScalar t, PetscScalar tarr[], PetscInt *itf, PetscScalar *alpha)
{
   PetscInt it1, it2;
   
   it1=findindex(tarr,n,t);
   
   if (it1<0) SETERRQ(1,"Error in findindex: time out of bound");

   it1=MIN(it1,n-2);  /*   it1=n-2 if t==tarr[n-1] */
   it2=it1+1;
   *itf=it1;
   *alpha=(tarr[it2]-t)/(tarr[it2]-tarr[it1]);
   return 0;
}
int findindex(int a[],int low,int high,int k)
{
  int mid=0;
  if(low<high)
  {
      mid=(high+low)/2;
      if(a[mid]==k)
        return mid;
      else if(a[mid]>k)
         return findindex(a,low,mid-1,k);
      else
        return findindex(a,mid+1,high,k);
  }
  else if(low==high)
  {
     if(a[low]==k)
        return low;
     else
    return -1;
  }
  else
    return -1;

}
Ejemplo n.º 9
0
int lvH_next (lv_State *L, Table *t, StkId key) {
    int i = findindex(L, t, key);  /* find original element */
    for (i++; i < t->sizearray; i++) {  /* try first array part */
        if (!ttisnil(&t->array[i])) {  /* a non-nil value? */
            setnvalue(key, cast_num(i+1));
            setobj2s(L, key+1, &t->array[i]);
            return 1;
        }
    }
    for (i -= t->sizearray; i < sizenode(t); i++) {  /* then hash part */
        if (!ttisnil(gval(gnode(t, i)))) {  /* a non-nil value? */
            setobj2s(L, key, key2tval(gnode(t, i)));
            setobj2s(L, key+1, gval(gnode(t, i)));
            return 1;
        }
    }
    return 0;  /* no more elements */
}
Ejemplo n.º 10
0
void TXCache::lookup(const void *key, void *data)
{
	status_t r;
	int i;
	t_fetchdata_params p;
	thread_id tid;

restart:
	m_lock.Lock();
	i=findindex(key);
	if(i<0){
		acquire_sem(m_fetchers_sem);
		i=findoldest();
		assert(m_fetcher[i]==0);
		reward(i);	// ceci empêchera i d'être choisi par findoldest() 
					//  dans un autre thread.
		memcpy(getkey(i),key,m_keysize);
		p.txcache=this;
		p.key=key;
		p.data=getdata(i);
		tid=spawn_thread(fetchdata_,"TXCache::fetchdata",
			B_NORMAL_PRIORITY,&p);
		if(tid<B_NO_ERROR){
			printf("spawn_thread failed file=%s line %d\n",__FILE__,__LINE__);
			exit(-1);
		}
		m_fetcher[i]=tid;
		m_lock.Unlock();	// on déverouille. On est protégé par m_fetchers_sem
		wait_for_thread(tid,&r);
		m_lock.Lock();
		m_fetcher[i]=0;
		release_sem(m_fetchers_sem);
	}else{
		tid=m_fetcher[i];
		if(tid){
			m_lock.Unlock();
			wait_for_thread(tid,&r);
			goto restart;
		}
	}
	memcpy(data,getdata(i),m_datasize);
	reward(i);
	m_lock.Unlock();
}
Ejemplo n.º 11
0
int luaH_next (lua_State *L, Table *t, StkId key) {
  unsigned int asize = luaH_realasize(t);
  unsigned int i = findindex(L, t, s2v(key), asize);  /* find original key */
  for (; i < asize; i++) {  /* try first array part */
    if (!isempty(&t->array[i])) {  /* a non-empty entry? */
      setivalue(s2v(key), i + 1);
      setobj2s(L, key + 1, &t->array[i]);
      return 1;
    }
  }
  for (i -= asize; cast_int(i) < sizenode(t); i++) {  /* hash part */
    if (!isempty(gval(gnode(t, i)))) {  /* a non-empty entry? */
      Node *n = gnode(t, i);
      getnodekey(L, s2v(key), n);
      setobj2s(L, key + 1, gval(n));
      return 1;
    }
  }
  return 0;  /* no more elements */
}
Ejemplo n.º 12
0
int kp_table_next(ktap_State *ks, Table *t, StkId key)
{
	int i = findindex(ks, t, key);  /* find original element */

	for (i++; i < t->sizearray; i++) {  /* try first array part */
	        if (!ttisnil(&t->array[i])) {  /* a non-nil value? */
			setnvalue(key, i+1);
			setobj(ks, key+1, &t->array[i]);
			return 1;
		}
	}

	for (i -= t->sizearray; i < sizenode(t); i++) {  /* then hash part */
		if (!ttisnil(gval(gnode(t, i)))) {  /* a non-nil value? */
			setobj(ks, key, gkey(gnode(t, i)));
			setobj(ks, key+1, gval(gnode(t, i)));
			return 1;
		}
	}

	return 0;  /* no more elements */
}
Ejemplo n.º 13
0
Archivo: tune.c Proyecto: aahud/harvey
void
tune_out(Rune *r, int n, long *x)
{
	static int state = 0;
	static Rune lastr;
	Rune *er, tr, rr;
	char *p;
	int i;

	USED(x);
	nrunes += n;
	er = r+n;
	for(p = obuf; r < er; r++){
		switch(state){
		case 0:
		case0:
			if((tr = findbyuni(t3, nelem(t3), *r)) != Runeerror){
				lastr = tr;
				state = 1;
			}else if(*r == 0x0b92/*ஒ*/){
				lastr = 0xe20a/**/;
				state = 3;
			}else if((tr = findbyuni(t1, nelem(t1), *r)) != Runeerror)
				p += runetochar(p, &tr);
			else
				p += runetochar(p, r);
			break;
		case 1:
		case1:
			if((i = findindex(t2, nelem(t2), *r)) != -1){
				if(lastr && lastr != Runeerror)
					lastr += i-1;
				if(*r ==0x0bc6/*ெ*/)
					state = 5;
				else if(*r ==0x0bc7/*ே*/)
					state = 4;
				else if(lastr == 0xe210/**/)
					state = 2;
				else if(lastr == 0xe340/**/)
					state = 6;
				else{
					if(lastr)
						p += runetochar(p, &lastr);
					state = 0;
				}
			}else if(lastr && lastr != Runeerror && (*r == 0x00b2/*²*/ || *r == 0x00b3/*³*/ || *r == 0x2074/*⁴*/)){
				if(squawk)
					EPR "%s: character <U+%.4X, U+%.4X> not in output cs\n", argv0, lastr, *r);
				lastr = clean ? 0 : Runeerror;
				nerrors++;
			}else{
				if(lastr)
					p += runetochar(p, &lastr);
				state = 0;
				goto case0;
			}
			break;
		case 2:
			if(*r == 0x0bb7/*ஷ*/){
				lastr = 0xe381/**/;
				state = 1;
				break;
			}
			p += runetochar(p, &lastr);
			state = 0;
			goto case0;
		case 3:
			state = 0;
			if(*r == 0x0bd7/*ௗ*/){
				rr = 0xe20c/**/;
				p += runetochar(p, &rr);
				break;
			}
			p += runetochar(p, &lastr);
			goto case0;
		case 4:
			state = 0;
			if(*r == 0x0bbe/*ா*/){
				if(lastr){
					if(lastr != Runeerror)
						lastr += 3;
					p += runetochar(p, &lastr);
				}
				break;
			}
			if(lastr)
				p += runetochar(p, &lastr);
			goto case0;
		case 5:
			state = 0;
			if(*r == 0x0bbe/*ா*/ || *r == 0x0bd7/*ௗ*/){
				if(lastr){
					if(lastr != Runeerror)
						lastr += *r == 0x0bbe/*ா*/ ? 3 : 5;
					p += runetochar(p, &lastr);
				}
				break;
			}
			if(lastr)
				p += runetochar(p, &lastr);
			goto case0;
		case 6:
			if(*r == 0x0bb0/*ர*/){
				state = 7;
				break;
			}
			p += runetochar(p, &lastr);
			state = 0;
			goto case0;
		case 7:
			if(*r == 0x0bc0/*ீ*/){
				rr = 0xe38d/**/;
				p += runetochar(p, &rr);
				state = 0;
				break;
			}
			p += runetochar(p, &lastr);
			lastr = 0xe2c1/**/;
			state = 1;
			goto case1;
		}
	}
Ejemplo n.º 14
0
bool Targets::isintargetlist(SpaceObject *o)
{
	STACKTRACE;
	return findindex(o) >= 0;
}
Ejemplo n.º 15
0
void 
inits3d (
    struct vtx_data **graph,	/* graph data structure for vertex weights */
    double **xvecs,		/* values to partition with */
    double *vals[8][MAXSETS],	/* values in sorted lists */
    int *indices[8][MAXSETS],	/* indices sorting lists */
    int nvtxs,		/* number of vertices */
    double *dist,			/* trial separation point */
    int startvtx[8][MAXSETS],	/* indices defining separation */
    double *size,			/* size of each set being modified */
    int *sets			/* set each vertex gets assigned to */
)
{
    double    xmid, ymid, zmid;	/* median x, y and z values */
    double    val, bestval;	/* values for determining set preferences */
    int     bestset = 0;	/* set vertex wants to be in */
    int       signx, signy, signz;	/* sign values for different target points */
    int       nsets = 8;	/* number of different sets */
    int       i, j;		/* loop counters */
    int       findindex();

/*
    xmid = .25 * (vals[0][1][indices[0][1][nvtxs / 2]] +
		  vals[0][1][indices[0][1][nvtxs / 2 - 1]]);
    ymid = .25 * (vals[0][2][indices[0][2][nvtxs / 2]] +
		  vals[0][2][indices[0][2][nvtxs / 2 - 1]]);
    zmid = .25 * (vals[0][4][indices[0][4][nvtxs / 2]] +
		  vals[0][4][indices[0][4][nvtxs / 2 - 1]]);
*/
    xmid = .5 * vals[0][1][indices[0][1][nvtxs / 2]];
    ymid = .5 * vals[0][2][indices[0][2][nvtxs / 2]];
    zmid = .5 * vals[0][4][indices[0][4][nvtxs / 2]];

    dist[0] = -xmid - ymid - zmid;
    dist[1] = xmid - ymid - zmid;
    dist[2] = -xmid + ymid - zmid;
    dist[3] = xmid + ymid - zmid;
    dist[4] = -xmid - ymid + zmid;
    dist[5] = xmid - ymid + zmid;
    dist[6] = -xmid + ymid + zmid;
    dist[7] = xmid + ymid + zmid;

    /* Now initialize startvtxs. */
    startvtx[0][1] = startvtx[2][3] = startvtx[4][5] = startvtx[6][7] = nvtxs / 2;
    startvtx[0][2] = startvtx[1][3] = startvtx[4][6] = startvtx[5][7] = nvtxs / 2;
    startvtx[0][4] = startvtx[1][5] = startvtx[2][6] = startvtx[3][7] = nvtxs / 2;
    startvtx[0][3] = startvtx[4][7] =
       findindex(indices[0][3], vals[0][3], dist[3] - dist[0], nvtxs);
    startvtx[1][2] = startvtx[5][6] =
       findindex(indices[1][2], vals[1][2], dist[2] - dist[1], nvtxs);
    startvtx[0][5] = startvtx[2][7] =
       findindex(indices[0][5], vals[0][5], dist[5] - dist[0], nvtxs);
    startvtx[1][4] = startvtx[3][6] =
       findindex(indices[1][4], vals[1][4], dist[4] - dist[1], nvtxs);
    startvtx[0][6] = startvtx[1][7] =
       findindex(indices[0][6], vals[0][6], dist[6] - dist[0], nvtxs);
    startvtx[2][4] = startvtx[3][5] =
       findindex(indices[2][4], vals[2][4], dist[4] - dist[2], nvtxs);
    startvtx[0][7] = findindex(indices[0][7], vals[0][7], dist[7] - dist[0], nvtxs);
    startvtx[1][6] = findindex(indices[1][6], vals[1][6], dist[6] - dist[1], nvtxs);
    startvtx[2][5] = findindex(indices[2][5], vals[2][5], dist[5] - dist[2], nvtxs);
    startvtx[3][4] = findindex(indices[3][4], vals[3][4], dist[4] - dist[3], nvtxs);

    /* Finally, determine the set sizes based on this splitter. */

    for (i = 0; i < nsets; i++)
	size[i] = 0;

    for (i = 1; i <= nvtxs; i++) {
	/* Which set is this vertex in? */
	signx = signy = signz = -1;
	bestval = 0;
	for (j = 0; j < nsets; j++) {
	    val = -dist[j] + 2 * (signx * xvecs[1][i] + signy * xvecs[2][i] + signz * xvecs[3][i]);
	    if (j == 0 || val < bestval) {
		bestval = val;
		bestset = (int) j;
	    }
	    if (signx == 1 && signy == 1)
		signz *= -1;
	    if (signx == 1)
		signy *= -1;
	    signx *= -1;
	}
	sets[i] = bestset;
	size[bestset] += graph[i]->vwgt;
    }
}
Ejemplo n.º 16
0
void main(){
	char *name=NULL;
	unsigned long maxl=MAXLENGTH;
	name = (char *)malloc(maxl * sizeof(char));
	int **cor;
	cor=malloc(1 * sizeof(int*));
	int nume = 0;
	int all = 0;
	int start;
	int end;
	int index = 0;
	int alloc = 0;
	struct node *first=NULL;
	while (getline(&name,&maxl,stdin)!=EOF){
		name[strlen(name)-1]='\0';
		if (name[0] == 'V'){
			if(first!=NULL){
				free(first);
				first=NULL;
			}
			index = 0;
			int len = strlen(name);
			int num = 0;
			int i = 2;
			int j = 0;
			int p = 1;
			int fau = 0;
			for (i = 2; i<strlen(name); i++){
				p = 1;
				for (j=1;j<=len-i-1;j++){
					p = p * 10;
				}
				if (!(name[i] - '0' >= 0 && name[i] - '0' <= 9)){
					fprintf(stdout, "Error: wrong V\n");
					fau = 1;
					break;
				}
				else{
					num = num + p*(name[i] - '0');
				}
			}

			if (num == 0 && fau == 0){
				fprintf(stdout, "Error: wrong V\n");
				continue;
			}
			else{
				all = num;
			}
			index = 1;
		}
		else if (name[0] == 'E'){
			alloc = 0;
			if (cor != NULL){
				free(cor);
			}
			cor = malloc(1 * sizeof(int*));
			if (index != 1){
				fprintf(stdout, "Error: Wrong input.\n");
				continue;
			}
			nume = 0;
			int i = 4;
			char x[100];
			int pointer = 0;
			int xp = 0;
			memset(x, 0, 100 * sizeof(char));
			int fault = 0;
			int outof = 0;
			for (i = 4; i<strlen(name) - 1; i++){
				if (fault == 1) break;

				if (name[i] == ',' && name[i - 1] != '>'){
					pointer = 1;
					cor = realloc(cor, (alloc + 1)*sizeof(int*));
					cor[alloc++] = malloc(2 * sizeof(int));
					cor[nume][0] = getint(x);
					if (cor[nume][0]<0 || cor[nume][0] >= all){
						fprintf(stdout, "Error: Wrong point\n");
						fault = 1;
						break;
					}
					memset(x, 0, 100 * sizeof(char));
					xp = 0;


				}
				else if (name[i] == '>' && pointer == 1){
					long long mul = (long)all*(long)all;
					if (nume >= mul){
						fprintf(stdout, "Error: Wrong input of E\n");
						fault = 1;
						break;
					}
					pointer = 0;
					cor[nume][1] = getint(x);
					if (cor[nume][1]<0 || cor[nume][1] >= all){
						fprintf(stdout, "Error: Wrong point\n");
						fault = 1;
						break;
					}
					struct node *anode = malloc(sizeof(struct node));
					anode->index = cor[nume][0];
					anode->key = 999999;
					anode->next = NULL;
					anode->child = NULL;
					struct node *tf = first;
					int same = 0;
					if (tf == NULL){
						first = anode;
						struct node *sc = malloc(sizeof(struct node));
						sc->next = NULL;
						sc->child = NULL;
						sc->index = cor[nume][1];
						sc->key = 999999;
						first->next =sc;
						same = 1;
					}
					else{
						while (tf != NULL){
							if (tf->index == anode->index){
								struct node *s2 = findindex(tf->next, cor[nume][1]);
								if (s2 == NULL){
									struct node *ss2 = malloc(sizeof(struct node));
									ss2->next = NULL;
									ss2->child = NULL;
									ss2->key = 999999;
									ss2->index = cor[nume][1];
									ss2->next = tf->next;
									tf->next = ss2;
								}
								same = 1;
								break;
							}
							else{
								tf = tf->child;
							}
						}
					}
					if (same == 0){
						anode->child = first;
						first = anode;
						struct node *s3 = malloc(sizeof(struct node));
						s3->next = NULL;
						s3->child = NULL;
						s3->key = 999999;
						s3->index = cor[nume][1];
						first->next = s3;
					}


					struct node *anode2=malloc(sizeof(struct node));
					anode2->index = cor[nume][1];
					anode2->key = 999999;
					anode2->child = NULL;
					anode2->next = NULL;
					struct node *tf2 = first;
					int same2 = 0;
					while (tf2 != NULL){
						if (tf2->index == anode2->index){
							struct node *s4 = findindex(tf2->next, cor[nume][0]);
							if (s4 == NULL){
								struct node *ss4 = malloc(sizeof(struct node));
								ss4->next = NULL;
								ss4->child = NULL;
								ss4->key = 999999;
								ss4->index = cor[nume][0];
								ss4->next = tf2->next;
								tf2->next = ss4;
							}
								same2 = 1;
								break;
							}
						else{
							tf2 = tf2->child;
						}
					}
					if (same2 == 0){
						anode2->child = first;
						first = anode2;
						struct node *s5 = malloc(sizeof(struct node));
						s5->child = NULL;
						s5->next= NULL;
						s5->key = 999999;
						s5->index = cor[nume][0];
						first->next = s5;
					}
					nume++;
					memset(x, 0, 100 * sizeof(char));
					xp = 0;
				}
				else if (name[i] >= '0' && name[i] <= '9'){
					x[xp++] = name[i];
				}
				else if(name[i]=='-'){
					fprintf(stdout, "Error: Wrong point\n");
					fault = 1;
					break;	
				}
				else{
					xp = 0;
				}

			}
			if (fault == 1) continue;
			index = 2;
			
		}
		else if (name[0] == 's'){
			if (index != 2){
				fprintf(stdout, "Error: Wrong input\n");
				continue;
			}
			int i = 0;
			int p1 = 0;
			char s1[100];
			memset(s1, 0, 100 * sizeof(char));
			int special=0;
			for (i = 2; i<strlen(name); i++){
				if (name[i] >= '0' && name[i] <= '9'){
					s1[p1++] = name[i];
				}
				else if (name[i] == ' '){
					start = getint(s1);
					p1 = 0;
					memset(s1, 0, 100 * sizeof(char));
				}else{
					special=1;
				}
			}
			end = getint(s1);
			if (start<0 || start >= all || end<0 || end >= all || special==1){
				fprintf(stdout, "Error: Wrong point\n");
				continue;
			}
			if (start == end){
				fprintf(stdout, "%d-%d\n", start, end);
				continue;
			}
			struct node *st = first;
			st = findchild(st, start);
			st->key = 0;
			struct node *head = first;
			struct node *hnext = first;
			struct node *h = first;
			while (head != NULL){
				while (h!= NULL){
					hnext = h;
					while (hnext->next!= NULL){
						struct node *fd = findchild(first, hnext->next->index);
						if (fd->key>(h->key + 1)){
							fd->key = h->key + 1;
							fd->parent = h->index;
						}
						hnext = hnext->next;
					}
					h = h->child;
				}
				h = first;
				head = head->child;
			}
			struct node *pa = findchild(first, end);
			struct node *sa = findchild(first, start);
			if ( pa==NULL || sa ==NULL||pa->key >= 999999){
				fprintf(stdout, "Error: no path from  %d to %d\n", start, end);
				continue;
			}
			else{
				int path[100000];
				int pi = 0;
				path[pi++] = end;
				struct node *st3 = pa;
				int counti = 0;
				while (pa->parent != start){
					path[pi++] = pa->parent;
					pa = findchild(first, pa->parent);
				}
				path[pi++] = start;
				int count = 0;
				for (count = pi - 1; count>0; count--){
					fprintf(stdout, "%d-", path[count]);
				}
				fprintf(stdout, "%d\n", path[0]);
			}
			struct node *head2=first;
			struct node *next2=first;
			while(head2!=NULL){
				next2=head2;
				while(next2!=NULL){
					next2->key=999999;
					next2=next2->next;
				}
				head2=head2->child;
			}
		}
		memset(name, 0, maxl * sizeof(char));
	}
}
Ejemplo n.º 17
0
int
main(int argc,char **argv)
{
    GR_COORD	x;
    GR_COORD	y;
    GR_SIZE		width;
    GR_SIZE		height;
    GR_COORD	rightx;		/* x coordinate for right half stuff */
    GR_BOOL		setsize;	/* TRUE if size of board is set */
    GR_BOOL		setmines;	/* TRUE if number of mines is set */
    GR_SIZE		newsize = 10;	/* desired size of board */
    GR_COUNT	newmines = 25;	/* desired number of mines */
    GR_WM_PROPERTIES props;

    setmines = GR_FALSE;
    setsize = GR_FALSE;

    argc--;
    argv++;
    while ((argc > 0) && (**argv == '-')) {
        switch (argv[0][1]) {
        case 'm':
            if (argc <= 0) {
                fprintf(stderr, "Missing mine count\n");
                exit(1);
            }
            argc--;
            argv++;
            newmines = atoi(*argv);
            setmines = GR_TRUE;
            break;

        case 's':
            if (argc <= 0) {
                fprintf(stderr, "Missing size\n");
                exit(1);
            }
            argc--;
            argv++;
            newsize = atoi(*argv);
            setsize = GR_TRUE;
            break;

        default:
            fprintf(stderr, "Unknown option \"-%c\"\n",
                    argv[0][1]);
            exit(1);
        }
        argc--;
        argv++;
    }
    if (argc > 0)
        savefile = *argv;

    srand(time(0));

    readgame(savefile);

    if (setsize) {
        if ((newsize < MINSIZE) || (newsize > MAXSIZE)) {
            fprintf(stderr, "Illegal board size\n");
            exit(1);
        }
        if (newsize != size) {
            if (steps && playing) {
                fprintf(stderr,
                        "Cannot change size while game is in progress\n");
                exit(1);
            }
            playing = GR_FALSE;
            size = newsize;
            if (!playing)
                mines = (size * size * MINEPERCENT) / 100;
        }
    }

    if (setmines) {
        if ((newmines <= 0) || ((newmines > (size * size) / 2))) {
            fprintf(stderr, "Illegal number of mines\n");
            exit(1);
        }
        if (newmines != mines) {
            if (steps && playing) {
                fprintf(stderr,
                        "Cannot change mines while game is in progress\n");
                exit(1);
            }
            playing = GR_FALSE;
            mines = newmines;
        }
    }

    findindex();

    /*
     * Parameters of the game have been verified.
     * Now open the graphics and play the game.
     */

    if (GrOpen() < 0) {
        fprintf(stderr, "Cannot open graphics\n");
        exit(1);
    }

    GrReqShmCmds(655360); /* Test by Morten Rolland for shm support */

    GrGetScreenInfo(&si);
    GrGetFontInfo(0, &fi);
    charheight = fi.height;

    /*
     * Create the main window which will contain all the others.
     */
#if 0
    COLS = si.cols - 40;
#else
    COLS = si.cols;
#endif
    ROWS = si.rows - 80;
    mainwid = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, COLS, ROWS,
                          0, BLACK, WHITE);

    /* set title */
    props.flags = GR_WM_FLAGS_TITLE | GR_WM_FLAGS_PROPS;
    props.props = GR_WM_PROPS_APPFRAME | GR_WM_PROPS_CAPTION;
    props.title = "Land Mine";
    GrSetWMProperties(mainwid, &props);

    /*
     * Create the board window which lies at the left side.
     * Make the board square, and as large as possible while still
     * leaving room to the right side for statistics and buttons.
     */
    width = COLS - RIGHTSIDE - (si.xdpcm * RIGHTGAP / 10) - BOARDBORDER * 2;
    height = (((long) width) * si.ydpcm) / si.xdpcm;
    if (height > ROWS /* - y * 2*/) {
        height = ROWS - BOARDBORDER * 2;
        width = (((long) height) * si.xdpcm) / si.ydpcm;
    }
    xp = width / size;
    yp = height / size;

    width = xp * size - 1;
    height = yp * size - 1;
    x = BOARDBORDER;
    y = (ROWS - height) / 2;

    rightx = x + width + (si.xdpcm * RIGHTGAP / 10);
    boardwid = GrNewWindow(mainwid, x, y, width, height, BOARDBORDER,
                           BLUE, WHITE);
    /*
     * Create the buttons.
     */
    x = rightx;
    y = (si.ydpcm * BOARDGAP / 10);
    quitwid = GrNewWindow(mainwid, x, y, BUTTONWIDTH, BUTTONHEIGHT,
                          1, RED, WHITE);

    y += (si.ydpcm * BUTTONGAP / 10);
    savewid = GrNewWindow(mainwid, x, y, BUTTONWIDTH, BUTTONHEIGHT,
                          1, GREEN, WHITE);

    y += (si.ydpcm * BUTTONGAP / 10);
    newgamewid = GrNewWindow(mainwid, x, y, BUTTONWIDTH, BUTTONHEIGHT,
                             1, GREEN, WHITE);

    /*
     * Create the statistics window.
     */
    x = rightx;
    y += (si.ydpcm * STATUSGAP / 10);
    width = COLS - x;
    height = ROWS - y;
    statwid = GrNewWindow(mainwid, x, y, width, height, 0,
                          0, 0);
    statwidth = width;
    statheight = height;

    /*
     * Create the GC for drawing the board.
     */
    boardgc = GrNewGC();
    cleargc = GrNewGC();
    delaygc = GrNewGC();
    redgc = GrNewGC();
    greengc = GrNewGC();
    statgc = GrNewGC();
    blackgc = GrNewGC();
    buttongc = GrNewGC();
    xorgc = GrNewGC();
    GrSetGCBackground(boardgc, BLUE);
    GrSetGCForeground(cleargc, BLUE);
    GrSetGCForeground(redgc, RED);
    GrSetGCForeground(greengc, GREEN);
    GrSetGCForeground(statgc, GRAY);
    GrSetGCForeground(delaygc, BLACK);
    GrSetGCForeground(blackgc, BLACK);
    GrSetGCMode(delaygc, GR_MODE_XOR);
    GrSetGCMode(xorgc, GR_MODE_XOR);
    GrSetGCUseBackground(boardgc, GR_FALSE);
    GrSetGCUseBackground(buttongc, GR_FALSE);

    GrSelectEvents(mainwid, GR_EVENT_MASK_CLOSE_REQ);

    GrSelectEvents(boardwid, GR_EVENT_MASK_EXPOSURE |
                   GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_KEY_DOWN);

    GrSelectEvents(statwid, GR_EVENT_MASK_EXPOSURE);

    GrSelectEvents(quitwid, GR_EVENT_MASK_EXPOSURE |
                   GR_EVENT_MASK_BUTTON_DOWN);

    GrSelectEvents(newgamewid, GR_EVENT_MASK_EXPOSURE |
                   GR_EVENT_MASK_BUTTON_DOWN);

    GrSelectEvents(savewid, GR_EVENT_MASK_EXPOSURE |
                   GR_EVENT_MASK_BUTTON_DOWN);

    setcursor();

    GrMapWindow(mainwid);
    GrMapWindow(boardwid);
    GrMapWindow(statwid);
    GrMapWindow(quitwid);
    GrMapWindow(savewid);
    GrMapWindow(newgamewid);

    if (!playing)
        newgame();

    while (GR_TRUE) {
        GR_EVENT event;

        GrGetNextEvent(&event);
        handleevent(&event);
    }
}