Here's a mini project to create a calculator that runs on console.
7 months(about 4 months active) into self-taught programming. 98% complete. Only bug is, its exiting may take about four confirmations before going through.
At the start of the program, addition is already active. Therefore, after every entry, the new input is added the old result, until a different operator is entered.
a = Adds = Subtract
d = Divide
m = Multiply
c = Clear
x = Exit
Its works for lower characters also.
*------------------------------------------------------------------------------------------------------------*
using System;
using System.Text;
public class ConsoleCalc0
{
public static void Main(string[] args)
{
string histDisp = default;
//string mainDisp = "0";
string input;
string[] ops = {"add", "minus", "multi", "div"};
string[] funcs = {"a","s","m","d","x"};
bool isNum;
//bool isOp;
bool closing = false;
bool refresh = true;
double newNum = 0;
double oldNum = 0;
double result = 0;
string operand = ops[0];
/*......................................................*/
Console.WriteLine("---Welcome to MiConsoleCalc---");
_App();
//////////////////////////////////////////////////
void _App()
{
Disp();
OnInput();
if (!closing)
{
_App();
}
else Exits();
}
void Disp()
{
Console.WriteLine("---------------------------------------");
Console.WriteLine(histDisp);
Console.WriteLine(operand+"........="+result+"\n"+oldNum);
}
void OnInput()
{
input = Console.ReadLine();
isNum = double.TryParse(input, out newNum);
if (isNum)
{
oldNum = newNum;
NumOp();
}
if (!isNum)
{
newNum = oldNum;
Functs();
}
if (histDisp==default)
{
refresh = true;
}
else refresh = false;
}
void Functs()
{
switch (input)
{
case "a":
operand = ops[0];
_App();
break;
case "s":
operand = ops[1];
_App();
break;
case "m":
operand = ops[2];
_App();
break;
case "d":
operand = ops[3];
_App();
break;
case "x":
closing = true;
break;
case "c":
newNum=0;
oldNum=0;
histDisp=default;
result = 0;
Console.WriteLine("..........reset..........");
break;
default: break;
}
}
void NumOp()
{
if(!refresh){
switch (operand)
{
case "add":
histDisp += " + " + oldNum.ToString();
result += newNum;
break;
case "minus":
histDisp += " - " + oldNum.ToString();
result -= newNum;
break;
case "multi":
histDisp += " * " + oldNum.ToString();
result *= newNum;
break;
case "div":
histDisp += " / " + oldNum.ToString();
result /= newNum;
break;
default: break;
}
}
if (refresh)
{
switch (operand)
{
case "add":
histDisp +=oldNum.ToString();
result = newNum;
break;
case "minus":
histDisp +=oldNum.ToString();
result = newNum;
break;
case "multi":
histDisp +=oldNum.ToString();
result = newNum;
break;
case "div":
histDisp +=oldNum.ToString();
result = newNum;
break;
default: break;
}
}
}
void Exits()
{
Console.WriteLine("...press any key to exit");
Console.ReadKey();
}
}
}
*--------------------------------------------------------------------------------------------*
Feel free to copy and experiment on. As an IT enthusiast, the best investment you can give yourself, is to learn to COde.
Download it Here
No comments:
Write Comments