oc带顺序连算不带优先级计算器(作为备忘,完成度:95%)
来源:互联网 发布:反转故事知乎 编辑:程序博客网 时间:2024/06/05 18:36
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutletUILabel *res;
@property(nonatomic,assign)double op1;
@property(nonatomic,assign)double op2;
@property(nonatomic,assign)int op;
@property(nonatomic,assign)char sign;
@end
@implementation ViewController
- (IBAction)onClick:(UIButton*)sender {
static BOOL p=NO;//判断是否第二个操作数
static BOOL q=NO;//小数操作
static int i=0;
static double a=0;
static double b=0;
static double c=0;
static double d=0;
double r=0;
switch(sender.tag)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
if(NO==p)
{
if(YES==q)
{
i++;
c=self.op1+(double)sender.tag/pow(10,i);
self.op1=c;
self.res.text=[NSStringstringWithFormat:@"%g",self.op1];
}else{
a=a*10+sender.tag;
self.op1=a;
self.res.text=[NSStringstringWithFormat:@"%g",self.op1];
}
}else{
if(YES==q)
{
i++;
d=self.op2+(double)sender.tag/pow(10,i);
self.op2=d;
self.res.text=[NSStringstringWithFormat:@"%g",self.op2];
}else{
b=b*10+sender.tag;
self.op2=b;
self.res.text=[NSStringstringWithFormat:@"%g",self.op2];
}
}
break;
case 10://+
p=YES;
q=NO;
i=0;
//判断上一次的运算符
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='+';
break;
case 11://-
p=YES;
q=NO;
i=0;
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='-';
break;
case 12://*
p=YES;
q=NO;
i=0;
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='*';
break;
case 13:// /
p=YES;
q=NO;
i=0;
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
self.op2=0;
b=0;
}
self.sign='/';
break;
case 14://=
if('+'==self.sign)
{
r=self.op1+self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
}
if('-'==self.sign)
{
r=self.op1-self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
}
if('*'==self.sign)
{
r=self.op1*self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
}
if('/'==self.sign)
{
r=self.op1/self.op2;
self.res.text=[[NSStringalloc] initWithFormat:@"%g",r];
self.op1=r;
}
self.sign='\0';
self.op2=0;
b=0;
break;
case 15://AC
p=NO;
q=NO;
a=0;
b=0;
c=0;
d=0;
i=0;
self.op1=0;
self.op2=0;
self.sign='\0';
self.res.text=[NSStringstringWithFormat:@"0"];
break;
case 16://.
q=YES;
break;
case 17://%
self.op1=self.op1/100;
self.res.text=[NSStringstringWithFormat:@"%g",self.op1];
break;
case 18://±
self.op1=-1*self.op1;
self.res.text=[NSStringstringWithFormat:@"%g",self.op1];
break;
}
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- oc带顺序连算不带优先级计算器(作为备忘,完成度:95%)
- 关于一个oc写的命令行版的通讯录程序(代码贴过来无缩进,作为备忘,代码完成度:90%)
- 嵌入式 LINUX 大作业 带优先级 计算器
- 关于以前写过的一个二叉树的代码 (作为备忘,完成度90%)
- android(安卓)实现计算器程序,带优先级算法
- AndroidStudio的简单计算器(带优先级)开发小程序~
- 运用递归实现计算器加减乘除带括号优先级算法
- 每日20行之4~~~两种方法java实现简易计算器 附带复杂计算器代码(优先级顺序)
- IOS小程序之带优先级判断的具备多元计算的计算器
- 用C实现一个计算器(带小数点和括号以及运算优先级)
- 基本功能计算器算法(优先级)
- 九度1019简单计算器到带括号的计算器
- 使用Python作为计算器
- OC 优先级排序方法
- OC 计算器(初级)
- 简单计算器(OC)
- OC学习备忘
- OC学习备忘
- VS中C++对象的内存布局
- UDT 分析
- NAT在NDIS中间层驱动中的实现
- 分治:棋盘覆盖的java程序实现
- 关于排序的稳定性与不稳定性
- oc带顺序连算不带优先级计算器(作为备忘,完成度:95%)
- Hackintosh Yosemite(10.10.4) on Lenovo M58p
- 最新的全网速下载Android Sdk更新与下载
- C++_如何得到类内字符串内容
- mysql的查询、子查询及连接查询
- ip_queue内核通信
- 整合spark和hive
- 对象的简单内存分析
- 黑马程序员--Java_IO流(二)