Author Topic: Printing Calculator  (Read 4602 times)

0 Members and 1 Guest are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Printing Calculator
« on: March 10, 2013, 05:14:12 am »
I wrote this "printing" calculator as I am doing C nowdays. Here's the source code:
Code: (C) [Select]
#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:
Code: [Select]
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.

« Last Edit: March 10, 2013, 05:42:21 am by parad0x »

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Printing Calculator
« Reply #1 on: March 10, 2013, 05:19:52 am »
Please indent your code if you share it anywhere with anyone if you want them to read it.
Easter egg in all *nix systems: E(){ E|E& };E

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Printing Calculator
« Reply #2 on: March 10, 2013, 05:43:52 am »
Please indent your code if you share it anywhere with anyone if you want them to read it.
Aye aye, Captain.;);)

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Printing Calculator
« Reply #3 on: March 10, 2013, 05:51:11 am »

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)
« Last Edit: March 10, 2013, 05:57:29 am by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Printing Calculator
« Reply #4 on: March 10, 2013, 06:01:23 am »
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.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Printing Calculator
« Reply #5 on: March 10, 2013, 06:20:37 am »
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.


*Borat voice* Very Nice.  :D 


i actually own that book hardcover its actually really good. youll learn alot from it.
The Bigger they are...The more likely you'll get your ass kicked

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Printing Calculator
« Reply #6 on: March 10, 2013, 06:28:50 am »
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.;)