lunes, 1 de mayo de 2017

C++ / MinGW - GCC Tutorial

  1. Operations


Hello World Code:

#include <iostream>

int main()
{
 std::cout << "Hello World!!!\n";
 return 0;
}


Operations Code:

#include <iostream>
int main()
 {
   int x = 10;
   int y = 8;
   std::cout << std::endl;
   std::cout << "x = " << x;
   std::cout << ", y = " << y;
   std::cout << ", add = " << x + y << ", multiplication = " << x * y;
   std::cout << ", subtraction = " << x - y << ", division = " << x / y;
   std::cout << std::endl;
   return 0;
_______________________________________________________________________

  2. Functions




Function 1 Code:

#include <iostream>
  
// function
// print a message
void ExampleFunction()
{
   std::cout << "An example of function\n";
}
  
// function main - print a message, then  
// call to ExampleFunction, then print a second message

int main()
{
    std::cout << "In main\n" ;
    ExampleFunction();
    std::cout << "Now, We are in main\n";
    return 0;
}



Function 2 Code:

#include <iostream>

int Add (int first, int second)
{
    std::cout << "In Add(), received " << first << " and " << second <<<"\n";
    return (first + second);
}
  
int main()
{
    using std::cout;
    using std::cin;
  
  
    cout << "We are in main()\n";
    int a, b, c;
    cout << "Write two numbers: ";
    cin >> a;
    cin >> b;
    cout << "\nCall Add()\n";
    c=Add(a,b);
    cout << "\nNow We are in Main().\n";
    cout << "c was configured as " << c;
    cout << "\nOutput...\n\n";
    return 0;
}

_______________________________________________________________________

  3. Characters and Sizeof



Characters Code:

/*print the values of ascii code in dec, hex and oct*/
#include <stdio.h>
#include <conio.h>

int main()
{
    int i;
   
    for(i = 0; i != 256; i++)
    switch (i)
    {
        case 9:
            printf("Character = 'tab' -- Dec = %i -- Hex = %X -- Oct = %o -- Write \\x%x o \\t\n", i, i, i, i);
            break;
        case 10:
            printf("Character = 'enter' -- Dec = %i -- Hex = %X -- Oct = %o -- Write \\x%x o \\n\n", i, i, i, i);
            break;
        case 13:
            printf("Character = 'return' -- Dec = %i -- Hex = %X -- Oct = %o -- Write \\x%x o \\r\n", i, i, i, i);
            break;
        default:
            printf("Character = %c -- Dec = %i -- Hex = %X -- Oct = %o -- Write \\x%x\n", i, i, i, i, i);
            break;
    }   
   
    getch();
    return 0;
}

Sizeof Code:

#include <iostream>
  
int main()
{
   using std::cout;
  
   cout << "The size of an int is:\t"    
      << sizeof(int)    << " bytes.\n";
   cout << "The size of a short int is:\t" 
      << sizeof(short)  << " bytes.\n";
   cout << "The size of a long int es:\t"  
      << sizeof(long)   << " bytes.\n";
   cout << "The size of a char is:\t"    
      << sizeof(char)   << " bytes.\n";
   cout << "The size of a float is:\t"   
       << sizeof(float)  << " bytes.\n";
   cout << "The size of a double is:\t"    
      << sizeof(double) << " bytes.\n";
   cout << "The size of a bool is:\t"      
      << sizeof(bool)   << " bytes.\n";
  
   return 0;
}

_______________________________________________________________________

  4. If ... Else


Fix Code:

   // use of pre and post suffix
   // use of inc 
   
   #include <iostream>
   int main()
   {
      using std::cout;
   
      int myAge = 20;     
      int yourAge = 31;
      cout << "I have: " << myAge << " years old.\n";
      cout << "You have: " << yourAge << " years old\n";
      myAge++;            // inc post
      ++yourAge;          // inc pre
      cout << "One year ago...\n";
      cout << "I have: " << myAge << " years old.\n";
      cout << "You have: " << yourAge << " years old\n";
      cout << "Two years ago\n";
      cout << "I have: " << myAge++ << " years old.\n";
      cout << "You have: " << ++yourAge << " years old\n";
     return 0;
   }


If Code:

 // Use of IF

   #include <iostream>
   
   int main()
   {
      using std::cout; 
      using std::cin;
   
      int RealMadrid, Barcelona;
      cout << "Enter the score for the Real Madrid: ";
      cin >> RealMadrid;
   
      cout << "\nEnter the score for the Barcelona ";
      cin >> Barcelona;
   
      cout << "\n";
   
      if (RealMadrid > Barcelona)
         cout << "Let's Go Real Madrid!\n";
   
      if (RealMadrid < Barcelona)
      {
         cout << "Go Barcelona!\n";
      }
   
      if (RealMadrid == Barcelona)
      {
         cout << "Can't be.\n";
         cout << "Give me the real score for the Barcelona: ";
         cin >> Barcelona;
   
        if (RealMadrid > Barcelona)
            cout << "Let's Go Real Madrid!";
   
         if (Barcelona > RealMadrid)
            cout << "Knew it! Go Barcelona!";
  
         if (Barcelona == RealMadrid)
            cout << "It really was a tie!";
      } 
      return 0;
   }

If ... Else Code:

  // IF and ELSE
   
   #include <iostream>
   int main()
   {
        
     using namespace std;
 
     int first, second;
     cout << "Write two numbers.\nFirst: ";
     cin >> first;
     cout << "\nSecond: ";
     cin >> second;
     cout << "\n\n";
 
     if (first >= second)
     {
        if ( (first % second) == 0) 
        {
           if (first == second)
              cout << "They're the same numbers\n";
           else
              cout << "They're divisible\n";
        }
        else
           cout << "They're not divisible\n\n";
     }
     else
        cout << "The second is major than the first\n";
     return 0;
  }

_______________________________________________________________________

  5. Local and Global Variables


Local and Global Variables Code:

 #include <iostream>
   
   void myFunction();           // prototype 
   int x = 2, y = 3;            // global

   int main()
   {
      using namespace std;
 
     cout << "x from main: " << x  << std::endl;
     cout << "y from main: " << y  << std::endl;
     myFunction();
     cout << "From my function" << std::endl;
     cout << "x from main: " << x < std::endl;
     cout << "y from main: " << y << std::endl;
     return 0;
  }

    void myFunction()
  {
     using std::cout;
     int x = 4;
     int y = 5;

     cout << "x from myFunction: " << x << std::endl;
     cout << "y from myFunction: " << y << std::endl;
  }


Inline Code:

 // Function inline
   #include <iostream>
 
   inline int Double(int);
 
   int main()
   {
     int objetive;
     using std::cout;
     using std::cin;
     using std::endl;

     cout << "Write a number: ";
     cin >> objetive;
     cout << "\n";

     objetive = Double(objetive);
     cout << "Objetive: " << objetive << endl;

     objetive = Double(objetive);
     cout << "Objetive: " << objetive << endl;

     objetive = Double(objetive);
     cout << "Objetive: " << objetive << endl;
     return 0;
  }

  int Double(int objetive)
  {
     return 2*objetive;
  }

_______________________________________________________________________

6. Class and Method and Pointers


Class Code:

   // Class object of a class
 
   #include <iostream>
 
   class Dog               
   {
     public:               // public members
     int Weight;      // variable 
  };        

  int main()
  {
     Dog Beethoven;
     Beethoven.Weight = 110; 
     std::cout << "Beethoven is a dog and his weight is: " ;
     std::cout << Beethoven.Weight << " Kgs.\n";
     return 0;
  }


Method Code:

    // Class and Methods
   
   #include <iostream>                    // cout
 
   class Dog  
   {
     public:                       // public section
       int GetAge();                // accessors function
       void ConfigureAge (int age);  // accessors function
       void Guau();               // general function
    private:                        // start private section
      int hisAge;                   // variable 
  };

  // Get Age
  int Dog::GetAge()
  {
     return hisAge;
  }

  // Configure age
  void Dog::ConfigureAge(int age)
  {
     hisAge = age;
  }

  // Guau
  void Dog::Guau()
  {
     std::cout << "guau guau.\n";
  }

  // create a Dog, configure age, guau, age and guau

  int main()
  {
     Dog Beethoven;
     Beethoven.ConfigureAge(10);
     Beethoven.Guau();
     std::cout << "Beethoven is a dog and he has " ;
     std::cout << Beethoven.GetAge() << " years old.\n";
     Beethoven.Guau();
     return 0;
  }

Pointer Code:

 //Get values of a function with pointers

   #include <iostream>

   using namespace std;
   
   short Factor(int n, int* pSquare, int* pCubeo);

   int main()
   {
     int number, square, cube;
     short error;

  cout << "Write a number (0 - 20): ";
     cin >> number;

  error = Factor(number, &square, &cube);

  if (!error)
     {
        cout << "number: " << number << endl;
        cout << "square: " << square << endl;
        cout << "cube: "  << cube   << endl;
     }

  else
        cout << "Error " << endl;
     return 0;
  }

   short Factor(int n, int *pSquare, int *pCube)
  {
    short Value = 0;

    if (n > 20)
        Value = 1;

    else
     {
        *pSquare = n*n;
        *pCube = n*n*n;
        Value = 0;
     }

    return Value;
  } 
 

_______________________________________________________________________

7. Prime Numbers



Code 1:


#include <stdio.h>
//prime numbers between 1 - 100
int main(void)
{
for (int j=2;j<=100;j++){
 int a=0;
 for(int i=1;i<=100;i++)
 {
    if(j%i==0) 
    a++;
 }
 if (a==2){ 
 printf("%d\n", j);
 }
 }
}

Code 2:


#include <stdio.h>
// We want to know if a number is prime
int main(){

int num1,i; 

printf("Give me a number: ");
scanf("%d",&num1); 

 
for(i=1;i<=num1;i++) {
    if(num1%i==0) {        
        printf("%d, it is divisive by %d \n",i,num1);
    }
}

} 

Code 3:

#include <stdio.h>
#include <fstream>
 #include <iostream>
 using namespace std;

int main() {
    int primos_por_mostrar, n, d;
    int es_primo;
 ofstream archivo;
    archivo.open("output.txt"); 

    printf("How many primes you desire to show: ");
    scanf("%d", &primos_por_mostrar);

    n = 2;
    while (primos_por_mostrar > 0) {

        /*  n is prime */
        es_primo = 1;
        for (d = 2; d < n; ++d) {
            if (n % d == 0) {
                es_primo = 0;
                break;
            }
        }
      
        if (es_primo) {
            printf("%d ", n);
     std::cout << std::endl; 
            archivo <<  n << endl;  
    std::cout << std::endl;   
            primos_por_mostrar--;
        }
        n++;
    }

    printf("\n");
    archivo.close();
    return 0;
}



https://www.amazon.com/Guillermo%20Perez/e/B01EZOTUVQ/

guillengap@gmail.com

 

No hay comentarios:

Publicar un comentario