22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Simple Data Processing<br />

A First C# Program<br />

2 Simple Data Processing<br />

2.1 A First C# Program<br />

2.1.1 The Program Example<br />

using System;<br />

In this chapter we are going to create a genuinely useful program (particularly if you<br />

are in the double glazing business). We will start by creating a very simple solution and<br />

investigating the C# statements that perform basic data processing. Then we will use<br />

additional features of the C# language to improve the quality of the solution we are<br />

producing.<br />

The first program that we are going to look at will read in the width and height of a<br />

window and then print out the amount of wood and glass required to make a window<br />

that will fit in a hole of that size. This is the problem we set out to solve as described in<br />

section1.2.2<br />

Perhaps the best way to start looking at C# is to jump straight in with our first ever C#<br />

program. Here it is:<br />

class GlazerCalc<br />

{<br />

static void Main()<br />

{<br />

double width, height, woodLength, glassArea;<br />

string widthString, heightString;<br />

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

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

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

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

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

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

}<br />

}<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 />

Code Sample 01 GlazerCalc Program<br />

This is a valid program. If you gave it to a C# compiler it would compile, and you<br />

could run it. The actual work is done by the two lines that I have highlighted. Broadly<br />

speaking the stuff before these two lines is concerned with setting things up and getting<br />

the values in to be processed. The stuff after the two lines is concerned with displaying<br />

the answer to the user.<br />

We can now go through each line in turn and try to see how it fits into our program.<br />

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

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

Saved successfully!

Ooh no, something went wrong!