您的位置:首页 > 移动开发 > IOS开发

iOS study Day6-计算器2

2014-03-25 15:53 162 查看
//要求使用面向对象的编程思想,编写一个程序:

//使其作为简单的打印计算器。该程序允许用户键入任何以下形式的表达式:

//number operator

//要求程序能识别以下运算符: + - * / s e,其中 s
是设置初始值

//以下是程序运行显示的示范操作:

//Begin Calculations

//s 10

//= 10.000000

/// 2

//= 5.000000

//- 55

//= - 50.000000

//s 100.25

//= 100.250000

//e 0

//= 100.250000

//End of Calculations

#import <Foundation/Foundation.h>

#import "Calc.h"
int main(int argc,
const char * argv[])
{

@autoreleasepool
{

bool notEnd = true;

Calc *deskcalc = [[Calc
alloc] init];

NSLog(@"Type in your expression.");
[deskcalc
clear];

while (notEnd)
{
[deskcalc
accumulator2];
}
}

return 0;
}

// Calc.h

#import <Foundation/Foundation.h>

@interface Calc : NSObject

{

double accmulator;
}

@property (nonatomic,assign)
double accmulator;

-(void)clear;
-(float)add:(double)value;
-(float)subtract:(double)value;
-(float)multiply:(double)value;
-(float)divide:(double)value;
-(double)accumulator2;
-(void)endit;

@end

//Calc.m

#import "Calc.h"

@implementation Calc
@synthesize accmulator;

-(void)setAccu:(double)value
{
accmulator = value;
NSLog(@"恢复初始值= %f",accmulator);
}
-(void)clear
{
accmulator =
0;
}
char operator;
double value2;
-(double)accumulator2
{
scanf("\n%c %lf",&operator,&value2 );

switch (operator)
{

case 's':
[self setAccu:value2];

break;

case '+':
[self add:value2];

break;

case '-':
[self subtract:value2];

break;

case '*':
[self multiply:value2];

break;

case '/':
[self
divide:value2];

break;

case 'e':

NSLog(@"= %f",
accmulator);

exit(0);

break;

default:

NSLog(@"输入错误,请重输");

break;
}

return 0;
}

-(float)add:(double)value
{

accmulator += value;

NSLog(@"= %f",accmulator);

return
accmulator;
}
-(float)subtract:(double)value
{

accmulator -= value;

NSLog(@"= %f",accmulator);

return
accmulator;
}
-(float)multiply:(double)value
{

accmulator *= value;

NSLog(@"= %f",accmulator);

return
accmulator;
}
-(float)divide:(double)value
{

accmulator /= value;

NSLog(@"= %f",accmulator);

return
accmulator;
}

-(void)endit
{

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: