Download ENGR/CS 101 Lecture 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Compiler wikipedia , lookup

C syntax wikipedia , lookup

Structured programming wikipedia , lookup

Go (programming language) wikipedia , lookup

Parsing wikipedia , lookup

One-pass compiler wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
ENGR/CS 101 CS Session
Lecture 2

Log into Windows/ACENET (reboot if in Linux)

Start Microsoft Visual Studio 2010



Lecture 2
Windows button -> All Programs ->
02-Programming -> Microsoft Visual Studio 2010
Choose C# as default environment, click Start
Visual Studio button
Wait for a long time... (next time should be faster)
ENGR/CS 101 Computer Science Session
1
Outline

Program specification

Program design

Programming languages

C# programming language


Types and variables

Assignment and expressions

Input and output
Using MS Visual Studio
Lecture 2
ENGR/CS 101 Computer Science Session
2
Program Specification

Work our way up to a GUI program to apply the
Vigenere cipher to a text file. Today's console
program will do the following:

Ask the user for an uppercase key letter (to
represent shift A-> key) and an uppercase letter to
encipher with this shift

Output the corresponding ciphertext letter

Example run (user input in bold):
Enter an uppercase key letter: I
Enter an uppercase letter to encipher: G
The corresponding ciphertext letter is: O
Lecture 2
ENGR/CS 101 Computer Science Session
3
Program Design


How will the program accomplish the
specifications?
Identify the data being used


shiftKey, plainLetter, cipherLetter
Write the steps an algorithm
1. Prompt user for shiftKey
2. Read shiftKey from the keyboard
3. Prompt user for plainLetter
4. Read plainLetter from the keyboard
5. Compute the cipherLetter
6. Output the cipherLetter to the screen
Lecture 2
ENGR/CS 101 Computer Science Session
4
Programming Languages

Syntax: What are the legal "sentences" in the
language?

Semantics: What do the "sentences" mean?

Compilers and interpreters enforce syntax.


Semantics determine whether the computation
is correct.
A program is not "working" if it gives the wrong
results!
Lecture 2
ENGR/CS 101 Computer Science Session
5
Compiling vs. Interpreting

Some languages are compiled with a program
called a compiler. Source code file is
translated into a machine code file.


Examples: C/C++, Java, Pascal, COBOL, Fortran
Other languages are interpreted. An
interpreter is a program that receives
programming language statements and
executes them directly.

Lecture 2
Examples: (original) BASIC, LISP, Prolog, LOGO
ENGR/CS 101 Computer Science Session
6
Source Code to Running Program

EDITOR -> programming language source file
-> COMPILER -> object file (+ libraries)
-> LINKER -> executable file
-> LOADER -> running program

Sometimes programs are run individually;
sometimes all work together in an Integrated
Development Environment (IDE)
Lecture 2
ENGR/CS 101 Computer Science Session
7
C# Programming Language

Developed by Microsoft for .NET framework

Syntax similar to C++ and Java

Semantics similar to Java

Object-oriented

Built-in support to make GUIs (Graphical User
Interfaces)
Lecture 2
ENGR/CS 101 Computer Science Session
8
Types and Variables



A variable is a named memory location that
holds a value. All memory is in bits.
A variable has a type that determines how the
bits are interpreted into a value. Numbers are
in binary. Characters are mapped to binary
numbers. E.g., ASCII or Unicode.
C# types include



Lecture 2
int for integers (e.g., 5, -25, 0)
char for characters (e.g. 'A', 'b', '7', '%')
string (e.g. "Hello!")
ENGR/CS 101 Computer Science Session
9
Types and Variables

Variables are declared by giving type and name

Syntax is: <type> <var1>, <var2>, ..., <varn>;

Examples:
char shiftKey, // key letter plainLetter, // user input
cipherLetter;// result
int shiftNumber; // # of shift places

// marks the beginning of a comment to the
end of the line
Lecture 2
ENGR/CS 101 Computer Science Session
10
Assignment and Expressions



Assignment means to store a value into a
variable.
Syntax is: <var> = <expression>;
The expression is evaluated and the result is
stored in the variable. An expression can be:




Lecture 2
A literal. E.g., 12 or 'A'
A variable. E.g., shiftNumber
A function call. (More on this later.)
An expression of one or more literals, variables, or
function calls.
ENGR/CS 101 Computer Science Session
11
Assignment and Expressions

Examples:
shiftKey = 'I';
plainLetter = shiftKey;
shiftNumber = shiftKey ­ 'A';
shiftKey =
char.Parse(System.Console.ReadLine());
Lecture 2
ENGR/CS 101 Computer Science Session
12
Computing the Cipher Letter


Assume that variable shiftKey holds the key
letter and variable plainLetter holds the
letter to be enciphered.
Since the alphabetic characters have
sequential mapping (i.e., 'A' is first, followed by
'B', etc.), the number of places to shift is the
key letter minus 'A'. In C# code, this is:
shiftNumber = shiftKey ­ 'A';
Lecture 2
ENGR/CS 101 Computer Science Session
13
Computing the Cipher Letter


To find the cipher letter, we determine the
index of plaintext letter (i.e., where in the
alphabet it is when we start counting at 0) using
a similar method, then add the shift number.
This will be the index of the ciphertext letter,
except that the number may be greater than 26.
To make it circular, we compute the modulus
with respect to 26. In code, this is:
(plainLetter ­ 'A' + shiftNumber) % 26

The modulus operator symbol is %
Lecture 2
ENGR/CS 101 Computer Science Session
14
Computing the Cipher Letter


Now we add this new index back to 'A' to find
the ciphertext letter.
However, C# is strict about types and is
unhappy that we are trying to add a number to
a character, so we have to tell the compiler to
treat 'A' as a number, then treat the result as a
character by casting. The code becomes:
cipherLetter = (char) (((plainLetter ­ 'A' + shiftNumber)
% 26) + (int) 'A');
Lecture 2
ENGR/CS 101 Computer Science Session
15
Output

C# output is done by calling a built-in function
that takes a string as an argument. There are
two forms:



System.Console.Write ( ) - displays string to screen
System.Console.WriteLine ( ) - displays string to
screen followed by a newline character
Strings can created by concatenating the items
to be displayed using +.
Lecture 2
ENGR/CS 101 Computer Science Session
16
Output

Examples:
System.Console.Write ("Enter an uppercase key letter: ");
System.Console.WriteLine ("The corresponding ciphertext letter is: " + cipherLetter); Lecture 2
ENGR/CS 101 Computer Science Session
17
Input

C# input is done by calling a built-in function
that has no arguments and returns a string that
contains what the user typed in.


System.Console.Read( )
Since all input is in the form of a string, it must
be converted to the appropriate type before
assignment. Each type has a Parse function
for this purpose. E.g.
shiftKey = char.Parse(System.Console.Read());
Lecture 2
ENGR/CS 101 Computer Science Session
18
Microsoft Visual Studio



Microsoft Visual Studio is an IDE for developing
applications for Windows in multiple
programming languages, including C#.
Free Express versions of each individual
compiler is available at
http://www.microsoft.com/express/Downloads/
EECS students can get the full version through
UE's MS Alliance program.
Lecture 2
ENGR/CS 101 Computer Science Session
19
Creating a Console Project




All C# code belongs to a project. Start with
New -> Project
Each project produces a particular kind of
application. We will be creating a console
application.
After selecting the console application template,
set the Name box to "cs101_program1", then
click OK.
A large text window and a Solution Explorer
panel will appear.
Lecture 2
ENGR/CS 101 Computer Science Session
20
MS VS Project Window
Program code goes here!
Lecture 2
ENGR/CS 101 Computer Science Session
21
Putting the Code Together
// Variable declarations
char shiftKey, // key letter plainLetter, // user input
cipherLetter;// result
int shiftNumber; // # of shift places
// Ask the user for key letter and a letter to encipher
System.Console.Write("Enter an uppercase key letter: ");
shiftKey = char.Parse(System.Console.ReadLine());
System.Console.Write
("Enter an uppercase letter to encipher: ");
plainLetter = char.Parse(System.Console.ReadLine());
Lecture 2
ENGR/CS 101 Computer Science Session
22
Putting the Code Together
// Compute the corresponding ciphertext letter
shiftNumber = shiftKey ­ 'A';
cipherLetter = (char)(((plainLetter ­ 'A' + shiftNumber)
% 26) + (int)'A');
// Display the result
System.Console.WriteLine
("The corresponding ciphertext letter is: "
+ cipherLetter);
Lecture 2
ENGR/CS 101 Computer Science Session
23
Building a Program


To build (i.e., compile) the program do Build ->
Build Solution
If there are no syntax errors, great! If there are
syntax errors, they will be listed at the bottom of
the screen. Correct and build again.
Lecture 2
ENGR/CS 101 Computer Science Session
24
Running a Console Program


To run the
program, do
Debug -> Start
Without
Debugging.
This will start
the console
window and run
the program.
Lecture 2
ENGR/CS 101 Computer Science Session
25