0% found this document useful (0 votes)
16 views3 pages

MbCalculatrice Xamlcs

This document contains code for an Xamarin.Forms calculator app. It defines a content page class called MbCalculatrice that contains methods for handling button clicks to perform numeric and operation buttons and display results. On button clicks it performs the appropriate calculation and displays the result.

Uploaded by

Le Louarn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

MbCalculatrice Xamlcs

This document contains code for an Xamarin.Forms calculator app. It defines a content page class called MbCalculatrice that contains methods for handling button clicks to perform numeric and operation buttons and display results. On button clicks it performs the appropriate calculation and displays the result.

Uploaded by

Le Louarn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

MbCalculatrice.

xamlcs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace CalculsetPlus
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MbCalculatrice : ContentPage
{
public MbCalculatrice()
{
InitializeComponent();
}
private decimal firstnumber;
private string operatorName;
private bool isOperatorClicked;
double memo;
private void BtOne_Clicked(object sender, EventArgs e)
{
lblResult.Text = "1";
}
private void BtnCommon_Clicked(object sender, EventArgs e)
{
var button = sender as Button;

if (lblResult.Text == "0" || isOperatorClicked)


{
isOperatorClicked = false;
lblResult.Text = button.Text;
}
else
{
lblResult.Text += button.Text;
}
}
private void BtnC_Clicked(object sender, EventArgs e)
{
lblResult.Text = "0";
}
private void BtnX_Clicked(object sender, EventArgs e)
{
string number = lblResult.Text;
if (number != "0")
{
number = number.Remove(number.Length - 1);
if (string.IsNullOrEmpty(number))
{
lblResult.Text = "0";
}
else
{
lblResult.Text = number;
}
}
}
private void BtnCE_Clicked(object sender, EventArgs eventArgs)
{
lblResult.Text = "0";
}
private async void BtnPrct_Clicked(object sender, EventArgs e)
{
try
{
string number = lblResult.Text;
if (number != "0")
{
decimal percentValue = Convert.ToDecimal(number);
string result = (percentValue / 100).ToString("0.##");
lblResult.Text = result;
}
}
catch (Exception ex)
{
await DisplayAlert("Error", ex.Message, "Ok");
}
}

private void BtnInverse_Clicked(object sender, EventArgs e)


{
memo = 1 / float.Parse(lblResult.Text);
lblResult.Text = memo.ToString();
}
private void BtnRacine_Clicked(object sender, EventArgs e)
{
memo = float.Parse(Math.Sqrt(double.Parse(lblResult.Text)).ToString());
lblResult.Text = memo.ToString();
}
private void BtnCarre_Clicked(object sender, EventArgs e)
{
memo = Math.Pow((double.Parse(lblResult.Text)), 2);
lblResult.Text = memo.ToString();
}
private void BtnCommonOperation_Clicked(object sender, EventArgs e)
{
var button = sender as Button;
isOperatorClicked = true;
operatorName = button.Text;
firstnumber = Convert.ToDecimal(lblResult.Text);
}
private void BtnEgal_Clicked(object sender, EventArgs e)
{
try
{
decimal secondNumber = Convert.ToDecimal(lblResult.Text);
string finalresult = Calculate(firstnumber,
secondNumber).ToString("0.##");
lblResult.Text = finalresult;
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "Ok");
}
}
public decimal Calculate(decimal firstNumber, decimal secondNumber)
{
decimal result = 0;

if (operatorName == "+")
{
return firstNumber + secondNumber;
}
else if (operatorName == "-")
{
return firstNumber - secondNumber;
}
else if (operatorName == "*")
{
return firstNumber * secondNumber;
}
else if (operatorName == "/")
{
return firstNumber / secondNumber;
}
return result;
}
private void BtnPlusmoins_Clicked(object sender, EventArgs e)
{
decimal a = Decimal.Parse(lblResult.Text);
lblResult.Text = (-1 * a).ToString();
}

}
}

You might also like