c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.228 seguidores
Pré-visualização50 páginas
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
Maersk.com/Mitas
\ufffde Graduate Programme 
for Engineers and Geoscientists
Month 16
I was a construction
supervisor in 
the North Sea 
advising and 
helping foremen 
solve problems
I was a
he
s
Real work 
International opportunities 
\ufffdree work placements
al Internationa
or\ufffdree wo
I wanted real responsibili\ufffd 
 I joined MITAS because 
www.discovermitas.com
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
29 
Console programs
Comment
There are following options to format a placeholder:
C Currency (depends on the local setting)
D Integer
E Exponential form (float, double)
F Fixed decimal (float, double)
G General (F or E)
N Numeric with thousands
X Hexadecimal
Exam06
Product calculation
The next example performs a calculation. The user must enter the unit price and number of units of an 
item. Then the program calculates the total price excl. VAT, VAT, total price incl. VAT and writes the 
result on the screen. If you run the program, the result could be the following:
How to
The starting point is again a Console Application project:
static void Main(string[] args)
{
Console.Write("Enter the unit price: ");
string text = Console.ReadLine();
double price = Convert.ToDouble(text);
Console.Write("Enter the number of units: ");
text = Console.ReadLine();
int quantity = Convert.ToInt32(text);
double amount = price * quantity;
double vat = amount * 0.25;
double total = amount + vat;
Console.WriteLine("{0, -15} {1, 10:F}", "Unit price", price);
Console.WriteLine("{0, -15} {1, 10:D}", "Number of units", quantity);
Console.WriteLine("{0, -15} {1, 10:F}", "Total excl. VAT", amount);
Console.WriteLine("{0, -15} {1, 10:F}", "VAT", vat);
Console.WriteLine("{0, -15} {1, 10:F}", "Total incl. VAT", total);
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
30 
Console programs
Explanation
The program works just like the previous program, only this time you must enter two values. Moreover, 
the placeholders are more complex. If for example you look at the statement:
Console.WriteLine("{0, -15} {1, 10:F}", "Unit price", price);
There are two placeholders. {0, -15} is the first, and insert the words \u201cUnit price\u201d. -15 means that the 
field is 15 characters wide, and when the number is negative, the value must be left justified. Note that 
there is no format character, and then it is the data type of the element that determines the format type. 
The next placeholder {1, 10:F} means that the next item to be formatted right-justified in a field of 10 
characters and as a decimal number. As the number of decimal places is not specified the default value 
is used, which is 2.
Exam07
Date and time
This program will print how much the time is:
The program will primarily show the formatting of the result, but also the use of type DateTime.
How to
The code can be written as shown below. Note that this time there are two methods that are called from 
Main():
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Time1(dt);
Time2(dt);
}
static void Time1(DateTime t)
{
Console.WriteLine("{0:D2} {1:D2} {2:D2} {3:D3}", t.Hour, t.Minute, t.Second, 
t.Millisecond);
}
static void Time2(DateTime t)
{
Console.WriteLine(t.ToLongDateString());
Console.WriteLine(t.ToLongTimeString());
}
Note that I did not show the whole code, but only the methods.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
31 
Console programs
Explanation
In Main() the machine clock is read and its value is stored in a variable:
DateTime dt = DateTime.Now;
DateTime is a class that contains a number of methods to date and time. Now is a property, which always 
contains the current value of the hardware clock. This value is stored in a variable called dt and which 
type is DateTime. Next, a method Time1() is called, where the variable dt is sent as a parameter. This 
means that it is known and can be used in the method Time1() with the name t. The method prints the 
time in terms of hours, minutes, seconds and milliseconds, each part separated by spaces. Please note the 
placeholders. For example means {0: D2} that the first variable to be formatted in a field as an integer, 
and that the field should be two characters in order to insert a leading 0, if there is only one digit. You 
will also notice how you refer to the values. t is a variable whose type is DateTime, which is a class. 
The class defines a number of properties, for example t.Hour for hours, and you refers to the individual 
characteristics by the variables name followed by a period and the feature name.
The class DateTime provides other opportunities. The method Time2() writes the current date and time, 
but here I used methods from the DateTime class that formats the result as a string.
We will turn your CV into 
an opportunity of a lifetime
Do you like cars? Would you like to be a part of a successful brand?
We will appreciate and reward both your enthusiasm and talent.
Send us your CV. You will be surprised where it can take you.
Send us your CV on
www.employerforlife.com
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
32 
Console programs
Comment
DateTime is a class (actually a struct), and as you can see, there are some new things such as properties 
etc. All this will be dealt with in detail later. The same applies to parameters that are also used in this 
example without going into detail on the meaning, but you can think of a parameter as a variable that 
specifies the value a method has to work on. In the examples here, the methods use the value of the 
variable dt, but it exists and is only known in Main() and is thus not known in the other two methods. We 
need a mechanism that can transfer dt, when the methods are called, and that\u2019s exactly what parameters 
are used to.
Exam08
Arguments on the command line
When you create an application using Visual Studio, the Main() method has a parameter, that I have not 
used so far. It is used on the command line to transfer the arguments to a program. That is if you have 
a program called Exam08.exe, you can execute the program from the command line by typing
Exam08 a b c
where a, b and c are arguments to the program and are arbitrary strings separated by at least one space. 
There are no restrictions on the number of arguments and an argument need not be a single character 
as above, but may be any string. If the program does not do anything by the arguments they are ignored, 
and it is up to the program to address these arguments.
The example shows an application where the user must transfer two integers as arguments on the 
command line, and the program will then print out the quotient and modulus. The result could, for 
example be the following:
How to
The