forked from yanshengjia/cpp-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lurenjia
committed
Jan 24, 2016
1 parent
46bd07c
commit 9979ca8
Showing
558 changed files
with
37,486 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Calculate the product of three integers | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
// function main begins program execution | ||
int main() | ||
{ | ||
int x; // first integer to multiply | ||
int y; // second integer to multiply | ||
int z; // third integer to multiply | ||
int result; // the product of the three integers | ||
|
||
cout << "Enter three integers: "; // prompt user for data | ||
cin >> x >> y >> z; // read three integers from user | ||
result = x * y * z; // multiply the three integers; store result | ||
cout << "The product is " << result << endl; // print result; end line | ||
} // end function main | ||
|
||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Exercise 2.16 Solution: ex02_16.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int number1; // first integer read from user | ||
int number2; // second integer read from user | ||
|
||
cout << "Enter two integers: "; // prompt user for data | ||
cin >> number1 >> number2; // read values from user | ||
|
||
// output the results | ||
cout << "The sum is " << number1 + number2 | ||
<< "\nThe product is " << number1 * number2 | ||
<< "\nThe difference is " << number1 - number2 | ||
<< "\nThe quotient is " << number1 / number2 << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Exercise 2.17 Solution: ex02_17.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
// Part a | ||
cout << "1 2 3 4\n"; | ||
|
||
// Part b | ||
cout << "1 " << "2 " << "3 " << "4\n"; | ||
|
||
// Part c | ||
cout << "1 "; | ||
cout << "2 "; | ||
cout << "3 "; | ||
cout << "4" << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Exercise 2.18 Solution: ex02_18.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int number1; // first integer read from user | ||
int number2; // second integer read from user | ||
|
||
cout << "Enter two integers: "; // prompt user for data | ||
cin >> number1 >> number2; // read two integers from user | ||
|
||
if ( number1 == number2 ) | ||
cout << "These numbers are equal." << endl; | ||
|
||
if ( number1 > number2 ) | ||
cout << number1 << " is larger." << endl; | ||
|
||
if ( number2 > number1 ) | ||
cout << number2 << " is larger." << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Exercise 2.19 Solution: ex02_19.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int number1; // first integer read from user | ||
int number2; // second integer read from user | ||
int number3; // third integer read from user | ||
int smallest; // smallest integer read from user | ||
int largest; // largest integer read from user | ||
|
||
cout << "Input three different integers: "; // prompt | ||
cin >> number1 >> number2 >> number3; // read three integers | ||
|
||
largest = number1; // assume first integer is largest | ||
|
||
if ( number2 > largest ) // is number2 larger? | ||
largest = number2; // number2 is now the largest | ||
|
||
if ( number3 > largest ) // is number3 larger? | ||
largest = number3; // number3 is now the largest | ||
|
||
smallest = number1; // assume first integer is smallest | ||
|
||
if ( number2 < smallest ) // is number2 smaller? | ||
smallest = number2; // number2 is now the smallest | ||
|
||
if ( number3 < smallest ) // is number3 smaller? | ||
smallest = number3; // number3 is now the smallest | ||
|
||
cout << "Sum is " << number1 + number2 + number3 | ||
<< "\nAverage is " << ( number1 + number2 + number3 ) / 3 | ||
<< "\nProduct is " << number1 * number2 * number3 | ||
<< "\nSmallest is " << smallest | ||
<< "\nLargest is " << largest << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Exercise 2.20 Solution: ex02_20.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int radius; // variable to store a circle's radius | ||
|
||
cout << "Enter the circle radius: "; // prompt user for radius | ||
cin >> radius; // read radius from user | ||
|
||
cout << "Diameter is " << radius * 2.0 | ||
<< "\nCircumference is " << 2 * 3.14159 * radius | ||
<< "\nArea is " << 3.14159 * radius * radius << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Exercise 2.21 Solution: ex02_21.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
cout << "********* *** * *\n" | ||
<< "* * * * *** * *\n" | ||
<< "* * * * ***** * *\n" | ||
<< "* * * * * * *\n" | ||
<< "* * * * * * *\n" | ||
<< "* * * * * * *\n" | ||
<< "* * * * * * *\n" | ||
<< "* * * * * * *\n" | ||
<< "********* *** * *" << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Exercise 2.23 Solution: ex02_23.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int number1; // first integer read from user | ||
int number2; // second integer read from user | ||
int number3; // third integer read from user | ||
int number4; // fourth integer read from user | ||
int number5; // fifth integer read from user | ||
int smallest; // smallest integer read from user | ||
int largest; // largest integer read from user | ||
|
||
cout << "Enter five integers: "; // prompt user for data | ||
cin >> number1 >> number2 >> number3 >> number4 >> number5; | ||
|
||
largest = number1; // assume first integer is largest | ||
smallest = number1; // assume first integer is smallest | ||
|
||
if ( number2 > largest ) // is number2 larger? | ||
largest = number2; // number2 is new largest | ||
|
||
if ( number3 > largest ) // is number3 larger? | ||
largest = number3; // number3 is new largest | ||
|
||
if ( number4 > largest ) // is number4 larger? | ||
largest = number4; // number4 is new largest | ||
|
||
if ( number5 > largest ) // is number5 larger? | ||
largest = number5; // number5 is new largest | ||
|
||
if ( number2 < smallest ) // is number2 smaller? | ||
smallest = number2; // number2 is new smallest | ||
|
||
if ( number3 < smallest ) // is number3 smaller? | ||
smallest = number3; // number3 is new smallest | ||
|
||
if ( number4 < smallest ) // is number4 smaller? | ||
smallest = number4; // number4 is new smallest | ||
|
||
if ( number5 < smallest ) // is number5 smaller? | ||
smallest = number5; // number5 is new smallest | ||
|
||
cout << "Largest is " << largest | ||
<< "\nSmallest is " << smallest << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Exercise 2.24 Solution: ex02_24.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int number; // integer read from user | ||
|
||
cout << "Enter an integer: "; // prompt | ||
cin >> number; // read integer from user | ||
|
||
if ( number % 2 == 0 ) | ||
cout << "The integer " << number << " is even." << endl; | ||
|
||
if ( number % 2 != 0 ) | ||
cout << "The integer " << number << " is odd." << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Exercise 2.25 Solution: ex02_25.cpp | ||
#include <iostream> // allows program to perform input and output | ||
using namespace std; // program uses names from the std namespace | ||
|
||
int main() | ||
{ | ||
int number1; // first integer read from user | ||
int number2; // second integer read from user | ||
|
||
cout << "Enter two integers: "; // prompt | ||
cin >> number1 >> number2; // read two integers from user | ||
|
||
// using modulus operator | ||
if ( number1 % number2 == 0 ) | ||
cout << number1 << " is a multiple of " << number2 << endl; | ||
|
||
if ( number1 % number2 != 0 ) | ||
cout << number1 << " is not a multiple of " << number2 << endl; | ||
} // end main | ||
|
||
/************************************************************************** | ||
* (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * | ||
* Pearson Education, Inc. All Rights Reserved. * | ||
* * | ||
* DISCLAIMER: The authors and publisher of this book have used their * | ||
* best efforts in preparing the book. These efforts include the * | ||
* development, research, and testing of the theories and programs * | ||
* to determine their effectiveness. The authors and publisher make * | ||
* no warranty of any kind, expressed or implied, with regard to these * | ||
* programs or to the documentation contained in these books. The authors * | ||
* and publisher shall not be liable in any event for incidental or * | ||
* consequential damages in connection with, or arising out of, the * | ||
* furnishing, performance, or use of these programs. * | ||
**************************************************************************/ |
Oops, something went wrong.