Rabu, 01 April 2026

C++ From Year 1995 File From "I"

 // Filename: IFELSETV.CPP

// This program uses embedded if-else statements to print

// a message for the user. The printed message corresponds

// to the user's favorite television channel.

#include <iostream.h>

void main()

{

  int channel;


  cout << "In this town, there are five non-cable television ";

  cout << "channels." << endl;

  cout << "What is your favorite (2, 4, 6, 8, or 11)? ";

  cin >> channel;

  cout << endl << endl;   // Output two blank lines


  // Use an embedded if to print appropriate messages

  if (channel == 2)

    { cout << "Channel 2 got top ratings last week!"; }

  else

    if (channel == 4)

     { cout << "Channel 4 shows the most news!"; }

       else

         if (channel == 6)

           { cout << "Channel 6 shows old movies!"; }

         else

           if (channel == 8)

             { cout << "Channel 8 covers many local events!"; }

           else

             if (channel == 11)

               { cout << "Channel 11 is public broadcasting!"; }

             else   // The logic gets here only if the

                    // user entered an incorrect channel

               { cout << "Channel " << channel

                      << " does not exist; it must be cable."; }


  cout << endl << endl << "Happy watching!";

  return;

}

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

  // Filename: INCDEC.CPP

  // Uses increment and decrement

  #include <iostream.h>

  void main()

  {

    int age;

    int lastYear, nextYear;


    cout << "How old are you? ";

    cin >> age;


    lastYear = age;

    nextYear = age;


    cout << "Wow, you're a year older than last year ("

         << --lastYear << ")" << endl;

    cout << "and you'll be " << ++nextYear

         << " next year."  << endl;


    return;

  }

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

// Filename: INITS2.CPP

// Makes sure that the user enters two initials

#include <ctype.h>

#include <iostream.h>


void main()

{

  char f, l;

  cout << "What is your first initial? ";

  cin >> f;

  cin.ignore(80,'\n');  // Discard the newline

  if (!isalpha(f))

    { cout << "That's not an initial!" << endl;

      return;

    }


  cout << "What is your last initial? ";

  cin >> l;

  if (!isalpha(l))

    { cout << "That's not an initial!" << endl;

      return;

    }

  // Converts to uppercase if needed

  f = toupper(f);

  l = toupper(l);

  cout << "Here are your initials: " << f << l << "" << endl;

  return;

}

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

// Filename: INTCHAR.CPP

 // Mixing integers and characters

 #include <iostream.h>

 void main()

 {

   int i, j, k;   // Define 3 integers

   char c, d, e;  // Define 3 characters


   // Mix the data

   i = 'A';   // Stores 65 in i

   j = 'B';   // Stores 66 in j

   k = 'C';   // Stores 67 in k


   c = 88;    // Stores 'X' in c

   d = 89;    // Stores 'Y' in d

   e = 90;    // Stores 'Z' in e


   cout << i << ", " << j << ", " << k << endl;

   cout << c << ", " << d << ", " << e << endl;

   return;

 }

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

// Filename: INTDIV.CPP

// Computes integer division

#include <iostream.h>

void main()

{

  int people, events;

  float avgEv;


  cout << "How many people will attend? ";

  cin >> people;

  cout << "How many events are scheduled? ";

  cin >> events;


  // Compute the average number of people per event

  avgEv = people / events;   // The integer division ensures

                             // that the fractional part of

                             // the answer is discarded

  cout << "There will be an average of " << avgEv

       << " people per event." << endl;

  return;

}

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

// Filename: INTFIVE.CPP

 // Compute five periods of interest

 #include <iostream.h>

 void main()

 {

   float intRate;     // Interest rate per period

   float principal;   // Loan amount


   cout << "Welcome to loan central!" << endl;  // Title

   cout << "------------------------" << endl << endl;


   cout << "How much was the loan for? ";

   cin >> principal;


   cout << "What is the period interest rate (i.e., .03 for 3%)? ";

   cin >> intRate;


   cout << "Here is the total owed after five periods" << endl;

   cout << "(Assuming no payment is made)" << endl;


   principal *= (1 + intRate);    // First period interest

   principal *= (1 + intRate);

   principal *= (1 + intRate);

   principal *= (1 + intRate);

   principal *= (1 + intRate);    // Fifth period interest


   cout.precision(2);

   cout.setf(ios::fixed);         // Ensure two decimal places

   cout.setf(ios::showpoint);

   cout << "$" << principal

        << " total amount owed after five periods." << endl;

   return;

 }

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

// Filename: INTFIVE2.CPP

// Compute five periods of interest

// Print the principal after each period

#include <iostream.h>


void main()

{

  float intRate;     // Interest rate per period

  float principal;   // Loan amount


  cout << "Welcome to loan central!" << endl;  // Title

  cout << "------------------------" << endl  << endl;


  cout << "How much was the loan for? ";

  cin >> principal;


  cout << "What is the period interest rate (i.e., .03 for 3%)? ";

  cin >> intRate;


  cout.precision(2);

  cout.setf(ios::fixed);

  cout.setf(ios::showpoint);

  principal *= (1 + intRate);    // 1st period interest

  cout << "Principal after 1 period: " << principal <<  endl;

  principal *= (1 + intRate);

  cout << "Principal after 1 period: " << principal <<  endl;

  principal *= (1 + intRate);

  cout << "Principal after 1 period: " << principal <<  endl;

  principal *= (1 + intRate);

  cout << "Principal after 1 period: " << principal <<  endl;

  principal *= (1 + intRate);    // 5th period interest


  cout << "Here is the total owed after five periods" << endl;

  cout << "(Assuming no payment is made)"  << endl;


  cout << "$" << principal << " total amount owed after five periods." << endl;

  return;

}

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

// Filename: INVENT2V.CPP

 #include <iostream.h>

 void main()

 {


   // Define the two inventory variables

   float price;

   char descrip[25];


   cout << "What is the item's description? ";

   cin.getline(descrip, 25);


   cout << "What is the item's price? ";

   cin >> price;


   cout << endl << endl << "Here is the item: " << endl;

   cout << "  Description: " << descrip;


   cout.precision(2);

   cout.setf(ios::fixed);

   cout.setf(ios::showpoint);

   cout << "  Price: " << price << endl;

   return;

 }

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

// Filename: INVSRCH.CPP

// Demonstrates the sequential parallel array searching

// technique. An inventory ID number is asked for. If

// the ID is found in the arrays, that product's

// inventory data is displayed for the user.

#include <iostream.h>


int GetKey();

int SearchInv(int keyVal, int prodID[]);

void PrintData(int foundSub, int prodID[], int numItems[],

               float price[], int reorder[]);


const int INV = 10; // Products in this sample array


void main()

{


  // First, initialize a few sample elements

  // Product IDs

  int prodID[INV] = {32, 45, 76, 10, 94,

                     52, 27, 29, 87, 60};

  // Number of each product currently in the inventory

  int numItems[INV] = {6, 2, 1, 0, 8,

                       2, 4, 7, 9, 3};

  // Price of each product

  float price[INV] = {5.43, 6.78, 8.64, 3.32, 1.92,

                      7.03, 9.87, 7.65, 4.63, 2.38};

  // Reorder levels

  int reorder[INV] = {5, 4, 1, 3, 5,

                      6, 2, 1, 1, 4};

  int keyVal, foundSub;


  // The program's primary logic begins here

  keyVal = GetKey();   // Ask the user for an ID

  foundSub = SearchInv(keyVal, prodID);   // Search the inventory

  if (foundSub == -99)

    { 

      cout << "That product is not in the inventory.";

      return;

    }

  // Here only if the item was found

  PrintData(foundSub, prodID, numItems, price, reorder);

  return;

}

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

int GetKey()

{

  // Ask the user for an ID

  int keyVal;

  cout << "** Inventory Search Program **" << endl;

  cout << endl << endl << "Enter a product ID: ";

  cin >> keyVal;

  return (keyVal);

}

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

int SearchInv(int keyVal, int prodID[])

{

  // Search the ID array and return the subscript of

  // the found product, or return -99 if not found

  int foundSub = -99;

  int c;

  for (c = 0; c < INV; c++)

    { 

      if (keyVal == prodID[c])

        { 

          foundSub = c;

          break;

        }

    }

  // The -99 will still be in foundSub

  // if the search failed

  return (foundSub);

}

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

void PrintData(int foundSub, int prodID[], int numItems[],

       float price[], int reorder[])

{

  // Print the data from the matching parallel arrays

  cout.precision(2);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << "Product ID: " << prodID[foundSub]

       << "\tNumber in stock: "

       << numItems[foundSub] << endl;

  cout << "Price: $" << price[foundSub] << "\tReorder: "

       << reorder[foundSub] << endl;

  return;

}

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

// Filename: INVSRCH2.CPP

// Demonstrates the sequential parallel array searching

// technique. An inventory ID number is asked for. If

// the ID is found in the arrays, that product's

// inventory data is displayed for the user.


#include <iostream.h>


int getKey(void);

int searchInv(int keyVal, int prodID[]);

void prData(int foundSub, int prodID[], int numItems[],

       float price[], int reorder[]);


const int INV = 10;   // Products in this sample array


void main()

{

  // First, initialize a few sample elements

  // Product IDs

  int prodID[INV] = {32, 45, 76, 10, 94,

                     52, 27, 29, 87, 60};

  // Number of each product currently in the inventory

  int numItems[INV] = {6, 2, 1, 0, 8,

                       2, 4, 7, 9, 3};

  // Price of each product

  float price[INV] = {5.43, 6.78, 8.64, 3.32, 1.92,

                      7.03, 9.87, 7.65, 4.63, 2.38};

  // Reorder levels

  int reorder[INV] = {5, 4, 1, 3, 5,

                      6, 2, 1, 1, 4};

  int keyVal, foundSub;


  // The program's primary logic begins here

  do

  {

    keyVal = getKey();   // Ask the user for an ID

    if (keyVal == -99)

      { break; }

    foundSub = searchInv(keyVal, prodID);   // Search the

                                            // inventory

    if (foundSub == -99)

      { cout << "That product is not in the inventory.";

        cout << endl << endl << "Press any key to continue...";


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

        continue;   // Loop again

      }

    // Here only if the item was found

    prData(foundSub, prodID, numItems, price, reorder);

    cout << endl << endl << "Press any key to continue...";

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

  } while (keyVal != -99);

  return;

}

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

int getKey(void)

{

  // Ask the user for an ID

  int keyVal;

  cout << "** Inventory Search Program **" << endl;

  cout << endl << endl << "Enter a product ID (-99 to quit): ";

  cin >> keyVal;

  return (keyVal);

}

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

int searchInv(int keyVal, int prodID[])

{

  // Search the ID array and return the subscript of

  // the found product. Return -99 if not found.

  int foundSub = -99;

  int c;

  for (c = 0; c < INV; c++)

    { if (keyVal == prodID[c])

      { foundSub = c;

        break;

      }

    }

  // The -99 will still be in foundSub

  // if the search failed

  return (foundSub);

}

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

void prData(int foundSub, int prodID[], int numItems[],

       float price[], int reorder[])

{

  // Print the data from the matching parallel arrays

  cout.precision(2);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << "Product ID: " << prodID[foundSub]

       << "\tNumber in stock: " << numItems[foundSub]

       << endl;

  cout << "Price: $" << price[foundSub]

       << "\tReorder: " << reorder[foundSub] << endl;

  return;

}

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

// Filename: IOFIX.CPP

// Makes a copy of a file

#include <fstream.h>


void main()

{

  ifstream inFp;

  ofstream outFp;

  char inFilename[25];   // Holds original filename

  char outFilename[25];   // Holds backup filename

  char inChar;   // Inputs character


  cout << "What is the name of the file you want to"

       << " back up? ";

  cin >> inFilename;

  cout << "What is the name of the file ";

  cout << "you want to copy " << inFilename << " to? ";

  cin >> outFilename;

  inFp.open(inFilename, ios::in);


  if (!inFp)

  {

    cout << endl << endl << "*** " << inFilename 

       << " does not exist ***";

    return;   // Exits program

  }

  outFp.open(outFilename, ios::out);

  if (!outFp)

  {

    cout << endl << endl << "*** Error opening " 

       << inFilename << " ***" << endl;

    return;   // Exits program

  }

  cout << endl << "Copying..." << endl;   // Waiting message


  //

  // The inserted code follows:

  while (inFp.get(inChar))

    { outFp << inChar;

    }

  //

  // The program continues


  cout << endl << "The file is copied." << endl;

  inFp.close();

  outFp.close();

  return;

}

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

// File name IOSTEST.CPP

// A demonstration of file streams

// which shows that we already know

// how to use files.


#include <fstream.h> // also includes iostream.h


void main()

  {

    float f;

    char text[100]; 

    // Open a file for output

    ofstream out("hello.txt"); 

    cout << "Please enter a line of text: ";

    // Get some text from the screen

    cin.getline(text,30);    

    // Make sure the file is open...

    if (out.is_open())

        {

          // Output the input

          out << text;       

          // Output a literal

          out << "The world is an orange";

          // Output a number

          f = 33.5F;

          out << f;        

          // Close the file so data is avaliable

          out.close();

        }

    cout << "Here is the file:" << endl;

    // Open the file for input

    ifstream in("hello.txt");               

    // Read file item by item (white space delimits)

    while (in.is_open() && !in.eof())

      {

        in >> text;

        cout << text << endl;

      }

    // Reposition to the start by closing and opening

    in.close();

    in.open("hello.txt");

        

    // Get the contents as text

    if (in)  // same as is_open()

      {

        in.getline(text,100);

        cout << endl << endl;

        cout << "Here is the file:" << endl;

        cout << text;

      }

  }     

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


Tidak ada komentar: