Skip to content

Commit

Permalink
Merge pull request ALX-SE-Algorithmia#95 from amprebonne/amprebonne
Browse files Browse the repository at this point in the history
amprebonne - Demo Project Calculator
  • Loading branch information
dohoudaniel authored Aug 12, 2023
2 parents 6c5de7a + 37a4305 commit 6e3c1bd
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
15 changes: 15 additions & 0 deletions amprebonne/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This is a calculator program for the Algorithmia Demo project found @ <a href="https:blahblahblah">amprebonne</a>.

+ Specifications
- User-friendly prompt: The program provides clear instructions and
prompts to guide users in entering their arithmetic expressions.
- Error handling: The program handles invalid input, such as non-numeric
values or invalid operators, by displaying error messages and prompting
the user to enter valid input.
- Decimal precision: The program supports calculations with decimal numbers,
allowing users to specify the number of decimal places to round the result or
enter decimal numbers directly.

+ features (user prompt, input, error checks, operation)


Binary file added amprebonne/cal.exe
Binary file not shown.
40 changes: 40 additions & 0 deletions amprebonne/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void displayASCIIAnimation(void) {
// ASCII animation script
const char* animation = "<script src=\"http://www.qqpr.com/ascii/js/1044.js\"></script>";
for (int i = 0; i < 3; i++) {
printf("%s\n", animation);
}
}

int exit() {
char choice[10];

// Display results and ask user to advance or exit
printf("Results:\n");
// ... Perform some calculations or display previous results here ...

printf("\nDo you want to:\n");
printf("1. Advance (enter 'advance')\n");
printf("2. Exit (enter 'exit')\n");
printf("Your choice: ");
scanf("%s", choice);

if (strcmp(choice, "advance") == 0) {
// Call the main function or perform the next steps
printf("Advancing...\n");
// ... Call the main function or perform further steps here ...
} else if (strcmp(choice, "exit") == 0) {
printf("Exiting...\n");
// Display the ASCII animation
displayASCIIAnimation();
printf("Exiting program.\n");
} else {
printf("Invalid choice.\n");
}

return 0;
}
43 changes: 43 additions & 0 deletions amprebonne/init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "main.h"

void artDisplay(void) {
system("clear"); // Clear screen in a cross-platform way

// ASCII art for "calc"
const char* calc[] = {
"\n\n"
" __ __ | __ \n"
" / / | | / \n"
" |___/|_/|_/|__/|___/\n\n\n"
};

// Display "calc"
for (int i = 0; i < sizeof(calc) / sizeof(calc[0]); i++) {
printf("%s", calc[i]);
}

// Sleep to allow the user to see the art
usleep(1000000);
}

void printIntro(void) {
// Continue initial prompts and demo
printf("To carry out an arithmetic operation, ");
printf("type in two numbers and any of the following operators.\n");
sleep(2);
printf(" multiplication '*'\n");
printf(" addition '+'\n");
printf(" subtraction '-'\n");
printf(" division '/'\n");
sleep(3);
printf("\nHere's a quick example:\n");
usleep(500000);
printf("2*4\n");
sleep(2);
printf("\nIn the example given above, the numbers are '2' and '4'.\n");
printf("The operator used was '*'\n");
}
24 changes: 24 additions & 0 deletions amprebonne/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "main.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
float operand1, operand2;
char operator;

artDisplay(); // Display ASCII art
printIntro(); // Display introductory messages

int result = checker(&operand1, &operand2, &operator);

if (result == 0) {
int mathResult = mathsFunction(operand1, operand2, operator);
if (mathResult != 0) {
printf("Error in math calculation.\n");
}
} else {
printf("Error in input.\n");
}

return 0;
}
13 changes: 13 additions & 0 deletions amprebonne/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef MAIN_H
#define MAIN_H

#include <stdbool.h>

void printIntro(void);
void artDisplay(void);

bool isValidOperator(char op);
int checker(float *operand1, float *operand2, char *op);
int mathsFunction(float operand1, float operand2, char op);

#endif // MAIN_H
48 changes: 48 additions & 0 deletions amprebonne/math.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "main.h"

bool isValidOperator(char op) {
return op == '+' || op == '-' || op == '*' || op == '/';
}

int checker(float *operand1, float *operand2, char *op) {
printf("Enter arithmetic operation (e.g., 2*5): ");
if (scanf("%f%c%f", operand1, op, operand2) != 3) {
printf("Invalid input format. Please use the format: operand operator operand.\n");
return 1; // Input error
}

if (!isValidOperator(*op)) {
printf("Invalid operator. Please use +, -, *, or /.\n");
return 1; // Input error
}

return 0; // Success
}

int mathsFunction(float operand1, float operand2, char op) {
// Perform arithmetic operation based on operator
float result;
switch (op) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
printf("Division by zero is not allowed.\n");
return 1;
}
result = operand1 / operand2;
break;
}
printf("Result: %.2f\n", result);
return 0;
}

0 comments on commit 6e3c1bd

Please sign in to comment.