计算器综合修改 #186
authorhcj <1224667075@qq.com>
Fri, 13 Sep 2024 04:07:14 +0000 (12:07 +0800)
committerhcj <1224667075@qq.com>
Fri, 13 Sep 2024 04:07:14 +0000 (12:07 +0800)
code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorModel.h
code/sdk/application/wearable/fd230app/nativeui/calculator/include/CalculatorView.h
code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorModel.cpp
code/sdk/application/wearable/fd230app/nativeui/calculator/src/CalculatorView.cpp
code/sdk/application/wearable/fd230app/nativeui/phone/src/PhoneDirView.cpp

index f3b884d..cbb79ae 100644 (file)
@@ -25,17 +25,17 @@ public:
     static CalculatorModel *GetInstance(void);
    
 
-    void number_push_stack(float);
-    float symbol_push_stack(char symbol);
-    float calculate_stack();
+    void number_push_stack(double );
+    void symbol_push_stack(char symbol);
+    double  calculate_stack();
     uint8_t get_stcak_size();
-    void  symbol_stack_top_uddata(float data);
+    void  symbol_stack_top_uddata(char data);
     void clean_stack();
 private:
     CalculatorModel();
     CalculatorModel(const CalculatorModel &);
     virtual ~CalculatorModel();
-    std::stack<float> numberStack;
+    std::stack<long double > numberStack;
     std::stack<char>  symbolStack;
    
 };
index 2b58c9a..99e983f 100644 (file)
@@ -25,6 +25,7 @@
 #include "PhoneService.h"
 #include "vector.h"
 #include "ChangeSliceListener.h"
+#include "cmsis_os2.h"
 
 namespace OHOS {
 
@@ -43,6 +44,11 @@ public:
     void symbol_text_updata_single_buf(const char *buf);
     void symbol_text_updata_calculate_buf(const char *buf);
 
+    void start_call_roll_timer();
+    void show_hint_label(bool data);
+
+    osTimerId_t call_roll_timer=NULL;
+
 private:
     uint8_t number_falg=0;
     bool button_init();
@@ -53,7 +59,7 @@ private:
 
     UIViewGroup *group{nullptr};
     UILabel *number{nullptr};
-
+    UILabelExt *hint{nullptr};
     UIView::OnClickListener* clickLeftListener{nullptr};
     UIView:: OnDragListener* dragListener{nullptr};
 
index 799bf7f..a161d49 100644 (file)
@@ -31,9 +31,9 @@ CalculatorModel::~CalculatorModel()
 }
 
 
-void CalculatorModel:: number_push_stack(float data)
+void CalculatorModel:: number_push_stack(double data)
 {
-    printf("%f\r\n",data);
+    printf("%lf\r\n",data);
     if(!symbolStack.empty())
     {
         if(symbolStack.top()=='-')
@@ -49,7 +49,7 @@ void CalculatorModel:: number_push_stack(float data)
 }
 
 
-void CalculatorModel:: symbol_stack_top_uddata(float data)
+void CalculatorModel:: symbol_stack_top_uddata(char data)
 {
     printf("%c\r\n",data);
     if(!symbolStack.empty())
@@ -64,16 +64,16 @@ uint8_t CalculatorModel::get_stcak_size()
     return numberStack.size();
 }
 
-float CalculatorModel:: symbol_push_stack(char symbol)
+void  CalculatorModel:: symbol_push_stack(char symbol)
 {
     printf("%c\r\n",symbol);
 
     if(numberStack.empty())
     {
-        return 0;
+        return ;
     }
     symbolStack.push(symbol);
-    return 0;
+    return ;
        
 }
 
@@ -90,17 +90,17 @@ void CalculatorModel:: clean_stack()
     }
 }
 
-float CalculatorModel:: calculate_stack()
+double  CalculatorModel:: calculate_stack()
 {
-    float sum=0;
+    double  sum=0;
     if(numberStack.empty())
     return sum;
-    float number_1= numberStack.top();
+    double  number_1= numberStack.top();
     numberStack.pop();
     sum=number_1;
     while (!numberStack.empty())
     {
-        float number_= numberStack.top();
+        double  number_= numberStack.top();
         numberStack.pop();
 
         if(symbolStack.empty())
@@ -120,9 +120,13 @@ float CalculatorModel:: calculate_stack()
         }
         if(symbol=='/')
         {
+            if(sum==0)
+            {
+                return sum;
+            }
             sum=number_/sum;
         }
-        printf("sum:%f\r\n",sum);
+        printf("sum:%lf\r\n",sum);
     }
     return sum;
 }
index ee01641..65f46ba 100644 (file)
@@ -9,6 +9,7 @@
 #include "calculator/include/CalculatorPresenter.h"
 #include "calculator/include/CalculatorModel.h"
 #include "wearable_log.h"
+#include <math.h>
 
 namespace OHOS {
 
@@ -43,11 +44,42 @@ CalculatorView::~CalculatorView()
         delete image1_;
         image1_=nullptr;
     }
+    if(hint)
+    {
+        delete hint;
+        hint=nullptr;
+    }
     if(group)
     {
         delete group;
         group =nullptr;
     }
+
+    if(call_roll_timer)
+    {
+        osTimerStop(call_roll_timer);
+        osTimerDelete(call_roll_timer);
+        call_roll_timer = NULL;
+        
+    }
+}
+static int8_t  roll_falg=0;//0.1s一次变换
+
+ static void  label_Timer_CallBack(int data)
+{
+    roll_falg--;
+    if(roll_falg<=0)
+    {
+        if(CalculatorView::GetInstance())
+        CalculatorView::GetInstance()->show_hint_label(false);
+
+        if(g_pMainView->call_roll_timer)
+        {
+            osTimerStop(g_pMainView->call_roll_timer);
+            osTimerDelete(g_pMainView->call_roll_timer);
+            g_pMainView->call_roll_timer = NULL;
+        }
+    }
 }
 
 CalculatorView *CalculatorView::GetInstance()
@@ -55,15 +87,53 @@ CalculatorView *CalculatorView::GetInstance()
     return g_pMainView;
 }
 
+
+void CalculatorView::show_hint_label(bool data)
+{
+   hint->SetVisible(data);
+   RootView::GetInstance()->Invalidate();
+}
+
+void CalculatorView::start_call_roll_timer()
+    {
+        uint32_t tick;
+        osStatus_t retTimer;
+        if(call_roll_timer)
+        {
+            roll_falg=10;
+        }
+        else
+        {
+            roll_falg=10;
+
+            call_roll_timer = osTimerNew((osTimerFunc_t)label_Timer_CallBack, osTimerPeriodic, NULL, NULL);
+            tick =100;// osMs2Tick(1000);
+            retTimer = osTimerStart(call_roll_timer, tick);
+            if (retTimer != osOK && call_roll_timer != NULL) {
+                osTimerDelete(call_roll_timer);
+                call_roll_timer = NULL;
+                return ;
+            }
+        }
+    }
+
+
 void CalculatorView::number_text_updata_buf(const char *buf)
 {
-    char new_buf[16]={0};
+    char new_buf[64]={0};
     const char*text_buf=NULL;
     if(number_falg==1)
     text_buf=number->GetText();
     else
     text_buf="";
-    sprintf_s(new_buf,16,"%s%s",text_buf,buf);
+    if(strlen(text_buf)>20)
+    {
+        //超出长度限制
+        start_call_roll_timer();
+        show_hint_label(true);
+        return;
+    }
+    sprintf_s(new_buf,64,"%s%s",text_buf,buf);
     number->SetText(new_buf);
     number_falg=1;
     RootView::GetInstance()->Invalidate();
@@ -72,7 +142,7 @@ void CalculatorView::number_text_updata_buf(const char *buf)
 
 void CalculatorView::symbol_text_updata_buf(const char *buf)
 {
-    char new_buf[16]={0};
+    char new_buf[64]={0};
     const char*text_buf=number->GetText();
     if(number_falg==0)
     {
@@ -85,7 +155,8 @@ void CalculatorView::symbol_text_updata_buf(const char *buf)
         RootView::GetInstance()->Invalidate();
         return;
     }
-    float  number_float=strtof(text_buf,NULL);
+    double number_float=strtod(text_buf,NULL);
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%lf %s",number_float,text_buf);
 
     CalculatorModel::GetInstance()->number_push_stack(number_float);
 
@@ -98,7 +169,7 @@ void CalculatorView::symbol_text_updata_buf(const char *buf)
 
     }
 
-    sprintf_s(new_buf,16,"%s",buf);
+    sprintf_s(new_buf,64,"%s",buf);
     number->SetText(new_buf);
     RootView::GetInstance()->Invalidate();
     
@@ -117,7 +188,7 @@ void CalculatorView::symbol_text_updata_single_buf(const char *buf)
     char new_buf[16]={0};
     const char*text_buf=number->GetText();
    
-    float number_float=strtof(text_buf,NULL);
+    double number_float=strtod(text_buf,NULL);
     if(buf[0]=='C')
     {
         CalculatorModel::GetInstance()->clean_stack();
@@ -126,15 +197,55 @@ void CalculatorView::symbol_text_updata_single_buf(const char *buf)
     }
     else if (buf[0]=='%')
     {
-        number_float=0.01*number_float;
-        sprintf_s(new_buf,16,"%.02f",number_float);
+    number_float=0.01*number_float;
+
+    double number_float_=number_float;
+    int decimal_places = 0;
+    double frac;
+
+    while (modf(number_float_ * pow(10, decimal_places), &frac) != 0.0f) 
+    {
+        decimal_places++;
+    }
+
+    printf("该浮点数的小数部分有 %d %lf 位。\n", decimal_places,number_float);
+    switch (decimal_places)
+    {
+        case 0:
+            sprintf_s(new_buf,64,"%.00lf",number_float);
+            break;
+        case 1:
+            sprintf_s(new_buf,64,"%.01lf",number_float);
+            break;
+        case 2:
+            sprintf_s(new_buf,64,"%.02lf",number_float);
+            break;
+        case 3:
+            sprintf_s(new_buf,64,"%.03lf",number_float);
+            break;
+        case 4:
+            sprintf_s(new_buf,64,"%.04lf",number_float);
+            break;
+        case 6:
+            sprintf_s(new_buf,64,"%.06lf",number_float);
+            break;
+        case 7:
+            sprintf_s(new_buf,64,"%.07lf",number_float);
+            break;
+        case 8:
+            sprintf_s(new_buf,64,"%.08lf",number_float);
+            break;
+        default:
+            sprintf_s(new_buf,64,"%.09lf",number_float);
+            break;
+    }
         number->SetText(new_buf);
         RootView::GetInstance()->Invalidate();
     }
     else if (buf[0]=='D')
     {
         number_float=0;
-        sprintf_s(new_buf,16,"%.02f",number_float);
+        sprintf_s(new_buf,16,"%.00lf",number_float);
         number->SetText(new_buf);
         number_falg=2;
         RootView::GetInstance()->Invalidate();
@@ -145,17 +256,60 @@ void CalculatorView::symbol_text_updata_single_buf(const char *buf)
 
 void CalculatorView::symbol_text_updata_calculate_buf(const char *buf)
 {
-    char new_buf[32]={0};
+    char new_buf[64]={0};
     const char*text_buf=number->GetText();
-    float number_float=strtof(text_buf,NULL);
+
+    double number_float=strtod(text_buf,NULL);
+
     CalculatorModel::GetInstance()->number_push_stack(number_float);
 
     number_float =CalculatorModel::GetInstance()->calculate_stack();
 
+    double number_float_=number_float;
+    int decimal_places = 0;
+    double frac;
+
+    while (modf(number_float_ * pow(10, decimal_places), &frac) != 0.0f) 
+    {
+        decimal_places++;
+    }
+
+    printf("该浮点数的小数部分有 %d %lf 位。\n", decimal_places,number_float);
+    switch (decimal_places)
+    {
+    case 0:
+        sprintf_s(new_buf,64,"%.00lf",number_float);
+        break;
+    case 1:
+        sprintf_s(new_buf,64,"%.01lf",number_float);
+        break;
+    case 2:
+        sprintf_s(new_buf,64,"%.02lf",number_float);
+        break;
+    case 3:
+        sprintf_s(new_buf,64,"%.03lf",number_float);
+        break;
+    case 4:
+        sprintf_s(new_buf,64,"%.04lf",number_float);
+        break;
+     case 6:
+        sprintf_s(new_buf,64,"%.06lf",number_float);
+        break;
+    case 7:
+        sprintf_s(new_buf,64,"%.07lf",number_float);
+        break;
+    case 8:
+        sprintf_s(new_buf,64,"%.08lf",number_float);
+        break;
+    default:
+        sprintf_s(new_buf,64,"%.09lf",number_float);
+        break;
+    }
+
     // CalculatorModel::GetInstance()->number_push_stack(number_float);
     number_falg=2;
 
-    sprintf_s(new_buf,32,"%.02f",number_float);
+    
     number->SetText(new_buf);
     RootView::GetInstance()->Invalidate();
 
@@ -194,18 +348,21 @@ bool CalculatorView::button_init()
             Button[showdial_i]->SetViewId(dial_id_str[showdial_i]);// Set style
             
             Button[showdial_i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919);
+            Button[showdial_i]->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff191919, UIButton::PRESSED);
             if(j>2)
             {
                 Button[showdial_i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFFFF8A36);
+                Button[showdial_i]->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xFFFF8A36, UIButton::PRESSED);
                 if(i==0)
                 {
                     Button[showdial_i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFFAAAAAA);
+                    Button[showdial_i]->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xFFAAAAAA, UIButton::PRESSED);
                 }
             }
 
             Button[showdial_i]->SetText(dial_id_str[showdial_i]);
             Button[showdial_i]->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 40);
-            Button[showdial_i]->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED);
+          
 
             group->Add(Button[showdial_i]);
             Button[showdial_i]->SetVisible(true);
@@ -240,7 +397,7 @@ bool CalculatorView::Label_init()
 
     // Set additional property
     number->SetText("");
-    number->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
+    number->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
     number->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_CENTER);
     number->SetRollSpeed(0);
     number->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 50);
@@ -254,8 +411,10 @@ bool CalculatorView::Label_init()
         image1_ = new UIImageView();
     }
     image1_->SetPosition(378, 85);         // widget's left position and top position
-    image1_->Resize(100, 100);          // widget's width and height
+    image1_->Resize(60, 60);          // widget's width and height
     image1_->SetViewId("D");// Set style
+    image1_->SetAutoEnable(false);
+    image1_->SetResizeMode(UIImageView::CENTER);
 
 
     char* DimagesPath = "/user/res/Vector.bin";
@@ -272,6 +431,31 @@ bool CalculatorView::Label_init()
     group->Add(image1_);
 
     image1_->SetVisible(true);
+
+
+      if (hint == nullptr) {
+        hint = new UILabelExt();
+    }
+    hint->SetPosition(109, 169);         // widget's left position and top position
+    hint->Resize(247, 60);          // widget's width and height
+    hint->SetViewId("label2");// Set style
+    hint->SetStyle(STYLE_BACKGROUND_COLOR, 0xff191919);
+    hint->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
+    hint->SetStyle(STYLE_BORDER_RADIUS, 25);
+    hint->SetStyle(STYLE_PADDING_BOTTOM, 8);
+    hint->SetStyle(STYLE_TEXT_FONT, 30);
+    hint->SetStyle(STYLE_LINE_HEIGHT, 20);
+
+    // Set additional property
+    hint->SetText("超过数字长度范围");
+    hint->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
+    hint->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
+    hint->SetRollSpeed(0);
+    hint->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
+    group->Add(hint);
+    hint->SetVisible(false);
+
+
     return true;
 }
 
index e198a81..a53ca83 100644 (file)
@@ -113,8 +113,8 @@ namespace OHOS
         if (label1_phone_name == nullptr) {\r
             label1_phone_name = new UILabelExt();\r
         }\r
-        label1_phone_name->SetPosition(0, 52);         // widget's left position and top position\r
-        label1_phone_name->Resize(466, 30);          // widget's width and height\r
+        label1_phone_name->SetPosition(40+20, 52);         // widget's left position and top position\r
+        label1_phone_name->Resize(466-80-40, 30);          // widget's width and height\r
         label1_phone_name->SetViewId("label1");// Set style\r
         label1_phone_name->SetStyle(STYLE_TEXT_FONT, 30);\r
 \r