Пример #1
0
// covert the number to decimal double
double Base2Decimal(const char* src, unsigned int src_base, unsigned int percision){
	unsigned int numSize = strlen(src);
	double digitPower = 0;
	double newNum = 0;
	int startNum = 0;
	int i = 0;
	unsigned int fracPointIndex = findPoint(src);

	// case of negative number
	(src[0] == '-') ? startNum = 1 : startNum = 0;

	// case there is no fraction point
	if (fracPointIndex == numSize)
		return (double)atoi(src);

	// create left side of fraction point
	for (i = fracPointIndex - 1; i >= startNum; --i)
		newNum += (pow(src_base, digitPower++))*(char2num(src[i]));

	digitPower = -1;
	// create right side of fraction point
	for (i = (int)fracPointIndex + 1; i < (int)numSize; ++i)
		newNum += (pow(src_base, digitPower--))*(char2num(src[i]));

	// case of negative number
	if (src[0] == '-')
		newNum = -(newNum);

	return newNum;
}
Пример #2
0
bool HttpUtils::urlDecode(const char* srcurl, std::string& dsturl){
    if(!srcurl)return false;
    char ch, ch1, ch2; 
    int i; 
    int srcurlsize = strlen(srcurl);
    for (i=0; i<srcurlsize; i++) {
        ch = srcurl[i]; 
        switch (ch) { 
            case '+': 
                //ss_string_append_char(dsturl, SS_CHAR_SPACE); 
                dsturl.append(1, (char)CHAR_SP);
                break;
            case '%': 
                if (i+2 < srcurlsize) { 
                    ch1 = char2num(srcurl[i+1]); 
                    ch2 = char2num(srcurl[i+2]); 
                    if ((ch1 != CHAR_ZERO) && (ch2 != CHAR_ZERO)) { 
                        //ss_string_append_char(dsturl, (char)((ch1<<4) | ch2)); 
                        dsturl.append(1, (char)((ch1<<4) | ch2)); 
                        i += 2; 
                        break; 
                    }
                }

            default: 
                //ss_string_append_char(dsturl, ch); 
                dsturl.append(1, ch);
                break; 
        } 
    }
    
    return true;
}
Пример #3
0
void
main(void)
{
    FILE *fin, *fout;
    int i, j, k, npath, d;
    char a, b;
    int m;

    fin = fopen("comehome.in", "r");
    fout = fopen("comehome.out", "w");
    assert(fin != NULL && fout != NULL);

    for(i=0; i<52; i++)
        for(j=0; j<52; j++)
            dist[i][j] = INF;

    for(i=0; i<26; i++)
        dist[i][i] = 0;

    fscanf(fin, "%d\n", &npath);
    for(i=0; i<npath; i++) {
        fscanf(fin, "%c %c %d\n", &a, &b, &d);
        a = char2num(a);
        b = char2num(b);
        if(dist[a][b] > d)
            dist[a][b] = dist[b][a] = d;
    }

    /* floyd warshall all pair shortest path */
    for(k=0; k<52; k++)
        for(i=0; i<52; i++)
            for(j=0; j<52; j++)
                if(dist[i][k]+dist[k][j] < dist[i][j])
                    dist[i][j] = dist[i][k]+dist[k][j];

    /* find closest cow */
    m = INF;
    a = '#';
    for(i='A'; i<='Y'; i++) {
        d = dist[char2num(i)][char2num('Z')];
        if(d < m) {
            m = d;
            a = i;
        }
    }

    fprintf(fout, "%c %d\n", a, m);
    exit(0);
}
Пример #4
0
unsigned int handleConstants(char line[])
{
	
	char temp[3];

	if(line[0] == 'C')
		return line[2];


	else if(line[0] == 'X')
	{
		temp[0]=line[2];
		temp[1]=line[3];
		temp[2]='\0';
		return str2num(temp,16);

	}

	else
	{
		int j = strlen(line)-1;
		unsigned int objCode= 0;
		int i = 0;
		while(i<=j)
			objCode += char2num(line[i++]) * pow(10,j--);


	}

}
Пример #5
0
unsigned int str2num(char num[],int base)
{
    int i,j;
    
    j = strlen(num) - 1;
    unsigned int ans = 0;
    
    for(i = 0; j>=0; i++,j--)
        ans += char2num(num[i]) * pow(base,j);
    
    return ans;
}
Пример #6
0
int numDecodings(char* s) {
    if(s==NULL||strlen(s)<=0){
    	return 0;
	}
	int len=strlen(s);
	if(len==1){
		return (s[0]!='0')?1:0;
	}
	else if(len==2){//求出字符长度为2的译码方法。 
		return ((s[0]!='0'&&s[1]!='0')?1:0)+((s[0]!='0'&&(char2num(s[0])*10+char2num(s[1])<=26))?1:0);
	}
	//用来保存结果 
	int *res=(int *)malloc(len*sizeof(int));
	if(res==NULL){
		exit(EXIT_FAILURE);
	} 
	memset(res,0,len*sizeof(int));
	res[0]= (s[0]!='0')?1:0;
	res[1]=((s[0]!='0'&&s[1]!='0')?1:0)+((s[0]!='0'&&(char2num(s[0])*10+char2num(s[1])<=26))?1:0);
	for(int i=2;i<len;i++){
		if(s[i]!='0'){//1)若第n个字符在1到9之间,则n长度的字符串能够解释的数目包含n-1长度字符串能够解释的数目。
			res[i]+=res[i-1];
		}
		//2)若第n-1个字符与第n个字符可以解释为一个字母时,则n长度的字符串能够解释的数目包含n-2长度字符串能够解释的数目。
		if(s[i-1]!='0'&&char2num(s[i-1])*10+char2num(s[i])<=26){//
			res[i]+=res[i-2];
		}
	}
	return res[len-1];
	
}
Пример #7
0
static uint32_t str2val(char *str) {
    int i;
    int value;
    int base = ishex(str) ? 16 : 10;

    if (str == NULL) return(0);

    for (i=0,value=0; str[i]; i++) {
        value = (value*base) + char2num(str[i]);
    }

    return(value);
}
Пример #8
0
main(int argc,char *argv[]){
  int k=5;
  int m=2608;
  read_genome_matrix(argv[1],k,m);
  free(classify_name);
  free(counts);

  /////////////////////////////////////
  char str[1000]="ATCC";
  char numseq[1000];
  char2num(str,numseq);
  /////////////////////////////////////
  get_fq(argv[2]);
  free(fqnames);
  ////////////////////////////////////
}
Пример #9
0
unsigned int assembleInstr(OPTAB optab[],char operand[],unsigned int address,int isIndex)
{
	char instr[25]="\0",hexOp[3],binary[5],hexAddr[5] = "ssss",finalHexInstr[7];
	int i,j,k,flag =0;
	unsigned int opcode,ans;
	
	if(!strcmp(operand,"RSUB"))
	{
//		printf("RSUB reached\n");
		flag = 1;
	}
	
	for(i = 0; i<OPTABLEN ;i++)
		if(!strcmp(optab[i].operand,operand))
		{
			opcode = optab[i].opcode;
			break;
		}
	
	
	getOpcode(hexOp,opcode);
	
	hex2bin(hexOp[0],binary);
	strcpy(instr,binary);
	
	hex2bin(hexOp[1],binary);
	strcat(instr,binary);
	
	if(isIndex == 1)	//if indexed addressing is used then set x bit to 1 otherwise set it to zero
		instr[8] = '1';
	else
		instr[8] = '0';
	
	addressConversion(hexAddr,address);
	
	hex2bin(hexAddr[0],binary);
	
	for(i=9,j=1;i<=11;i++,j++)
		instr[i] = binary[j];
	
	//puts(hexAddr);
	
	for(i=1;i<=3;i++)
	{
		hex2bin(hexAddr[i],binary);
		strcat(instr,binary);
	}
		
	//puts(instr);
	instr[24] = '\0';

	for(i=0,k=0;k<strlen(instr);i++)
	{
		for(j=3,ans=0;j>=0;j--,k++)
			ans += char2num(instr[k]) * pow(2,j) ;
	
		finalHexInstr[i] = num2HexChar(ans);
		
	//	if(flag == 1)
	//	printf("%u\n",ans);
	}

	finalHexInstr[i] = '\0';
		
	//puts(finalHexInstr);
	return str2num(finalHexInstr,16);
	
}
Пример #10
0
 unsigned int MetaGenericEvent::paramUInt(unsigned int id)
 {  
   return char2num((char)_data[id]);
 }
Пример #11
0
//Read the protein alignment file (must be Pfam format!)
void ProteinDomains::ReadDomains(char* inFileName, Motif** inputMotifs, int numMotifs)
{
	int i,j,a;
	int lineCount=0;
	double currCount=0;
	char line[LONG_STR];
	int protAlignLen=0;
	char name[STR_LEN];
	char currMotifName[STR_LEN];
	char seq[LONG_STR];

	strcpy(inputFN, inFileName);
	numDomains = numMotifs;

	//Open the file
	FILE* in = fopen(inputFN, "r");
	if(in==NULL){perror("Cannot open protein domain file");exit(1);}

	//Count the number of motifs
	fgets(line, LONG_STR, in);
	while(!feof(in)){		
		if(strlen(line)>2){//if it's not a newline
			lineCount++;
			sscanf(line, "%s %s ", name, seq);
			protAlignLen = strlen(seq);
		}
		fgets(line, LONG_STR, in);
	}

	domainMotif = new ProteinMotif(protAlignLen);
	individualMotifs = new ProteinMotif*[numDomains];
	for(i=0; i<numDomains; i++){
		individualMotifs[i]=new ProteinMotif(protAlignLen);
	}
	//Read through again, this time extracting a domain motif for every PSSM in our binding motif set
	for(i=0; i<numMotifs; i++){
		strcpy(currMotifName, inputMotifs[i]->GetName());
		strcpy(individualMotifs[i]->name, currMotifName);
		currCount=0;
		
		fseek(in, 0, SEEK_SET);
		while(!feof(in)){		
			fgets(line, LONG_STR, in);
			if(strlen(line)>2){//if it's not a newline
				sscanf(line, "%s %s ", name, seq);
				if(strstr(name, currMotifName)!=NULL){
					currCount++;
					//Add to the individual motif
					protAlignLen = strlen(seq);
					for(j=0; j<protAlignLen; j++){
						if(char2num(seq[j])!=-1)
							individualMotifs[i]->f[j][char2num(seq[j])]++;
					}
				}
			}
		}
		if(currCount==0){
			printf("Error: %s not found in protein motif file... exiting!\n\n", currMotifName);
			exit(1);
		}else{
			for(j=0; j<protAlignLen; j++){
				for(a=0; a<AA; a++){
					individualMotifs[i]->f[j][a] = individualMotifs[i]->f[j][a]/currCount;
				}
			}
		}
	}
	//Read through once more, this time just constructing the overall alignment
	strcpy(domainMotif->name, "OverallDomain");
	fseek(in, 0, SEEK_SET);
	fgets(line, LONG_STR, in);
	while(!feof(in)){		
		sscanf(line, "%s %s ", name, seq);
		if(strlen(line)>2){//if it's not a newline
			//Add to the domain motif
			protAlignLen = strlen(seq);
			for(j=0; j<protAlignLen; j++){
				if(char2num(seq[j])!=-1)
					domainMotif->f[j][char2num(seq[j])]++;
			}
		}
		fgets(line, LONG_STR, in);
	}
	for(j=0; j<protAlignLen; j++){
		for(a=0; a<AA; a++){
			domainMotif->f[j][a] = domainMotif->f[j][a]/lineCount;
		}
	}
}
Пример #12
0
int calculator (void)
{


    typedef int counters;
    double totalCount=0.0;
    char currentChar = 'I', previousChar;
    bool decimalFlag;
    counters j, k=0, z=0;

    char NUMchars[] = {'1','2','3','4','5','6','7','8','9','0'};
    int NUMmatch[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    struct charNUM correspondingNum[MAXlength];
    struct NUMOP EXPRESSION[MAXlength];

    EXPRESSION[0].parenthesis = '0';

    printf("Write expression here: (Type Q to quit)\n");

    for (j=0; j<MAXlength; j++){
        correspondingNum[j].numLength = 0;
        correspondingNum[j].decimals = 0;
    }

    while (currentChar!='\n')
    {
        currentChar = getchar();

        if (currentChar=='Q')
            return 0;
        else if (currentChar == '\n')
            break;

        for (j=0; j<MAXlength; j++){
            if (currentChar == NUMchars[j]){
                correspondingNum[k].singleNum[z] = NUMmatch[j];
                if (!decimalFlag)
                    ++correspondingNum[k].numLength;
                else
                    ++correspondingNum[k].decimals;

                ++z;
                break;

            }
            if (j == MAXlength - 1){
                    if (currentChar == '.'){
                        decimalFlag = 1;
                        break;
                    }
                    else
                        decimalFlag = 0;

                    if (currentChar == leftPar){
                            if (previousChar == leftPar){
                                ++k;
                                EXPRESSION[k].parenthesis = currentChar;
                                EXPRESSION[k-1].operation = '+';
                                EXPRESSION[k-1].actualNum = 0;
                                break;

                            }
                            else
                                EXPRESSION[k].parenthesis = currentChar;

                            previousChar = currentChar;
                            z = 0;
                            break;
                    }


                   if (currentChar == rightPar){
                            if (previousChar == rightPar){
                                ++k;
                                EXPRESSION[k].parenthesis = currentChar;
                                EXPRESSION[k-1].operation = '+';
                                EXPRESSION[k].actualNum = 0;

                                break;

                            }
                            else
                                EXPRESSION[k].parenthesis = currentChar;

                            previousChar = currentChar;
                            z = 0;
                            break;
                    }


                EXPRESSION[k].operation = currentChar;
                ++k;
                EXPRESSION[k].parenthesis = '0';
                z=0;
                previousChar = currentChar;
            }
        }

    }

    for (j=0; j<=k; j++){

        EXPRESSION[j].actualNum = char2num(correspondingNum[j]);
        if (EXPRESSION[j].actualNum == 0.0 && EXPRESSION[j-1].operation == '/'){
            printf("NOOOOOOOOOOOOOO  *UNIVERSE IMPLODES*\n\n");
            return 0;
        }
    }

    totalCount = deal_with_parentheses(EXPRESSION, k);

    printf ("=%.3f\n\n\n", totalCount);

    return 1;
}
//usage:feature = feaGen( training,depth,ndim);
//state:finishing
//version:v1
//notation: maximum size of buffer is 1024
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray * prhs[])
{
	// check the input parameter
	if (nlhs != 1)
		mexErrMsgTxt(" error in using feaGen, one output is needed");
	if (nrhs != 3)
		mexErrMsgTxt(" error in using feaGen, three input are needed");
	if (!mxIsCell(prhs[0]))
	{
		mexErrMsgTxt(" input type error");
	}

	mxArray * pp, *ptemp;
	mwSize M;
	mwSize depth, ndim;
	int i, j, k, m, n, intTemp;
	double *pointer;

	//get parameter
	depth = mxGetScalar(prhs[1]);
	ndim = mxGetScalar(prhs[2]);
	M = mxGetM(prhs[0]);
	//alloc the space

	char *buf = (char*) mxMalloc((unsigned int) (MAXSIZE) * sizeof(char));

	char ** fea;
	fea = (char**) mxMalloc((unsigned int) ndim * sizeof(char*));
	for (i = 0; i < ndim; i++)
	{
		fea[i] = (char*) mxMalloc((unsigned int) (MAXSIZE) * sizeof(char));
	}

	feature fv(ndim, depth, M);

	for (i = 0; i < M; i++)
	{
		fv.addLine();
		ptemp = mxGetCell(prhs[0], i);
		if (mxGetString(ptemp, buf, (unsigned int) MAXSIZE))
			mexErrMsgTxt("error in the buffer of this function:line53");
#ifdef debug
		mexPrintf("%s",buf);
#endif
		for (j = 0; buf[j] != '\0'; j++)
		{
			intTemp = char2num(buf[j]);
			for (int l = 0; l < ndim; l++)
			{

				fea[l][j] = (int)(intTemp & 0x1) + '0';
				intTemp >>= 1;
			}
		}

		int len = j;
#ifdef debug
		mexPrintf("%s",len);
#endif
		char *temp;
		temp = (char*) mxMalloc((ndim * depth + 1) * sizeof(char));
		for (j = 0; j <= len - depth; j++)
		{
			intTemp = 0;
			int mm, nn;
			for (nn = 0; nn < ndim; nn++)
			{
				for (mm = 0; mm < depth; mm++)
				{
					temp[intTemp++] = fea[nn][mm + j];
				}
			}
			*(temp + ndim * depth) = '\0';
			intTemp = ndim * depth;
			int index = to2num(temp, intTemp);
			if( index < 0 || index > power2(ndim,depth))
			{
				mexErrMsgTxt("error in input format line:90");
			}
			fv.store(index);
		}
		mxFree(temp);

	}

	mxFree(buf);
	for (i = 0; i < ndim; i++)
	{
		mxFree(fea[i]);
	}
	mxFree(fea);

	int lineSize = fv.getLineSize();
	plhs[0] = mxCreateDoubleMatrix(fv.getLen(), lineSize, mxREAL);
	pointer = mxGetPr(plhs[0]);
	fv.transaction(pointer);

}