![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
simple calculator
Okie dokie, time to show off my beginner C skills :p
I made this calculator using simple commands, and basic C language. Using the functions and commands that I used, could you recommend any changes?
/*This is a calculator program. It may be edited, but the original author must be mentioned. *
*it was written on 2005-06-23 by Jesse H-K */
#include <stdio.h>
#define FALSE 0
#define TRUE !FALSE
int main()
{
float num1, num2, total;
char oper;
int trigger, choice, done;
done = FALSE;
while(!done)
{
printf("\n\n\n\t\t~~ C A L C U L A T O R ~~");
printf("\n\nThis will calculate the answer to a SIMPLE math question (eg: 2 * 3)");
printf("\nOperators are as follows:");
putchar('\n');
putchar('\n');
puts(" / = divide ");
puts(" * = multiply ");
puts(" + = add");
puts(" - = subtract");
putchar('\n');
printf("Enter the first number: ");
scanf("%f", &num1);
fflush(stdin);
printf("Enter the operator: "); /* enter +, -, etc */
oper = getchar();
fflush(stdin);
printf("Enter the second number: ");
scanf("%f", &num2);
if(oper == '+')
{
total = num1+num2;
printf("\n\n%.2f + %.2f = %.2f",num1, num2, total);
}
else if(oper == '-')
{
total = num1-num2;
printf("\n\n%.2f - %.2f = %.2f", num1, num2, total);
putchar('\n');
}
else if(oper == '*')
{
total = num1 * num2;
printf("\n\n%.2f * %.2f = %.2f", num1, num2, total);
putchar('\n');
}
else if(oper == '/')
{
total = num1 / num2;
printf("\n\n%.2f / %.2f = %.2f", num1, num2, total);
putchar('\n');
}
else
{
printf("Follow the directions directions next time");
}
putchar('\n');
putchar('\n');
printf("Repeat?\n\n");
puts("no = 1");
puts("yes = 2");
fflush(stdin);
printf("\tEnter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
done = TRUE;
break;
case 2:
break;
}
}
fflush(stdin);
return(0);
} /* end of program */comments? ![]() Last edited by Jessehk; May 24th, 2005 at 9:35 PM. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
looks gooddon't forget comments, those are very important too :p |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I would change the main section to this:
bool valid = true;
switch (oper)
{
case '+':
total = num1 + num2;
break;
case '-':
total = num1 - num2;
break;
case '*':
total = num1 * num2;
break;
case '/':
total = num1 / num2;
break;
default:
printf("Follow the directions next time!\n");
valid = false;
break;
}
if (valid)
{
printf("\n\n%.2f %c %.2f = %.2f\n", num1, oper, num2, total);
}The general code, however, is pretty damn good. |
|
|
|
|
|
#4 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
I'l think about those suggestions Ooble ( although I didn't know there was a case placeholder ) ![]() |
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Just realised you're using C. Change bool to int, and stick these two lines just underneath the headers:
#define true 1 #define false 0 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|