C++/Data Type/Int

Материал из C\C++ эксперт
Перейти к: навигация, поиск

addition operator in cout

 
#include <iostream>
using namespace std;
int main(void)
{
   int total, added, dropped;
   cout << "Enter total: ";
   cin >> total;
   cout << "Enter added: ";
   cin >> added;
   total = total + added;
   cout << "Enter dropped ";
   cin >> dropped;
   total -= dropped;
   cout << "Number: " << total << endl;
   return 0;
}


Computes the lowest common denominator.

 
#include <iostream>
using namespace std;
int main()
{
  int a, b, d, min;
  cout << "Enter two numbers: ";
  cin >> a >> b;
  min = a > b ? b : a;
  for(d = 2; d<min; d++) 
    if(((a%d)==0) && ((b%d)==0)) 
       break;
  if(d==min) {
    cout << "No common denominators\n";
    return 0;
  }
  cout << "The lowest common denominator is " << d << ".\n";
  return 0;
}


convert string (char) to int

  
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char **argv)
{ 
   char letter;
   int count = 0;
   int line_limit;
   line_limit = atoi(argv[1]);
   while ((letter = cin.get()) != -1){
       cout.put(letter);
       if ((letter == "\n") && (++count == line_limit))
         break;
     }
}


cout: output INT_MIN, INT_MAX and UINT

 

#include <iostream>
#include <climits>       // Definition of INT_MIN, ...
using namespace std;
int main()
{
  cout << "Range of types int and unsigned int" << endl;
  cout << "Type   Minimum    Maximum" << endl;
  cout << "int" << INT_MIN << "   " <<  INT_MAX << endl;
  cout << "unsigned int" <<  " 0 " << UINT_MAX << endl;
  return 0;
}


Define, input and output int value

 
#include <iostream>
using namespace std;
int main()
{
  int b, e, r;
  cout << "Enter base: ";
  cin >> b;
  cout << "Enter exponent: ";
  cin >> e;
  r = 1;
  for( ; e; e--) 
    r = r * b;
  
  cout << "Result: " << r;
  return 0;
}


Divided by int

 
#include <iostream>
using namespace std;
int main(void)
{
   int firstOp = 10, secondOp = 4;
   float result = firstOp / secondOp;
   cout << firstOp << " / " << secondOp << " = " << result;
   return 0;
}


Inputting int Values for Multiple Variables

 
#include <iostream>
using namespace std;
int main(void)
{
   int myWeight, myHeight;
   string myName;
   cout << "Enter your name: ";
   cin >> myName;
   cout << "Enter your weight in pounds: ";
   cin >> myWeight;
   cout << "Enter your height in inches: ";
   cin >> myHeight;
   cout << "Your name score is " << myName << "\n";
   cout << "Your weight in pounds is " << myWeight << "\n";
   cout << "Your height in inches is " << myHeight << "\n";
   return 0;
}


Integer pointer

  
#include <iostream>
using std::cout;
using std::endl;
int main()
{
  int x = 5;
  // "p" points to "x"
  int* p = &x;
  cout << "x = " << x << endl;
  // change the value of "x" through "p"
  *p = 6;
  cout << "x = " << x << endl;
  return 0;
}


Int value operations

 

#include <iostream>
#include <string>
using namespace std;
int main(void)
{
   int total, added, dropped, tuition;
   cout << "Enter total: ";
   cin >> total;
   cout << "Enter add: ";
   cin >> added;
   total = total + added;
   cout << "Enter dropped? ";
   cin >> dropped;
   total -= dropped;
   cout << "Total number: " << total << endl;
   tuition = (total + dropped) * 72;
   cout << "Total tuition: $" << tuition << endl;
   cout << "Average tuition: $" 
        << (float) tuition / total;
   return 0;
}


Using atoi: convert string to int

  
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
   int i = atoi( "2593" );
   cout << "The string \"2593\" converted to int is " << i
        << "\nThe converted value minus 593 is " << i - 593 
        << endl;
   return 0;
}


Using cin Object to Read Integer Values from Keyboard

  
#include <iostream>
using namespace std;
int main(){
   int a = 0, b = 0, c = 0;
   cout << "Enter values for a, b, and c: ";
   cin >> a >> b >> c;
   cout << a << " " << b << " " << c << endl;
   return 0;
}