LAB 1 Compiler Construction
LAB 1 Compiler Construction
LAB # 01
Statement Purpose:
This Lab will provide you an introduction to C# syntax so that you can easily design
compiler in C#.
Activity Outcomes:
This Lab teaches you the following topics:
Doing arithmetic operations in C#
Displaying and retrieving values from DatagridView in C#
Implementing Stack data structure in C#
Instructor Note:
Here are some useful links for learning C#
http://www.c-sharpcorner.com/beginners/
http://www.completecsharptutorial.com/
http://csharp.net-tutorials.com/basics/introduction/
http://www.csharp-station.com/tutorial.aspx
Introduction
C# (pronounced as see sharp) is a multi-paradigm programming language encompassing strong
typing, imperative, declarative, functional, generic, object-oriented (class-based), and
component-oriented programming disciplines. It was developed by Microsoft within its .NET
initiative. C# is a general-purpose, object-oriented programming language. Its development team
is led by Anders Hejlsberg. The most recent version is C# 6.0 which was released in 2015. C# is
intended to be suitable for writing applications for both hosted and embedded systems, ranging
from the very large that use sophisticated operating systems, down to the very small having
dedicated functions.
Although C# applications are intended to be economical with regard to memory and processing
power requirements, the language was not intended to compete directly on performance and size
with C or assembly language.
Lab Activities:
Activity 1:
Design a calculator in C# Windows Form Application
Solution:
Open a Windows Form Application
Drag some buttons and a textbox from Toolbox onto Form. Example is
provided below:
Copy and paste the code provided below into your class.
using System;
using System.Windows.Forms;
namespace RedCell.App.Calculator.Example
{
public partial class Form1 : Form
{
private double accumulator = 0;
private char lastOperation;
public Form1()
{
InitializeComponent();
}
private void Operator_Pressed(object sender, EventArgs e)
{
// An operator was pressed; perform the last operation and store the
new
operator.
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(Display.Text);
switch (lastOperation)
{
case '+': accumulator += currentValue; break;
case '-': accumulator -= currentValue; break;
case '×': accumulator *= currentValue; break;
case '÷': accumulator /= currentValue; break;
default: accumulator = currentValue; break;
}
}
lastOperation = operation;
Display.Text = operation == '=' ? accumulator.ToString() : "0";
}
private void Number_Pressed(object sender, EventArgs e)
{
// Add it to the display.
string number = (sender as Button).Text;
Display.Text = Display.Text == "0" ? number : Display.Text + number;
}
}
}
Activity 2:
Display and retrieve data from data grid view.
Solution:
Displaying Data in Data Grid View
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";
string[] row = new string[] { "1", "Product 1", "1000"
};
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
dataGridView1.Rows.Add(row);
}
}
}
Activity 3:
Implement stack data structure.
Solution:
using System;
using System.Collections;
namespace CollectionsApplication
{
class Program
{
static void Main(string[] args)
{
Stack st = new Stack();
st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');
Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
Console.WriteLine();
st.Push('V');
st.Push('H');
Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
Console.WriteLine();
Console.WriteLine("Removing values ");
st.Pop();
st.Pop();
st.Pop();
Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
}
}
}
When the above code is compiled and executed, it produces the following
result:
Current stack:
W G M A
The next poppable value in stack: H
Current stack:
H V W G M A
Removing values
Current stack:
G M A
Home Activities:
Activity 1:
Implement scientific calculator.
Activity 2:
Insert values into Data grid View at run time.
Assignment:
Complete Home Activities before next Lab