22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Simple Data Processing<br />

Manipulating Data<br />

The way that an operator behaves depends on the context of its use. Later on we will<br />

see that the + operator, which normally performs a numeric calculation, can be used<br />

between strings to concatenate them together, i.e. "Ro" + "b" would give the result<br />

"Rob".<br />

If you want complete control over the particular kind of operator the compiler will<br />

generate for you the program must contain explicit casts to set the correct context for<br />

the operator.<br />

using System;<br />

class CastDemo<br />

{<br />

static void Main ()<br />

{<br />

int i = 3, j = 2 ;<br />

float fraction ;<br />

fraction = (float) i / (float) j ;<br />

Console.WriteLine ( "fraction : " + fraction ) ;<br />

}<br />

}<br />

Code Sample 03 Casting Demo<br />

The (float) cast in the above tells the compiler to regard the values in the integer<br />

variables as floating point ones, so that we get 1.5 printed out rather than 1.<br />

Programmer’s Point: Casts can add clarity<br />

I tend to put the casts in even if they are not needed, this can make the program clearer. It may not affect the result of<br />

the calculation but it will inform the reader of what I am trying to do.<br />

2.2.9 Programs and Patterns<br />

At this point we can revisit our double glazing program and look at the way that it<br />

works. The code that actually does the work boils down to just a few lines which read<br />

in the data, store it in an appropriate type of location and then uses the stored values to<br />

calculate the result that the user requires:<br />

string widthString = Console.ReadLine();<br />

double width = double.Parse(widthString);<br />

string heightString = Console.ReadLine();<br />

int height = double.Parse(heightString);<br />

woodLength = 2 * ( width + height ) * 3.25 ;<br />

glassArea = 2 * ( width * height ) ;<br />

Console.WriteLine ( "The length of the wood is " +<br />

woodLength + " feet" ) ;<br />

Console.WriteLine("The area of the glass is " +<br />

glassArea + " square metres" ) ;<br />

The interesting thing about this is that it is a pattern of behaviour which can be reused<br />

time and time again.<br />

As an example, consider another friend of yours, who runs a chemist shop. He wants a<br />

program that will work out the total cost of tablets that he buys, and the number of<br />

bottles that he needs. He enters the cost of the tablets and the number he wants. The<br />

tablets are always sold in bottles that can contain up to 100 tablets.<br />

You can very easily modify your program to do this job, the only hard part is figuring<br />

out how many bottles that are needed for a particular number of tablets. If you just<br />

divide the number of tablets by 100 an integer division will give you the wrong answer<br />

(for any number of tablets less than 100 your program will tell you that 0 bottles are<br />

C# <strong>Programming</strong> © Rob Miles 2015 32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!