Read More
C++ fundamentals
#include
#include
using namespace std;
// Entry point of the program
int main()
{
// Get the student's name
cout<< "Welcome to the Fall 2020-2021 Semester grading system. What is your name? ";
string name;
cin>> name;
// Get the student's course
cout<< "Hello " << name << "! Please enter the name of the course to be processed: ";
string course;
cin>> course;
// Ask if it's first time
cout<< "Hello " << name << "! If you take the " << course << " for the first time, enter YES otherwise NO: ";
string isFirstTime;
cin>>isFirstTime;
// Accept only a YES or NO answe
if (isFirstTime != "YES" &&isFirstTime != "NO")
return 0;
string grade;
if (isFirstTime == "YES")
{
// Ask for a letter grade
cout<< "What is your grade from (A,B,C,D,F)? ";
cin>> grade;
// Stop if the grade is invalid
if (grade != "A" && grade != "B" && grade != "C" && grade != "D" && grade != "F")
{
cout<< "Invalid grade selection. Goodbye..." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
// Ask for grade switch if grade is A to D and want to go S
if (grade == "A" || grade == "B" || grade == "C" || grade == "D")
{
cout<< "Do you want to switch your grade from " << grade << " to S (YES or NO)? ";
string switchGrade;
cin>>switchGrade;
if (switchGrade != "YES" &&switchGrade != "NO")
{
cout<< "Invalid grade selection. Goodbye..." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
if (switchGrade == "YES")
// Switch grade if needed
grade = "S";
}
// Ask for a grade switch for grade is F and want to go U
else if (grade == "F")
{
cout<< "Do you want to switch your grade from " << grade << " to U (YES or NO)? ";
string switchGrade;
cin>>switchGrade;
if (switchGrade != "YES" &&switchGrade != "NO")
{
cout<< "Invalid grade selection. Goodbye..." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
if (switchGrade == "YES")
// Switch grade if needed
grade = "U";
}
}
else if (isFirstTime == "NO")
{
// Ask for previous letter grade
cout<< "Please enter your previous grade from (A,B,C,D,F,S,U): ";
string previousGrade;
cin>>previousGrade;
// Stop if the grade is invalid
if (previousGrade != "A" &&previousGrade != "B" &&previousGrade != "C"
&&previousGrade != "D" &&previousGrade != "F" &&previousGrade != "S"
&&previousGrade != "U")
{
cout<< "Invalid grade selection. Goodbye..." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
// Ask for current grade
cout<< "Please enter your current grade from (A,B,C,D,F): ";
string currentGrade;
cin>>currentGrade;
// Stop if the grade is invalid
if (currentGrade != "A" &¤tGrade != "B" &¤tGrade != "C"
&¤tGrade != "D" &¤tGrade != "F")
{
cout<< "Invalid grade selection. Goodbye..." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
// Choose a new grade
cout<< "Please enter the grade you want to choose from (A,B,C,D,F,S,U): ";
string newGrade;
cin>>newGrade;
// If a student has received "U" the previous semester and got one of (A, B, C, D) in
// the current semester
if (previousGrade == "U" && (currentGrade == "A" || currentGrade == "B"
|| currentGrade == "C" || currentGrade == "D"))
{
// students cannot choose other than "S" or current letter
// grade.
if (newGrade != "S" &&newGrade != currentGrade)
{
cout<< "Invalid grade selection. If you received U before, you may choose S or current letter grade." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
}
// If a student has received an "S" previous semester and, got an "F" current semester
else if (previousGrade == "S" &¤tGrade == "F")
{
// Only F is acceptable
if (newGrade != "F")
{
cout<< "Invalid grade selection. If you received S before and fail this semester, you get F." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
}
// If a student has received an "F" previous semester and, got an "F" current semester
else if (previousGrade == "F" &¤tGrade == "F")
{
// Only F is acceptable
if (newGrade != "F")
{
cout<< "Invalid grade selection. If you received F before, and fail this semester, you get F." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
}
// The student cannot choose a higher or lower grade than the letter grade he / she has received
else if ((currentGrade == "A" || currentGrade == "B" || currentGrade == "C" || currentGrade == "D")
&&newGrade != currentGrade)
{
if (newGrade != "S")
{
cout<< "Invalid grade selection. You cannot choose a higher or lower grade than you deserve." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
}
grade = newGrade;
}
// Report final grade
cout<< name << ", your final grade for " << course << " is " << grade << ". Goodbye..." <<="" p="" style="box-sizing: border-box; margin: 0px; padding: 0px;">
return 0;
}
Floating Point Numbers Decoding
/*! \file prog1.cpp
\brief Assignment 1 program
The program asks for input from the user and displays the read number as a
IEEE-754 floating point number in binary form.
*/
#include
#include
#include
/*! \fn void printBinFloat(int32_t x)
\brief Prints the given number as a IEEE-754 floating point number
\param num Number to be printed.
The function prints the number in hexadecimal, in binary and then prints
the IEEE-754 field values. After that, it prints the decoded floating point
number in binary.
*/
void printBinFloat(int32_t num)
{
// print hexadecimal number
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << num << " = ";
// print binary number
int tmpnum = num;
for (int i = 0; i < 8; i++) // print 8 nibbles
{
for (int i = 0; i < 4; i++) // print 4 bits
{
std::cout << (tmpnum & 0x80000000 ? '1':'0');
tmpnum <<= 1;
}
std::cout << " ";
}
std::cout << std::endl;
int sign = (num & 0x80000000) ? 1 : 0; // get the sign bit (31)
int exp = (num >> 23) & 0xFF; // get the 8 exponent bits from 23 to 30
exp -= 127; // remove bias
uint32_t sig = num & 0x7FFFFF; // get the 23 bits of significand
// print floating point fields
std::cout << "sign: " << sign << std::endl;
std::cout << " exp: 0x" << std::hex << std::setw(8) << std::setfill('0') << exp << " (" << std::dec << exp << ")" << std::endl;
std::cout << " sig: 0x" << std::hex << std::setw(8) << std::setfill('0') << sig << std::endl;
// print floating point number
std::cout << ((sign)? '-' : '+'); // print sign
if (exp == 128 && sig == 0) // infinite
{
std::cout << "inf";
}
else if (exp == -127 && sig == 0) // zero
{
std::cout << "0";
}
else // floating point value
{
// add 1 to significand
sig |= 0x800000;
// print initial zeros if negative exponent
for (int j = exp; j <= -1; j++)
{
std::cout << "0";
if (j == exp)
std::cout << ".";
}
// print 24 bits of significand
for (int i = 0; i < 24; i++)
{
std::cout << (sig & 0x800000 ? '1':'0');
if (i == exp)
std::cout << ".";
sig <<= 1;
}
// print end zeros if positive exponent
for (int j = 23; j <= exp; j++)
{
std::cout << "0";
if (j + 1 == exp)
std::cout << ".";
}
}
std::cout << std::endl;
}
/*! \fn int main()
\brief Main function
The main function for the program prompts user for a number and prints it
using the printBinFloat function. It loops until the user decides to end
the program.
*/
int main()
{
while(true)
{
uint32_t number;
std::cout << "Number? ";
std::cin >> std::hex >> number;
printBinFloat(number);
std::cout << "End program (Y/N)? ";
char quit;
std::cin >> quit;
if (quit == 'Y' || quit == 'y')
break;
std::cout << std::endl;
}
return 0;
}