added more code
This commit is contained in:
39
CPE221/CreateHeaders.bash
Normal file
39
CPE221/CreateHeaders.bash
Normal file
@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
if [ -e Cpp_header.cpp ]
|
||||
then
|
||||
|
||||
if [ -e a.out ]
|
||||
then
|
||||
rm a.out
|
||||
fi
|
||||
OSNAME=`uname -s`
|
||||
if [ $OSNAME == "SunOS" ]
|
||||
then
|
||||
CC Cpp_header.cpp
|
||||
elif [ $OSNAME == "Linux" ]
|
||||
then
|
||||
g++ Cpp_header.cpp
|
||||
else
|
||||
echo "machine OS ($OSNAME) not recognized. contact TA"
|
||||
exit
|
||||
fi
|
||||
if [ -e a.out ]
|
||||
then
|
||||
rm a.out
|
||||
for x in `ls `
|
||||
do
|
||||
if [ -d $x ]
|
||||
then
|
||||
echo "Copying Cpp_header.cpp to directory $x"
|
||||
cp Cpp_header.cpp $x/$x.cpp
|
||||
fi
|
||||
done
|
||||
else
|
||||
echo "The File Cpp_header.cpp does not compile."
|
||||
echo "Fix the problems and run this script again."
|
||||
fi
|
||||
else
|
||||
echo "The file Cpp_header.cpp does not exist."
|
||||
echo "Correct this problem and run the script again"
|
||||
fi
|
||||
|
54
CPE221/DataTypes1_01.cpp
Normal file
54
CPE221/DataTypes1_01.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
// DataTypes 1, program 1: DataTypes1_01.cpp
|
||||
//
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <cctype> // required for chharacter functions
|
||||
|
||||
using namespace std;
|
||||
|
||||
// This program looks at char variables and how they correspond
|
||||
// to integers.
|
||||
//
|
||||
int main()
|
||||
{
|
||||
const float PI = 3.14159;
|
||||
int count; // loop control variable
|
||||
char letter; // holds character corresponding to count
|
||||
string line; // used for program flow control
|
||||
|
||||
// can you assign a character variable to a string
|
||||
letter='j';
|
||||
line = letter;
|
||||
cout << "Assigned character variable to a string variable: " << line << endl;
|
||||
|
||||
// print out the corresponding character for an integer value
|
||||
// The machine has the ascii character set, so only integers
|
||||
// 0 to 127 have significance. However, there are other
|
||||
// characters available for values 128 to 255. Note that since
|
||||
// char variables are only one byte, the highest integer value
|
||||
// that can be assigned to them is 255. A value of 256 results
|
||||
// in a wrapping back to 0, 257 to 1, etc.
|
||||
|
||||
for (count = 0 ; count < 260; count++)
|
||||
{
|
||||
letter = count; // use implicit coercion to assign an integer value
|
||||
// to the char variable letter
|
||||
cout << "Count: " << count;
|
||||
cout << " integer(0-256): " << count%256 << " is: " << letter << endl;
|
||||
|
||||
// put a pause in the printing of the values after every 20 values.
|
||||
// Note testing for a vlue of 19 instead of 0 since count starts at 0
|
||||
// if I tested for equal to 0, I would get a pause after one value and then every
|
||||
// twenty. by testing on 19, the first pause occurs after 20 values every time
|
||||
|
||||
if (count%20 == 19)
|
||||
{
|
||||
cout << "press enter to continue:";
|
||||
getline(cin,line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
58
CPE221/DataTypes2_02.cpp
Normal file
58
CPE221/DataTypes2_02.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// ***** Program Ch03_2.cpp ***********
|
||||
// Example of arithmetic operations, increment and decrement
|
||||
// operators
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
// declare variables for use in the program and assign them an initial
|
||||
// value. These variables are used in various expressions to illustrate
|
||||
// mathematical computations in C++
|
||||
int num1 = 10, num2 = 20, num3 = 30;
|
||||
float flt1 = 40, flt2 = 50.0, flt3 = 60.5;
|
||||
int num;
|
||||
float flt;
|
||||
|
||||
|
||||
// Output the values of the variables used in the program. This information
|
||||
// is then used to verify the results shown for the mathematical operations
|
||||
|
||||
cout << "num1 = " << num1 << " : num2 = " << num2 << " : num3 = " << num3 << endl;
|
||||
cout << "flt1 = " << flt1 << " : flt2 = " << flt2 << " : flt3 = " << flt3 << endl;
|
||||
cout << "=================================================================\n";
|
||||
|
||||
// Addition and subtraction
|
||||
// Perform integer and floating point addition and subtraction and output
|
||||
// the results.
|
||||
cout << "Addition and subtraction\n\n";
|
||||
num = num1 - num2 + num3 +5;
|
||||
cout << "num1 - num2 + num3 +5 = " << num << endl;
|
||||
flt = -flt1 + flt2 + flt3;
|
||||
cout << "-flt1 + flt2 + flt3 = " << flt << endl;
|
||||
cout << "=================================================================\n";
|
||||
|
||||
// Integer and Floating point multiplication examples
|
||||
cout << "Multiplication\n\n";
|
||||
cout << "num1*num2 = " << num1*num2 << endl;
|
||||
cout << "flt1*flt3 = " << flt1*flt3 << endl;
|
||||
cout << "=================================================================\n";
|
||||
|
||||
|
||||
// Example of floating point division
|
||||
cout << "floating point division\n";
|
||||
cout << "flt1/flt2 = " << flt1/flt2 << endl;
|
||||
cout << "16.0000/5.0000 = " << 16.0000/5.0000 << endl;
|
||||
cout << "16.0000/4.0000 = " << 16.0000/4.0000 << endl;
|
||||
|
||||
// Example of integer division
|
||||
cout << "\nInteger division: " << num1 << "/" << num2 << " = "
|
||||
<< num1/num2 << endl;
|
||||
cout << "integer division2, 19/3 = " << 19/3 << endl;
|
||||
cout << "integer division3, 17/9 = " << 17/9 << endl;
|
||||
cout << "=================================================================\n";
|
||||
|
||||
return 0;
|
||||
}
|
31
CPE221/DataTypes2_02b.cpp
Normal file
31
CPE221/DataTypes2_02b.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
//
|
||||
// ***** Program Ch03_2b.cpp ***********
|
||||
// Example of arithmetic operations, increment and decrement
|
||||
// operators
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
cout << "Integer division\n\n";
|
||||
cout << "10/2: " << 10/2 << endl;
|
||||
cout << "10/3: " << 10/3 << endl;
|
||||
cout << "10/4: " << 10/4 << endl;
|
||||
cout << "10/6: " << 10/6 << endl;
|
||||
|
||||
cout << "\nModulus operation\n";
|
||||
// Modulus operation examples. These examples show the
|
||||
// result of the modulos operation on various integer division values.
|
||||
|
||||
cout << "10 mod 2: " << 10%2 << endl;
|
||||
cout << "10 mod 3: " << 10%3 << endl;
|
||||
cout << "10 mod 4: " << 10%4 << endl;
|
||||
cout << "10 mod 6: " << 10%6 << endl;
|
||||
cout << "*******************************\n";
|
||||
//cout << "10.5 mod 6.5: " << 10.5%6.5 << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
53
CPE221/DataTypes2_02c.cpp
Normal file
53
CPE221/DataTypes2_02c.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
//
|
||||
// ***** Program Ch03_2c.cpp ***********
|
||||
// Example of arithmetic operations, increment and decrement
|
||||
// operators
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int num1 = 10; // variable used for increment and decrement operations
|
||||
|
||||
// Perform prefix and postfix increment and decrement operations
|
||||
// on the num1 variable. These operations are performed in an output
|
||||
// statement to illustrate the difference between the two methods
|
||||
|
||||
// increment and decrement operators - POSTFIX
|
||||
cout << "POSTFIX incremennt and decrement operations\n";
|
||||
cout << "*******************************************\n";
|
||||
cout << "Perform Postfix increment:\n";
|
||||
cout << "\tnum1 before postfix ++ operation: " << num1 << endl;
|
||||
cout << "\tnum1 during postfix ++ operation: " << num1++ <<endl;
|
||||
cout << "\tnum1 after postfix ++ operation: " << num1 << endl;
|
||||
cout << "*******************************************\n";
|
||||
|
||||
cout << "Perform Postfix decrement:\n";
|
||||
cout << "\tnum1 before postfix -- operation: " << num1 << endl;
|
||||
cout << "\tnum1 during postfix -- operation: " << num1-- <<endl;
|
||||
cout << "\tnum1 after postfix -- operation: " << num1 << endl;
|
||||
cout << "*******************************************\n";
|
||||
|
||||
|
||||
|
||||
// increment and decrement operators - PREFIX
|
||||
cout << "PREFIX increment and decrement operations\n";
|
||||
cout << "*******************************************\n";
|
||||
cout << "Perform Prefix increment:\n";
|
||||
cout << "\tnum1 before prefix ++ operation: " << num1 << endl;
|
||||
cout << "\tnum1 during prefix ++ operation: " << ++num1 <<endl;
|
||||
cout << "\tnum1 after prefix ++ operation: " << num1 << endl;
|
||||
cout << "*******************************************\n";
|
||||
|
||||
cout << "Perform Prefix decrement:\n";
|
||||
cout << "\tnum1 before prefix -- operation: " << num1 << endl;
|
||||
cout << "\tnum1 during prefix -- operation: " << --num1 <<endl;
|
||||
cout << "\tnum1 after prefix -- operation: " << num1 << endl;
|
||||
cout << "*******************************************\n";
|
||||
|
||||
return 0;
|
||||
}
|
63
CPE221/DataTypes2_03.cpp
Normal file
63
CPE221/DataTypes2_03.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
//
|
||||
// Chpater 03 program 3: Ch_03_03.cpp
|
||||
// Type coercion - implicit and explicit, mixed data type statements
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int num1 = 50; // variable used in coercion equations
|
||||
int num; // variable to hold coerced values
|
||||
float flt1 = 3.14159; // variable used in coercion equations
|
||||
float flt; // variable to hold coerced values
|
||||
|
||||
// If you want to coerce a number from one data type
|
||||
// to another, show it by using type casting. do not let
|
||||
// the computer do it automatically by using implicit type coercion
|
||||
|
||||
// implicit type coercion - conversion is done automatically
|
||||
cout << "Implicit type coercion of integer to float and float to an int.i\n";
|
||||
num = flt1;
|
||||
flt = num1;
|
||||
cout << "int num contains the value for 3.14159: " << num << endl;
|
||||
cout << "float flt contains the value for 50: " << flt << endl;
|
||||
cout << "***********************************\n\n";
|
||||
|
||||
// explicit type coercion - type casting
|
||||
// here num1 is converted to a float before the multiplication,
|
||||
// then the overall result is converted to an integer.
|
||||
cout << "Explicit type coercion of int to a float and a float to an int\n";
|
||||
num = int(flt1*float(num1));
|
||||
cout << "type cast value for num: " << num << endl;
|
||||
cout << "calculated float value: " << flt1*num1 << endl;
|
||||
cout << "***********************************\n\n";
|
||||
|
||||
// in this statement, flt1 is converted to an integer to
|
||||
// perform integer division, and then the result is made a float
|
||||
cout << "integer division result converted to a float\n";
|
||||
flt = float(num1/int(flt1));
|
||||
cout << "calculated integer value: " << num1/int(flt1) << endl;
|
||||
cout << "type cast value for flt: " << flt << endl;
|
||||
cout << "***********************************\n\n";
|
||||
|
||||
// mixed data type statements
|
||||
// illustration of floating point arithmetic with implicit coercion from int to float
|
||||
cout << "Mixed expressions output\n";
|
||||
cout << "Value of 3.0/2 + 0.75 = " << 3.0/2 + 0.75 << endl;
|
||||
|
||||
// Illustration of integer division followed by implicit coercion to a float
|
||||
flt1 = 100/40*1.0;
|
||||
cout << "Value of 100/40*1.0 = " << flt1 << endl;
|
||||
|
||||
flt1 = 1.0*100/40;
|
||||
cout << "Value of 1.0*100/40 = " << flt1 << endl;
|
||||
|
||||
flt1 = float(num1/13); // integer division typecast to float
|
||||
cout << "float(num1/13) = " << flt1 << endl;
|
||||
|
||||
flt1 = float(num1)/13; // integer division typecast to float
|
||||
cout << "float(num1)/13 = " << flt1 << endl;
|
||||
|
||||
return 0;
|
||||
}
|
59
CPE221/DataTypes2_05.cpp
Normal file
59
CPE221/DataTypes2_05.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
//
|
||||
// Chpater 03 program 5: Ch_03_05.cpp
|
||||
#include <iostream>
|
||||
#include <iomanip> // for using setw and setprecision
|
||||
#include <string> // so that the string class can be used
|
||||
using namespace std;
|
||||
main()
|
||||
{
|
||||
|
||||
string str1 = "This is a programming class";
|
||||
string str2 = "Instructor";
|
||||
string str3 = "cl";
|
||||
string::size_type length; // holds length of a string variable
|
||||
string::size_type pos; // holds character position in a string
|
||||
|
||||
// output values for string::npos under various conditions
|
||||
cout << endl;
|
||||
cout << "Value for string::npos: " << string::npos << endl;
|
||||
|
||||
int number = string::npos;
|
||||
cout << "string::npos assigned to an integer variable: " << number << endl;
|
||||
|
||||
unsigned int number2 = string::npos;
|
||||
cout << "string::npos assigned to an unsigned integer variable: " << number2 << endl;
|
||||
|
||||
// output how much memory certain data types use
|
||||
cout << "size of int: " << sizeof(int) << endl;
|
||||
cout << "size of long: " << sizeof(long) << endl;
|
||||
cout << "size of long long: " << sizeof(long long) << endl;
|
||||
cout << "size of Ulong: " << sizeof(unsigned long) << endl;
|
||||
cout << "size of string::size_type: " << sizeof(string::size_type) << endl;
|
||||
cout << endl;
|
||||
|
||||
|
||||
cout << "The following two strings will be manipulated "
|
||||
<< "in the program\n";
|
||||
cout << " 0123456789012345678901234567890\n";
|
||||
cout << "str1: " << str1 << endl;
|
||||
cout << "str2: " << str2 << endl << endl;
|
||||
|
||||
// show that length() and size() both perform the same function
|
||||
cout << "Determine number of characters in str1 and str2 "
|
||||
<< "using length and size\n";
|
||||
length = str1.length();
|
||||
cout << "The length of str1 is: " << length << endl;
|
||||
length = str2.size();
|
||||
cout << "The size of str2 is: " << length << endl << endl;
|
||||
|
||||
cout << "Use the find function to find various sub strings in str1\n";
|
||||
pos = str1.find("gram");
|
||||
cout << "\"gram\" first occurs in str1 at position: " << pos << endl;
|
||||
|
||||
pos = str1.find("GRAM");
|
||||
cout << "\"GRAM\" is not in str1, so string::npos is returned: " << pos << endl;
|
||||
|
||||
pos = str1.find(str3+"as");
|
||||
cout << "\"" << str3 + "as" << "\" is found in str1 at position: " << pos << endl;
|
||||
return 0;
|
||||
}
|
72
CPE221/DataTypes2_06.cpp
Normal file
72
CPE221/DataTypes2_06.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
//
|
||||
// Chpater 03 program 6: Ch_03_06.cpp
|
||||
#include <iostream>
|
||||
#include <iomanip> // for using setw and setprecision
|
||||
#include <string> // so that the string class can be used
|
||||
using namespace std;
|
||||
main()
|
||||
{
|
||||
string str1 = "This is a programming class";
|
||||
string str2 = ""; // str2 contains the substring,
|
||||
|
||||
// output what is contained in string1, with a position indicator
|
||||
cout << endl;
|
||||
cout << "Substrings will be found in the following string:\n";
|
||||
cout << " 0123456789012345678901234567890\n";
|
||||
cout << "str1: " << str1 << endl << endl;
|
||||
|
||||
// Look at various substring values
|
||||
// the substrings are output between the arrow indicators
|
||||
// of ==> and <==
|
||||
|
||||
// Length of 0 attempt - null string
|
||||
str2 = str1.substr(4,0);
|
||||
cout << "str1.substr(4,0) should return null string\n";
|
||||
cout << "str2 contains the null ==>" << str2 << "<== string\n";
|
||||
|
||||
// two valid attempts - one with literals, one using find function
|
||||
str2 = str1.substr(6,5);
|
||||
cout << "\nstr1 substring (6,5) is ==>" << str2 << "<==" << endl;
|
||||
|
||||
cout << endl << "substring of str1 starting with 'pro'\n" << "and containing 8 characters\n";
|
||||
str2 = str1.substr(str1.find("pro"),8);
|
||||
cout << "str1 substring (str1.find(\"pro\"),8) is ==>" << str2 <<"<==" << endl;
|
||||
|
||||
// substring going past end of string
|
||||
cout << "\nExtracting beyond the end of the string is okay\n";
|
||||
str2 = str1.substr(20,20);
|
||||
cout << "str1 substring(20,20) is ==>" << str2 << "<==" << endl;
|
||||
|
||||
// substring starting at position 1
|
||||
str2 = str1.substr(1,5);
|
||||
cout << "\nstr1 substring(1,5) is ==>" << str2 << "<==" << endl << endl;
|
||||
|
||||
cout.flush(); // make sure the above is printed
|
||||
|
||||
//cout << "Substring with a negative starting position: " << endl;
|
||||
// this results in a runtime error with out of range substring error
|
||||
//str2 = str1.substr(-40,3);
|
||||
//cout << "str2:" << str2 << "<==\n";
|
||||
|
||||
// here length is -3, -3 will be converted to a large
|
||||
// positive number due to the way unsigned integers are
|
||||
// stored in the computer. therefore , the rest of the
|
||||
// string from the starting position will be pulled out
|
||||
// as the substring
|
||||
str2 = str1.substr(4,-3);
|
||||
cout << "Substring starting at position 4 with length -3\n";
|
||||
cout << "str2:" << str2 << "<==\n\n";
|
||||
|
||||
cout << "-3 assinged to unsigned integer data types becomes\n";
|
||||
cout << "a large positive number\n";
|
||||
cout << "-3 assigned to unsigned int: " << unsigned(-3) << endl;
|
||||
cout << "-3 assigned to unsigned long: " << (unsigned long)(-3) << endl << endl;
|
||||
|
||||
cout << "\nThe following causes an abort due to starting position\n";
|
||||
cout << "Attempting str1 substring (40,3):\n";
|
||||
str2 = str1.substr(40,3);
|
||||
cout << str2;
|
||||
// starting beyond the end of the string
|
||||
|
||||
return 0;
|
||||
}
|
11
CPE221/Final Exam/in1.txt
Normal file
11
CPE221/Final Exam/in1.txt
Normal file
@ -0,0 +1,11 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5 6 7 8
|
||||
-9
|
||||
-8
|
||||
0
|
||||
0
|
||||
-7
|
||||
|
12
CPE221/Final Exam/in2.txt
Normal file
12
CPE221/Final Exam/in2.txt
Normal file
@ -0,0 +1,12 @@
|
||||
0 1 -1 -5
|
||||
0
|
||||
-9
|
||||
7
|
||||
3
|
||||
0 7 123 -54 0
|
||||
0
|
||||
776
|
||||
-3432
|
||||
8 6 0 0
|
||||
45
|
||||
|
90
CPE221/Loops1_05.cpp
Normal file
90
CPE221/Loops1_05.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// Chpater 06 program 5: Ch_06_05.cpp
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
#include<iomanip>
|
||||
using namespace std;
|
||||
|
||||
// Event controlled loops
|
||||
// 1) Sentinel loops - Loop is executed until a special value
|
||||
// is used (typically read) indicating the termination of the loop
|
||||
// 2) End-of-File controlled loops - Loop is executed until the
|
||||
// End-of-File(EOF) is reached.
|
||||
// 3) Flag-controlled loops - The loop is executed until a
|
||||
// Boolean variable is changed (from true to false or false
|
||||
// to true) such that the loop is exited.
|
||||
|
||||
int main()
|
||||
{
|
||||
bool finished; // flag used for loop control
|
||||
int value; // number read from the input file
|
||||
int sum; // sum of the numbers read from the file
|
||||
int line_num; // number of lines read from the file
|
||||
ifstream InData; // input file stream variable
|
||||
|
||||
// Flag Controlled Loop - uses a flag which is a boolean variable.
|
||||
// The flag records whether or not the event that controls
|
||||
// the loop has occurred.
|
||||
|
||||
// Flags can be initialized to true or false. The value
|
||||
// used is usuall decided by the variable name so that
|
||||
// reading the statement indicates the desired action.
|
||||
|
||||
// Examples while (!negative), while (DataOk)
|
||||
|
||||
// In this example, integers are read from a file until one
|
||||
// of the following occurs: Either the end of the file is
|
||||
// reached, or the sum of the integers exceeds 300.
|
||||
|
||||
cout << "\n\nFlag controlled loop example. In this case, the\n";
|
||||
cout << "flag is changed when the sum of integers entered\n";
|
||||
cout << "exceeds 300. The loop terminates when the EOF is\n";
|
||||
cout << "reached or the sum exceeds 300\n\n";
|
||||
|
||||
cout << "File: data_file1.txt has integers that sum to > 300.\n\n";
|
||||
|
||||
line_num = 1;
|
||||
sum = 0;
|
||||
finished = false; // initialize this to false since we are not done
|
||||
|
||||
cout << boolalpha;
|
||||
|
||||
// open the input file stream
|
||||
InData.open("data_file1.txt");
|
||||
|
||||
cout << left << setw(17) << "Line and Value" <<
|
||||
setw(20) << " sum" << right << setw(10)<< "finished" << setw(10)
|
||||
<< "InData" << endl;
|
||||
cout << left << setw(17) << "--------------" <<
|
||||
setw(20) << "-------------" << right << setw(10)<< "--------" << setw(10)
|
||||
<< "------" << endl;
|
||||
|
||||
InData >> value; // Priming read to read the first value
|
||||
|
||||
// Loop executes As long as the input stream is valid and the sum is less than 300
|
||||
|
||||
while (InData && !finished )
|
||||
{
|
||||
sum += value;
|
||||
cout << "Line " << line_num << ": " << left << setw(9) << value;
|
||||
cout << ":: sum is " << setw(10) << sum << right << setw(10)<< finished << setw(10)
|
||||
<< bool(InData) << endl;
|
||||
|
||||
line_num++;
|
||||
// test the sum to see if we have reached 301 or higher
|
||||
// could use finished = sum > 300 this sets the value false
|
||||
// to finished until sum exceeds 300
|
||||
if (sum > 300 )
|
||||
finished = true; //finished = sum > 300;
|
||||
else
|
||||
InData >> value; // Read the next value
|
||||
}
|
||||
|
||||
// print out the status of the input stream and finished
|
||||
|
||||
cout << boolalpha; // prints out true or false for boolean variables.
|
||||
cout << "finished is : " << finished << endl;
|
||||
cout << "The status of the input stream is: " << bool(InData) << endl;
|
||||
|
||||
InData.close(); // close the file used for the input stream
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user