Thread: Digital calculator :D

  1. #1
    deletedforumuser
    Guest

    Digital calculator :D

    Nothing. Just posting this for fun. it takes around 30 seconds to make.

    I accomplished alot of things. I'm learning Direct3d right now.

    Well, about 5 months ago. I was trying to make a small console digital calculator. I didn't even finished it and it was around 1000+ line of codes.

    Now, today, i just said, why not try the digital calculator again with my C++ skills.

    Haha, i just realized how easy it was.

    Here's the code.

    Code:
    
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    float value1;
    string variable = "";
    float value2;
    while(1)
    {
    cout<<">>";
    cin >> value1;
    cin >> variable;
    cin >> value2;
    cin.get();
    if(variable == "+")
    {
    cout <<value1 + value2;
    }
    else if(variable == "-")
    {
    cout <<value1 - value2;
    }
    else if(variable == "/")
    {
    cout <<value1 / value2;
    }
    else if(variable == "*")
    {
    cout <<value1 * value2;
    }
    cin.get();
    system("cls");
    }
    cin.get();
    return 0;
    }
    
    Oh well, back to Directx. Cya.

    By the way, if your wondering how to use it.
    Example:


    5 * 5
    5 + 5

    Dont forget the spaces.

    5(space) * (space) 5
    Last edited by kevinawad; 10-04-2008 at 10:34 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Just a comment - I think the "variable" should be more appropriately called the "operator" .

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You somehow also messed up indentation of the code you posted (but I do assume you properly indented the code when you wrote it).
    Simple, but effective. You can't ask for more, can you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think it would take no more lines in straight C.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It probably would not. It's a too simple application.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I think it would take no more lines in straight C.
    And the point is?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post
    I think it would take no more lines in straight C.
    I think the 1000+ lines vs. <100 lines was not C vs. C++, but 0 months experience vs. 5 months experience.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float mul(float a, float b) { return a*b; }
    float add(float a, float b) { return a+b; }
    float sub(float a, float b) { return a-b; }
    float div(float a, float b) { return a*b; }
    float safe(float a, float b, float (*func)(float,float)) { return (func)?func(a,b):NAN; }
    
    int main(void)
    {
      float (*operators)(float,float)[] = {mul,add,0,sub,0,div}, a, b;
      char op;
    
      printf(">>");
      while(sscanf("&#37;f %c %f", &a, &op, &b) == 3)
      {
        if(op >= '*' && op <= '/')
          printf("%f\n", safe(a, b, operators[op-'*']));
        else
          fputs("Invalid entry! (Example: 5 * 5)", stderr);
        printf(">>");
      }
    
      return 0;
    }
    Happy, nonoob?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. X509v3 digital certificate generator
    By CypherHackz in forum C Programming
    Replies: 1
    Last Post: 03-17-2008, 06:18 AM
  2. Digital Rights
    By MadCow257 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-21-2006, 07:05 PM
  3. Digital Tour in Flash
    By GanglyLamb in forum Tech Board
    Replies: 3
    Last Post: 03-01-2005, 11:58 AM
  4. The mistake of going to one of only two digital thaters in ohio
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-21-2002, 05:59 PM
  5. Wav file to digital value conversion
    By RpiMatty in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2001, 06:42 AM