Ejemplo n.º 1
0
int main()
{
	int n;
	printf("Enter the number: ");
	scanf("%d",&n);
	hundreds(n);
	return 0;
}
 string numberToWords(int num) {
     int n[4] = {num / 1000000000, num / 1000000 % 1000, num / 1000 % 1000, num % 1000};
     string res = "";
     for (int i=0; i<4; ++i) {
         if (!n[i]) continue;
         string m = hundreds(n[i]);
         res += (res == "" ? "" : " ") + m + steps[3-i];
     }
     return res == "" ? "Zero" : res;
 }
Ejemplo n.º 3
0
std::vector< std::vector<double> > read_matlab( std::string filename )
{
 // Matrix output;
 std::vector< std::vector<double> > output; 
 
 FILE* fp; 
 fp = fopen( filename.c_str() , "rb" );
 if( fp == NULL )
 {
  std::cout << "Error: could not open file " << filename << "!" << std::endl;
  return output;
 }
 
 typedef unsigned int UINT;
 UINT UINTs = sizeof(UINT);
 
 // read the basic header information 
 
 UINT temp;
 
 fread( (char*) &temp , UINTs , 1 , fp );
 
 UINT type_numeric_format = thousands(temp);
 UINT type_reserved = hundreds(temp);
 UINT type_data_format = tens(temp);
 UINT type_matrix_type = ones(temp);
/*
	cout << type_numeric_format << " " 
		<< type_reserved << " " 
		<< type_data_format << " " 
		<< type_matrix_type << endl;
*/
	
 // make sure it's a matlab L4 file 
 	  
 if( type_numeric_format != 0 || // 	little-endian
     type_reserved != 0 || // should always be 0
     type_data_format > 5 || // unknown format
     type_matrix_type != 0 ) // want full matrices, not sparse 
 {
  std::cout << "Error reading file " << filename << ": I can't read this format yet!" << std::endl;
  return output;
 } 

 // get the size of the data 
 
 UINT rows;
 fread( (char*) &rows , UINTs , 1, fp );
 
 UINT cols;
 fread( (char*) &cols, UINTs , 1 , fp );
 
 // resize the output accordingly 

 // output.SetSize( rows, cols );
 


 std::vector<double> TemplateRow(cols,0.0);
 output.resize( rows , TemplateRow );

 // make sure we're not dealing with complex numbers 
 
 UINT imag;
 fread( (char*) &imag, UINTs, 1 , fp );
 if( imag != 0 )
 {
  std::cout << "Error: I can't read imaginary matrices yet!" << std::endl;
  return output;
 }
 
 // Get the name of the variable. We don't tend to use this on reading (for now). 
 // But if we were to output a more complex data structure with 
 // vector< vector<double> > and vector<string>, we could! 

 // if we actually use the names, then I'd suggest that we do a little parsing: 
 // is it a MultiCellDS field array? 
 // Make a format for that. Something like this:
 // MultiCellDS_Fields:name1,name2,...,nameN, where N = rows - 3; 

 UINT name_length;
 fread( (char*) &name_length, UINTs, 1 , fp );
 char* name;
 name = new char [name_length];

 // this is the end of the 20-byte header 

 // read the name
 
 fread( name , name_length , 1 , fp );

 // read the real part of the matrix
 int i = 0;
 int j = 0;
 
 switch( type_data_format )
 {
  case 0:
   // all fields are doubles
   
   for( int n=0; n < rows*cols ; n++ )
   {
	double temp;
	fread( (char*) &temp, sizeof(double), 1 , fp );
	(output[i])[j] = temp; 
    i++;
	
	if( i == rows )
    { i=0; j++; }
   }
   break;
   
  case 1:
   // all fields are floats 
   
   for( int n=0; n < rows*cols ; n++ )
   {
	float temp;
	fread( (char*) &temp, sizeof(float), 1 , fp );
	(output[i])[j] = (double) temp; 
    i++;
	
	if( i == rows )
    { i=0; j++; }
   }
   break;
   
  case 2:
   // all fields are signed ints of size 4 bytes  
   
   for( int n=0; n < rows*cols ; n++ )
   {
	int temp;
	fread( (char*) &temp, sizeof(int), 1 , fp );
	(output[i])[j] = (double) temp; 
    i++;
	
	if( i == rows )
    { i=0; j++; }
   }  
   break;
   
  case 3:
   // all fields are signed ints of size 2 bytes  
   
   for( int n=0; n < rows*cols ; n++ )
   {
	short temp;
	fread( (char*) &temp, sizeof(short), 1 , fp );
	(output[i])[j] = (double) temp; 
    i++;
	
	if( i == rows )
    { i=0; j++; }
   }  
   break;
   
  case 4:
   // all fields are unsigned ints of size 2 bytes  
   
   for( int n=0; n < rows*cols ; n++ )
   {
	unsigned short temp;
	fread( (char*) &temp, sizeof(unsigned short), 1 , fp );
	(output[i])[j] = (double) temp; 
    i++;
	
	if( i == rows )
    { i=0; j++; }
   }  
   break;  
  
  case 5:
   // all fields are unsigned ints of size 1 bytes  
   
   for( int n=0; n < rows*cols ; n++ )
   {
	unsigned char temp;
	fread( (char*) &temp, sizeof(unsigned char), 1 , fp );
	(output[i])[j] = (double) temp; 
    i++;
	
	if( i == rows )
    { i=0; j++; }
   }  
   break; 
   
  default:
   std::cout << "Error: Unknown format!" << std::endl;
   break;
 }
 
 // read the imaginary part of the matrix (not supported!)
 
 fclose( fp );
 
 delete name;
 return output;
}
Ejemplo n.º 4
0
FILE* read_matlab_header( int* rows, int* cols , std::string filename )
{
 FILE* fp; 
 fp = fopen( filename.c_str() , "rb" );
 if( fp == NULL )
 {
  std::cout << "Error: could not open file " << filename << "!" << std::endl;
  return NULL;
 }
 
 typedef unsigned int UINT;
 UINT UINTs = sizeof(UINT);
 
 // read the basic header information 
 
 UINT temp;
 
 fread( (char*) &temp , UINTs , 1 , fp );
 
 UINT type_numeric_format = thousands(temp);
 UINT type_reserved = hundreds(temp);
 UINT type_data_format = tens(temp);
 UINT type_matrix_type = ones(temp);
	
 // make sure it's a matlab L4 file 
 	  
 if( type_numeric_format != 0 || // 	little-endian
     type_reserved != 0 || // should always be 0
     type_data_format > 5 || // unknown format
     type_matrix_type != 0 ) // want full matrices, not sparse 
 {
  std::cout << "Error reading file " << filename << ": I can't read this format yet!" << std::endl;
  fclose( fp );
  return NULL;
 } 

 // get the size of the data 
 
// UINT rows;
 fread( (char*) rows , UINTs , 1, fp );
 
// UINT cols;
 fread( (char*) cols, UINTs , 1 , fp );
 
 // resize the output accordingly 

 // make sure we're not dealing with complex numbers 
 
 UINT imag;
 fread( (char*) &imag, UINTs, 1 , fp );
 if( imag != 0 )
 {
  std::cout << "Error: I can't read imaginary matrices yet!" << std::endl;
  fclose( fp );
  return NULL;
 }

 UINT name_length;
 fread( (char*) &name_length, UINTs, 1 , fp );
 char* name;
 name = new char [name_length];

 // this is the end of the 20-byte header 

 // read the name
 
 fread( name , name_length , 1 , fp );
 delete name; 
  
 return fp; 
}
Ejemplo n.º 5
0
void loops(){

    //Variables
        int a;
        int b;
        int c = 0;

        a = 1;
        b = 1;

        // Print backround information

        printf("This program will print the first twenty terms of the Fibonacci series using a loop. ");


        // Deterime first twenty terms of the Fibonacci series
        int counter;
        for (counter = 1; counter <= 20; counter = counter + 1) {

            a = b;
            b = c;
            c = (a + b);

            printf("%d\n",c);

}


void arrays(){
 const char *dayOfTheWeek[7];

        dayOfTheWeek[0] = "Sunday";
        dayOfTheWeek[1] = "Monday";
        dayOfTheWeek[2] = "Tuesday";
        dayOfTheWeek[3] = "Wednesday";
        dayOfTheWeek[4] = "Thursday";
        dayOfTheWeek[5] = "Friday";
        dayOfTheWeek[6] = "Saturday";

        int temperature[7];
        
        int day;
        for (day = 0; day <= 6; day = day + 1) {
            printf( "Please Enter the temperature for " , dayOfTheWeek[day] , " : ");
            scanf("%d", temperature[day]);
        }

        printf("----RESULTS-----\n");
        int indexOfMax = 0;
       
        for (day = 0; day <= 8; day = day + 1) {
            if (temperature[day] > temperature[indexOfMax]) {
                indexOfMax = day;
            }
            printf("Temperature for ",  dayOfTheWeek[day] , " : " ,temperature[day]);
        }

}

void methods(){
  // Variable
        int number;

        //Get number from user
        printf("Please enter a number from 10 to 99 in digit representaion:");
        scanf( "%d", & number);

        if (number >= 10 && number <= 19) {
            teens(number);
        } else {
            hundreds(number);
            tens(number);
            ones(number);
        }
}