Saturday, 11 January 2014

CHAPTER 2—C++ PROGRAMMING BASICS codes;

Code:
/*7.  You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5
  and adding 32. Write a program that allows the user to enter a floating-point number representing
  degrees Celsius, and then displays the corresponding degrees Fahrenheit.*/
#include<iostream.h>
#include<conio.h>

void main(void)
{
 cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 cout<<"-------------------------------------------------------------------------------\n\n";
 float c_temp;

 do{
 cout<<"Enter the degrees Celsius\t\t\t\xdb ";
 cin >>c_temp;
 cout<<"The corresponding degrees Fahrenheit is :\t\xdb "<<((9/5)*c_temp)+32<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:
/*8.  When a value is smaller than a field specified with setw(), the unused locations are, by default,
  filled in with spaces. The manipulator setfill() takes a single character as an argument and causes this
  character to be substituted for spaces in the empty parts of a field. Rewrite the WIDTH program so
  that the characters on each line between the location name and the population number are filled in
  with periods instead of spaces, as in 

    Portcity.....2425785
*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
//WIDTH program:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!// width2.cpp
!// demonstrates setw manipulator
!#include &ltiostream>
!#include &ltiomanip>     // for setw
!using namespace std;
!
!int main()
!   {
!   long pop1=2425785, pop2=47, pop3=9761;
!
!   cout << setw(8) << "LOCATION" << setw(12)
!        << "POPULATION" << endl
!        << setw(8) << "Portcity" << setw(12) << pop1 << endl
!        << setw(8) << "Hightown" << setw(12) << pop2 << endl
!        << setw(8) << "Lowville" << setw(12) << pop3 << endl;
!   return 0;
!   }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void main(void)
{
 cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 cout<<"-------------------------------------------------------------------------------\n\n";
 long pop1=2425785, pop2=47, pop3=9761;
 
 do{
 cout << setw(8) << "LOCATION" << setw(12)
      << "POPULATION" << endl
         << setw(8) << "Portcity" << setw(12) << setfill('.') << pop1 << endl
         << setw(8) << "Hightown" << setw(12) << setfill('.') << pop2 << endl
         << setw(8) << "Lowville" << setw(12) << setfill('.') << pop3 << endl;
    cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:
/*9.  If you have two fractions, a/b and c/d, their sum can be obtained from the formula 

  a      c      a*d + b*c
 --- + ---  =  -----------
  b      d         b*d



 For example, 1/4 plus 2/3 is 
  1     2       1*3 + 4*2       3 + 8       11
 --- + ---  =  -----------  =  -------  =  ----
  4     3          4*3            12        12



 Write a program that encourages the user to enter two fractions, and then displays their sum in
 fractional form. (You don’t need to reduce it to lowest terms.) The interaction with the user might
 look like this: 

 Enter first fraction: 1/2
     Enter second fraction: 2/5
     Sum = 9/10


 You can take advantage of the fact that the extraction operator (>>) can be chained to read in more
 than one quantity at once: 
 
 cin >> a >> dummychar >> b;
*/
#include<iostream.h>
#include<conio.h>
Code:
/*10.  In the heyday of the British empire, Great Britain used a monetary system based on pounds,
  shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for
  this old system used the pound sign, £, and two decimal points, so that, for example, £5.2.8 meant
  5 pounds, 2 shillings, and 8 pence. (Pence is the plural of penny.) The new monetary system,
  introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S.
  dollars and cents). We’ll call this new system decimal pounds. Thus £5.2.8 in the old notation is
  £5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-
  shillings-pence format to decimal pounds. An example of the user’s interaction with the program
  would be 

    Enter pounds: 7
    Enter shillings: 17
    Enter pence: 9
    Decimal pounds = £7.89



  In both Borland C++ and Turbo C++, you can use the hex character constant ‘\x9c’ to represent the
  pound sign (£). In Borland C++, you can put the pound sign into your program directly by pasting it
  from the Windows Character Map accessory.*/
#include<iostream.h>
#include<conio.h>
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!1 pound    = 20 shilling
!1 shilling = 12 pence
!so    1 pound    = (20*12) 240 pence [in old system]
!also  1 pound    =         100 pence [in new system]
!hence the transformation formula is :
!new_pence = (old_pence*100)/240
---------------------------------------------------------------------
!number of pence{old_pence} = (input_shillings*12) + input_pence
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void main(void)
{
 cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 cout<<"-------------------------------------------------------------------------------\n\n";
 int pounds, shillings, pence;
 do{
 cout<<"Enter pounds     : ";
 cin >>pounds;
 cout<<"Enter shillings  : ";
 cin >>shillings;
 cout<<"Enter pence      : ";
 cin >>pence;
 pence = ((shillings*12)+pence)*100/240;
 //To make the programme more really (pence must not pass value 100).
 if (pence >= 100){
  //shillings here is only a gate, not by it's mean at all.
  shillings = pence%100;
  pounds += (pence-shillings)/100;
  pence = shillings;}
 cout<<"Decimal pounds   = \x9c"<<pounds<<"."<<pence<<endl;
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:
/*11.  By default, output is right-justified in its field. You can left-justify text output using the
  manipulator setiosflags(ios::left). (For now, don’t worry about what this new notation means.) Use
  this manipulator, along with setw(), to help generate the following output:
  
  Last name    First name    Street address    Town    State
      -----------------------------------------------------------
      Jones    Bernard    109 Pine Lane    Littletown    MI
      O’Brian    Coleen    42 E. 99th Ave.    Bigcity    NY
      Wong    Harry    121-A Alabama St.    Lakeville    IL
*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main(void)
{
 cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 cout<<"-------------------------------------------------------------------------------\n\n";
 char *l_name[3] = {"Jones", "O'Brian", "Wong"},
   *f_name[3] = {"Bernard", "Coleen", "Harry"},
   *adress[3] = {"109 Pine Lane", "42 E. 99th Ave.", "121-A Alabama St."},
   *town[3]   = {"Littletown", "Bigcity", "Lakeville"},
   *state[3]  = {"MI", "NY", "IL"};
 
 do{
 cout<<setiosflags(ios::left)<<setw(11)<<"Last name"
                          <<setw(12)<<"First name"
        <<setw(20)<<"Street adress"
        <<setw(12) <<"Town"
        <<setw(7) <<"State"
        <<endl;
 for(int i=0;i<60;i++) cout<<"-";
 for(int j=0;j<3;j++){
  cout<<endl<<setiosflags(ios::left)<<setw(11)<<l_name[j]
            <<setw(12)<<f_name[j]
            <<setw(20)<<adress[j]
            <<setw(12)<<town[j]
            <<setw(7) <<state[j]
            <<endl;}
 cout<<"\n !Press c to continue or any key to exit."<<endl<<endl;
 }while(getch()=='c');
}
Code:
/*12.  Write the inverse of Exercise 10, so that the user enters an amount in Great Britain’s new
  decimal-pounds notation (pounds and pence), and the program converts it to the old pounds-
  shillings-pence notation. An example of interaction with the program might be
  
    Enter decimal pounds: 3.51
    Equivalent in old notation = £3.10.2.



  Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the
  decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler
  warning. You can use statements like 

    float decpounds;    // input from user (new-style pounds)
    int pounds;         // old-style (integer) pounds
    float decfrac;      // decimal fraction (smaller than 1.0)

  pounds = static_cast<int>(decpounds); // remove decimal fraction
    decfrac = decpounds - pounds;  // regain decimal fraction



  You can then multiply decfrac by 20 to find shillings. A similar operation obtains pence.*/
#include<iostream.h>
#include<conio.h>
//Old programme:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!1 pound    = 20 shilling
!1 shilling = 12 pence
!so    1 pound    = (20*12) 240 pence [in old system]
!also  1 pound    =         100 pence [in new system]
!hence the transformation formula is :
!new_pence = (old_pence*100)/240

2 comments: