首页 > 代码库 > 初学者如何做一个简单的计算器,代码分享
初学者如何做一个简单的计算器,代码分享
先新建一个类 startCalculator
声明如下
#import <Foundation/Foundation.h>
@interface StartCalculator : NSObject
//声明两个要计算的变量
@property float opValue1;
@property float opValue2;
//声明一个运算符
@property char op;
//普通方法
//- (float) gzyWorkAdd;
//
//- (float) gzyWorkSubtraction;
//
//- (float) gzyWorkMultiplication;
//
//- (float) gzyWorkDivision;
//用switch语句生成的方法
- (float) yes : (char) n;
@end
---------------------------------------------------------------------------------
实现部分如下
#import "StartCalculator.h"
@implementation StartCalculator
//用普通方法
//- (float) gzyWorkAdd
//{
// return self.opValue1 + self.opValue2;
//}
//
//- (float) gzyWorkSubtraction
//{
// return self.opValue1 - self.opValue2;
//}
//
//- (float) gzyWorkMultiplication
//{
// return self.opValue1 * self.opValue2;
//}
//
//- (float) gzyWorkDivision
//{
// return self.opValue1 / self.opValue2;
//}
//用switch进行判断
- (float) yes : (char) n
{
switch (n) {
case ‘+‘:
return self.opValue1 + self.opValue2;
case ‘-‘:
return self.opValue1 - self.opValue2;
case ‘*‘:
return self.opValue1 * self.opValue2;
case ‘/‘:
return self.opValue1 / self.opValue2;
default:
return 0;
}
}
@end
————————————————————————————————————————————————
viewcontroller部分的声明
#import <UIKit/UIKit.h>
#import "StartCalculator.h"
@interface ViewController : UIViewController
//用于给类定义一个scl的实例
{
StartCalculator *scl;
}
@end
——————————————————————————————————————
实现部分如下
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *coculatorLabel;
@end
@implementation ViewController
- (IBAction)didClick00:(id)sender {
//判断是否用了加减乘除
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 ;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick01:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 1;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 1;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 1;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 1;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 1;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick02:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 2;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 2;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 2;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 2;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 2;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick03:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 3;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 3;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 3;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 3;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 3;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick04:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 4;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 4;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 4;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 4;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 4;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick05:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 5;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 5;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 5;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 5;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 5;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick06:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 6;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 6;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 6;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 6;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 6;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick07:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 7;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 7;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 7;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 7;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 7;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick08:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 8;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 8;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 8;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 8;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 8;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)didClick09:(id)sender {
if (scl.op == ‘+‘) {
scl.opValue2 = scl.opValue2 * 10 + 9;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘-‘){
scl.opValue2 = scl.opValue2 * 10 + 9;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘*‘){
scl.opValue2 = scl.opValue2 * 10 + 9;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else if (scl.op == ‘/‘){
scl.opValue2 = scl.opValue2 * 10 + 9;
//显示在label里
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
else {
scl.opValue1 = scl.opValue1 * 10 + 9;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
}
}
- (IBAction)add:(id)sender {
scl.op = ‘+‘;
}
- (IBAction)sub:(id)sender {
scl.op = ‘-‘;
}
- (IBAction)multi:(id)sender {
scl.op = ‘*‘;
}
- (IBAction)division:(id)sender {
scl.op = ‘/‘;
}
- (IBAction)didClickEquarl:(id)sender {
float result = [scl yes:scl.op];
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",result];
scl.op = 0;
scl.opValue1 = 0;
scl.opValue2 = 0;
}
- (IBAction)AC:(id)sender {
scl.opValue1 = 0;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue1];
scl.opValue2 = 0;
self.coculatorLabel.text = [NSString stringWithFormat:@"%.1f",scl.opValue2];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scl = [[StartCalculator alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
结合storyboard链接这些代码即可