⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calc.c

📁 C语言源码
💻 C
📖 第 1 页 / 共 3 页
字号:
                        if(digitkeyhit==TRUE)
                            {    strbuf[0]=strbuf[0]==' '?'-':' ';
                                 strcpy(txtscreen.caption,strbuf);
                            }
                        else if(runflag==TRUE) /*表示没有新的输入,就是在之前的计算结果上正负转换*/
                            {    num1=-num2;
                                 sprintf(txtscreen.caption,"%G",num1);
                            }
                        else   /*原数的正负互换*/
                            {    num1=-num1;
                                 sprintf(txtscreen.caption,"%G",num1);
                            }
                        runflag=FALSE;
                        if(ctnflag==FALSE) operatoror=0;
                        break;
                    case 12:   /*输入一个小数点*/
                        if(top==1) /*表示还没有输入数,保持0.状态*/
                            {  strbuf[top++]='0';
                                 strbuf[top++]='.';
                                 strbuf[top]=0;
                                 strcpy(txtscreen.caption,strbuf);
                                 digitkeyhit=TRUE;
                                 runflag=FALSE;
                                 pointflag=TRUE;
                                 if(ctnflag==FALSE) operatoror=0;
                            }
                        else if(top<15&&pointflag==FALSE)
                            {  strbuf[top++]='.';
                                 strbuf[top]=0;
                                 strcpy(txtscreen.caption,strbuf);
                                 digitkeyhit=TRUE;
                                 runflag=FALSE;
                                 pointflag=TRUE;
                                 if(ctnflag==FALSE) operatoror=0;
                            }
                        break;
                    case 13:case 14:case 15:case 16:   /* 四则运算加减乘除运算符 */
                        if(digitkeyhit) /*若此运算符之前已经输入了一个数*/
                            num1=atof(strbuf);
                        if(ctnflag) /*之前的输入中,已有运算符的输入*/
                            if(digitkeyhit==TRUE) /*如1+2+的情况*/
                                 DoubleRun(); /*先计算出1+2*/
                            else
                                 ;
                        else /*之前的输入中,没有运算符的输入,如:1+的情况*/
                            if(operatoror==0)
                                 num2=num1;
                            else
                                 ;
                        Resetbuf();
                        operatoror=key;
                        ctnflag=TRUE;
                        runflag=TRUE;
                        break;
                    case 17:case 18:case 19:   /*单运算 开方,百分比,倒数*/
                        if(digitkeyhit)
                            num1=atof(strbuf); /*num1保存当前操作数*/

                        SingleRun(key);
                        Resetbuf();
                        ctnflag=FALSE;
                        operatoror=0;
                        runflag=FALSE;
                        break;
                    case 20:    /*获取运算结果及等于操作*/
                        if(digitkeyhit) num1=atof(strbuf);
                        if(operatoror)
                            DoubleRun(); /*第一个操作数,保存在num2中*/
                        else
                            num2=num1;
                        Resetbuf();
                        ctnflag=FALSE;
                        runflag=TRUE;
                        break;
                    case 21:case 22:case 23:case 24: /*值的保存的一些操作*/
                        if(digitkeyhit) num1=atof(strbuf);
                        StoreSet(key);
                        Resetbuf();
                        break;
                    case 25:    /*删除数字的整数部分的最后一位数(BackSpace键)*/
                        if(top>1)
                            if(strbuf[--top]=='.')
                                {  if(strbuf[1]=='0'&&strbuf[2]=='.')
                                         strbuf[--top]=0;
                                     else
                                         strbuf[top]=0;
                                     pointflag=FALSE;
                                }
                            else
                                strbuf[top]=0;
                        operatoror=0;
                        ctnflag=FALSE;
                        runflag=FALSE;
                        strcpy(txtscreen.caption,strbuf);
                        break;
                    case 26:   /*清除当前显示的值(Del键)*/
                        Resetbuf();
                        num1=0;
                        strcpy(txtscreen.caption,strbuf);
                        TextBox(txtscreen);
                        break;
                    case 27:   /*清除所有的值,包括存储的,已经运算了的(ESC键)*/
                        Resetbuf();
                        num1=num2=0;
                        ctnflag=FALSE;
                        operatoror=0;
                        runflag=FALSE;
                        errorflag=FALSE;

                        strcpy(txtscreen.caption,"0.");
                        TextBox(txtscreen);

                        break;
                    case 0:
                        Unload();
                        break;
             }
         if(errorflag==FALSE)
             {  if(atof(txtscreen.caption)==0)
                        strcpy(txtscreen.caption,"0");
                    if(strchr(txtscreen.caption,'.')==NULL)
                        strcat(txtscreen.caption,".");
             }
         TextBox(txtscreen); /*显示txtscreen结构变量的值*/
    }

void DoubleRun() /*四则运算*/
    {  switch(operatoror)
             {      case 13: num2+=num1;break;  /*加*/
                    case 14: num2-=num1;break; /*减*/
                    case 15: num2*=num1;break; /*乘*/
                    case 16: if(num1==0)     /*除*/
                                         errorflag=TRUE;
                                     else
                                         num2/=num1;
                                     break;
             }
         if(errorflag)
             strcpy(txtscreen.caption,"Can't divide by zero!");
         else
             sprintf(txtscreen.caption,"%G",num2);
    }

void SingleRun(int key) /*单运算*/
    {  switch(key)
             {  case 17:  /*求开方*/
                        if(num1<0)
                             errorflag=TRUE;
                        else
                             num1=sqrt(num1);
                        break;
                    case 18:  /*求百分比*/
                             num1/=100;
                        break;
                    case 19: /*求倒数*/
                        if(num1==0)
                             errorflag=TRUE;
                        else
                             num1=1/num1;
                        break;
                }
         if(errorflag==TRUE)
             if(num1<0)
                 strcpy(txtscreen.caption,"Can't blower than zero!");
             else
                 strcpy(txtscreen.caption,"Can't equal to zero!");
         else
             sprintf(txtscreen.caption,"%G",num1);
    }

void StoreSet(int key)  /*记忆存储操作*/
   {  switch(key)
             {  case 21:   /*保存清除*/
                        store=0;
                        lblstore.caption[0]=0;
                        break;
                case 22:  /*取出保存的值*/
                        num1=store;
                        sprintf(txtscreen.caption,"%G",store);
                        runflag=FALSE;
                        if(ctnflag==FALSE) operatoror=0;
                        break;
                case 23:  /*保存当前数字*/
                        store=num1;
                        strcpy(lblstore.caption,"M");
                        break;
               case 24:  /*保存值与当前数字相加*/
                        store+=num1;
                        strcpy(lblstore.caption,"M");
                        break;
            }
         Label(lblstore);
    }


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -