Spec of the program outputs:

processroguishSoftware and s/w Development

Nov 18, 2013 (3 years and 10 months ago)

112 views

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

1

Spec of the program outputs:

What's your name?
Dennis


Hi! Dennis!



Input the price for 10% discount:
200


Input the quantity:
5


Input the price for 20% discount:
500


Input the quantity:
4




Total items: 9


Amount: 2500.00


Tax rate: 8.5%


Total amount: 2712.50


Press any key to continue




8/20/2007

Chung
-
Chih Li @ ISU ITK 279

2


#include <iostream>

#include <string>


using namespace std;


void main()

{


string name;


int itemNo = 0;


int a,b;


int total_item;


float price_1, price_2;


const float taxRate = 0.085;


float discount = 0.1, Discount (0.2);


double amount;




cout << "What's your name? ";


cin >> name;


cout << "
\
nHi! " << name << "!
\
n
\
n";




CPP program for the baby counter (part 1)

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

3


cout << "
\
t Input the price for ";


cout << (int) (discount*100) << "% discount: ";


cin >> price_1;


cout << "
\
t Input the quantity: ";


cin >> a;



cout << "
\
t Input the price for ";


cout << (int) (Discount*100) << "% discount: ";


cin >> price_2;


cout << "
\
t Input the quantity: ";


cin >> b;



total_item = a + b;


cout << "
\
n
\
n
\
t Total items:
\
t" << total_item << endl;



amount = price_1*a*(1
-
discount)+price_2*b*(1
-
Discount);


cout << "
\
t Amount:
\
t" << amount << endl;



cout << "
\
t Tax rate:
\
t" << taxRate << endl;


amount = (price_1*a*(1
-
discount)+price_2*b*(1
-
Discount))




*(1+taxRate);


cout << "
\
t Total amount:
\
t" << amount << endl << endl;

}

CPP program for the baby counter (part 2)

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

4


#include

<iostream>

#include

<string>


using namespace

std;


/* This is a program that can do something ..


By Chung
-
Chih Li

*/

void main()

{


string name; // string is a class


int itemNo = 0;


int a,b;


int total_item;


float price_1, price_2;


const float taxRate = 0.085;


float discount = 0.1, Discount = 0.2;


double amount;


cout << "What's your name? ";


cin >> name;


cout << "
\
nHi! " << name << "!
\
n
\
n";




Directives

These two statements use the
include
directive to ask the
compiler to provide necessary
accesses to the two

standard
libraries.

The directive
using

asks the compiler to use
the definitions in namespace std.


alternatively: (for this program)

using
std::cin;

using

std::cout;

using

std::endl;

using

std::ios;

using

std::string;


8/20/2007

Chung
-
Chih Li @ ISU ITK 279

5


#include <iostream>

#include <string>


using namespace std;


/*

This is a program that can do something ..


By Chung
-
Chih Li

*/

void main()

{


string name;
//

string is a class


int itemNo = 0;


int a,b;


int total_item;


float price_1, price_2;


const float taxRate = 0.085;


float discount = 0.1, Discount = 0.2;


double amount;



cout << "What's your name? ";


cin >> name;


cout << "
\
nHi! " << name << "!
\
n
\
n";




Comments

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

6


#include <iostream>

#include <string>


using namespace std;


void main()

{


string
name
;


int
itemNo

= 0;


int
a,b
;


int
total_item
;


float
price_1, price_2
;


const float
taxRate

= 0.085;


float
discount

= 0.1,
Discount

(0.2);


double
amount
;



cout << "What's your name? ";


cin >> name;


cout << "
\
nHi! " << name << "!
\
n
\
n";




Identifiers

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

7


#include <iostream>

#include <string>


using namespace std;


void

main()

{


string

name
;


int

itemNo

= 0;


int

a,b
;


int

total_item
;


float

price_1, price_2
;


const

float

taxRate

= 0.085;


float

discount

= 0.1,
Discount

(0.2);


double

amount
;



cout << "What's your name? ";


cin >> name;


cout << "
\
nHi! " << name << "!
\
n
\
n";




Variables and
Types

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

8



string name;


int itemNo =
0
;


int a,b;


int total_item;


float price_1, price_2;


const float taxRate =
0.085
;


float discount =
0.1
, Discount (
0.2
);


double amount;



cout << "
What's your name?

";


cin >> name;


cout << "
\
nHi!

" << name << "
!
\
n
\
n
";




cout << "
\
t Input the price for

";


cout << (int) (discount*
100
) << "
% discount:

";


cin >> price_1;


cout << "
\
t Input the quantity:

";


cin >> a;





Literals (constants)

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

9


#include <iostream>

#include <string>


using namespace std;


void main()

{


string name;


int itemNo
= 0
;


int a,b;


int total_item;


float price_1, price_2;


const float taxRate
= 0.085
;


float discount
= 0.1
, Discount
(0.2)
;


double amount;



cout << "What's your name? ";


cin >> name;


cout << "
\
nHi! " << name << "!
\
n
\
n";




Value Initialization

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

10


short

s;

int

i;

long

l;

float

f;

double

d;

long

double

u;

char

c;

bool

b;






cout << "sizeof(short) :
\
t
\
t "


<<
sizeof(short)


<< " sizeof(s) : "


<<
sizeof(s)

<< endl;




Simple Types

sizeof(short) : 2 sizeof(s) : 2

sizeof(int) : 4 sizeof(i) : 4

sizeof(long) : 4 sizeof(l) : 4

sizeof(double) : 8 sizeof(d) : 8

sizeof(long double) : 8 sizeof(u) : 8

sizeof(char) : 1 sizeof(c) : 1

sizeof(bool) : 1 sizeof(b) : 1


Press any key to continue


8/20/2007

Chung
-
Chih Li @ ISU ITK 279

11



total_item = a + b;



total_item = a;


a = a;



amount = (price_1*a*(1
-
discount)+price_2*b*(1
-
Discount))




*(1+taxRate);


Assignment I


a + 1 = a

L
-
value = R
-
value

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

12



a += 2;

a /= 2;

a %= 2;

a *= a;

........

total_item = a + b;


cout << (a = 2*3);


a = b = 3;

a = (b = 3);


Assignment II

a = a+b = 3;

?

a = 3;

a *= a*= 2;

cout << a;

36

(a = b) = 3;

a = 3;

a = a * ( a *=2);

cout << a;

a = 3;

a = a * ( a = a*2);

cout << a;

?

?

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

13







Arithmetic operators and their precedence rules


amount =


(
price_1*a*
(
1
-
discount
)
+price_2*b*
(
1
-
Discount
)
)
*









(
1+taxRate
)
;



precedence rules:

% * /

+
-

* / %

>

+
-

a
-
b
-
c
-
d ((a
-
b)
-
c)
-
d

a=b=c=d a=(b=(c=d))

left
-
to
-
right

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

14

Increment and Decrement

i++;

++i;

i =
i+1;

i
--
;

--
i;

i = i
-
1;

n = (i++);


n = (++i);

n = i;

i = i+1;

i = i+1;

n = i;

i=0;

s = (++(++i))++;

cout << i << endl;

cout << s << endl;

i=0;

i = i+1;

i = i+1;

s = i;

i = i+1;

cout...

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

15

Type Coercion (Type Casting)

Input the price for 10% discount:
200

Input the quantity:
5

Input the price for 20% discount:
500

Input the quantity:
4


float discount = 0.1, Discount (0.2);


cout << "
\
t Input the price for ";

cout <<
(int) (discount*100)

<< "% discount: ";

......

cout << "
\
t Input the price for ";

cout <<
(int) (Discount*100)

<< "% discount: ";



8/20/2007

Chung
-
Chih Li @ ISU ITK 279

16

Type Coercion (Type Casting),
(Implicit)

double score_1=1, score_2=2;

int no_test=2;

double average;

.....

average = (score_1 + score_2 )/ no_test;


int i=1, j=2, k=3;

double r=1, s=2, t=3;

double a,b,c,d,e;

...

a = r/t + s/t; //
0.33333... + 0.66666...


b = i/k + j/k; //
1/3 + 2/3


〠‫ ‰


挠㴠⡩⭪⤯欻(††⼯/(
ㄫ㈩1⼠㌠3†


㌯3


搠d⁩⽴/⬠ 樯j㬠 
0.㌳3㌳⸮.‫ 0


e = (i+r)/k; //
(1+1.0)/3


㈮〯㌠


〮㘶㘶0




8/20/2007

Chung
-
Chih Li @ ISU ITK 279

17







Type Casting,
(Explicit)

int score_1=1, score_2=2;

int no_test=2;

double average;

.....

average = (score_1 + score_2 )/ no_test;

1

average = (score_1 + score_2 )/ ((double)no_test);

1.5

average = (double)(score_1 + score_2 )/no_test;

average = (double)(
(score_1 + score_2 )/no_test
);

1

average =(score_1 + score_2 +
0.0
)/no_test;

1.5

older form

...... (.....)/

double(no_test);

...... (.....)/

static_cast<double>(no_test);


new notation for C++
standard

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

18

More on Type Casting

int i;

i = 1/2.0;

double a;


a = static_cast<int>(1/2.0);

double p=2.99;

const double tax=0.085;

double total = p*(1+tax);


cout << total;

total =
double
(
int
(p*(1+tax)*100))/100;


3.24415

3.24

total = (
int
(p*(1+tax)*100))/100;


3

a = 0.0

a = 1/2.0;

a = 0.5

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

19


#include

<iostream>

#include

<string>


using namespace std;

cout, the Class

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

scientific notation:

1.2293e+009

fixed point:

1229312345.67

12.00

ref: page 490

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

20

The insertion operator:

<<

Standard Console I/O: cin and cout

cout <<

x;

Insert
the value of

x to the standard output stream.

cout << x,y;

cout << x << y
;

x=5; y=9;

cout << x << y << “ “ << x << “ “ << y << “d“
;

59 5 9d

The printing will take place at the end of
the cout statement. After printing, the
output stream will become empty again.

Why we need a output stream?

Some location in the main
memory (e.g.
004787f4
)

59 5 9d

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

21

Evaluation order in a
<<
statement

cout << "a" << (cout << "B") << endl;

cout << (cout << "a") << "B" << endl;

i=0;

cout << i++ << i++ << i++ << endl;

2

3

1

2

0

1

0
i

210

B

a

a

004787f4

Ba004787f4

004787f4B

a004787f4B

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

22

The extraction operator:

>>

cin >>

x
: Extract the value in the input stream to variable x.

cin >> x,y;

cin >> x >> y
;

cin >> x >> y;

cin >> name;

5 9.6 Dennis



Values in the input stream are separated by blank space or returns,

or some proper
delimiters
.



If there are more variables than values in the stream, the computer

will
halt and wait

for more inputs.

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

23

The extraction operator:

>>

int i,j,k;



cout << "Input 3 numbers, 1st:"; cin >> i;


cout <<

2nd:"; cin >> j;


cout <<

3rd:"; cin >> k;


cout << "The three numbers are:"


<< i << " " << j << " " << k << "
\
n";


Input 3 numbers, 1st:
10

2nd:
200

3rd:
3000

The three numbers are:
10 200 3000

Input 3 numbers, 1st:
1 2 3

2nd:3rd:The three numbers are:
1 2 3

Input 3 numbers, 1st:
2

2nd:
4.3

3rd:The three numbers are:
2 4
-
858993460

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

24

Escape Sequences

cout << "What's your name? ";

cin >> name;

cout << "
\
n
Hi!
\
"
" << name << "
\
"
!
\
n
\
n
";


cout << "
\
t

Input the price for ";

What's your name?
Dennis


Hi! "Dennis"!



Input the price for 10% discount:

In a string that to be printed, C++ considers a character
that immediately follows
\

as a special symbol that
“escapes”

from its regular C++ meaning..

\
n

\
t


\
\


\



\


8/20/2007

Chung
-
Chih Li @ ISU ITK 279

25

More on Escape Sequences

cout << "What's your name? ";

cin >> name;

cout <<
"
\
nHi! "
"

<< name << "
"!
\
n
\
n";


cout << "
\
t Input the price for ";

What's your name?
Dennis


Hi! << name << !



Input the price for 10% discount:

cout << x,y;

cout << x y;

exception:

cout <<
"
Dennis
"

"
Li
"

"
!
"
;

8/20/2007

Chung
-
Chih Li @ ISU ITK 279

26

Nasty C++ Programs

a = (++n) + (i++);

OK!

n = 0;

a = n + (++n);


n = 0;

a = (++n) + n;


n = 0;

a = (++n) + (++n);


a=
0
+1



a=
1
+1


a=1+
1



a=1+
0


2



2

4

KISS principle:

Keep It Simple, Stupid!!

?