// 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;
}
}
}
}
Tidak ada komentar:
Posting Komentar