Esempio n. 1
0
int main(int argc, char *argv[])
{
	int nameLength = 3;
	char name[3] = {'m', 'r', 'c'};

	printf("String:  %s\n", name);
	printf("As ints: %d %d %d\n", name[0], name[1], name[2]);
	printf("As num:  %ld\n", charToNum(name, nameLength, 0) +
														charToNum(name, nameLength, 1) +
														charToNum(name, nameLength, 2));
	return 0;
}
Esempio n. 2
0
void Modules::subsets(string soFar, string rest, char out, ofstream &outFile) {
    if (rest == "") {	// convert char symbols from the sub-strings of soFar to pitches, then print out
        for (int i = 0; i < static_cast<int>(soFar.length()); i++) {
            cout << TABLE[charToNum(soFar[i])];
            if (out == 'y')
                outFile << TABLE[charToNum(soFar[i])];
        }
        cout << endl;
        if (out == 'y')
            outFile << endl;
    } else {
        // add to subset, remove from rest, recursion
        subsets(soFar + rest[0], rest.substr(1), out, outFile);
        // don't add to subset, remove from rest, recursion
        subsets(soFar, rest.substr(1), out, outFile);
    }
}
Esempio n. 3
0
void Modules::permute(string soFar, string rest, int arrSize, char out, ofstream &outFile) {
    if (rest == "") {	// convert char symbols from the sub-strings of soFar to pitches, then print out
        for (int i = 0; i < arrSize; i++) {
            cout << TABLE[charToNum(soFar[i])];
            if (out == 'y')
                outFile << TABLE[charToNum(soFar[i])];
        }
        cout << endl;
        if (out == 'y')
            outFile << endl;
    } else {
        for (int i = 0; i < static_cast<int>(rest.length()); i++) {
            string next = soFar + rest[i];
            string remaining = rest.substr(0, i) + rest.substr(i + 1);
            permute(next, remaining, arrSize, out, outFile);
        }
    }
}
Esempio n. 4
0
File: gauss.c Progetto: 0lumide/cs
//This method offers no error checking, so use strIsNum to check if string is a valid number before 
//using this function
int strToNum(char * str){
	int num = 0;
	int i = 0;
	while((str[i] != '\0')&&(str[i] != '\n')){
		num += charToNum(str[i]) * pow(10, i);
		i++;
	}
	return num;
}
Esempio n. 5
0
 int romanToInt(string s) {
     int ans = 0, last = INT_MAX;
     for (char c : s) {
         int n = charToNum(c);
         if (n > last) ans -= 2 * last;
         ans += n;
         last = n;
     }
     return ans;
 }
inline int getNum(char *str, int base)
{
    int num = 0, exp = 1, len = strlen(str);
    for (int i = len - 1; i >= 0; i--)
    {
        num += charToNum(str[i])*exp;
        exp *= base;
    }

    return num;
}
Esempio n. 7
0
/**
 * Returns non-zero if the account number could be valid according to the Luhn
 * algorithm (used for credit card numbers).
 */
int luhn(char *number) {
  int length = strlen(number);
  int lastIndex = length - 1;
  int sum = 0;
  for (int i = 0; i < length; i++); {
    int index = length - 1 - i;
    int num = charToNum(number[index]);
    if (i % 2 == 1) {
      sum += sumOfDigits(num * 2);
    } else {
      sum += num;
    }
  }
  int checksum = (sum * 9) % 10;
  return checksum = 0;
}
Esempio n. 8
0
void cmdIndexed(u08 cmdInput)
{
    switch (cpState)
    {
        case INDEX:
            //
            // Validate index parameter (0-9,a-z)
            //
            if (-1 == (input[INDEX] = charToNum(cmdInput)))
            {
                cmdInit();
                return;
            }
            cpState =  PARAM1;
            break;

        case PARAM1:
            input[PARAM1] = cmdInput;
            cpState = PARAM2;
            break;

        case PARAM2:
            if (input[INDEX] > 1)
            {
                uart_send_buffered(input[COMMAND]);
                uart_send_buffered(numToChar(input[INDEX] - 2));
                uart_send_buffered(input[PARAM1]);
                uart_send_buffered(cmdInput);
            }
            else
            {
                switch (input[COMMAND])
                {
                    case CMD_PIXEL_ON:
                    case CMD_PIXEL_OFF:
                        dm_pixel(input[INDEX],
                                (input[COMMAND] == CMD_PIXEL_ON ? 1 : 0),
                                input[PARAM1] - '0',
                                cmdInput - '0');
                        break;
                }
            }
            cmdInit();
            break;
    }
}
Esempio n. 9
0
void cmdSetBits(u08 cmdInput)
{
    char length;
    if (LENGTH == cpState)
    {
        //
        // Validate length parameter (0-9,a-z)
        //
        if (-1 == (length = charToNum(cmdInput)))
        {
            cmdInit();
            return;
        }
        //
        // if length > 2 then subtract 2 and pass this command on to the
        // next controller in the chain
        //
        input[REMAINING] = length * 5;
        length -= 2;
        if (length > 0)
        {
            uart_send_buffered(input[COMMAND]);
            uart_send_buffered(numToChar(length));
        }
        input[INDEX] = 0;
        cpState = PARAM1;
        return;
    }
    if (input[INDEX] < 10)
    {
        dm_progColumn(input[INDEX] / 5, input[INDEX] % 5, cmdInput);
    }
    else
    {
        uart_send_buffered(cmdInput);
    }
    input[INDEX]++;
    input[REMAINING]--;
    if (0 == input[REMAINING])
    {
        cmdInit();
    }
}
Esempio n. 10
0
File: stego.c Progetto: sebros/stego
void getCmdLineParamsToConfig(int argc, char **argv, ConfigInfo *configinfo)
{
	char c;

    while ((c = getopt (argc, argv, "i:e:m:o:p:l:t:ch")) != -1)
    {
    	switch (c)
    	{
    	case 'i':
    		configinfo->cover_file = optarg;
    		break;
    	case 'e':
    		configinfo->stego_file = optarg;
    		break;
    	case 'm':
    		configinfo->message_file = optarg;
    		break;
        case 'o':
        	configinfo->output_file = optarg;
        	break;
        case 'p':
        	configinfo->password = optarg;
        	break;
        case 'l':
        	if( 0 == strcmp(optarg, "1") ) { configinfo->lsb_usage = LSB_USAGE_1; }
        	else if( 0 == strcmp(optarg, "2") ) { configinfo->lsb_usage = LSB_USAGE_2; }
        	else { configinfo->lsb_usage = LSB_USAGE_1; }
        	break;
        case 't':
    		configinfo->treshold = DEFAULT_TRESHOLD;

        	if( 2 == strlen(optarg) )
        	{
        		if( 2 == charToNum(optarg[0]) )
        		{
        			configinfo->treshold = 20;
        		}
        		else if ( 1 == charToNum(optarg[0]) )
        		{
        			if( (0<=charToNum(optarg[1])) && (10>charToNum(optarg[1])) )
        			{
        				configinfo->treshold += charToNum(optarg[1]);
        			}
        		}
        	}
        	else if (1 == strlen(optarg))
        	{
    			if( (0<=charToNum(optarg[0])) && (10>charToNum(optarg[0])) )
    			{
    				configinfo->treshold = charToNum(optarg[0]);
    			}
        	}

        	break;
        case 'c':
        	configinfo->only_calc = TRUE;
        	break;
        case 'h':
        	printHelp();
        	exit(0);
        	break;
        case '?':
        	printHelp();
        	exit(1);
        	break;
        default:
        	printHelp();
        	exit(1);
        	break;
        }
    }

    if( (configinfo->cover_file != NULL) && (configinfo->stego_file != NULL) && (configinfo->message_file == NULL) && (configinfo->output_file == NULL) )
    {
    	if(configinfo->only_calc == TRUE) return;
    	printHelp();
    	exit(1);
    }

    if( (configinfo->stego_file != NULL) && (configinfo->cover_file != NULL) && (configinfo->message_file == NULL) )
    {
    	if(configinfo->only_calc == TRUE) return;
    	printHelp();
    	exit(1);
    }

    if( (configinfo->cover_file == NULL) && (configinfo->stego_file == NULL) )
    {
    	printHelp();
    	exit(1);
    }

}
Esempio n. 11
0
aMap readhap( char *fname,int minDist,double minMaf,int startPos,int stopPos,int skiptrans){
  //  fprintf(stderr,"[%s] fname:%s\tminDist:%d LENS:%d\n",__FUNCTION__,fname,minDist,LENS);
  gzFile gz=getgz(fname,"rb");
  
  char *buf = new char[LENS];
  int viggo=3;
  aMap myMap;//<- this will be our return object

  gzgets(gz,buf,LENS);
  while(gzgets(gz,buf,LENS)){//loop over sites
    hapSite hs;
    int p = atoi(strtok(buf,"\t\n "));// pos
    if(p<startPos)
      continue;
    if(p>stopPos)
      continue;

    hs.allele1 = charToNum(strtok(NULL,"\t\n ")[0]);
    hs.freq = atof(strtok(NULL,"\t\n "));
    if(hs.freq<minMaf||(1-hs.freq)<minMaf)
      continue;
    char strand= strtok(NULL,"\t\n ")[0];
    hs.allele2 = charToNum(strtok(NULL,"\t\n ")[0]);
    
    if(hs.allele1==-1||hs.allele2==-1)
      continue;
    if(strand=='-'){
      hs.allele1 = flip(hs.allele1);
      hs.allele2 = flip(hs.allele2);
    }
    if(hs.allele1==hs.allele2)
      continue;

    if(skiptrans){
      if(hs.allele1 == 0 && hs.allele2 == 2)
	continue;
      if(hs.allele1 == 2 && hs.allele2 == 0)
	continue;
      if(hs.allele1 == 1 && hs.allele2 == 3)
	continue;
      if(hs.allele1 == 3 && hs.allele2 == 1)
	continue;

    }
    if(myMap.count(p)>0){
      if(viggo>0){
	//	fprintf(stderr,"[%s] Duplicate positions found in file: %s, pos:%d\n",__FUNCTION__,fname,p);
	//fprintf(stderr,"[%s] Will only use first entry\n",__FUNCTION__);
	//fprintf(stderr,"[%s] This message is only printed 3 times\n",__FUNCTION__);
	viggo--;
      }
    }else{
      myMap[p]=hs;
    }
  }
  //  fprintf(stderr,"[%s] We have read: %zu sites from hapfile (after filtering for start/stop pos):%s\n",__FUNCTION__,myMap.size(),fname);
  //fprintf(stderr,"[%s] will remove snp sites to close:\n",__FUNCTION__);

  assert(myMap.size()>0);
  int *vec = new int[myMap.size() -1];
  aMap::iterator it = myMap.begin();
  for(int i=0;i<myMap.size()-1;i++){
    aMap::iterator it2 = it;
    it2++;
    vec[i]=it2->first - it->first;
    it=it2;
  }
  it = myMap.begin();
  aMap newMap;
  for(int i=0;i<myMap.size()-1;i++){
    if(std::abs(vec[i])>=minDist){
      newMap[it->first] = it->second;
      //     fprintf(stdout,"test\t%d\n",it->first);
    }
    it++;
  }
  newMap[it->first] = it->second;
  //..  exit(0);
  delete [] vec;
  fprintf(stderr,"[%s] We now have: %lu snpSites after filtering based on hapMapfile\n",__FUNCTION__,newMap.size());

#if 0
  for(aMap::iterator it=newMap.begin();it!=newMap.end();++it)
    print(stdout,it->first,it->second);
  exit(0);
#endif
  delete [] buf;
  gzclose(gz);
  return newMap;
}