Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58d9d88
commit a4ba76c
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'dart:math'; | ||
|
||
class Calc { | ||
// instance fiels | ||
double num1; | ||
double num2; | ||
|
||
// constructor | ||
Calc(double firstNum, double secondNum) { | ||
this.num1 = firstNum; | ||
this.num2 = secondNum; | ||
} | ||
|
||
//class method | ||
//class methods | ||
double add(double firstNum, double secondNum) { | ||
return firstNum + secondNum; | ||
} | ||
|
||
double subtract(double firstNum, double secondNum) { | ||
return firstNum - secondNum; | ||
} | ||
|
||
double multiply(double firstNum, double secondNum) { | ||
return firstNum * secondNum; | ||
} | ||
|
||
double expontentiate(double firstNum, double secondNum) { | ||
// Math.pow returns a double, so use typecasting to account for large numbers | ||
|
||
double exponentProd = pow(firstNum, secondNum); | ||
|
||
return exponentProd; | ||
} | ||
|
||
double divide(double firstNum, double secondNum) { | ||
return firstNum / secondNum; | ||
} | ||
|
||
static void backToMainMenu() { | ||
print("\nReturning to main menu...\n"); | ||
wholeThing(); | ||
} | ||
|
||
static void wholeThing() { | ||
// TODO : adding a Delay | ||
|
||
print(""" | ||
\nWelcome to the Dart Calculator! | ||
\n=============================== | ||
\nThis calculator performs operations on two numbers. | ||
\nPlease type your first number \nType 'exit' to exit at anytime! \n"""); | ||
|
||
String firstInput = | ||
stdin.readLineSync(encoding: Encoding.getByName('utf-8')); | ||
firstInput = firstInput.trim(); | ||
double firstNum = 0.0; | ||
|
||
if (firstInput == "exit") { | ||
exit(0); | ||
} else { | ||
firstNum = double.parse(firstInput); | ||
} | ||
|
||
print("\nGreat! Now input your second number.\n"); | ||
|
||
String secondInput = | ||
stdin.readLineSync(encoding: Encoding.getByName('utf-8')); | ||
secondInput = secondInput.trim(); | ||
double secondNum = 0.0; | ||
|
||
if (secondInput == "exit") { | ||
exit(0); | ||
} else { | ||
secondNum = double.parse(secondInput); | ||
} | ||
|
||
Calc calc1 = new Calc(firstNum, secondNum); | ||
|
||
print("""\nAwesome! Now, choose the operation you want. | ||
\nType + for addition | ||
\nType - for subtraction | ||
\nType * for multiplication | ||
\nType ** to raise the first number to the power of the second | ||
\nType / to divide"""); | ||
|
||
String operator = stdin.readLineSync(encoding: Encoding.getByName("utf-8")); | ||
operator = operator.trim(); | ||
|
||
switch (operator) { | ||
case "+": | ||
print("${calc1.num1} plus ${calc1.num2} equals = ${calc1.add(firstNum, secondNum)}"); | ||
backToMainMenu(); | ||
break; | ||
case "-": | ||
print("${calc1.num1} minus ${calc1.num2} equals = ${calc1.subtract(firstNum, secondNum)}"); | ||
backToMainMenu(); | ||
break; | ||
case "*": | ||
print("${calc1.num1} multiplied by ${calc1.num2} equals = ${calc1.multiply(firstNum, secondNum)}"); | ||
backToMainMenu(); | ||
break; | ||
case "**": | ||
print("${calc1.num1} raised to the power of ${calc1.num2} equals = ${calc1.expontentiate(firstNum, secondNum)}"); | ||
backToMainMenu(); | ||
break; | ||
case "/": | ||
print("${calc1.num1} divided by ${calc1.num2} equals = ${calc1.divide(firstNum, secondNum)}"); | ||
backToMainMenu(); | ||
break; | ||
case "exit": | ||
exit(0); | ||
break; | ||
default: | ||
print("Input not recognized."); | ||
backToMainMenu(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'calc.dart'; | ||
|
||
void main(List<String> args) { | ||
Calc.wholeThing(); | ||
} |