三个计算器

第一个计算器

计算器
功能:
支持算数优先级,
支持指数运算 pow(double x,double y) 表示x^y,
以e为底的对数运算 log(),
以10为底的对数运算 log10(),
三角运算 sin(),cos(),tan(),
反正弦 asin(),acos(),atan(),
双曲正弦 sinh(),cosh(),tanh(),
开平方根 sqrt()
设计思路:
先将所输入的式子全部看成字符串,以加减乘除符号为分界线,将式子分割成许多单元;
以 %[.()0123456789abcdefghijklmnopqrstuvwxyz] 的方式将每个单元的字符串内容
保存起来再进入 FinalValue() 每个单元转换为最终的double型的值;
如此后式子就可以当成全部为double型变量处理。
注意:
()括号内全为double型,
不支持嵌套计算 比如无法计算sin(pow(a,b)),
由于思路是以加减乘除为分界线,所以输入的double型变量必须为正数。。。。。。。

#include
#include
#include
#include

#define M 100
#define N 10

#define sin(x) sin(3.1415926*(x)/180.0)
#define cos(x) cos(3.1415926*(x)/180.0)
#define tan(x) tan(3.1415926*(x)/180.0)
#define asin(x) asin(3.1415926*(x)/180.0)
#define acos(x) acos(3.1415926*(x)/180.0)
#define atan(x) atan(3.1415926*(x)/180.0)
#define sinh(x) sinh(3.1415926*(x)/180.0)
#define cosh(x) cosh(3.1415926*(x)/180.0)
#define tanh(x) tanh(3.1415926*(x)/180.0)



double FinalValue(char data[]);


double FinalValue(char data[]){
    char func[N];
    double func_num, func_num1;
    char cfunc_num[N]={'\0'},cfunc_num1[N]={'\0'};
    
    double final_value;
    sscanf(data, "%[0123456789]", func);//判断data中开头的是否为数字

    if(strlen(func)!=0){
        final_value = atof(data);//atof函数将字符串转换为double型
    }
    else{
        sscanf(data, "%[^(]", func);

        if(strcmp(func,"pow\0")==0){
            sscanf(data, "%*[^(](%[^,],%[^)]", cfunc_num, cfunc_num1);
            func_num = atof(cfunc_num);
            func_num1 = atof(cfunc_num1);
            final_value = pow(func_num, func_num1);
            return final_value;
        }

        sscanf(data,"%*[^(](%[^)]",cfunc_num);
        func_num = atof(cfunc_num);

        if (strcmp(func, "sin\0") == 0)
            final_value = sin(func_num);
        else if (strcmp(func, "cos\0") == 0)
            final_value = cos(func_num);
        else if (strcmp(func, "tan\0") == 0)
            final_value = tan(func_num);
        else if (strcmp(func, "asin\0") == 0)
            final_value = asin(func_num);
        else if (strcmp(func, "acos\0") == 0)
            final_value = acos(func_num);
        else if (strcmp(func, "atan\0") == 0)
            final_value = atan(func_num);
        else if (strcmp(func, "sinh\0") == 0)
            final_value = sinh(func_num);
        else if (strcmp(func, "cosh\0") == 0)
            final_value = cosh(func_num);
        else if (strcmp(func, "tanh\0") == 0)
            final_value = tanh(func_num);
        else if (strcmp(func, "sqrt\0") == 0)
            final_value = sqrt(func_num);
        else if (strcmp(func, "log\0") == 0)
            final_value = log(func_num);
        else if (strcmp(func, "log10\0") == 0)
            final_value = log10(func_num);
        else {
            printf("Exist wrong function or wrong writing of function.");
            exit(1);
        }
    }
    return final_value;
}
   


int main()
{
    double num[M] = {0};
    char cStart_num[N]={'\0'};//最开头的数。
    char data[N]={'\0'};//表示以运算符(+-*/)分隔开的每个单元,先将每个单元以字符串的形式储存起来,并进行后续分析。
    double unit;//表示以运算符(+-*/)分隔开的每个单元的最终值。比如sin90°的最终值是1,11的最终值是11.
    double sum;
    char c;//运算符


    while(scanf("%[.,()0123456789abcdefghijklmnopqrstuvwxyz]",cStart_num)!='\n')
    {
        
        num[0]=FinalValue(cStart_num);
        int k=1;
        while(scanf("%c",&c)!='\n')
        {
           if(c=='\n')
                break;
            else
            {
            scanf("%[.,()0123456789abcdefghijklmnopqrstuvwxyz]",data);
            unit = FinalValue(data);
            switch(c)
            {
            case'+':
                k++;
                num[k-1]=unit;
                break;
            case'-':
                k++;
                num[k-1]=-unit;
                break;
            case'*':
                num[k-1]*=unit;
                break;
            case'/':
                num[k-1]/=unit;
                break;

            }
            }
       
        
        } 
        int i;
        sum=0;
        for(i=0;i

第二个计算器

支持优先级(包括算术优先级和括号优先级),支持负数运算,支持小数运算,不支持特殊函数运算(如三角函数等)。代码写得很丑。。。。

#include 
#include 
#include 

#define MAX_LEN 100

typedef enum { OK = 1, ERROR = 0 } Status;

float BasicOperate(char operationalFormula[]);

Status MidOperationalFormula(char *dst, char *src, int n, int m);

Status LeftOperationalFormula(char *dst, char *src, int n);

int NumOfBrackets(char operationalFormula[], int len);

float BasicOperate(char operationalFormula[]) {
  float num[MAX_LEN] = {0};
  char *tempOperationalFormula;
  char operator;
  float unit = 0;
  char temp;
  int k = 1;
  int i;
  int nn;
  float sum = 0;

  nn = strlen(operationalFormula);
  operationalFormula[nn] = 'S';

  tempOperationalFormula = operationalFormula;

  sscanf(tempOperationalFormula, "%c", &temp);
  if (temp == '-') {
    tempOperationalFormula += 1;
    sscanf(tempOperationalFormula, "%g", &unit);
    tempOperationalFormula = strpbrk(tempOperationalFormula, "+-*/S");
    num[0] = -unit;
  } else {
    sscanf(tempOperationalFormula, "%g", &unit);
    tempOperationalFormula = strpbrk(tempOperationalFormula, "+-*/S");
    num[0] = unit;
  }

  sscanf(tempOperationalFormula, "%c", &operator);
  tempOperationalFormula += 1;

  while (operator!= 'S') {

    sscanf(tempOperationalFormula, "%c", &temp);
    if (temp == '-') {
      tempOperationalFormula += 1;
      sscanf(tempOperationalFormula, "%g", &unit);
      tempOperationalFormula = strpbrk(tempOperationalFormula, "+-*/S");
      unit = -unit;
    } else {
      sscanf(tempOperationalFormula, "%g", &unit);
      tempOperationalFormula = strpbrk(tempOperationalFormula, "+-*/S");
    }

    switch (operator) {
    case '+':
      k++;
      num[k - 1] = unit;
      break;

    case '-':
      k++;
      num[k - 1] = -unit;
      break;

    case '*':
      num[k - 1] *= unit;
      break;

    case '/':
      if (unit == 0) {
        printf("Illegal Input!\n");
        break;
      }
      num[k - 1] /= unit;
      break;
    }

    sscanf(tempOperationalFormula, "%c", &operator);
    tempOperationalFormula += 1;
  }
  for (i = 0; i < k; i++) {
    sum += num[i];
  }
  return sum;
}

Status LeftOperationalFormula(char *dst, char *src, int n) {
  char *p = src;
  char *q = dst;

  while (n--)
    *(q++) = *(p++);
  *(q++) = '\0';
  return OK;
}

Status MidOperationalFormula(char *dst, char *src, int n, int m) {
  char *p = src;
  char *q = dst;
  p += m;
  while (n--) {
    *(q++) = *(p++);
  }
  *(q++) = '\0';
  return OK;
}

int NumOfBrackets(char operationalFormula[], int len) {
  int ret = -1;
  int i;
  int numOfLeftBrackets = 0;
  int numOfRightBrackets = 0;
  for (i = 0; i < len; i++) {
    if (operationalFormula[i] == '(') {
      numOfLeftBrackets++;
    }
  }
  for (i = 0; i < len; i++) {
    if (operationalFormula[i] == ')') {
      numOfRightBrackets++;
    }
  }
  if (numOfLeftBrackets == numOfRightBrackets) {
    ret = numOfRightBrackets;
  }
  return ret;
}

int main(void) {
  char operationalFormula[MAX_LEN + 1];
  float sum, tempSum;
  char tempValue[MAX_LEN + 1];
  int n;
  int len;
  char leftOperationalFormula[MAX_LEN + 1],
      basicOperationalFormula[MAX_LEN + 1], *rightOperationalFormula;
  char *leftBracket, *rightBracket;

  while (1) {

    scanf("%s", operationalFormula);
    len = strlen(operationalFormula);
    n = NumOfBrackets(operationalFormula, len);
    if (n < 0) {
      printf("Illegal opeereational formula!\n");
      continue;
    }
    printf("\n");
    while (n--) {
      leftBracket = strrchr(operationalFormula, '(');
      rightBracket = strchr(operationalFormula, ')');

      if (leftBracket - rightBracket > 0) {
        rightBracket = strchr(leftBracket, ')');
      }
      LeftOperationalFormula(leftOperationalFormula, operationalFormula,
                             leftBracket - operationalFormula);
      printf("leftOperationalFormula=  %s\n", leftOperationalFormula);
      rightOperationalFormula = rightBracket + 1;
      printf("rightOperationalFormula=  %s\n", rightOperationalFormula);
      MidOperationalFormula(basicOperationalFormula, operationalFormula,
                            (rightBracket - operationalFormula) -
                                (leftBracket - operationalFormula) - 1,
                            leftBracket - operationalFormula + 1);
      printf("basicOperationalFormula=  %s\n", basicOperationalFormula);
      tempSum = BasicOperate(basicOperationalFormula);
      sprintf(tempValue, "%g", tempSum);
      printf("tempValue=  %s\n", tempValue);
      strcpy(operationalFormula, leftOperationalFormula);
      strcat(operationalFormula, tempValue);
      strcat(operationalFormula, rightOperationalFormula);
      printf("operationalFormula=  %s\n\n", operationalFormula);
    }
    sum = BasicOperate(operationalFormula);
    printf("%g", sum);

    printf("\n");
    fflush(stdin);
  }

  // printf("%d", sum);
  return 0;
}

第三个计算器

用python实现,功能和第二个差不多。这就是为毛很想学python的原因了。

#计算器

i=eval(input("请输入式子:"))
print("结果为:",i)

你可能感兴趣的:(三个计算器)