Rabu, 01 April 2026

C++ From Year 1995 File From "R"

 // File String: RADIO.CPP

// An OOP program to list favorite radio stations

//

#include <iostream.h>

#include <string.h>


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

// String - a class to manage strings

//

class String

  {

    public:

      // Constructors

      String();                       // Default

      String(const String& n);        // Copy

      String(const char * newString); // Normal            

      // Destructor

      ~String();

      // Access functions

      const char * Get() const;         

      void Set(const char * newString);

      int Length() const;

    private:            

      // Data member

      char * string;

  };


// Default constructor - ensure string is initialized

String::String() : string(0)

  {

  }


// Copy constructor - ensure string is duplicated  

String::String(const String& n) : string(0)

  {

    if (n.string)

      {

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

         strcpy(string,n.string);

      }

  }

  

// Make a string for myself

String::String(const char * newString) : string(0)

  {

    if (newString)

      {

         string = new char[strlen(newString) + 1];

         strcpy(string,newString);

      }

  }          


// Destructor

String::~String()

  {

    delete string;

  }


// Provide access

const char * String::Get() const

  {

    return string;

  }

// Replace  

void String::Set(const char * newString)

  {

    delete string;

    if (newString)

      {

         string = new char[strlen(newString) + 1];

         strcpy(string,newString);

      }

    else

      string = 0;

  }

// Get length  

int String::Length() const

  {

    if (!string)

      return 0;

    return strlen(string);

  }          

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

// The main function

//

void main()

  {

    String s("Microsoft");

    cout << "1: s is '" << s.Get() << "'" << endl;

    

    String s1(s);           

    cout << "2: s1 is '" << s1.Get() << "'" << endl;

    s1.Set("Where do you want to go today?(tm)");

    cout << "s1 is '" << s1.Get() << "'" << endl;

    

    cout << "3: s is " << s.Length() << " long" << endl;

    cout << "4: s1 is " << s.Length() << " long" << endl;

    

    String * s2 = new String;

    cout << "5: s2 is " << s.Length() << " long" << endl;

    

    s2->Set("Visual C++");                     

    cout << "6: s2 is " << s.Length() << " long" << endl;

    

    int totalLength = s.Length() + s1.Length() + s2->Length();

    cout << "Total length = " << totalLength << endl;

    

  }

   

================================
 // Filename: RADIOST.CPP
 // Defines a structure for a radio station listener
 // database and defines an array of structure
 // variables for that database

 // Before defining structure variables,
 // you must define the structure's format
 struct radioList {
   char listName[25];   // Listener name
   char dateFirst[9];   // Date first tuned in (i.e., 11/02/93)
   int age;
   char sex;
   int progSegment;     // Favorite daily program segment number
 };

 void main()
 {
   struct radioList listeners[100];   // Define 100 variables
   radioList *owner;           // A pointer to a structure
                                      // variable
// Rest of program would follow
========================
// Filename: RADIOST2.CPP
// Defines a structure for a radio station listener
// database and defines an array of structure
// variables for that database. The user can then
// enter and display data from the structures.

// Before defining structure variables,
// you must define the structure's format
struct radioList {
  char listName[25];   // Listener name
  char dateFirst[9];   // Date first tuned in (i.e., 11/02/93)
  int age;
  char sex;
  int progSegment;     // Favorite daily program segment number
};

#include <iostream.h>

void dispMenu();
int getAns();
void addList(struct radioList listeners[]);
void dispList(struct radioList listeners[]);
void pressKey();

// What? Global variables? These variables really are global to
// the program because there is only one listener array and
// each function that accesses that array (plus any you might
// later add) needs to know how many elements are currently in
// the array.
int numList;   // The number of listeners added so far
// The number of array elements to be defined follows
const int NUM = 100;

void main()
{
  radioList listeners[NUM];   // Defines an array
                              // of structure variables
  int ans;
  do
  { dispMenu();
    ans = getAns();
    switch (ans)
    { case (1) : addList(listeners);
                 break;
      case (2) : dispList(listeners);
                 break;
      case (3) : return;
      default :  cout << "** You must enter 1, 2, or 3" << endl;
                 break;
    }
  } while ((ans >= 1) && (ans <= 3));
}
//***********************************************************
void dispMenu()
{
  cout << endl << endl << "\t\t** Radio Listener Database **" << endl << endl;
  cout << "Here are your choices:" << endl;
  cout << "\t1. Enter listener data" << endl;
  cout << "\t2. Display listener data" << endl;
  cout << "\t3. Quit the program" << endl;
  cout << "What is your choice?";
  return;
}
//***********************************************************
int getAns()
{
  int ans;   // Temporary housing for the cout variable
  // Simply gets an integer
  cin >> ans;
  return (ans);
}
//***********************************************************
void addList(struct radioList listeners[])
{
  if (numList == NUM)
    { cout << endl << "The listener list is full; you cannot add more" << endl;
      pressKey();
      return;   // Return from the function early
    }
  cin.ignore(80,'\n');  // Eliminate any newline
  cout << endl << "What is the name of the listener? ";
  cin.get(listeners[numList].listName, 25);
  cin.ignore(80,'\n');
  cout << "What date did " << listeners[numList].listName
       << " first listen to us ";
  cout << "(e.g., 11/10/92)? ";
  cin.get(listeners[numList].dateFirst, 9);
  cout << "How old is " << listeners[numList].listName
       << "? ";
  cin >> listeners[numList].age;
  cout << "Is " << listeners[numList].listName
       << " male (M) or female (F)? ";
  cin >> listeners[numList].sex;
  cin.ignore(80,'\n');
  cout << "What is " << listeners[numList].listName
       << "'s favorite segment (1, 2, or 3)? ";
  cin >> listeners[numList].progSegment;
  numList++;   // Increment the counter for the next entry
  return;
}
//***********************************************************
void dispList(struct radioList listeners[])
{
  int ctr;
  if (numList == 0)
    { cout << endl << endl <<"There are no listeners in the list!"
           << endl << endl;
      return ; }   // Return early
  for (ctr = 0; ctr < numList; ctr++)
  { cout << endl << "Listener #" << (ctr+1) << ": " << endl;
    cout << "  Name: " << listeners[ctr].listName << endl;
    cout << "  First listen date: " << listeners[ctr].dateFirst;
    cout << "  Age: " << listeners[ctr].age
         << "\tSex: " << listeners[ctr].sex << endl;
    cout << "  Program segment: " << listeners[ctr].progSegment
         << endl;
    if (((ctr%3) == 0) && (ctr != 0))
    { pressKey(); }   // Scroll after 4 lines but not first time
  }
 return;
}
//***********************************************************
void pressKey()
{
  cin.ignore(80,'\n');  // Eliminate any extra characters on input
  cout << "\n\nPress any key to continue...";
  cin.get();
  return;
 }
====================================
// Filename: RADIOST3.CPP
// Defines a structure for a radio station listener
// database and defines an array of structure
// variables for that database. The user can then
// enter and display data from the structures.

// Before defining structure variables,
// you must define the structure's format
struct radioList {
  char listName[25];   // Listener name
  char dateFirst[9];   // Date first tuned in (i.e., 11/02/93)
  int age;
  char sex;
  int progSegment;     // Favorite daily program segment number
};

#include <iostream.h>

void dispMenu();
int getAns();
void addList(struct radioList listeners[]);
void dispList(struct radioList listeners[]);
void pressKey();

// What? Global variables? These variables really are global to
// the program because there is only one listener array and
// each function that accesses that array (plus any you might
// later add) needs to know how many elements are currently in
// the array.
int numList;   // The number of listeners added so far
// The number of array elements to be defined follows
const int NUM = 100;

void main()
{
  radioList listeners[NUM];   // Defines an array
                              // of structure variables
  int ans;
  do
  { dispMenu();
    ans = getAns();
    switch (ans)
    { case (1) : addList(listeners);
                 break;
      case (2) : dispList(listeners);
                 break;
      case (3) : return;
      default :  cout << endl << "** You must enter 1, 2, or 3";
                 break;
    }
  } while ((ans >= 1) && (ans <= 3));
  return;
}
//***********************************************************
void dispMenu()
{
  cout << endl << endl << "\t\t** Radio Listener Database **"
       << endl << endl;
  cout << "Here are your choices:" << endl;
  cout << "\t1. Enter listener data" << endl;
  cout << "\t2. Display listener data" << endl;
  cout << "\t3. Quit the program" << endl;
  cout << "What is your choice? ";
  return;
}
//***********************************************************
int getAns()
{
  int ans;   // Temporary housing for the cout variable
  // Simply gets an answer
  cin >> ans;
  return (ans);
}
//***********************************************************
void addList(struct radioList listeners[])
{
  if (numList == NUM)
    { cout << endl << "The listener list is full; you cannot add more" << endl;
      pressKey();
      return;   // Return from the function early
    }
  cin.ignore(80,'\n');  // Eliminate any newline
  cout << endl << "What is the name of the listener? ";
  cin.get(listeners[numList].listName, 25);
  cin.ignore(80,'\n');
  cout << "What date did " << listeners[numList].listName
       << " first listen to us ";
  cout << "(i.e., 11/10/92)? ";
  cin.get(listeners[numList].dateFirst, 9);
  cout << "How old is " << listeners[numList].listName
       << "? ";
  cin >> listeners[numList].age;
  cout << "Is " << listeners[numList].listName
       << " male (M) or female (F)? ";
  cin >> listeners[numList].sex;
  cin.ignore(80,'\n');
  do
  { cout << "What is " << listeners[numList].listName
         << "'s favorite segment (1, 2, or 3)? ";
    cin >> listeners[numList].progSegment;
  } while ((listeners[numList].progSegment < 1) ||
           (listeners[numList].progSegment > 3));
  numList++;   // Increment the counter for the next entry
  return;
}
//***********************************************************
void dispList(struct radioList listeners[])
{
  int ctr;
  if (numList == 0)
    { cout << endl << endl << "There are no listeners in the list!"
           << endl << endl;
      return ; }   // Return early
  for (ctr = 0; ctr < numList; ctr++)
  { cout << endl << "Listener #" << (ctr+1) << ": " << endl;
    cout << "  Name: " << listeners[ctr].listName << endl;
    cout << "  First listen date: " << listeners[ctr].dateFirst;
    cout << "  Age: " << listeners[ctr].age
         << "\tSex: " << listeners[ctr].sex << endl;
    cout << "  Program segment: " << listeners[ctr].progSegment
         << endl;
    if (((ctr%3) == 0) && (ctr != 0))
    { pressKey(); }   // Scroll after 4 linesbut not first time
  }
 return;
}
//***********************************************************
void pressKey()
{
  cin.ignore(80,'\n');  // Eliminate any extra characters on input
  cout << endl << endl << "Press any key to continue...";
  cin.get();
  return;
}
===================================================
// Filename: RADIUS1.C
// Calculate the area of a circle with radius of 2.4
#include <iostream.h>

void main()
{
  const float PI = 3.14159;
  double area;

  area = PI * (2.4 * 2.4);  // Make sure the square computes first
  cout.precision(2);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout << "The area of the circle is " << area << endl;
  return;
}
================================
// Filename: RADIUS2.CPP
// Calculate the area of a circle with radius of 2.4
#include <iostream.h>

void main()
{
  const float PI = 3.14159;
  float area;
  float radius;

  cout << "What is the radius of the circle? ";
  cin >> radius;
  area = PI * (radius * radius);  // Make sure the square computes first
  cout.precision(2);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout << "The area of the circle is " << area << endl;
  return;
}
===================================
// Filename: RANGSWIT.CPP
// Use a menu to provide switch with a range of choices
#include <iostream.h>
void main()
{
  int menu;
  float bonus;

  cout << "Here are your choices:" << endl << endl;
  cout << "\t1. Sold fewer than 100 products" << endl;
  cout << "\t2. Sold between 101 and 200 products" << endl;
  cout << "\t3. Sold more than 200 products" << endl;
  do
    { cout << endl << "What is your choice (1-3) ? ";
      cin >> menu;
    } while ((menu < 1) || (menu > 3));

  switch (menu)
  { case (1) : { bonus = 0.00;
                 break; }
    case (2) : { bonus = 50.00;
                 break; }
    case (3) : { bonus = 100.00;
                 break; }
  } // No default necessary due to do-while loop
  cout.precision(2);
  cout.setf(ios::showpoint);
  cout.setf(ios::fixed);
  cout << endl << "The bonus is $" << bonus << endl;

  return;
}
==============================
// Filename: RE1.CPP
// Reads and displays a file

#include <fstream.h>


void main()
{
  ifstream fp;
  char filename[12];   // Holds user's filename
  char inChar;   // Inputs character

  cout << "What is the name of the file you want to see? ";
  cin >> filename;
  fp.open(filename, ios::in);
  if (!fp)
  {
    cout << endl << endl << "*** That file does not exist ***" << endl;
    return;   // Exits program
  }
  while (fp.get(inChar))
  {  cout << inChar;  }
  fp.close();
  return;
}
==============================
// Filename: RELAT1ST.CPP
 // Prints the results of several relational operations
 #include <iostream.h>
 void main()
 {
   int high = 45;
   int low = 10;
   int middle = 25;
   int answer;


   answer = high > low;
   cout << "High > low is " << answer << endl;

   answer = low > high;
   cout << "Low > high is " << answer << endl;

   answer = middle == middle;
   cout << "Middle == middle is " << answer << endl;

   answer = high >= middle;
   cout << "High >= middle is " << answer << endl;

   answer = middle <= low;
   cout << "Middle <= low is " << answer << endl;

   answer = 0 == 0;
   cout << "Bonus relation: 0 == 0 is " << answer << endl;

   return;
 }
=============================

Tidak ada komentar: