Rabu, 01 April 2026

C++ From Year 1995 Bonus

 // Filename: BPROJ1.CPP

// This program displays the character for each ASCII value


#include <iostream.h>


void main()

  {

    // Output a heading

    cout << "Value   Character" << endl;

    

    // For all the characters (missing out the first

    // few unprintable characters.

    for (int i = 32; i <= 255; i++)

      {

         // Make the value print in 3 characters

         cout.width(3);

         // Print the value, followed by some spaces

         // followed by the character value and

         // finally a new line

         cout << i << "     " << (char)i << endl;

      }

  }

=====================

// Filename: BPROJ2.CPP

// This program demonstrates the basic structure

// of a C++ program 

#include <iostream.h>


void main()

  {

    // Declare and initialise variables

    int value1 = 10;

    int value2 = 20;

    int value3 = 40;                   

    // Declare a variable for working out the

    // sum of the numbers

    int sum;             

    // Declare another variable for the result

    int average;                              

    // Add three numbers into sum

    sum = value1 + value2 + value3;

    // divide the sum by the number of values to

    // get the average

    average = sum / 3;

    // Output the result

    cout << "The average of " << value1 << ", "

         << value2 << " and " << value3 << " is "

         << average << endl;

    cout << "The sum of the numbers is " << sum;        

  }

=====================

// BPROJ3.CPP 

// A simple review of character and numeric

// input and output.

#include <iostream.h>


void main()

  {

    // Declare variables

    int decimals = 0;

    float number;

    const int descLen = 80;

    char numberDesc[descLen];

           

    cout << "Enter a floating point number: ";

    cin >> number;

    cin.ignore(80,'\n');

    

    cout << "How many decimal places do you want? ";

    cin >> decimals;

    cin.ignore(80,'\n');

    

    cout << "How do you want to describe the number?" << endl;

    cin.getline(numberDesc,descLen);

    

    cout << endl;

    cout << numberDesc << ": ";

    

    // Format the number as requested

    cout.precision(decimals);

    cout.setf(ios::fixed);

    cout.setf(ios::showpoint);

    

    cout << number;

  }

=====================

// Filename : BPROJ4.CPP

// This project does some calculations

// and then tests them.


#include <iostream.h>


void main()

  {

    char burgerType = ' ';        

    char cheese = ' ';

    char fries = ' ';

    float burgerCost = 0.0F;

    float friesCost = 0.0F;

    float totalCost = 0.0F;

    

    cout << "Welcome to Spencer's Burger Bar!" << endl;

    

    cout << endl << "What sort of burger would you like?" << endl;

    cout << "[s]mall, [q]uarter pounder, [b]loatburger? ";

    cin >> burgerType;

    if (burgerType >= 'a' && burgerType <= 'z')

      burgerType += 'A' - 'a';

    if (burgerType != 'S' &&   burgerType != 'Q'

        && burgerType != 'B')

      burgerType = 'Q';

      

    cout << "Is that with cheese?";    

    cin >> cheese;

    if (cheese == 'Y' || cheese == 'y')

      cheese = 'Y';

    else

      cheese = 'N';

    

    cout << "[r]egular or [l]arge fries?";

    cin >> fries;

    if (fries == 'r' || fries == 'R' ||

        fries == 'l' || fries == 'L')

      {

        if (fries == 'r' || fries == 'l')

          fries += 'A' - 'a';

      }

    else

      fries = 'R';

     

    // Calculate cost - everything is relative

    

    burgerCost = 1.75F;

    if (burgerType == 'S')

      burgerCost *= 0.60F;

    if (burgerType == 'B')

      burgerCost *= 1.7F;                   

    if (cheese == 'Y')

      burgerCost += 0.50F;

      

    if (fries == 'R')

      friesCost = 1.00F;

    else

      friesCost = 1.50F;

      

    totalCost = burgerCost + friesCost;

    

    if (fries == 'L' && burgerType == 'B')

      {

        cout << "There is a special on the Bloat & large fries!" << endl;

        totalCost = totalCost * 0.9F;

      }

      

    cout << "That will be $";

    cout.setf(ios::fixed);

    cout.setf(ios::showpoint);

    cout.precision(2);

    cout << totalCost << "." << endl;

    cout << "Have a nice day!";

  }    

     

      

      

        

  =======================

// BPROJ5.CPP

// Switching and calculations explored

#include <iostream.h>


void main()

  {

    int method = 1;

    float f = 1.99F;

    int i = 10;

    float answer = 0;

    // This program simply demonstrates some rounding errors

    cout << "1. i * f" << endl;

    cout << "2. (int)(i * f)" << endl;

    cout << "3. i * (int)f" << endl;

    cout << "4. (int)(i * f) * f" << endl;

    cout << "5. (int)(i *f) * (int)f" << endl;

    cout << "Enter method: ";

    cin >> method;

    switch (method)

      {

        case 1:

          answer = i * f;

          break;

        case 2:

          answer = (int)(i * f);

          break;

        case 3: 

          answer = i * (int)f;

          break;              

        case 4:

          answer = (int)(i * f) * f;

          break;

        case 5:

          answer = (int)(i * f) * (int)f;

          break;

        default:

          cout << "Invalid input";

          return;

      } 

      

    cout << "The answer is " << answer << endl;

  }

============================

// Filename: BPROJ6.CPP

// Factors - a factor is a number that

// multiplied by another number exactly makes another number


#include <iostream.h>


void main()

  {

    int product = 1;

    while (product)

      {

        cout << endl << "Enter number to test:";

        cin >> product;

        if (product)

          {

             cout << "The factors of " << product << " are:" << endl;

             for (int i = 2; i < product ; i++)

               {

                 if (product % i)

                   continue;

                 cout << i << endl;

               }

          }

      }

  }

        

==============================
// Filename: BPROJ7.CPP
// Exporing function calls at the burger bar
#include <iostream.h>

inline char UpperCase(char c)
  {
    if (c >= 'a' && c <= 'z')
      return c + 'A' - 'a';
    else
      return c;
  }  
      
float PriceBurger(char burgerType,int withCheese)
  {        
    float burgerCost = 1.75F;
    if (burgerType == 'S')
      burgerCost *= 0.60F;
    if (burgerType == 'B')
      burgerCost *= 1.7F;                   
    if (withCheese)
      burgerCost += 0.50F;
    return burgerCost;  
  }  
  
float PriceFries(char fries)
  {
    if (fries == 'R')
      return 1.00F;
    else
      return 1.50F;
  }  
  
void GetBurgerType(char& burgerType, char& cheese)
  {    
    do
      {
         cout << endl << "What sort of burger would you like?" << endl;
         cout << "[s]mall, [q]uarter pounder, [b]loatburger? ";
         cin >> burgerType;
         burgerType = UpperCase(burgerType);
      }
    while (burgerType != 'S' &&   burgerType != 'Q'
           && burgerType != 'B');
      
    do
      {
         cout << "Is that with cheese?";    
         cin >> cheese;             
         cheese = UpperCase(cheese);
       }
    while (cheese != 'Y' && cheese != 'N');
  }

char GetFries()
  {    
    char fries;
    do
      {
         cout << "[r]egular or [l]arge fries?";
         cin >> fries;
         fries = UpperCase(fries);
      }
    while (fries != 'R' && fries != 'L');
    return fries;
  }
    
void Special(char fries, char burgerType, float& totalCost)
  {
    if (fries == 'L' && burgerType == 'B')
      {
        cout << "There is a special on the Bloat & large fries!" << endl;
        totalCost = totalCost * 0.9F;
      }
  }      

void main()
  {
    char burgerType;
    char cheese;
    
    cout << "Welcome to Spencer's Burger Bar!" << endl;
    GetBurgerType(burgerType,cheese);        
    char fries = GetFries();
    float totalCost = PriceBurger(burgerType, (cheese == 'Y')) 
                    + PriceFries(fries);
    Special(fries,burgerType,totalCost);
    cout << "That will be $";
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << totalCost << "." << endl;
    cout << "Have a nice day!";
  }    
  =============================
// File name: BPROJ8.CPP
// Something interesting with arrays and pointers.

#include <iostream.h>

// A routine to return a value 0 for non-alpha
// and 1 to 26 for a to z in either upper or lowercase
inline int Letters(char p)
  {
    if (p >= 'a' && p <= 'z')
      return p - 'a' + 1;
    if (p >= 'A' && p <= 'Z')
      return p - 'A' + 1;
    else
      return 0;
  }
        

void main()
  {
    const int maxLen = 80;
    char input[maxLen];
    int letters[27] = {0};
    
    char * inpPtr;
    cout << "Give me a string please" << endl;
    cin.getline(input,maxLen);
    
    inpPtr = input;
    
    while (*inpPtr)  // While not string terminator
      letters[Letters(*inpPtr++)]++;  // !!! Guess what this does !!!  
    
    cout << "Your string contains: " << endl;
    cout << "Non-alpha " << letters[0] << endl;
    for (int i = 1; i < 27; i++)
      if (letters[i])
        cout << (char)('A' + i - 1) << "         "
             << letters[i] << endl;
  }

Tidak ada komentar: