Implementation of the iOS calculator

Source: Internet
Author: User

Simple implementation of the main.m//calcultor/* Calculator 1. Addition 2. Subtraction 3. Multiplication 4. Except method to achieve two number of operations */#import <Foundation/Foundation.h> #import "Calcultor.h" #import "Menu.h" int main (int argc, con        St char * argv[]) {Calcultor * cal = [[Calcultor alloc]init];        [Cal Setnumber1:10];        [Cal Setnumber2:10];        [Cal Setresult:0];    [Cal Setcalcultoroperator: ' + '];    [Cal initwithnumber1:10 andinitwithnumber2:10 andinitwithcalcultoroperator: ' + '];    [Cal counter];    NSLog (@ "%@", Cal);    Double number1; Double number2;    Double result;    char operator;    NSLog (@ "Please enter a formula such as a+b:\n");    scanf ("%lf", &number1);    scanf ("%c", &operator);    scanf ("%lf", &number2);    [Cal Setnumber1:number1];  [Cal Setnumber2:number2]; [Cal Setcalcultoroperator:operator];        [Cal Counter:operator];        NSLog (@ "You calculated the result is =%.lf", [Cal Counter:operator]); Menu * menu = [[Menu Alloc]init];///[menu ShowMenu]; return 0;}
#import <Foundation/Foundation.h> #import "Calcultor.h" @interface menu:nsobject{    calcultor *  _cal;} /** Display menu options */-(void) ShowMenu: (Calcultor *) cal; @end

#import "Menu.h" @implementation menu/** display menu options */-(void) ShowMenu: (Calcultor *) Cal; {    //    NSLog (@ "menu");//    NSLog (@ "1. Calculation +-*/");//    NSLog (@ "2. Calculate the square of a number");//    NSLog (@ "Please enter your options:");//    int selectnumber;//    scanf ("%d", &selectnumber);//    switch () {//Case://            break;//            / /        default://            break;//    }} @end

#import <Foundation/Foundation.h> @interface calcultor:nsobject{    @public    /** used to record the operand 1*/    double _ Number1;        /** used to record operands 2*/    double _number2;        /** used to record results */        double _result;        /** used to record operators */    char   _calcultoroperator;} @property declaration @property double number1; @property double number2; @property double result; @property Char calcultoroperator;/** addition */-(double) add;/** subtraction */-(double) subtract;/** multiplication */-(double) multiply;/** Division */-(double) divide ;-(double) simple implementation of the square;/** Calculator */-(double) counter: (char) calcultor;//-(void) InitWithNumber1: (double) number1 AndInitWithNumber2: (Double) number2 andinitwithcalcultoroperator: (char) calcultor; @end

#import "Calcultor.h" @implementation calcultor-(ID) init{if (self = [super init]) {_result = 0.0; } return self;}    /** addition */-(double) add{return (_number1 + _number2); NSLog (@ "%.1lf", _result);}    /** subtraction */-(double) subtract{return _number1-_number2; NSLog (@ "%.1lf", _result);}    /** multiplication */-(double) multiply{return _number1 * _NUMBER2; NSLog (@ "%.1lf", _result);}    /** Division */-(double) divide{return _number1/_number2; NSLog (@ "%.1lf", _result);} -(double) square{return _number1 * _NUMBER1;} Simple implementation of the/** Calculator */-(double) counter: (char) calcultor{//char calcultor;//local variable switch (calcultor) {case                ' + ': _result = [self add];            Break                Case '-': _result = [self subtract];            Break                Case ' * ': _result = [self multiply];            Break                Case '/': _result = [self divide];            Break               DefaultBreak } return _result;} -(void) InitWithNumber1: (double) number1 andInitWithNumber2: (double) number2 andinitwithcalcultoroperator: (char) calcultor//{//_number1 = number1;//_number2 = number2;//_calcultoroperator = calcultor;//}/**!!! Debugging with!!! */-(NSString *) description{return [NSString stringwithformat:@ "operand 1:%.1lf operand 2:%.1lf result:%.1LF operator:%c", _number1,_nu Mber2,_result,_calcultoroperator];} @end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of the iOS calculator

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.