Rabu, 01 April 2026

C++ From Year 1995 File From "A"

 // Filename: 1STLONG.CPP

// Longer C++ program that demonstrates comments,

// variables, constants, and simple input/output

#include <iostream.h>

void main()

  {

    int i, j;    // These three lines declare four variables

    char c;

    float x;


    i = 4;       // i is assigned an integer literal

    j = i + 7;   // j is assigned the result of a computation

    c = 'A';     // Enclose all character literals

                 // in single quotation marks


    x = 9.087;   // x is a floating-point value


    x = x * 12.3F;// Overwrites what was in x

                  // with something else

                  // (The F stops a conversion warning forcing

                  // the literal to be floating point)


    // Sends the values of the four variables to the screen

    cout << i << ", " << j << ", " << c << ", " << x << endl;


    return;   // Not required, but helpful

  }

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

// Filename: ABS.CPP

// Computes the difference between two ages

#include <iostream.h>

#include <math.h>

void main()

{

  float age1, age2, diff;

  cout << endl << "What is the first child's age? ";

  cin >> age1;

  cout << "What is the second child's age? ";

  cin >> age2;


  // Calculates the positive difference

  diff = age1 - age2;

  diff = fabs(diff);   // Determines the absolute value


  cout << endl << "They are " << diff << " years apart.";

  return;

}

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

// Filename: AD1.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  if (isalpha('2'))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: AD2.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  char ch = 'a';

  if (islower(ch))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: AD3.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  char ch = 'a';

  if (isupper(ch))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: AD4.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  char ch = 'a';

  if (isdigit(ch))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: ALCHANGE.CPP

// Stores the alphabet in a file, changes three letters from it,

// and then prints the file


#include <fstream.h>



void main()

{

  fstream fp;

  char ch;   // Holds A through Z


  // Opens in update mode so that you can

  // read file after writing to it

  fp.open("c:\\letters.txt", ios::in | ios::out);

  if (!fp)

  {

    cout << endl << "*** Error opening file ***" << endl;

    return;

  }

  for (ch = 'A'; ch <= 'Z'; ch++)

    { fp << ch; }   // Writes letters

  fp.seekg(7L, ios::beg);  // Skips eight letters, points to H

  fp >> ch;

  // Changes the H to an *

  fp.seekg(-1L, ios::cur);

  fp << '*';

  fp.seekg(15L, ios::beg);   // Skips 16 letters, points to P

  fp >> ch;

  // Changes the P to an *

  fp.seekg(-1L, ios::cur);

  fp << '*';

  fp.seekg(19L, ios::beg);   // Skips 19 letters, points to T

  fp >> ch;

  // Changes the T to an *

  fp.seekg(-1L, ios::cur);

  fp << '*';

  fp.seekg(0L, ios::beg);

  while (fp.get(ch))

    { cout << ch; }   // Outputs characters to the screen


  fp.close();

}

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

// Filename: ALCHECK.CPP

// Asks the user for the number of checks written

// last month, then allocates that many floating-point

// values. The user then enters each check into the

// allocated array and the program prints the total

// after all checks are entered.

#include <iostream.h>


int   HowMany();

void  GetChecks(int noOfChecks, float * theChecks);

float GetTotal(int noOfChecks, const float * theChecks);


void main()

{

  int noOfChecks;

  float total;

  float * theChecks;  // Will point to allocated memory


  cout << "** Monthly Checkbook Program **" << endl << endl;

  

  noOfChecks = HowMany();          // Ask the user how many checks

  

  // Allocate the memory, 1 float per check

  theChecks = new float [noOfChecks];

  GetChecks(noOfChecks, theChecks);          // Get the values

  total = GetTotal(noOfChecks, theChecks);   // Add them up

  cout.precision(2);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << endl << "Your total was $" << total

       << " for the month." << endl;

  delete [] theChecks;

  return;

}

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

int HowMany()

{

  int ans;  // To hold the cin value

  cout << "How many checks did you write last month? ";

  cin >> ans;

  return (ans);

}

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

void GetChecks(int noOfChecks, float * theChecks)

{

  int ctr;

  // No need or vehicle for passing allocated memory. The

  // memory does not go away between functions or blocks.

  cout << endl << "You now must enter the checks, one at a time."

       << endl << endl;

  for (ctr=0; ctr< noOfChecks; ctr++)

  {

    cout << "How much was check " << (ctr+1) << " for? ";

    cin >> theChecks[ctr];  // Store value on the heap

  }

  return;

}

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

float GetTotal(int noOfChecks, const float * theChecks)

{

  // Add up the check totals

  int ctr;

  float total = 0.0;

  for (ctr=0; ctr<noOfChecks; ctr++)

  {

    total += theChecks[ctr];

  }

  return total;

}

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

// Filename: ALPH.CPP

// Stores the alphabet in a file, then reads

// two letters from it


#include <fstream.h>



void main()

{

  char ch;   // Holds A through Z


  // Opens in update mode so that you can

  // read file after writing to it

  fstream fp("alph.txt", ios::in | ios::out);

  if (!fp)

  {

    cout << endl << "*** Error opening file ***";

    return;

  }


  for (ch = 'A'; ch <= 'Z'; ch++)

    fp << ch;   // Writes letters

  

  fp.seekg(8L, ios::beg);  // Skips eight letters, points to I

  fp >> ch;

  cout << "The first character is " << ch << endl;

  

  fp.seekg(16L, ios::beg);   // Skips 16 letters, points to Q

  fp >> ch;

  cout << "The second character is " << ch << endl;

}

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

// Filename: AP1.CPP

// Adds three names to a disk file


#include <fstream.h>



void main()

{

  ofstream  fp;

  fp.open("C:\\NAMES.DAT", ios::app);   // Adds to file

  fp << "Johnny Smith" << endl;

  fp << "Laura Hull" << endl;

  fp << "Mark Brown" << endl;

  fp.close();   // Releases the file

}

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

// Filename: ARAEQUAL.CPP

#include <iostream.h>

void main()

{

  char dogName[20];   //  Reserves space for the dog's name

  dogName = "Ross";   // INVALID!

  cout << dogName;   // The program will never get here

  return;

}

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

// Filename: AREAPASS.CPP

// Contains a function that computes a circle's area

#include <iostream.h>


double computeArea(double radius);


void main()

{

  double radius;

  double area;


  cout << "What is the circle's radius? ";

  cin >> radius;


  area = computeArea(radius);

  cout << "The area is " << area << endl;


  return;

}

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

double computeArea(double radius)

{

  const float PI = 3.14159;

  return (PI * (radius * radius));

}

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

// Filename: ARRHEAP.CPP

// Allocates an array of heap pointers

#include <iostream.h>


void  AllMemory(float * cities[5]);

void  GetCity(float * cities[5]);

float CalcCity(float * cities[5]);

void  FreeAll(float * cities[5]);


void main()

{

  float * cities[5];     // Five city's worth of data

  float avg=0.0;

  AllMemory(cities);

  cout.precision(2);     // Ensure that dollar

  cout.setf(ios::fixed); // amounts display

  cout.setf(ios::showpoint);


  GetCity(cities);

  avg = CalcCity(cities);// Total each city

  avg /= 15.0F;          // Calculate average from total

  cout << endl << "The average is $" << avg << endl;

  

  FreeAll(cities);       // Why not a simple delete[]?


}

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

void AllMemory(float * cities[5])

{

  // Allocate each array's three values

  int ctr;

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

    { cities[ctr] = new float [3]; }

}

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

void GetCity(float * cities[5])

{

  // This function gets the total number

  // of values for each city. Each city has 3

  // salespeople covering the territories.

  int ctr1, ctr2;

  // Use a nested for-loop to get each city's values

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

    {  cout << "City #" << (ctr1+1) << ":" << endl;

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

        { 

          cout << "What is value #" << (ctr2+1) << "? ";

          cin >> cities[ctr1][ctr2];

        }

    }

}

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

float CalcCity(float * cities[5])

{

  // Add up the total sales in each city

  int ctr1, ctr2;

  float totalCity=0.0, total=0.0;

  cout << endl;

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

 {

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

      { 

        float* &tempCity = cities[ctr1];

        totalCity+= tempCity[ctr2];

      }

     cout << "City #" << (ctr1+1) << " total is $"

          << totalCity << endl;

     total += totalCity; // Add to grand total

     totalCity = 0.0;    // Zero for next city

  }

  return total;

}

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

void FreeAll(float * cities[5])

{

  // Free each array's three values

  int ctr;

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

    { delete cities[ctr]; }

  return;

}

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

// Filename: ARRTEMP.CPP

// Fills an array with seven temperature readings, converts

// those readings to Celsius, and prints the results

#include <iostream.h>


void FillTemps(float temps[7]);

void CalcCelsius(float temps[7]);

void PrintCelsius(float temps[7]);


void main()

{


  float temps[7];   // The user will initialize the array


  FillTemps(temps);     // Get the seven values from the user

  CalcCelsius(temps);   // Convert the values to Celsius

  PrintCelsius(temps);  // Print the Celsius readings


  return;

}

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

void FillTemps(float temps[7])

{

  int ctr;

  cout << "** Temperature Conversion **" << endl;

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

  for (ctr = 0; ctr < 7; ctr++)

    { 

      cout << "What is the Fahrenheit reading for day #"

           << (ctr+1) << "? ";

      cin >> temps[ctr];

    }

  return;

}

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

void CalcCelsius(float temps[7])

{

  // Change each temperature to Celsius

  int ctr;

  for (ctr = 0; ctr < 7; ctr++)

    {

      // The Fs after the numbers are floats not Fahrenheits

      temps[ctr] = (temps[ctr] - 32.0F) * (5.0F / 9.0F);

    }

  return;

}

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

void PrintCelsius(float temps[7])

{

  // Print the Celsius readings

  int ctr;

  cout.precision(1);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << "Here are the equivalent Celsius readings:"

       << endl << endl;

  for (ctr = 0; ctr < 7; ctr++)

    { 

      cout << "Day #" << (ctr + 1) << ": "

           << temps[ctr] << endl;

    }

  cout << endl << "Stay warm!" << endl;

  return;

}

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

// Filename: ARRTEMP2.CPP

// Fills an array with temperature readings, converts

// those readings to Celsius, and prints the results.


#include <iostream.h>


void FillTemps(double temps[]);

void CalcCelsius(double temps[]);

void PrintCelsius(double temps[]);


const int DAYS  = 21;


void main()

{


  double temps[DAYS];   // The user will initialize the array


  FillTemps(temps);     // Get the values from the user

  CalcCelsius(temps);   // Convert the values to Celsius

  PrintCelsius(temps);  // Print the Celsius readings


  return;

}

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

void FillTemps(double temps[DAYS])

{

  int ctr;

  cout << "** Temperature Conversion **" << endl;

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

  for (ctr = 0; ctr < DAYS; ctr++)

    { cout << "What is the Fahrenheit reading for day #"

           << (ctr+1) << "? ";

      cin >> temps[ctr];

    }

  return;

}

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

void CalcCelsius(double temps[DAYS])

{

  // Change each temperature to Celsius

  int ctr;

  for (ctr = 0; ctr < DAYS; ctr++)

    { temps[ctr] = (temps[ctr] - 32.0) * (5.0 / 9.0); }

  return;

}

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

void PrintCelsius(double temps[DAYS])

{

  // Print the Celsius readings

  int ctr;

  cout.precision(1);

  cout.setf(ios::fixed);

  cout.setf(ios::showpoint);

  cout << endl << "Here are the equivalent Celsius readings:"

       << endl << endl;

  for (ctr = 0; ctr < DAYS; ctr++)

    { cout << "Day #" << (ctr+1) << ": "

           << temps[ctr] << endl; }

  cout << endl << "Stay warm!" << endl;

  return;

}

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

// AVETEMP.CPP

// Calculate the average of 10 days readings

// above zero


#include <iostream.h>


void main()

  {                             

 

    int count = 0;       // Count of readings above zero

    int sum = 0;         // Running total of above zero readings

    int temperature = 0; 

    float average = 0.0; 

    

    for (int day = 0; day < 10; day++) // for 10 days

      {

        cout << "Please input reading for day " 

             << day + 1  << ": " ;

        cin  >> temperature;

        if (temperature <= 0)

          continue;          // Skip count & sum code if too low

        sum += temperature;  // sum temperature readings

        count++;             // count

      }

    if (count > 0)  // check for zero divide

      {

        average = (float)sum / count;

        cout << "The average of " << count

             << " readings above zero was " << average

             << endl;

      }                 

    else

      {

        cout << "There were no above zero readings to average." << endl;

      }

  }

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

Tidak ada komentar: