0% found this document useful (0 votes)
27 views12 pages

Lab Assignment

The document contains code for two solutions: 1. A scientific calculator application with buttons to perform arithmetic operations and display the result. It uses variables to track the current operation and accumulated value. 2. An application that dynamically inserts rows of data into a DataGridView. The code defines the column names and adds multiple rows with product details on button click.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Lab Assignment

The document contains code for two solutions: 1. A scientific calculator application with buttons to perform arithmetic operations and display the result. It uses variables to track the current operation and accumulated value. 2. An application that dynamically inserts rows of data into a DataGridView. The code defines the column names and adds multiple rows with product details on button click.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPILER CONSTRUCTION

LAB ASSIGNMENT 01

SINAN AHMED IHSAN


SP21-BCS-019
Question 01:

Scientific Calculator
Solution:
OUTPUT
Code
using System;
using System.Windows.Forms;
namespace WinFormsApp1

{
public partial class Form1 : Form
{
private double accumulator = 0;
private char lastOperation;
public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(textBox1.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;
textBox1.Text = operation == '=' ? accumulator.ToString() : "0";

}
private void button7_Click(object sender, EventArgs e)
{
string number = (sender as Button).Text;
textBox1.Text = textBox1.Text == "0" ? number : textBox1.Text + number;
}

private void button12_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
textBox1.Text = textBox1.Text == "0" ? number : textBox1.Text + number;
}

private void button5_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button9_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button4_Click(object sender, EventArgs e)


{

string number = (sender as Button).Text;


if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button11_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button8_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button3_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button6_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button2_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button13_Click(object sender, EventArgs e)


{
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(textBox1.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;
textBox1.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void button14_Click(object sender, EventArgs e)


{
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(textBox1.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;
textBox1.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void button15_Click(object sender, EventArgs e)


{
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(textBox1.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;
textBox1.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void button16_Click(object sender, EventArgs e)


{
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(textBox1.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;
textBox1.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void button17_Click(object sender, EventArgs e)


{
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(textBox1.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;
textBox1.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void button10_Click(object sender, EventArgs e)


{
string number = (sender as Button).Text;
if (textBox1.Text == "0")
{
textBox1.Text = number;
}
else
{
textBox1.Text += number;
}
}

private void button18_Click(object sender, EventArgs e)


{
string functionName = (sender as Button).Text;

if (double.TryParse(textBox1.Text, out double input))


{
double result = Math.Sin(input);
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "Invalid Input";
}

private void button19_Click(object sender, EventArgs e)


{
string functionName = (sender as Button).Text;

if (double.TryParse(textBox1.Text, out double input))


{
double result = Math.Cos(input);
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "Invalid Input";
}

private void button20_Click(object sender, EventArgs e)


{
string functionName = (sender as Button).Text;

if (double.TryParse(textBox1.Text, out double input))


{
double result = Math.Tan(input);
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "Invalid Input";
}
}
}
}
Question 02:
Insert values into Data grid View at run time
SOLUTION:
OUTPUT

CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Labtask2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy