In: Computer Science
Create an ASP.Net Website using Visual Studio with C#:
Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results
Create 4 buttons to add, subtract, multiply, and divide
Prevent the user from entering text in the number fields
Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box
Create two additional buttons:
- One to store data - The store data will store the results into array
- One to display data - The display data will display the contents of the array (use 10 for the array size)
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new Asp.net website using C# is created using Visual Studio 2017 with name "simpleCalculatorUsingASPNet".This website contains a web page with name "Calculator.aspx".
Calculator.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calculator.aspx.cs" Inherits="simpleCalculatorUsingASPNet.Calculator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%-- title for web page --%>
<title>Simple Calculator</title>
<%-- internal stylesheet --%>
<style type="text/css">
.auto-style1 {
width: 39%;
height: 199px;
}
.auto-style2 {
text-align: center;
}
</style>
<%-- <script> is used for javascript --%>
<script>
//javascript function that allows only numbers to be entered
function numbersOnly(evt)
{
var et = event || evt;
var cCode = et.which || et.keyCode;
//allows only numbers to be entered and backspace
if (cCode > 31 && (cCode < 48 || cCode > 57))
{
return false;//return false
}
else {
return true;//return true
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table class="auto-style1">
<tr>
<td class="auto-style2" colspan="2"><strong>SIMPLE
CALCULATOR</strong></td>
</tr>
<tr>
<td>First Number</td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server"
onkeypress="return numbersOnly();"></asp:TextBox>
</td>
</tr>
<tr>
<td>Second Number</td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server"
onkeypress="return numbersOnly();"></asp:TextBox>
</td>
</tr>
<tr>
<td>Result</td>
<td>
<asp:TextBox ID="txtResult"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click"
Text="Add" />
<asp:Button ID="btnSubtract" runat="server"
OnClick="btnSubtract_Click" Text="Subtract" />
<asp:Button ID="btnMultiply" runat="server"
OnClick="btnMultiply_Click" Text="Multiply" />
<asp:Button ID="btnDivide" runat="server"
OnClick="btnDivide_Click" Text="Divide" />
<asp:Button ID="btnStoreResult" runat="server"
OnClick="btnStoreResult_Click" Text="Store Result" />
<asp:Button ID="btnDisplayResult" runat="server"
OnClick="btnDisplayResult_Click" Text="Display Result" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblResult"
runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
**********************************
Calculator.aspx.cs :
//namespace
using System;
//application namespace
namespace simpleCalculatorUsingASPNet
{
public partial class Calculator : System.Web.UI.Page
{
//declaring array to store result
static double[] resultArray = new double[10];
double result;
static int i = 0;
//addition button click
protected void btnAdd_Click(object sender, EventArgs e)
{
//make addition and display in the textbox
result = (int.Parse(txtFirstNumber.Text) +
int.Parse(txtSecondNumber.Text));
txtResult.Text = result.ToString();
}
//subtract button click
protected void btnSubtract_Click(object sender, EventArgs e)
{
//make subtraction and display in the textbox
result = (int.Parse(txtFirstNumber.Text) -
int.Parse(txtSecondNumber.Text));
txtResult.Text = result.ToString();
}
//multiply button click
protected void btnMultiply_Click(object sender, EventArgs e)
{
//make multiply and display in the textbox
result = (int.Parse(txtFirstNumber.Text) *
int.Parse(txtSecondNumber.Text));
txtResult.Text = result.ToString();
}
//divide button click
protected void btnDivide_Click(object sender, EventArgs e)
{
//checking second number
if(int.Parse(txtSecondNumber.Text)==0)
{
//if second number is zero
txtResult.Text = "Can not divide by zero";
}
else
{
//make division and display in the textbox
result = (int.Parse(txtFirstNumber.Text) /
int.Parse(txtSecondNumber.Text));
txtResult.Text = result.ToString();
}
}
//store result button click
protected void btnStoreResult_Click(object sender, EventArgs
e)
{
if(i<10)
{
resultArray[i] = int.Parse(txtResult.Text);//stores result in
array
i++;//increment value of i
}
}
//display result button click
protected void btnDisplayResult_Click(object sender, EventArgs
e)
{
string result = "";//variable to store array element
for (int i = 0; i < resultArray.Length; i++)
{
if(resultArray[i]!=0)
{
//display only when array element is not zero
result = result + resultArray[i] + " ";//concatenate each element
with array
}
}
//display result on the label
lblResult.Text = result;
}
}
}
======================================================
Output : Run application using F5 and will get the screen as shown below
Screen 1 :Calculator.aspx
Screen 2:Screen showing addition
Screen 3:Screen showing subtraction
Screen 4:Screen showing multiplication
Screen 5:Screen showing division
Screen 6 :Screen when display button is clicked
Screen 7:Screen when second number is zero
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.