C + + arithmetic simple design

Source: Internet
Author: User

If you want to use C + + to write a simple arithmetic program, it is difficult to believe that people, this is not easy? Then in less than five minutes, rinsed, the following code, a debugging, no problem .....

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     DoubleA =0.0, B =0.0;6     Doubleresult =0.0;7     Charoper;8cout <<"Please enter two number:" ;9Cin >> a >>b;Tencout <<"Please enter an operator:"; oneCIN >>oper; a     Switch(oper) -     { -      case '+': result = a + b; break; the      case '-': result = a-b; break; -      case '*': result = A * b; break; -      case '/': result = a/b; break; -     default: +cout <<"without this operator"<<endl; -          break; +     } acout <<"The result of the operation Is:"<< result <<endl; atSystem"Pause"); -     return 0; -}

indeed, The above code is not a problem, I believe that careful friends will be in the division operation to add a pair of the denominator is a zero judgment ...

so, What's the difference between C + + and other languages if the program is this way? Such a powerful C + +, let a four algorithm simple into such a son, but also really sorry for its flexibility and Complexity.

so, isn't it interesting to make the program look more complicated? of course, it's all about looking complicated ...

Since it's more complicated, we'll get a class Out.

#ifndef Mycalc_h_#defineMycalc_h_classmycalc{ public:    /** Return value: operation result * Parameter: operator*/    DoubleGetResult (Const Char&oper); /** function: return operand a*/    DoubleGetnumbera ()Const; /** function: return operand b*/    DoubleGetnumberb ()Const; /** Function: Set the value of operand a*/    voidSetnumbera (Const Double&a); /** Function: Set the value of operand b*/    voidSetnumberb (Const Double&b);    Mycalc (); Mycalc (DoubleADoubleb): M_numa (a), m_numb (b) {}////initialize list, Initialize two number    Virtual~Mycalc ();Private:    Doublem_numa;////operand a    Doublem_numb;////operand b};#endif
myCalc.h

Since the class has come out, the implementation must not be run off, so go on

1#include"myCalc.h"2#include <iostream>3 4 DoubleMycalc::getresult (Const Char&Oper)5 {6     Switch(oper)7     {8      case '+': 9         returnGetnumbera () +Getnumberb ();Ten          break; one      case '-':  a         returnGetnumbera ()-Getnumberb (); -          break; -      case '*':  the         returnGetnumbera () *Getnumberb (); -          break; -      case '/':  -         if(0.0==Getnumberb ()) +         { -Std::cerr <<"Divsion by Zero"<<std::endl; +             return 0; a         } at         returnGetnumbera ()/Getnumberb (); -          break; -  -     default: -Std::cerr <<"there is no operator \ '"<< Oper <<"\ '"<<std::endl; -          break; in     } -     return 0; to } +  - DoubleMycalc::getnumbera ()Const the { *     returnm_numa; $ }Panax Notoginseng DoubleMycalc::getnumberb ()Const - { the     returnm_numb; + } a  the  + voidMycalc::setnumbera (Const Double&A) - { $M_numa =a; $ } -  - voidMycalc::setnumberb (Const Double&B) the { -M_numb =b;Wuyi } the  - Mycalc::mycalc () wu { - } aboutmycalc::~Mycalc () $ { -}
MyCalc.cpp

well, The class has been written, so does it look more "tall"?

The task is done, so let's try this piece of Code.

1#include <iostream>2#include"myCalc.h"3 using namespacestd;4 intMain ()5 {6     DoubleNumA =0.0, NumB =0.0;7     CharOper =0;8cout <<"Enter the Numbers:";9CIN >> NumA >>numB;Tencout <<endl; onecout <<"Enter an operator:"; aCIN >>oper; - mycalc mycalc (numA, numB); -cout <<"Result is:"<< Mycalc.getresult (oper) <<endl; theSystem"Pause"); -     return 0; -}
Maincalc

This is the end of the program, using only the knowledge of the tip of the C + + iceberg, and there are a number of areas where the program deserves Improvement. You are welcome to Exchange.

C + + arithmetic simple design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.