zoukankan      html  css  js  c++  java
  • 四则小运算

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string>
    bool CheckSymbol(char s)
    {
        char ch[4]={'+','-','*','/'};
        for (int i=0;i<4;i++)
        {
            if(s==ch[i]) return true;
        }
      return false;
    }
     
    bool CheckValid(const char p[10])
    {
       
      int start=-1,i=0;
      while (p[i])
       {
          if(isdigit(p[i])||p[i]=='.')
          {
            if(start==-1)
            {
               start=i;
            }
          }
          else
          {
             return false;
          }
          i++;
       }
             
      return true;
    }
    void main(void)
    {
        char b[10],ch;
        float a,c,digits[1];
        scanf("%f%c%s",&a,&ch,b);
        if(CheckValid(b))
        {
            if (CheckSymbol(ch))
            {
                digits[0]=atoi(b);
                if (ch=='+')
                    c=a+digits[0];
                if (ch=='-')
                    c=a-digits[0];
                if (ch=='*')
                    c=a*digits[0];
                if (ch=='/')
                {
                    if(digits[0]==0)printf("除数不能为零 ");
                    else c=a/digits[0];
                    return;
                }
                 
                printf("%5.2f%c%5.2f",a,ch,digits[0]),
                    printf("="),
                    printf("%5.2f ",c);
            }
            else printf("四则运算符号有误 ");
        }
        else printf("error,右操作含有非法字符 ");
     
     }

  • 相关阅读:
    【网络】【交换机】相关字符串处理
    python【telnet】使用
    【?】使用汇总
    【dbm】【功率】换算
    快速【kill进程】
    常用功能【时间log】
    python切换镜像源
    git报remote HTTP Basic Access denied错误的解决方法
    求取1到n的素数的数学思想——埃拉托斯特尼筛法
    MySQL count(*) 和 count(字段) 区别
  • 原文地址:https://www.cnblogs.com/xhl1234/p/5269672.html
Copyright © 2011-2022 走看看