Day 4 ANSI C++

This commit is contained in:
ganome 2024-08-25 20:00:46 -06:00
parent 1ae6715e5a
commit 4c7c1cfb2a
Signed by untrusted user who does not match committer: Ganome
GPG Key ID: 944DE53336D81B83
23 changed files with 464 additions and 0 deletions

11
.gitignore vendored
View File

@ -44,3 +44,14 @@ cpp/ANSI/W1/Day3/3.7/printchars2
cpp/ANSI/W1/Day3/3.8/enumeratedconstants
cpp/ANSI/W1/Day3/3.9/enumeratedconstants2
cpp/ANSI/W1/Day3/Ex.SpecialChars/specialchars
cpp/ANSI/W1/Day4/4.1/complexexpressions
cpp/ANSI/W1/Day4/4.2/integerOverflow
cpp/ANSI/W1/Day4/4.3/prefixPostfixOperators
cpp/ANSI/W1/Day4/4.4/nestedif
cpp/ANSI/W1/Day4/4.5/elseif
cpp/ANSI/W1/Day4/4.6/complexnestedif
cpp/ANSI/W1/Day4/4.7/brokenbracesif
cpp/ANSI/W1/Day4/4.8/fixedbracesif
cpp/ANSI/W1/Day4/Quiz/quiz
cpp/ANSI/W1/Day4/Ex.LogicalOperators/logicaloperators
cpp/ANSI/W1/Day4/4.9/conditionaloperator

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o complexexpressions main.cpp $(LDFLAGS)
.PHONY: test clean
test: complexexpressions
./complexexpressions
clean:
rm -f complexexpressions

View File

@ -0,0 +1,17 @@
// Demonstrating how to use Complex Expressions
#include <iostream>
int main()
{
using namespace std;
int a=0, b=0, x=0, y=35;
cout << "a: " << a << " b: " << b;
cout << " x: " << x << " y: " << y << endl;
a = 9;
b = 7;
y = x = a+b;
cout << "a: " << a << " b: " << b;
cout << " x: " << x << " y: " << y << endl;
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o integerOverflow main.cpp $(LDFLAGS)
.PHONY: test clean
test: integerOverflow
./integerOverflow
clean:
rm -f integerOverflow

View File

@ -0,0 +1,19 @@
// Demonstration of subtraction and Interger Overflow
#include <iostream>
using namespace std;
int main()
{
typedef unsigned int UINT;
UINT difference;
UINT bigNumber = 100;
UINT smallNumber = 50;
difference = bigNumber - smallNumber;
cout << "Difference is: " << difference;
difference = smallNumber - bigNumber;
cout << "\nNow the difference is: " << difference << endl;
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o prefixPostfixOperators main.cpp $(LDFLAGS)
.PHONY: test clean
test: prefixPostfixOperators
./prefixPostfixOperators
clean:
rm -f prefixPostfixOperators

View File

@ -0,0 +1,34 @@
/* Demonstrate use of prefix and postix increment
and decrement operators */
#include <iostream>
int main()
{
using namespace std;
int myAge = 39; // Initialize two Integers
int yourAge = 75;
cout << "I am: " << myAge << " years old.\n";
cout << "You are: " << yourAge << " years old\n";
myAge++; // Postfix increment
++yourAge; // prefix increment
cout << "One year passes...\n";
cout << "I am: " << myAge << " years old.\n";
cout << "You are: " << yourAge << " years old\n";
myAge++; // Postfix increment
++yourAge; // prefix increment
cout << "Another year passes...\n";
cout << "I am: " << myAge << " years old.\n";
cout << "You are: " << yourAge << " years old\n";
cout << "Let's print it again.\n";
cout << "I am: " << myAge << " years old.\n";
cout << "You are: " << yourAge << " years old\n";
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o nestedif main.cpp $(LDFLAGS)
.PHONY: test clean
test: nestedif
./nestedif
clean:
rm -f nestedif

View File

@ -0,0 +1,41 @@
/* Demonstrate use of if statement
used with relational operators */
#include <iostream>
int main()
{
using namespace std;
int metsScore, yankeesScore;
cout << "Enter the score for the Mets: ";
cin >> metsScore;
cout<< "\nEnter the score of the Yankees: ";
cin >> yankeesScore;
cout << endl;
if (metsScore > yankeesScore)
cout << "Let's Go Mets!\n";
if (metsScore < yankeesScore)
{
cout << "Go Yankees!\n";
}
if (metsScore == yankeesScore)
{
cout << "A tie? Naah, Can't be.\n";
cout << "Give me the real score for the Yanks: ";
cin >> yankeesScore;
if (metsScore > yankeesScore)
cout << "Knew it! Let's go Mets!\n";
if (metsScore > yankeesScore)
cout << "Wow, go Yanks!\n";
if (metsScore == yankeesScore)
cout << "Wow, it really was a tie!\n";
}
cout << "\n Thanks for telling me.\n";
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o elseif main.cpp $(LDFLAGS)
.PHONY: test clean
test: elseif
./elseif
clean:
rm -f elseif

View File

@ -0,0 +1,21 @@
/* Demonstrates the use of the else clause
while using the if condition */
#include <iostream>
int main()
{
using namespace std;
int firstNumber, secondNumber;
cout << "Please enter a big number: ";
cin >> firstNumber;
cout << "\nPlease enter a smaller number: ";
cin >> secondNumber;
if (firstNumber > secondNumber)
cout << "\nThanks!\n";
else
cout << "\nWow, you should go back to first grade.\n";
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o complexnestedif main.cpp $(LDFLAGS)
.PHONY: test clean
test: complexnestedif
./complexnestedif
clean:
rm -f complexnestedif

View File

@ -0,0 +1,32 @@
// Demonstrates advanced If statements
#include <iostream>
int main()
{
/*Ask for two numbers
Assign the numbers to bigNumber and littleNumber
if bigNumber is bigger than littleNumber,
see if they are evenly divisle
If they are, see if they are the same number */
using namespace std;
int firstNumber, secondNumber;
cout << "Enter two number.\nFirst: ";
cin >> firstNumber;
cout << "\nSecond: ";
cin >> secondNumber;
if ( firstNumber >= secondNumber)
{
if ( (firstNumber % secondNumber) == 0) //evenly divisble - no remainder
{
if (firstNumber == secondNumber)
cout << "They are the same!\n";
}
else
cout << "They are not evenly divisble!\n";
return 0;
}
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o brokenbracesif main.cpp $(LDFLAGS)
.PHONY: test clean
test: brokenbracesif
./brokenbracesif
clean:
rm -f brokenbracesif

View File

@ -0,0 +1,22 @@
/* Demonstrates why braces are important inside
nested if statements. This program will exit early.
Not at the intended else clause, but in the nested if.*/
#include <iostream>
int main()
{
using namespace std;
int x;
cout << "Enter a number less than 10 or greater than 100: ";
cin >> x;
cout << "\n";
if (x >= 10)
if (x > 100)
cout << "More than 100, Thanks!\n";
else
cout << "Less than 10, Thanks\n";
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o fixedbracesif main.cpp $(LDFLAGS)
.PHONY: test clean
test: fixedbracesif
./fixedbracesif
clean:
rm -f fixedbracesif

View File

@ -0,0 +1,23 @@
/* Demonstrates the proper way to use braces
to seperate if statements in advanced nested
if statements*/
#include <iostream>
int main ()
{
using namespace std;
int x;
cout << "Enter a number less than 10 or greater than 100: ";
cin >> x;
cout << endl;
if (x >= 100)
{
if (x > 100)
cout << "More than 100, Thanks!\n";
}
else
cout << "Less than 10, Thanks!\n";
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o conditionaloperator main.cpp $(LDFLAGS)
.PHONY: test clean
test: conditionaloperator
./conditionaloperator
clean:
rm -f conditionaloperator

View File

@ -0,0 +1,29 @@
// Demonstrates the conditional operator "?"
#include <iostream>
int main()
{
using namespace std;
int x, y, z;
cout << "Enter two numbers.\n";
cout << "First number: ";
cin >> x;
cout << "\nSecond number: ";
cin >> y;
cout << endl;
if (x > y)
z = x;
else
z = y;
cout << "After it test, z: " << z << endl;
/* This line is read as “If expression1 (x>y) is true, return the value of expression2; otherwise,
return the value of expression3. Assigned to variable "z" */
z = (x > y) ? x : y;
cout << "After conditional test, z: " << z << endl;
return 0;
}

View File

@ -0,0 +1,13 @@
CFLAGS = -std=c++03 -O2
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
1.1: main.cpp
c++ $(CFLAGS) -o logicaloperators main.cpp $(LDFLAGS)
.PHONY: test clean
test: logicaloperators
./logicaloperators
clean:
rm -f logicaloperators

View File

@ -0,0 +1,15 @@
/* A list of the logical operators in C++ */
#include <iostream>
int main()
{
using namespace std;
cout << "The && operator means AND . ie if ( (x == 5) && (y == 5) )\n\n";
cout << "The || operator means OR . ie if ( (x == 5) || (y == 5) )\n\n";
cout << "The ! operator mean NOT equal to. ie if ( var1 != 420)\n\n";
// Appendix C has order of operations or relational precedence on conditional statements
return 0;
}

View File

@ -0,0 +1,12 @@
#include <iostream>
int main()
{
using namespace std;
int x = 8+2*3;
cout << x << endl;
return 0;
}

View File

@ -0,0 +1,58 @@
Quiz
1. What is an expression?
2. Is x = 5 + 7 an expression? What is its value?
3. What is the value of 201 / 4?
Depends on the datatype. If its an INT it would be 50. float would have the decimal. etc...
4. What is the value of 201 % 4?
0 because there would be a remainder so its not perfectly divisble
5. If myAge, a, and b are all int variables, what are their values after
myAge = 39;
a = myAge++;
b = ++myAge;
6. What is the value of 8+2*3?
14 - follows pemdas?
7. What is the difference between if(x = 3) and if(x == 3)?
Around page 80
8. Do the following values evaluate true or false?
a. 0
b. 1
c. 1
d. x = 0
e. x == 0 // assume that x has the value of 0
------------------------------------------------
Exercises
1. Write a single if statement that examines two integer variables and changes the
larger to the smaller, using only one else clause.
2. Examine the following program. Imagine entering three numbers, and write what
output you expect.
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << “Please enter three numbers\n”;
cout << “a: “;
cin >> a;
cout << “\nb: “;
cin >> b;
cout << “\nc: “;
cin >> c;
if (c = (a-b))
cout << “a: “ << a << “ minus b: “ << b << ” equals c: “ << c;
else
cout << “a-b does not equal c: “;
return 0;
}