EvilZone
Programming and Scripting => C - C++ => : parad0x March 10, 2013, 05:14:12 AM
-
I wrote this "printing" calculator as I am doing C nowdays. Here's the source code:
#include <stdio.h>
int main(){
int num, num1, result;
char ch;
printf("Begin Calculations\n");
printf("------ --------------\n");
scanf("%d %c", &num, &ch);
if(ch != 'S' && ch != 's'){
printf("Set Accumulator first!\n");
}
else {
printf("Accumulator set to %d.\n", num);
while(ch != 'E' && ch != 'e'){
scanf("%d %c", &num1, &ch);
switch(ch){
case '+':
printf("Add %d.\n", num1);
result = num + num1;
printf("=%d\tContents of Acumulator\n", result);
break;
case '-':
result = num - num1;
printf("Subtract %d.\n", num1);
printf("=%d\tContents of Acumulator\n", result);
break;
case '*':
result = num * num1;
printf("Multiply by %d.\n", num1);
printf("=%d\tContents of Acumulator\n", result);
break;
case '/':
if(num1 == 0){
printf("Cannot divide by zero.\n");
}
else{
printf("Divide by %d.\n", num1);
result = num / num1;
printf("=%d\tContents of Acumulator\n", result);
}
break;
case 's':
num = num1;
printf("Accumulator set to %d.\n", num);
break;
case 'S':
num = num1;
printf("Accumulator set to %d.\n", num);
break;
case 'e':
printf("End of program.\n");
break;
case 'E':
printf("End of program.\n");
break;
default:
printf("Unknown operator!\n");
break;
}
}
}
return 0;
}
Sample run:
Begin Calculations
------ --------------
15 s
Accumulator set to 15.
12 +
Add 12.
=27 Contents of Acumulator
15 -
Subtract 15.
=0 Contents of Acumulator
10 e
End of program.
-
Please indent your code if you share it anywhere with anyone if you want them to read it.
-
Please indent your code if you share it anywhere with anyone if you want them to read it.
Aye aye, Captain.;);)
-
you really should include instructions cause i didnt what to do, once i complied. also you should try to modularize your code. instead of putting all the code into 'main' break it up into functions.
(P.S. space out your code a little more, its indented but still hard to read)
-
you really should include instructions cause i didnt what to do, once i complied. also you should try to modularize your code. instead of putting all the code into 'main' break it up into functions.
(P.S. space out your code a little more, its indented but still hard to read)
I am not that level, I know only loops and conditions in C cause I started it yesterday from this book:Programming in C, 3rd Edition. (http://evilzone.org/ebooks/programming-in-c-3rd-edition/msg46563/?PHPSESSID=nf5i7o4og85cq6kvne00vnn3r7balenk#msg46563)
-
I am not that level, I know only loops and conditions in C cause I started it yesterday from this book:Programming in C, 3rd Edition. (http://evilzone.org/ebooks/programming-in-c-3rd-edition/msg46563/?PHPSESSID=nf5i7o4og85cq6kvne00vnn3r7balenk#msg46563)
*Borat voice* Very Nice. :D
i actually own that book hardcover its actually really good. youll learn alot from it.
-
i actually own that book hardcover its actually really good. youll learn alot from it.
I also own this book hardcover and now reading this book.;)