Ejemplo n.º 1
0
int main(void)
{
    char (*a)[1000],(*b)[1000],(*c)[1100];
    char sum;
    int T,i = 0,j = 0;
    scanf("%d",&T);
    c = (char (*)[1100])malloc(sizeof(char) * 1100 * T);
    a = (char (*)[1000])malloc(sizeof(char) * 1000 * T);
    b = (char (*)[1000])malloc(sizeof(char) * 1000 * T);

    for(i = 0 ;i < T;i++)
    {
        memset(a[i],0,sizeof(a[i]));
        memset(b[i],0,sizeof(b[i]));
        memset(c[i],0,sizeof(c[i]));

        scanf("%s",a[i]);
        scanf("%s",b[i]);

        int len_a,len_b;
        len_a = strlen(a[i]);
        len_b = strlen(b[i]);
        int carry = 0;
        j = 0;
        while(len_a > j || len_b > j)
        {
            if(j < len_a && j < len_b)
                c[i][j] = char2int(a[i][len_a -1 - j]) + char2int(b[i][len_b -1 - j]) + carry;
            else if(j < len_b && j >= len_a)
                c[i][j] = char2int(b[i][len_b -1 - j]) + carry;
            else if(j < len_a && j >= len_b)
                c[i][j] = char2int(a[i][len_a -1 - j]) + carry;
            else
                break;
            carry = c[i][j] / 10;
            c[i][j] = c[i][j] % 10;
            c[i][j] = int2char(c[i][j]);
            j++;
        }
         if(carry)
             c[i][j] = int2char(carry);
    }
    for(i = 0;i < T ;i++)
    {
         printf("Case %d:\n", i+1);
         printf("%s + %s = ", a[i],b[i]);

         for(j = strlen(c[i]) - 1;j >= 0;j--)
             printf("%c", c[i][j]);
         if(i != T -1)
             printf("\n");
         printf("\n");
    }
    free(a);
    free(b);
    free(c);
}
Ejemplo n.º 2
0
xmlNodePtr nodeRef(NODE node)
{
	xmlNodePtr nd;
	nd = xmlNewNode(NULL, BAD_CAST "nd");
	xmlNewProp(nd, BAD_CAST "ref", BAD_CAST xmlEscape(int2char(node.id)));
	
	return nd;
}
Ejemplo n.º 3
0
char* int2char(int* num_seq, int size)
{
  char* seq = (char*) calloc(size+1, sizeof(char));
  for (int i=0; i<size; i++)
    seq[i] = int2char(num_seq[i]);

  return (seq);
}
Ejemplo n.º 4
0
xmlNodePtr wayElement(int id)
{
	xmlNodePtr osmWay;
	osmWay = xmlNewNode(NULL, BAD_CAST "way");
	xmlNewProp(osmWay, BAD_CAST "id", BAD_CAST xmlEscape(int2char(id)));
	
	return osmWay;
}
u16 mlgPassed(u16 len, u8 * buf_tx)
{
    u8 i = 0,crc_code;
    u8 *buf_tx_ptr;

	buf_tx_ptr = &buf_tx[len];
	mlgInfo.speedKM = info.speed;    //info.speed;
	*buf_tx_ptr++ = USD_CODE;
	//bufCopy(MLG_STRING,buf_tx_ptr,sizeof(MLG_STRING));
	memcpy(buf_tx_ptr,MLG_STRING, sizeof(MLG_STRING));
	buf_tx_ptr += sizeof(MLG_STRING)-1;
	*buf_tx_ptr++ = COMMA_CODE;

    mlgInfo.mileage = mileage; 
	i = getLenNum(mlgInfo.mileage);
	int2char((char * )buf_tx_ptr,mlgInfo.mileage,i,10);
	buf_tx_ptr += i;
	*buf_tx_ptr++ = COMMA_CODE;

	i = 3;
	if(mlgInfo.speedKM < 10) i = 1;
	else if(mlgInfo.speedKM < 100) i = 2;
	int2char((char * )buf_tx_ptr,mlgInfo.speedKM,i,10);
	buf_tx_ptr += i;
	 
    *buf_tx_ptr++ = ASTERISK_CODE;
    //CRC calculation
	*buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = CR_CODE;
    //*buf_tx_ptr = NL_CODE;
    *buf_tx_ptr++ = NL_CODE;	 // added ++ for finish 0
    //*buf_tx_ptr = 0;	         // added finish 0

	cnt_tx = findLen((const u8*)&buf_tx[len+1],MAX_SIZE_PACKET_GPRS_SIM900);
	crc_code = crcNavi((const u8*)&buf_tx[len+1],cnt_tx);
	int2char((char * )&buf_tx[cnt_tx+len+2],(crc_code >> 4) & 0x0F,1,16);
	int2char((char * )&buf_tx[cnt_tx+len+3],(crc_code & 0x0F),1,16);
	cnt_tx += 6;
	return cnt_tx;

}
Ejemplo n.º 6
0
void decodeHexString(const uint8_t *data, uint32_t length, std::string& str, bool swapEndian)
{
	if (swapEndian)
	{
		for (int i = length - 1; i >= 0; --i)
		{
			uint8_t c = data[i];
			str.push_back(int2char(c / 16));
			str.push_back(int2char(c % 16));
		}
	}
	else
	{
		for (int i = 0; i < length; ++i)
		{
			uint8_t c = data[i];
			str.push_back(int2char(c / 16));
			str.push_back(int2char(c % 16));
		}
	}
}
Ejemplo n.º 7
0
void decodeHexString(const uint8_t *data, uint32_t length, char* key, bool swapEndian)
{
	if (swapEndian)
	{
		for (int i = length - 1; i >= 0; --i)
		{
			uint8_t c = data[i];
			*key++ = int2char(c / 16);
			*key++ = int2char(c % 16);
		}
	}
	else
	{
		for (int i = 0; i < length; ++i)
		{
			uint8_t c = data[i];
			*key++ = int2char(c / 16);
			*key++ = int2char(c % 16);
		}
	}
	*key = '\0';
}
Ejemplo n.º 8
0
//-------------------------------------------------------------------------------------------
void TbFunFTPRead(void)
{
   //should make MAX checking
  static u16 nReadPrev = 0;
  if( (!sFTPState.flagAlrSent)  || (sFTPState.flagTimeout) )
   {
     if(!updateFlag)
	   {
	     if(sFTPState.flagLastPage)
		  {
		    //currState = stEndGPRS;  //was earlier - when there was not PUT-cmds
			currState = stOpenPutFTP;
			sFTPState.flagLastPage = 0;
			sFTPState.flagOutBoot = 1;
		  }
		 else  /* not last page*/
		  {
		   nRead = FTP_PACKET_SIZE - bytesFTPToReadForPacket;
		   if(sFTPState.flagFrontBlock)
		      nRead = FTP_SIZE_FRONT_BLOCK;
		   if(nRead != nReadPrev)  /* if we received less bytes we asked than we should scan added (to pageSize) number of bytes*/
		     {
				   //"AT+FTPGET=2,",
				   memset(ftpReadNumber,0,sizeof(ftpReadNumber));
				   iLen = getLenNum(nRead);
				   int2char((char * )ftpReadNumber,nRead,iLen,10);
				   memset(ftpReadBuf,0,sizeof(ftpReadBuf));
				   strcpy(ftpReadBuf,(char *)QUERIES[14]);
				   strncat(ftpReadBuf,ftpReadNumber,strlen(ftpReadNumber) );
				   strncat(ftpReadBuf,"\r\n",sizeof("\r\n") );
				   //sFTPState.flagScannedLess = 1;
			  }
			//else
			//   sFTPState.flagScannedLess = 0;

			 nReadPrev = nRead;
		     //at+ftpget=2,1024
			 if(sFTPState.flagPrepareToSend)
				{ 
				  sFTPState.flagPrepareToSend = 0;
				  if(!checkTimeoutFunFTP())
					 cmdSend((u8 *)ftpReadBuf);
				  timerFDTimeout = TIMEOUT_BEGIN_GPRS_CMD;
				  sFTPState.flagAlrSent = 1;
				  sFTPState.flagTimeToSend = 1;
				}
		  }
	   }
   }
}
Ejemplo n.º 9
0
/* 
   args:
      int_buf        array of ints, one base-36 digit per int
      int_buf_len    length of array of ints
      s              pre-allocated buffer into which the representation
                     of the int array will be placed.  the string is
		     the null-terminated. no check is made to determine
		     that the buffer is large enough, that is the caller's
		     responsibility, int_buf_len + 1 is enough.

   this function converts an array of integers, each element representing
   a base-36 value, into a character string representing the array
   leading 0's in the integer value are omitted
*/
void int2string(unsigned int *int_buf, int int_buf_len, char *s) {
  int int_buf_ind = 0, s_ind = 0;

  /* skip leading 0's */
  while (!int_buf[int_buf_ind] && int_buf_ind<int_buf_len) int_buf_ind++;

  while (int_buf_ind < int_buf_len) {
    s[s_ind] = int2char(int_buf[int_buf_ind]);
    s_ind++;
    int_buf_ind++;
  }
  s[s_ind] = '\0';
  return;
}
//-------------------------------------------------------------------------------------------
u16 gngsaPassed(u16 len, u8 * buf_tx)
{
    u8 *buf_tx_ptr,crc_code;
	buf_tx_ptr = &buf_tx[len];
	//$GNGSA,04,02*5A
   	int2char((char * )gngsaInfo.GPSNumber,info.satinfo.inuseGPS,2,10);
   	int2char((char * )gngsaInfo.GLNNumber,info.satinfo.inuseGLN,2,10);

	*buf_tx_ptr++ = USD_CODE;
	//bufCopy(GNGSA_STRING,buf_tx_ptr,sizeof(GNGSA_STRING));
	memcpy(buf_tx_ptr,GNGSA_STRING,sizeof(GNGSA_STRING));
	buf_tx_ptr += sizeof(GNGSA_STRING)-1;
	*buf_tx_ptr++ = COMMA_CODE;
	//bufCopy(gngsaInfo.GLNNumber,buf_tx_ptr,sizeof(gngsaInfo.GLNNumber));
	memcpy(buf_tx_ptr,gngsaInfo.GLNNumber,sizeof(gngsaInfo.GLNNumber));
	buf_tx_ptr += sizeof(gngsaInfo.GLNNumber);
    *buf_tx_ptr++ = COMMA_CODE;

	//bufCopy(gngsaInfo.GPSNumber,buf_tx_ptr,sizeof(gngsaInfo.GPSNumber));
	memcpy(buf_tx_ptr,gngsaInfo.GPSNumber,sizeof(gngsaInfo.GPSNumber));
	buf_tx_ptr += sizeof(gngsaInfo.GPSNumber);

    *buf_tx_ptr++ = ASTERISK_CODE;
    //CRC calculation
	*buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = CR_CODE;
    *buf_tx_ptr = NL_CODE;
	cnt_tx = findLen((const u8*)&buf_tx[len+1],MAX_SIZE_PACKET_GPRS_SIM900);
	crc_code = crcNavi((const u8*)&buf_tx[len+1],cnt_tx);
	int2char((char * )&buf_tx[cnt_tx+len+2],(crc_code >> 4) & 0x0F,1,16);
	int2char((char * )&buf_tx[cnt_tx+len+3],(crc_code & 0x0F),1,16);
	cnt_tx += 6;
	return cnt_tx;

}
Ejemplo n.º 11
0
xmlNodePtr nodeElement(NODE node)
{
	if (strcmp(srid, "4326") != 0) 
	{
		node = transformPoint(node, srid);
	}

	xmlNodePtr osmNode;
	osmNode = xmlNewNode(NULL, BAD_CAST "node");
	xmlNewProp(osmNode, BAD_CAST "id", BAD_CAST xmlEscape(int2char(node.id)));
	xmlNewProp(osmNode, BAD_CAST "lon", BAD_CAST xmlEscape(dbl2char(node.x)));
	xmlNewProp(osmNode, BAD_CAST "lat", BAD_CAST xmlEscape(dbl2char(node.y))); 
	return osmNode;
	
}
Ejemplo n.º 12
0
int main()
{
    int re,i,l,f,bin,bout;
    char in[1000],out[1001];
    int len,num[1000];

    for(i=0; i<10; i++) {
        c2i['0'+i]=i;
        i2c[i]='0'+i;
    }
    for(i=0; i<26; i++) {
        c2i['A'+i]=i+10;
        c2i['a'+i]=i+36;
        i2c[i+10]='A'+i;
        i2c[i+36]='a'+i;
    }
    scanf("%d",&re);
    while(re--) {
        scanf("%d%d",&bin,&bout);
        scanf("%s",in);
        len=strlen(in);
        for(i=0; i<len; i++)
            num[i]=char2int(in[i]);
        l=0;
        while(1) {
            f=0;
            for(i=0; i<len-1; i++)
                if(num[i]) {
                    f=1;
                    num[i+1]+=(num[i]%bout)*bin;
                    num[i]/=bout;
                }
            if(num[i]) f=1;
            out[l++]=int2char(num[i]%bout);
            num[i]/=bout;
            if(f==0) break;
        }
        printf("%d %s\n",bin,in);
        printf("%d ",bout);
        if(l==1) putchar('0'); /**/
        else {
            l--;
            while(l--) putchar(out[l]);
        }
        printf("\n\n");
    }
}
Ejemplo n.º 13
0
/*****************************************************
 * go_back_n()
 * return: void
 * purpose: run the go_back_n protocol
 ******************************************************/
void
go_back_n(struct RecvState (* query)()){

	// while(1) {

		recv(sock,act,sizeof(act), MSG_DONTWAIT);
		//recv(sock,act,sizeof(act), 0);
		sleep(1);
		if(strcmp(act, bad) == 0){
			/* if sender send bad, then receiver do nothing. */
			return ;
		}

		int check = atoi(act);
		int random = rand() % P1;
		//printf("check = %d, random = %d\n", check, random);
		if(random < P2 && check == cur && cur < total){

			printf("Frame %d Received \n", check % Mod);
			cur++;
		}
		if(check == cur && cur < total){
			send(sock, act, sizeof(act), 0);
			printf("Frame %d Received \n", check % Mod);
			cur++;
		}
		if(cur >= total) {
			int2char(cur-1);
			send(sock, act, sizeof(act), 0);
			sleep(1);
			send(sock, act, sizeof(act), 0);
			sleep(1);
			send(sock, act, sizeof(act), 0);

		}
		query();
	// }

}
Ejemplo n.º 14
0
//-------------------------------------------------------------------------------------------
void TbFunFTPWrite(void)  //"AT+FTPPUT=2,8\r\n",
{
   u8 * ptrDataDst,* ptrDataSrc;
   u8 k;
   u16 len;
   if(!sFTPState.flagWriteRawData)
     {
       memset(ftpDataPut,0,sizeof(ftpDataPut));
       strcpy((char *)ftpDataPut,"Updated ");
	   ptrDataDst = &ftpDataPut[strlen((char *)ftpDataPut)];
	   ptrDataSrc = (u8 *)readRTCTime(&rtc);
	   k = 0;
	   len = strlen((const char *)readRTCTime(&rtc));
	   while(k < len)
	     {  
		   k++;
		   *ptrDataDst++ = *ptrDataSrc++;
		 }

	   memset(ftpReadNumber,0,sizeof(ftpReadNumber));
	   len = strlen((char *) ftpDataPut );
	   iLen = getLenNum(len);
	   int2char((char * )ftpReadNumber,len,iLen,10);
	   memset(ftpPutWriteBuf,0,sizeof(ftpPutWriteBuf));
	   strcpy(ftpPutWriteBuf,(char *)QUERIES[15]);
	   strncat(ftpPutWriteBuf,ftpReadNumber,strlen((char *)ftpReadNumber) );
	   strncat(ftpPutWriteBuf,"\r\n",sizeof("\r\n") );
	    
       cmdPrepareSend((u8 *)ftpPutWriteBuf, TIMEOUT_OPEN_PUT_CMD);

	 }
   else if(sFTPState.flagPrepareToSend)
     {
	    sFTPState.flagPrepareToSend = 0;
		cmdSend(ftpDataPut);
		sFTPState.flagTimeToSend = 1;
	 }
}
Ejemplo n.º 15
0
double HairpinLoopEnergy(int size, int pos_i, int* int_seq)
{
   double energy = 0.0;
   int i, k; // only_Cs;
   char* tetra_loop_assign;
   int pos_j = pos_i + size + 1;

   int bp_i = int_seq[pos_i];
   int bp_j = int_seq[pos_j];
   int mis_i = int_seq[pos_i+1];
   int mis_j = int_seq[pos_j-1];

   double energy_help = 0.0;

   // loop_destabilizing_energies (not necessary here, since this function is
   // just used for the difference of two assignments of the same HL)
   //------------------------------
   /*if (size <= 30)
      energy += loop_destabilizing_energies[3*size-1];
   else
   {
      energy += loop_destabilizing_energies[3*30-1];
      energy += 1.75*RT*log((double)(size/30.0));
   }*/


   // terminal mismatch (fuer size > 3)
   //----------------------------------
   if (size > 3)
   {
      //**************************************************************
      // If the HPLoop would only consist of C's, a penalty of 0.3*size+1.6 has to be added.
      // NOT TAKEN INTO ACCOUNT, SINCE THIS IS NOT TAKEN INTO ACCOUNT IN THE VIENNA PACKAGE AS WELL.
      /*only_Cs = 1;
      for (i=1; i<=size; i++)
         if (int_seq[pos_i+i] != 1)
         {
            only_Cs = 0;
            break;
         }
      if ((only_Cs == 1) && (size > 3))
         energy += 0.3*size+1.6;*/
      //**************************************************************

      //penalties for all bases of the loop
      energy_help = 0.0;
      for (i=1; i<=size; i++)
         energy_help = Sum_MaxDouble(energy_help,BasePenalty(pos_i+i,int_seq[pos_i+i]));

      // terminal-mismatch energy for the closing BP and its penalty
      energy = Sum_MaxDouble4(energy, energy_help, mismatch_energies_hairpin[64*bp_i+16*mis_i+4*bp_j+mis_j], PairPenalty(pos_i,pos_j,bp_i,bp_j));

      if (size == 4)
      {
         /*for tetraloops, consider a special term*/
         tetra_loop_assign = (char*) malloc(sizeof(char)*7);
         for (k=0; k<6; k++)
            tetra_loop_assign[k] = int2char(int_seq[pos_i+k]);
         tetra_loop_assign[6] = '\0';
         energy = Sum_MaxDouble(energy,tetra_loop_energy(tetra_loop_assign));
      }
   }
   else if (size == 3)
   {
      energy_help = Sum_MaxDouble3(BasePenalty(pos_i+1,mis_i), BasePenalty(pos_i+2,int_seq[pos_i+2]), BasePenalty(pos_j-1,mis_j));
      energy = Sum_MaxDouble3(energy, energy_help, PairPenalty(pos_i,pos_j,bp_i,bp_j));

      if (((bp_i==0) && (bp_j==3)) || ((bp_i==3) && (bp_j==0)) || ((bp_i==2) && (bp_j==3)) || ((bp_i==3) && (bp_j==2)))
         energy = Sum_MaxDouble(energy,terminalAU);
      // If the HL would only consist of Cs, a penalty of 1.4 has to be added.
      if ((int_seq[pos_i+1] == 1) && (int_seq[pos_i+2] == 1) && (int_seq[pos_i+3] == 1))
         energy = Sum_MaxDouble(energy,Ctriloop);
      // If the HL would only consist of Gs, a term of -2.2 has to be added. This is not considered in the ViennaPackage, so do we
      //if ((int_seq[pos_i+1] == 2) && (int_seq[pos_i+2] == 2) && (int_seq[pos_i+3] == 2))
      //   energy = Sum_MaxDouble(energy,Gtriloop);
   }
   else
   {
      printf("HairpinLoopSize too small\n");
      exit(1);
   }

   //if closingHL the last BP in a stack
   energy = Sum_MaxDouble(energy, Zero_or_StemEndAU(BP_Pos_Nr[pos_i], BP2int(bp_i,bp_j)));

   return energy;
}
Ejemplo n.º 16
0
double BestHairpinLoopEnergy(int bp_pos, int size, int bp_i, int bp_j)
{
   double energy = 0.0;
   double min = MAX_DOUBLE;
   double energy_help;

   int bp_pos_i = BP_Order[bp_pos][0];
   int bp_pos_j = BP_Order[bp_pos][1];

   // loop_destabilizing_energies
   //****************************
   if (size <= 30)
      energy += loop_destabilizing_energies[3*size-1];
   else
   {
      energy += loop_destabilizing_energies[3*30-1];
      energy += 1.75*RT*log((double)(size/30.0));
   }

   // terminal mismatch (size > 3)
   //**************************************
   if (size > 3)
   {
      // The exact assignment of the interior loop bases (not adjacent to the closing BP) is not important here.
      // Their valid assignments can be restricted by the constraints, but one assignment per position has to be
      // valid und this one is chosen for the best energy.

      // If the HPLoop would only consist of C's, a penalty of 0.3*size+1.6 has to be added.
      // This means that just in case that all bases in the loop are restricted to C by the
      // constraints the energy has to be corrected.
      // NOT TAKEN INTO ACCOUNT, SINCE THIS IS NOT TAKEN INTO ACCOUNT IN THE VIENNA PACKAGE AS WELL.

      // bool onlyCs = true;
      // for (int p=bp_pos_i+1; p<bp_pos_j; p++)
      //    if ( !((seq_constraints[p][1] == 1) && (Sum_SeqConst(p) == 1)))
      //    {
      //       onlyCs = false;
      //       break;
      //    }

      // if (onlyCs)
      //    energy = Sum_MaxDouble(energy,0.3*size+1.6);

      if (size != 4)
      {
         // finding the minimal terminal-mismatch energy for the given closing BP,
         // the remaining bases in the loop don't contribute to the energy, i.e. they
         // are not fixed until the traceback, their penalty is also considered there
         // (since at least one base has to be valid for each position, the exact
         // assignment has not to be fixed here)
         min = MAX_DOUBLE;
         for (int i=0; i<4; i++)
            for (int j=0; j<4; j++)
            {
               energy_help = Sum_MaxDouble3(BasePenalty(bp_pos_i+1,i), BasePenalty(bp_pos_j-1,j), mismatch_energies_hairpin[64*bp_i+16*i+4*bp_j+j]);
               if (energy_help < min)
                  min = energy_help;
            }
         energy = Sum_MaxDouble(energy,min);
      }
      else
      {
         char* tetra_plus_closing;
         tetra_plus_closing = (char*) malloc(sizeof(char)*6);
         tetra_plus_closing[0] = int2char(bp_i);
         tetra_plus_closing[5] = int2char(bp_j);

         // consider all cases of the tetraloop (4*4*4*4) and add term-mismatch and
         // possibly a bonus, furthermore add the penalties for all 4 bases
         //      i2   j2
         //   i           j
         //     bp_i-bp_j
         //       .   .

         min = MAX_DOUBLE;
         for (int i=0; i<4; i++)
            for (int j=0; j<4; j++)
               for (int i2=0; i2<4; i2++)
                  for (int j2=0; j2<4; j2++)
                  {
                     energy_help = Sum_MaxDouble4(BasePenalty(bp_pos_i+1,i), BasePenalty(bp_pos_i+2,i2), BasePenalty(bp_pos_j-2,j2),BasePenalty(bp_pos_j-1,j));
                     tetra_plus_closing[1] = int2char(i);
                     tetra_plus_closing[2] = int2char(i2);
                     tetra_plus_closing[3] = int2char(j2);
                     tetra_plus_closing[4] = int2char(j);
                     energy_help = Sum_MaxDouble3(energy_help, tetra_loop_energy(tetra_plus_closing), mismatch_energies_hairpin[64*bp_i+16*i+4*bp_j+j]);

                     if (energy_help < min)
                        min = energy_help;
                  }
         energy = Sum_MaxDouble(energy,min);
         free(tetra_plus_closing);
      }
   }
   else if (size == 3)
   {
      min = MAX_DOUBLE;
      for (int i=0; i<4; i++)
         for (int j=0; j<4; j++)
            for (int i2=0; i2<4; i2++)
            {
               energy_help = Sum_MaxDouble3(BasePenalty(bp_pos_i+1,i), BasePenalty(bp_pos_i+2,i2), BasePenalty(bp_pos_j-1,j));

               // If the HL would only consist of Cs, a penalty of 1.4 has to be added.
               // This is just the best solution, if all other bases are forbidden by the constraints.
               if ((i==1) && (i2==1) && (j==1))
                  energy_help = Sum_MaxDouble(energy_help,Ctriloop);

               // If the HL would only consist of Gs, a term of -2.2 has to be added.
               // This is not considered in the ViennaPackage, so do we
               //if ((i==2) && (i2==2) && (j==2))
               //   energy_help = Sum_MaxDouble(energy_help,Gtriloop);

               if (energy_help < min)
                  min = energy_help;
            }
      energy = Sum_MaxDouble(energy,min);

      if (((bp_i==0) && (bp_j==3)) || ((bp_i==3) && (bp_j==0))|| ((bp_i==2) && (bp_j==3)) || ((bp_i==3) && (bp_j==2)))
         energy = Sum_MaxDouble(energy,terminalAU);
   }
   else
   {
      cerr << "HairpinLoopSize too small" << endl;
      exit(1);
   }
   return energy;
}
Ejemplo n.º 17
0
void InitBlaze(const char *port_name,speed_t speed){
  port = SerialOpen(port_name,speed);
  SerialWrite(port,int2char("b",MaxValue,true));
}
Ejemplo n.º 18
0
/*
int fibonacci(int x){
	if(x<=0) return 0;
	if(x==1 || x==2) return 1;
	return fibonacci(x-1)+fibonacci(x-2);
}
*/
void shell(void *userdata)
{
	char ichar;
	char command_buffer[MAX_COMMAND_LENGTH+1];
	char command[MAX_ARGC][MAX_COMMAND_LENGTH+1];
	int argv_index = 0;
	int char_index = 0;
	int index = 0;
	int fib_result = 0;
	char fib_result_char[10];
	while(1){
		while(char_index<=MAX_COMMAND_LENGTH){
			char_index++;
			command_buffer[char_index] = '\0';
		}
		print_str("arno@mini-arm-os:~$ ");
		char_index = 0;
		
		//command input
		while(1){
			ichar = usart2_rx();
			if(ichar == 13){//enter
				print_str("\n");
				break;
			}
			else if(ichar == 127){//backspace
				if(char_index!=0){
					print_str("\b");
					print_str(" ");
					print_str("\b");
					char_index--;
					command_buffer[char_index] = '\0';
				}
			}
			else{
				if(char_index<MAX_COMMAND_LENGTH){
					command_buffer[char_index] = ichar;
					print_str(&command_buffer[char_index]);
					char_index++;
				}
			}
		}
		
		//command identification
		if(char_index != 0){
			for(argv_index = 0; argv_index<MAX_ARGC; argv_index++)
				for(char_index = 0; char_index<=MAX_COMMAND_LENGTH; char_index++)
					command[argv_index][char_index] = '\0';
			argv_index = 0;
			char_index = 0;
			for(index = 0; index<MAX_COMMAND_LENGTH; index++){
				if(command_buffer[index] == ' '){
					if(char_index!=0){
						char_index = 0;
						argv_index++;
					}
				}
				else if(command_buffer[index] != '\0'){
					command[argv_index][char_index] = command_buffer[index];
					char_index++;
				}
				else {}
			}
			
			if(char_index==0)
				argv_index--;
			
			if(strcmp(command[0], "fibonacci\0")){
				if(argv_index != 1){
					print_str("Command should be: fibonacci+(int)");
					print_str("\n");
				}
				else if(str2int(command[1])==-999){
					print_str("Command should be: fibonacci+(int)");
					print_str("\n");
				}
				else{
					fib_result = fibonacci(str2int(command[1]));
					int2char(fib_result, fib_result_char);
					print_str(fib_result_char);
					print_str("\n");
				}
			}
			
		}
		char_index = 0;
		argv_index = 0;
		index = 0;
	}
}
Ejemplo n.º 19
0
void MotorR(int speed){
  SerialWrite(port,int2char("u",speed,true));
}
Ejemplo n.º 20
0
void SetMotors(int speedA,int speedB){
  
  char *buffer = int2char("y",speedA,false);
  SerialWrite(port,int2char(buffer,speedB,true));
}
Ejemplo n.º 21
0
//整数输出函数
void print_int(int a)
{
	unsigned char buff[10];
	int2char(buff,a);
	print_str(buff);
}
Ejemplo n.º 22
0
int main()
{
	s = socket(AF_INET, SOCK_STREAM, 0);
	if(s == -1){
		puts("Building a socket failed!");
		return 0;
	}

	ser.sin_family = AF_INET;
	ser.sin_port = htons(6500);
	ser.sin_addr.s_addr = inet_addr("127.0.0.1");

	connect(s, (struct sockaddr *) &ser, sizeof(ser));

	puts("TCP Connection Established.");
	puts("Enter the number of Frames: ");
	scanf("%d",&total);

	sseed = (unsigned int)time(NULL); /* initial the random value */
	srand(sseed);

	int2char(total);
	send(s, act, sizeof(act),0);
/************************ stop and wait ****************************/
	int cur = 0, canSend = 1;
	while(1)
	{
		if(cur >= total) {canSend = 0; break;}
		if(canSend){
			int random = rand() % P1;
			if(random < P2){						    /* 20% */
				printf("Frame %d Sent\n", cur % Mod);
				send(s, bad, sizeof(bad), 0);
				canSend = 0;
			}
			else {						/* 80% */
				int2char(cur);
				send(s, act, sizeof(act), 0);
				printf("Frame %d Sent\n", cur % Mod);
				canSend = 0;
			}
		}

		recv(s, act, sizeof(act), 0);
		if(strlen(act) != 0){
			if(strcmp(act, bad) == 0){
				printf("Time Out, Resent Frame %d onwards\n", cur % Mod);
				canSend = 1;
			}
			else {
				int check = atoi(act);

				if(check == cur){
					printf("recv from receiver as ACK act = %d\n", cur % Mod);
					cur++;
					if(cur >= total) {canSend = 0; break;}
					else canSend = 1;
				}
			}
		}
	}
	close(s);
	return 0;
}
Ejemplo n.º 23
0
char xorChar(char a, char b){
	return int2char(char2int(a)^char2int(b));
}
Ejemplo n.º 24
0
/*Текст программы*/
int main(void)
{

OperationCode=accumulator=InstructionRegister=operand=0;
for (InstructionCounter=0; InstructionCounter<100; InstructionCounter++)
{
    memory[InstructionCounter][0]='+';
    for (i=1; i<5; i++)
    memory[InstructionCounter][i]='0';
}
n=0;
while (n!=5)
{
printf ("      \nПрограммная модель компьютера SC, выполняющего программы на языке SA \n\n");
printf ("\t\t\t   Выполнил: Красняков К.В. \n");
printf ("\t\t\t   Группа П-01, второй курс \n");
printf ("\t\t\t   Логин p01s11 \n\n\n");
printf ("[1] - Ввод данных с клавиатуры\n");
printf ("[2] - Ввод данных из файла\n");
printf ("[3] - Вывод дампа памяти на экран\n");
printf ("[4] - Вывод дампа памяти в файл\n");
printf ("[5] - Выход\n");
scanf ("%d", &n);

/*Ввод данных с клавиатуры*/
if (n==1)
{

   printf ("Введите значения: \n");
   for (InstructionCounter=0; InstructionCounter<100; InstructionCounter++)
   {
       input (InstructionCounter);
       if (memory[InstructionCounter][0]=='-' && memory[InstructionCounter][1]=='9' && memory[InstructionCounter][2]=='9' && memory[InstructionCounter][3]=='9' && memory[InstructionCounter][3]=='9')
       {
          printf ("Процесс ввода завершен \n");
          break;
       }
   }
}

/*Ввод данных из файла*/
if (n==2)
{
   printf ("Введите имя файла \n");
   scanf ("%s", &path);
   if ((f=fopen(path, "r"))==NULL)
    printf ("%s","Can't open file");
 else
 {
     for (InstructionCounter=0; InstructionCounter<100; InstructionCounter++)
     {
         fileread (InstructionCounter);
         if (memory[InstructionCounter][0]=='-' && memory[InstructionCounter][1]=='9' && memory[InstructionCounter][2]=='9' && memory[InstructionCounter][3]=='9' && memory[InstructionCounter][3]=='9')
         {
            printf ("Процесс ввода завершен \n");
            break;
         }
     }
 }
}

/*Вывод дампа памяти*/
if (n==3)
{
   printf ("\nДамп памяти:\n");
   for (InstructionCounter=0; InstructionCounter<100; InstructionCounter++)
   {

        if (InstructionCounter%10==0)
           printf ("\n");
        for (i=0; i<5; i++)
            printf ("%c", memory[InstructionCounter][i]);
        printf (" ");
   }
}

/*Вывод дампа памяти в файл*/
if (n==4)
{
   printf ("Введите имя файла \n");
   scanf ("%s", &path);
   f1=fopen(path, "w");
   for (InstructionCounter=0; InstructionCounter<100; InstructionCounter++)
   {

        if (InstructionCounter%10==0)
           fprintf (f1,"\n");
        for (i=0; i<5; i++)
            fprintf (f1,"%c", memory[InstructionCounter][i]);
        fprintf (f1," ");
   }
   printf ("Готово! \n");
}
/*Выход из програмы*/
if (n==5)
{
   fcloseall();
   exit(0);
}

/*Обработка введенных данных*/
if (n==1 || n==2)
{
InstructionCounter=0;
while (InstructionCounter<100)
{
    char2int (InstructionCounter);
        if (OperationCode==READ)
        {
           printf ("\nВведите значения: \n");
           input (operand);
        }
        else
            if (OperationCode==WRITE)
            {
               printf ("\nСлово ячейки %d: ", operand);
               for (i=0; i<5; i++)
                   printf ("%c",memory[operand][i]);
            }
            else
                if (OperationCode==LOAD)
                   accumulator=char2int(operand);
                else
                    if (OperationCode==STORE)
                    {  int2char(accumulator);
                       for (i=0; i<5; i++)
                           memory[operand][i]=c[i];
                     }
                     else
                         if (OperationCode==ADD)
                            accumulator+=char2int(operand);
                         else
                             if (OperationCode==SUBTRACT)
                                accumulator-=char2int(operand);
                             else
                                 if (OperationCode==DIVIDE)
                                 {
                                     if (char2int(operand)==0)
                                     {
                                        printf ("\nОшибка: Деление на 0\n");
                                        break;
                                     }
                                     else
                                         accumulator/=char2int(operand);
                                 }
                                 else
                                     if (OperationCode==MULTIPLY)
                                        accumulator*=char2int(operand);
                                     else
                                         if (OperationCode==BRANCH)
                                            InstructionCounter=operand-1;
                                         else
                                             if (OperationCode==BRANCHNEG)
                                             {
                                                if (accumulator<0)
                                                   InstructionCounter=operand-1;
                                             }
                                             else
                                                 if (OperationCode==BRANCHZERO)
                                                 {
                                                    if (accumulator==0)
                                                       InstructionCounter=operand-1;
                                                 }
                                                 else
                                                     if (OperationCode==HALT)
                                                        break;
                                                     else
                                                         if (OperationCode==SHR)
                                                         {
                                                            accumulator=char2int(operand);
                                                            accumulator=accumulator%1000*10;
                                                         }
                                                         else
                                                             if (char2int(InstructionCounter)>0)
                                                                printf ("\nНеизвестная команда в строке %d, возможно данные программы \n", InstructionCounter);
    InstructionCounter++;
}

/*Вывод дампа памяти на экран*/
printf ("\nДамп памяти:\n");
for (InstructionCounter=0; InstructionCounter<100; InstructionCounter++)
{
    if (InstructionCounter%10==0)
       printf ("\n");
    for (i=0; i<5; i++)
        printf ("%c", memory[InstructionCounter][i]);
    printf (" ");
}

}
delay(2000);
}
return (0);
}
u16 pgioPassed(u16 len, u8 * buf_tx)
{
    u8 i,k,crc_code;
    u8 *buf_tx_ptr;
	buf_tx_ptr = &buf_tx[len];
	pgioInfo.uID = config.idBlock;
	pgioInfo.flagStart = 'C';
	pgioInfo.orderMsgNumber	+= 1;
	BKPWriteReg(BKP_DR_MSG_NUMBER1,pgioInfo.orderMsgNumber);
	BKPWriteReg(BKP_DR_MSG_NUMBER2,pgioInfo.orderMsgNumber >> 16);
	 
	pgioInfo.stateInput		= readStateInput();  //stub
	pgioInfo.stateOutputs	= readStateOutput();  //stub
	pgioInfo.stateADC[0]    = ain_read(0);
	pgioInfo.stateADC[1]    = ain_read(1);
	pgioInfo.stateADC[2]    = ain_read(2);
	pgioInfo.stateADC[3]    = ain_read(3);
	pgioInfo.stateThermo[0]	= 0;
	pgioInfo.stateThermo[1]	= 0;
	pgioInfo.stateThermo[2]	= 0;
	pgioInfo.stateThermo[3]	= 0;
	pgioInfo.stateThermo[4]	= 0;
	pgioInfo.stateThermo[5]	= 0;
	pgioInfo.stateThermo[6]	= 0;
	pgioInfo.stateThermo[7]	= 0;
	pgioInfo.stateFuel[0]	= 0;
	pgioInfo.stateFuel[1]	= 0;

	*buf_tx_ptr++ = USD_CODE;
	//bufCopy(PGIO_STRING,buf_tx_ptr,sizeof(PGIO_STRING));
	memcpy(buf_tx_ptr,PGIO_STRING, sizeof(PGIO_STRING));
	buf_tx_ptr += sizeof(PGIO_STRING)-1;
	*buf_tx_ptr++ = COMMA_CODE;

	int2char((char * )buf_tx_ptr,pgioInfo.uID,6,10);
	buf_tx_ptr += 6;  //should exact
	*buf_tx_ptr++ = COMMA_CODE;
	*buf_tx_ptr++ = pgioInfo.flagStart;
	*buf_tx_ptr++ = COMMA_CODE;
	int2char((char * )buf_tx_ptr,pgioInfo.orderMsgNumber,8,10);
    buf_tx_ptr += 8;  //should exact
	*buf_tx_ptr++ = COMMA_CODE;
	//*buf_tx_ptr++ = pgioInfo.stateInput;
	int2char((char * )buf_tx_ptr,pgioInfo.stateInput,2,16);
	buf_tx_ptr += 2;
	*buf_tx_ptr++ = COMMA_CODE;
	//*buf_tx_ptr++ = pgioInfo.stateOutputs;
	int2char((char * )buf_tx_ptr,pgioInfo.stateOutputs,2,16);
	buf_tx_ptr += 2;
	*buf_tx_ptr++ = COMMA_CODE;

	cnt_tx = snprintf((char * )buf_tx_ptr,5,"%2.2f",((double)pgioInfo.stateADC[0])*koefADC);
	buf_tx_ptr += cnt_tx; //cnt_tx; 
	*buf_tx_ptr++ = COMMA_CODE;
	cnt_tx = snprintf((char * )buf_tx_ptr,5,"%2.2f",((double)pgioInfo.stateADC[1])*koefADC);
	buf_tx_ptr += cnt_tx; //cnt_tx; 
	*buf_tx_ptr++ = COMMA_CODE;

	cnt_tx = snprintf((char * )buf_tx_ptr,5,"%2.1f",((double)pgioInfo.stateADC[2]) * koefVPower);
	buf_tx_ptr += cnt_tx; //cnt_tx; 
	*buf_tx_ptr++ = COMMA_CODE;
	cnt_tx = snprintf((char * )buf_tx_ptr,5,"%2.1f",((double)pgioInfo.stateADC[3]) * koefVBAT);
	buf_tx_ptr += cnt_tx; //cnt_tx; 
	*buf_tx_ptr++ = COMMA_CODE;

	for(k = 0; k < 8; k++)
	  {
			i = getLenNum(pgioInfo.stateThermo[k]);
			int2char((char * )buf_tx_ptr,pgioInfo.stateThermo[k],i,10);
		    buf_tx_ptr += i;  //should exact
			*buf_tx_ptr++ = COMMA_CODE;
	  }

	i = getLenNum(pgioInfo.stateFuel[0]);
	int2char((char * )buf_tx_ptr,pgioInfo.stateFuel[0],i,10);
    buf_tx_ptr += i;  //should exact
	*buf_tx_ptr++ = COMMA_CODE;
	i = getLenNum(pgioInfo.stateFuel[1]);
	int2char((char * )buf_tx_ptr,pgioInfo.stateFuel[1],i,10);
    buf_tx_ptr += i;  //should exact
    *buf_tx_ptr++ = ASTERISK_CODE;
    //CRC calculation
	*buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = CR_CODE;
    *buf_tx_ptr = NL_CODE;
	cnt_tx = findLen((const u8*)&buf_tx[len+1],MAX_SIZE_PACKET_GPRS_SIM900);
	crc_code = crcNavi((const u8*)&buf_tx[len+1],cnt_tx);
	int2char((char * )&buf_tx[cnt_tx+len+2],(crc_code >> 4) & 0x0F,1,16);
	int2char((char * )&buf_tx[cnt_tx+len+3],(crc_code & 0x0F),1,16);
	cnt_tx += 6;

	//crc_ending(len);

	return cnt_tx;

}
u16 gprmcPassed(u16 len, u8 * buf_tx)
{
    u8 *buf_tx_ptr,crc_code;
	buf_tx_ptr = &buf_tx[len];
	gprmcInfo.longitude = info.lon;
	gprmcInfo.latitude = info.lat;
	gprmcInfo.longLetter = (info.lon > 0)?'E':'W';
	gprmcInfo.latLetter = (info.lat > 0)?'N':'S';
	gprmcInfo.stateValidGPS	= (info.sig == NMEA_SIG_BAD)?'V':'A';
	gprmcInfo.speedMiles = info.speed / 1.62;
	gprmcInfo.direction	 = info.direction;	   //testing 231.7

	int2char((char * )gprmcInfo.timeDigits,info.utc.hour,2,10);
	int2char((char * )&gprmcInfo.timeDigits[2],info.utc.min,2,10);
	int2char((char * )&gprmcInfo.timeDigits[4],info.utc.sec,2,10);

	int2char((char * )gprmcInfo.dateDigits,info.utc.day,2,10);
	int2char((char * )&gprmcInfo.dateDigits[2],(info.utc.mon+1),2,10);  //+1 - for debugging
	int2char((char * )&gprmcInfo.dateDigits[4],info.utc.year,2,10);

	//$GPRMC,092611,A,5548.0474,N,04906.4238,E,00.08,215.1,291111,,*19
	*buf_tx_ptr++ = USD_CODE;
	//bufCopy(GPRMC_STRING,buf_tx_ptr,sizeof(GPRMC_STRING));
	memcpy(buf_tx_ptr,GPRMC_STRING,sizeof(GPRMC_STRING));
	buf_tx_ptr += sizeof(GPRMC_STRING)-1;
	*buf_tx_ptr++ = COMMA_CODE;

	//bufCopy(gprmcInfo.timeDigits,buf_tx_ptr,sizeof(gprmcInfo.timeDigits));
	memcpy(buf_tx_ptr,gprmcInfo.timeDigits,sizeof(gprmcInfo.timeDigits));
	buf_tx_ptr += sizeof(gprmcInfo.timeDigits);
	*buf_tx_ptr++ = '.';
	*buf_tx_ptr++ = '0';
	*buf_tx_ptr++ = '0';

    *buf_tx_ptr++ = COMMA_CODE;
    *buf_tx_ptr++ = gprmcInfo.stateValidGPS;
    *buf_tx_ptr++ = COMMA_CODE;

	cnt_tx = snprintf((char * )buf_tx_ptr,10,"%4.4f",gprmcInfo.latitude);
	buf_tx_ptr += cnt_tx; 
    *buf_tx_ptr++ = COMMA_CODE;
    *buf_tx_ptr++ = gprmcInfo.latLetter;
    *buf_tx_ptr++ = COMMA_CODE;

	if((gprmcInfo.longitude < 99999.9999) && (gprmcInfo.stateValidGPS == 'A'))
	   *buf_tx_ptr++ = '0';
	cnt_tx = snprintf((char * )buf_tx_ptr,11,"%5.4f",gprmcInfo.longitude);
	buf_tx_ptr += cnt_tx; 
    *buf_tx_ptr++ = COMMA_CODE;
    *buf_tx_ptr++ = gprmcInfo.longLetter;
    *buf_tx_ptr++ = COMMA_CODE;

	cnt_tx = snprintf((char * )buf_tx_ptr,7,"%3.2f",gprmcInfo.speedMiles);
	buf_tx_ptr += cnt_tx; 
    *buf_tx_ptr++ = COMMA_CODE;
	cnt_tx = snprintf((char * )buf_tx_ptr,6,"%3.1f",gprmcInfo.direction);
	buf_tx_ptr += cnt_tx; 

    *buf_tx_ptr++ = COMMA_CODE;
	//bufCopy(gprmcInfo.dateDigits,buf_tx_ptr,sizeof(gprmcInfo.dateDigits));
	memcpy(buf_tx_ptr,gprmcInfo.dateDigits,sizeof(gprmcInfo.dateDigits));
	buf_tx_ptr += sizeof(gprmcInfo.dateDigits);
    *buf_tx_ptr++ = COMMA_CODE;
    *buf_tx_ptr++ = COMMA_CODE;
    *buf_tx_ptr++ = COMMA_CODE;
    *buf_tx_ptr++ = 'A';

    *buf_tx_ptr++ = ASTERISK_CODE;
    //CRC calculation
	*buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = 'E';
    *buf_tx_ptr++ = CR_CODE;
    *buf_tx_ptr = NL_CODE;
	cnt_tx = findLen((const u8*)&buf_tx[len+1],MAX_SIZE_PACKET_GPRS_SIM900 ); //GPS_SENDING_BUFFER_SIZE
	crc_code = crcNavi((const u8*)&buf_tx[len+1],cnt_tx);
	int2char((char * )&buf_tx[cnt_tx+len+2],(crc_code >> 4) & 0x0F,1,16);
	int2char((char * )&buf_tx[cnt_tx+len+3],(crc_code & 0x0F),1,16);
	cnt_tx += 6;
	return cnt_tx;
}
Ejemplo n.º 27
0
void main (void)
{
	float period;
   	float bpm;
   	unsigned int intbpm;
   	char stringbpm[3]; 
	// Configure the LCD
	LCD_4BIT();
	//initialize string
	stringbpm[2] = '\0';
   
	//PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable)
	PORT_Init();     // Initialize Port I/O
	//SYSCLK_Init ();  // Initialize Oscillator
	//UART0_Init();    // Initialize UART0
	TIMER0_Init();

	printf("\x1b[2J"); // Clear screen using ANSI escape sequence.

	printf ("Period measurement at pin P0.1 using Timer 0.\n"
	        "File: %s\n"
	        "Compiled: %s, %s\n\n",
	        __FILE__, __DATE__, __TIME__);

    while (1)
    {
    	// Reset the counter
		TL0=0; 
		TH0=0;
		TF0=0;
		overflow_count=0;
		
		while(P0_1!=0); // Wait for the signal to be zero
		while(P0_1!=1); // Wait for the signal to be one
		TR0=1; // Start the timer
		while(P0_1!=0) // Wait for the signal to be zero
		{
			if(TF0==1) // Did the 16-bit timer overflow?
			{
				TF0=0;
				overflow_count++;
			}
		}
		while(P0_1!=1) // Wait for the signal to be one
		{
			if(TF0==1) // Did the 16-bit timer overflow?
			{
				TF0=0;
				overflow_count++;
			}
		}
		TR0=0; // Stop timer 0, the 24-bit number [overflow_count-TH0-TL0] has the period!
		period=(overflow_count*65536.0+TH0*256.0+TL0)*(12.0/SYSCLK);
		// Send the period to the serial port
		printf( "\rf=%fs" , period);
		bpm = 1.0/(period/60.0);
		intbpm = bpm;
		//printf("\nbpm=%d\n", intbpm);
		LCDprint("BPM:",1,1);
		int2char(stringbpm, intbpm, 2);
		LCDprint(stringbpm,2,1);
	}
	
}