140 likes | 156 Vues
Learn about rules for division in C++ involving integers and decimals, assignment conversions, forcing type changes, and standard input/output methods. Includes examples and format manipulations.
E N D
Programming C++ Numerical Data Input/Output
Rules for Division • C++ treats integers differently from decimal numbers. • 100 is an int type. • 100.0 , 100.0000, and 100. are double type. • The general rule for division of int and double types is: • double/double -> double (normal) • double/int -> double (normal) • int/double -> double (normal) • int/int -> int (note: the decimal part is discarded)
Rules for Division • Examples: • 220. / 100.0 double/double -> double result is 2.2 • 220. / 100 double/int -> double result is 2.2 • 220 / 100.0 int/double -> double result is 2.2 • 220 / 100 int/int -> int result is 2 • Summary: division is normal unless both the numerator and denominator are int, then the result is an int (the decimal part is discarded).
Assignment Conversions • A decimal number assigned to an int type variable is truncated. • An integer assigned to a double type variable is converted to a decimal number. • Example 1: double yy = 2.7; int i = 15; int j = 10; i = yy; // i is now 2 yy = j; // yy is now 10.0
Assignment Conversions • Example 2: int m, n; double xx; m = 7; n = 2.5; xx = m / n; n = xx + m / 2; // What is the value of n?
Assignment Conversions • Example 2: int m, n; double xx; m = 7; n = 2.5; // 2.5 converted to 2 and assigned to n xx = m/n; // 7/2=3 converted to 3.0 and assigned to xx n = xx+m/2; // m/2=3 : integer division // xx+m/2 : double addition because xx is double // convert result of m/2 to double (i.e. 3.0) // xx+m/2=6.0 // convert result of xx+m/2 to int (i.e. 6) // because n is int
Forcing a Type Change • You can change the type of an expression with a cast operation. • Syntax: variable1 = type(variable2); variable1 = type(expression); • Example: int x=1, y=2; double result1 = x/y; // result1 is 0.0 double result2 = double(x)/y; // result2 is 0.5 double result3 = x/double(y); // result3 is 0.5 double result4 = double(x)/double(y);//result4 is 0.5 double result5 = double(x/y); // result5 is 0.0 int cents = int(result4*100); // cents is 50
Standard Input/Output • cin - the standard input stream Input operator “>>” • Extracts data from input “stream” (the keyboard by default). • Skips over white spaces. • Extracts only characters of the right form and performs automatic conversion to the type specified.
Standard Input/Output • cout - the standard output stream Output operator “<<” • Inserts data into the output “stream” (the screen by default). • Example: int id, score; cout << "Enter student ID and score: "; cin >> id >> score; cout << "Student ID: " << id << " score: " << score << endl;
Standard Input/Output Some special output characters: \a bell \t tab \n new line \' single quote \" double quote
Format Manipulation #include <iomanip> • setw(int size) Specifies the number of characters to use in displaying the next value, which is right-justified. Ex: cout << setw(5) << 12; //output 3 spaces and then 12 • setprecision(int digit) Specifies the number of significant digits for all subsequent output. • cout << fixed << setprecision(2); the precision is: two digits after the decimal point. • Fixed: uses fixed-point notation in the float field
Format Manipulation #include <iomanip> #include <iostream> using namespace std; int main() { cout << setprecision(2) << 12345678 << endl; // output 12345678 cout << setprecision(2) <<123.45678 << endl; //output 1.2e+002 return 0; }
// Demonstrate the features of output format manipulators // Input: cost of lunch, number of people attending lunch // Output: lunch cost per person #include <iomanip> // Use IO manipulators #include <iostream> using namespace std; int main() { double cost_of_lunch, cost_per_person; int number_of_people; cout << "Press return after entering a number.\n"; cout << "Enter the cost of lunch:\n"; cin >> cost_of_lunch; cout << "Enter the number of people attending lunch:\n"; cin >> number_of_people; cost_per_person = cost_of_lunch / number_of_people; //cout << setiosflags(ios::fixed) << setprecision(2); cout << "If the lunch cost $"; cout << cost_of_lunch; cout << ", and you have " << number_of_people << " persons attending, then \n";
cout << "the cost per person is $" << cost_per_person << ".\n"; /* cout << "the cost per person is $"; cout << setprecision(4) << cost_per_person << ".\n"; */ return 0; } Output of example program: Press return after entering a number. Enter the cost of lunch: 800.75 Enter the number of people attending lunch: 9 If the lunch cost $800.75, and you have 9 attending, then the cost is $88.9722. Using setprecision(4) in the last cout statement can change the final result to $88.97.