Rabu, 01 April 2026

C++ From Year 1995 File From "N"

 // File name: NAMENAME.CPP

// Demonstration of implicit use of constructors

// by Visual C++


#include <iostream.h>

#include <string.h>


//

// Name - a trivial class

//

class Name

  {

    public:

      // Constructors

      Name();                     // Default

      Name(const Name& n);        // Copy

      Name(const char * newName); // Normal            

      // Destructor

      ~Name();

      // Access function

      const char * GetName() const;

    private:            

      // Data member

      char * name;

  };


// Default constructor - ensure name is initialized

Name::Name()

  {

    name = 0;

    cout << "Default constructor used" << endl;

  }


// Copy constructor - ensure string is duplicated  

Name::Name(const Name& n)

  {

    if (n.name)

      {

         name = new char[strlen(n.name) + 1];

         strcpy(name,n.name);

      }

    else

      name = 0;

    cout << "Copy constructor used - "

         << (name == 0?name : "") << endl;

  }

  

// Make a name for myself

Name::Name(const char * newName)

  {

    if (newName)

      {

         name = new char[strlen(newName) + 1];

         strcpy(name,newName);

      }

    else

      name = 0;

    cout << "const char* constructor used - "

         << (name != 0?name : "") << endl;

  }          


// Destructor

Name::~Name()

  {

    cout << "Destructor - " << (name != 0?name : "") << endl;

    delete name;

  }


// Provide access

const char * Name::GetName() const

  {

    return name;

  }


// Global function with pass by value

void PrintName(Name n)

  {

    cout << "In PrintName  - " << n.GetName() << endl;

  }  


// main() function to excercise the class

void main()

  {

    cout << "-- Start of main() --" << endl;

    Name n("Norman Lamont");

    

    cout << "-- Before PrintName --" << endl;

    PrintName(n);

    

    cout << "-- Before n1 declaration --" << endl;

    Name n1(n);

    

    cout << "-- Before n2 declaration --" << endl;

    Name n2;


    cout << "-- Before n2 = n1 --" << endl;

    n2 = n1; // Unsafe!!!


    cout << "-- End of main() --" << endl;

  }


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

// Filename: INITIALS.CPP

// Shows a routine that when given a name finds

// the initials

//


#include <string.h>  // header for strlen function

#include <iostream.h>


// GetInitials prototype: returns 1 if successful,

//                                0 if fails

int GetInitials(char initials[],const char name[]);


void main()

  {

     char initials[15] = ""; // Allow for silly strings 

     char name[30] = "";

     while (1)               // Do forever

       {

          cout << "Type name, (0 to quit): ";

          cin.getline(name,30); 

          if (name[0] == '0')// Stop when the user asks

            break;

              

          //

          // Extract the initials, and report if error

          //

          if (GetInitials(initials,name))

            {

               cout << "The initials for '" << name 

                    << "' are: '" << initials << "'" << endl;

            }

          else

            {

               cout << "Something wrong with '" 

               << name << "'" << endl;

            }  

       }

  }

//***********************************************************  

int GetInitials(char initials[],const char name[])

  {

    int count = 0;        

    int initialCount = 0;

    

    int length = strlen(name);

    if (length == 0) // error if no string

      return 0;                               

     

    while (count < length)

      {

        while (count < length

               && name[count] == ' ')

          count++;

        if (count < length)

          {

            initials[initialCount] = name[count];

            initialCount++;

            count++;

          }

        while (count < length

               && name[count] != ' ')

          count++;

      }

    initials[initialCount] = '\0';// Ensure terminated string

    return (initialCount > 0);    // Success if found

                                  // one or more initials

  } 

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

// Filename: NESTFOR.CPP

// Two nested loops

#include <iostream.h>

void main()

{

  int inner, outer;


  // One loop nested in another

  cout << "Showing the loop control variables:" << endl;

  cout << "Outer   Inner" << endl;

  for (outer = 0; outer < 2; outer++)

    { 

      for (inner = 0; inner <= 3; inner++)

        { 

          // The '\t' outputs a tab character

          cout << outer << '\t' << inner << endl; 

        }

    }


  cout << endl;   // Blank line between loop outputs


  cout << "Here is a loop from 1 to 10, printed three times:" << endl;

  for (outer = 0; outer < 3; outer++)

    { 

      for (inner = 1; inner <= 10; inner++)

        { 

          cout << inner << ' '; 

        }

      cout << endl;   // Executes once each outer iteration

    }


  return;

}

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

// Filename: NEWLINE.CPP

// Program that uses endl

#include <iostream.h>


void main()

{

  cout << "Line #1";

  cout << "Line #2";


  cout << endl << endl;

  cout << "Line #3" << endl;

  cout << "Line #4";

  return;

}

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

#include <iostream.h>

void main()

{

  int out, in;

  for (out = 0; out < 10; out++)

   {  

     for (in = 0; in < 5; in++)

      {

        if ((in * out) % 2)

          {  

            cout << in << '\t' << out << endl;

            break;

          }

        cout << '\x07';

      }

    }

}

=============================
// Filename: NOBREAKS.CPP
// Not using break statements inside a switch lets
// you cascade through the subsequent case blocks
#include <iostream.h>

void main()
{
  char request;

  cout << "Sales Reporting System"  << endl << endl;

  cout << "Do you want the Country, Region, State, or Local report";
  cout << " (C, R, S, or L)? ";
  cin >> request;
  cout << endl;

  //
  // Convert to uppercase - letters are contiguous
  //
  if (request >= 'a' || request <= 'z')
    request += ('A' - 'a');
  
  switch (request)
    { 
      case ('C') : 
        { 
          cout << "Country's sales: $343,454.32" << endl; 
        }
      case ('R') : 
        { 
          cout << "Region's sales: $64,682.01" << endl; 
        }
      case ('S') : 
        { 
          cout << "State's sales: $12,309.82" << endl; 
        }
      case ('L') : 
        { 
          cout << "Local's sales: $3,654.58" << endl;
          break; 
        }
      default    : 
        { 
          cout << "You did not enter C, R, S, or L"  << endl;
          break; 
        }
    }
  return;
}
==============================
// Filename: NUMSORT.CPP
// Initializes and sorts an array of 20 values.
// First, an ascending sort is done. Then the
// program takes those sorted values and
// performs a descending sort.
const int NUM = 20;

#include <iostream.h>

// Program prototypes follow
void prAra(int ara[NUM]);
void ascSort(int ara[NUM]);
void desSort(int ara[NUM]);
void main()
{
  int ara[NUM] = {53, 23, 12, 56, 88,  7, 32, 41, 59, 91,
                  88, 62,  4, 74, 32, 33, 42, 26, 80,  3};
  cout << "Before the sort, here are the values: " << endl;
  prAra(ara);

  // Perform an ascending sort
  ascSort(ara);
  cout << endl << endl << "After the ascending sort:" << endl;
  prAra(ara);

  // Perform a descending sort
  desSort(ara);
  cout << endl << endl << "After the descending sort:" << endl;
  prAra(ara);

  return;
}
//**********************************************************
void prAra(int ara[NUM])
{
  int ctr;   // for-loop control variable
  for (ctr = 0; ctr < NUM; ctr++)
    { if ((ctr % 10) == 0)   // Print a newline after
        { cout << endl; }    // every 10 values
      cout.width(4);
      cout << ara[ctr]; // Print values in four spaces
    }
  return;
}
//**********************************************************
void ascSort(int ara[NUM])
{
  int inner, outer;   // for-loop control variables
  int temp;           // Temporary value for swapping
  int didSwap;        // 1 if a swap took place in each pass
  for (outer = 0; outer < (NUM - 1); outer++)
    {
      didSwap = 0;   // Initialize after each pass
      // Next loop steps through each pair of values
      for (inner = outer; inner < NUM; inner++)
        {
          if (ara[outer] > ara[inner])   // First of two is higher
            { temp = ara[outer];   // Swap
              ara[outer] = ara[inner];
              ara[inner] = temp;
              didSwap = 1;   // Indicate that a swap occurred
            }
        }
      if (!didSwap)   // If no swap happened,
        { break; }    // terminate the sort
    }
  return;
}
//**********************************************************
void desSort(int ara[NUM])
{
  // Only one change is needed for a descending sort
  int inner, outer;   // for-loop control variables
  int temp;           // Temporary value for swapping
  int didSwap;        // 1 if a swap took place in each pass
  for (outer = 0; outer < (NUM - 1); outer++)
    {
      didSwap = 0;   // Initialize after each pass
      // Next loop steps through each pair of values
      for (inner = outer; inner < NUM; inner++)
        {
          if (ara[outer] < ara[inner])   // First of two is higher
            { temp = ara[outer];   // Swap
              ara[outer] = ara[inner];
              ara[inner] = temp;
              didSwap = 1;   // Indicate that a swap occurred
            }
        }
      if (!didSwap)   // If no swap happened,
        { break; }    // terminate the sort
    }
  return;
}
================================
// Filename: NUMSORT2.CPP
// Initializes and sorts an array of 20 values.
// First, an asending sort is done, and then
// the program takes those sorted values and
// performs a descending sort.

const int NUM = 20;

#include <iostream.h>

// Program prototypes follow
void prAra(int ara[NUM]);
void Sort(int direction, int ara[NUM]);
int Test(int direction, int val1, int val2);

void main()
{
  int ara[NUM] = {53, 23, 12, 56, 88,  7, 32, 41, 59, 91,
                  88, 62,  4, 74, 32, 33, 42, 26, 80,  3};
  cout << "Before the sort, here are the values: " << endl;
  prAra(ara);

  // Perform an asending sort
  Sort(1, ara);  // 1 means ascending
  cout << endl << endl << "After the ascending sort:" << endl;
  prAra(ara);

  // Perform a descending sort
  Sort(0, ara);  // 0 means descending
  cout << endl << endl << "After the descending sort:" << endl;
  prAra(ara);
  return;
}
//**********************************************************
void prAra(int ara[NUM])
{
  int ctr;   // for-loop control variable
  for (ctr=0; ctr<NUM; ctr++)
    { if ((ctr % 10) == 0)  // Print a newline after
        { cout << endl; }   // every ten values
      cout.width(4);
      cout << ara[ctr];  // Prints values in 4 spaces
    }
  return;
}
//**********************************************************
void Sort(int direction, int ara[NUM])
{
  int inner, outer;    // for-loop control variables
  int temp;            // Temporary value for swapping
  int didSwap;         // 1 if a swap took place in each pass
  for (outer = 0; outer < (NUM-1); outer++)
    {
      didSwap = 0;   // Initialize after each pass
      // Next loop steps through each pair of values
      for (inner = outer; inner < NUM; inner++)
        {
          if (Test(direction, ara[outer], ara[inner]))
            { temp = ara[outer];   // Swap
              ara[outer] = ara[inner];
              ara[inner] = temp;
              didSwap = 1;  // Indicate that a swap occurred
            }
        }
      if (!didSwap)  // If no swap happened...
        { break; }   // Terminate the sort
    }
  return;
}
//**********************************************************
int Test(int direction, int val1, int val2)
{
  int trueFalse;  // Holds result of swap
  // direction is 1 for asending sort and 0 for descending
  if (direction)  // Asending
    { trueFalse = (val1 > val2); }
  else            // Descending
    { trueFalse = (val1 < val2); }
  return trueFalse;
}
===============================
// Filename: NUMSWITC.CPP
// Print the name of the number entered by the user
#include <iostream.h>
void main()
{
  int num;

  do
  { cout << "Enter a number from 1 to 5: ";
    cin >> num;
  } while ((num < 1) || (num > 5));

  cout << "You entered a ";  // The switch will finish this

  switch (num)
  { case (1) : { cout << "one." << endl;
                 break; }
    case (2) : { cout << "two." << endl;
                 break; }
    case (3) : { cout << "three." << endl;
                 break; }
    case (4) : { cout << "four." << endl;
                 break; }
    case (5) : { cout << "five." << endl;
                 break; }
  }
  return;
}

Tidak ada komentar: