0% found this document useful (0 votes)
90 views8 pages

LAB 1 Compiler Construction

This lab introduces C# syntax and data structures through activities on arithmetic operations, displaying data in datagridviews, and implementing stacks. The activities guide students to build a basic calculator application, populate and retrieve data from a datagridview, and implement a stack with push, pop and peek operations in C#. The lab aims to teach basic C# concepts to help students design compilers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views8 pages

LAB 1 Compiler Construction

This lab introduces C# syntax and data structures through activities on arithmetic operations, displaying data in datagridviews, and implementing stacks. The activities guide students to build a basic calculator application, populate and retrieve data from a datagridview, and implement a stack with push, pop and peek operations in C#. The lab aims to teach basic C# concepts to help students design compilers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Spring 2021 Session 2018-22

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

1. There are two kinds of buttons, numbers and operators.


2. There is a display that shows entries and results.
3. There is an accumulator variable to store the accumulated value.
4. There is a lastOperation variable to store the last operator, because we won't evaluate until
another operator is pressed.
When a number is pressed, it is added to the end of the number currently on the display. If
a 0 was on the display we replace it, just to look nicer.

If the C operator is pressed, we reset the accumulator to 0.


Otherwise, we perform the last operation against the accumulator and the currently entered
number. If there wasn't a lastOperation, then we must be starting a new calculation, so we set the
accumulator to the currentValue as the first operation.

Activity 2:
Display and retrieve data from data grid view.

Solution:
Displaying Data in Data Grid View

1) Create a windows Form application


2) Drag data grid view tool and a button from toolbox on form.
3) Copy and paste the code provided below behind the button.

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);
}
}
}

Data Retrieval from Data Grid View


1) First populate the data grid view with some data
2) You can retrieve data from data grid view via loops

for (int rows = 0; rows < dataGrid.Rows.Count; rows++)


{
for (int col= 0; col < dataGrid.Rows[rows].Cells.Count; col++)
{
string value = dataGrid.Rows[rows].Cells[col].Value.ToString();
}
}
example without using index
foreach (DataGridViewRow row in dataGrid.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
string value = cell.Value.ToString();
}
}

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

You might also like