C++/Data Type/Int
Содержание
- 1 addition operator in cout
- 2 Computes the lowest common denominator.
- 3 convert string (char) to int
- 4 cout: output INT_MIN, INT_MAX and UINT
- 5 Define, input and output int value
- 6 Divided by int
- 7 Inputting int Values for Multiple Variables
- 8 Integer pointer
- 9 Int value operations
- 10 Using atoi: convert string to int
- 11 Using cin Object to Read Integer Values from Keyboard
addition operator in cout
<source lang="cpp">
- 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;
}
</source>
Computes the lowest common denominator.
<source lang="cpp">
- 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;
}
</source>
convert string (char) to int
<source lang="cpp">
- 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; }
}
</source>
cout: output INT_MIN, INT_MAX and UINT
<source lang="cpp">
- 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;
}
</source>
Define, input and output int value
<source lang="cpp">
- 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;
}
</source>
Divided by int
<source lang="cpp">
- include <iostream>
using namespace std; int main(void) {
int firstOp = 10, secondOp = 4; float result = firstOp / secondOp; cout << firstOp << " / " << secondOp << " = " << result; return 0;
}
</source>
Inputting int Values for Multiple Variables
<source lang="cpp">
- 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;
}
</source>
Integer pointer
<source lang="cpp">
- 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;
}
</source>
Int value operations
<source lang="cpp">
- 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;
}
</source>
Using atoi: convert string to int
<source lang="cpp">
- 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;
}
</source>
Using cin Object to Read Integer Values from Keyboard
<source lang="cpp">
- 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;
}
</source>