鱼C论坛

 找回密码
 立即注册
查看: 2096|回复: 3

一个计算器程序,完整的一B 没BUG

[复制链接]
发表于 2012-1-28 04:11:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/* Program 7.15 An improved calculator */
#include <stdio.h>                /* Standard input/output                    */
#include <string.h>               /* For string functions                     */
#include <ctype.h>                /* For classifying characters               */
#include <stdlib.h>               /* For converting strings to numeric values */
#include <math.h>                 /* For power() function                     */
#define BUFFER_LEN 256            /* Length of input buffer                   */
int main(void)
{
  char input[BUFFER_LEN];               /* Input expression                  */
  char number_string[30];               /* Stores a number string from input */
  char op = 0;                          /* Stores an operator                */
  unsigned int index = 0;        /* Index of the current character in input   */
  unsigned int to = 0;           /* To index for copying input to itself      */
  size_t input_length = 0;       /* Length of the string in input             */
  unsigned int number_length = 0;  /* Length of the string in number_string   */
  double result = 0.0;           /* The result of an operation                */
  double number = 0.0;           /* Stores the value of number_string         */
  printf("\nTo use this calculator, enter any expression with"
                                          " or without spaces");
  printf("\nAn expression may include the operators:");
  printf("\n          +, -, *, /, %%, or ^(raise to a power).");
  printf("\nUse = at the beginning of a line to operate on ");
  printf("\nthe result of the previous calculation.");
  printf("\nUse quit by itself to stop the calculator.\n\n");
  /* The main calculator loop */
  while(strcmp(fgets(input, BUFFER_LEN, stdin), "quit\n") != 0)
  {
    input_length = strlen(input);             /* Get the input string length */
    input[--input_length] = '\0';             /* Remove newline at the end   */
    /* Remove all spaces from the input by copying the string to itself */
    /* including the string terminating character                       */
    for(to = 0, index = 0 ; index<=input_length ; index++)
    if(*(input+index) != ' ')                       /* If it is not a space  */
      *(input+to++) = *(input+index);               /* Copy the character    */
    input_length = strlen(input);               /* Get the new string length */
    index = 0;                               /* Start at the first character */
   if(input[index]== '=')                    /* Is there =?                  */
     index++;                                /* Yes so skip over it          */
   else
   {                                       /* No - look for the left operand */
     /* Look for a number that is the left operand for the 1st operator */
     /* Check for sign and copy it */
     number_length = 0;                            /* Initialize length      */
     if(input[index]=='+' || input[index]=='-')             /* Is it + or -?  */
       *(number_string+number_length++) = *(input+index++); /* Yes so copy it */
     /* Copy all following digits */
     for( ; isdigit(*(input+index)) ; index++)             /* Is it a digit? */
      *(number_string+number_length++) = *(input+index);   /* Yes - Copy it  */
     /* copy any fractional part */
     if(*(input+index)=='.')                         /* Is it decimal point? */
     { /* Yes so copy the decimal point and the following digits */
       *(number_string+number_length++) = *(input+index++);    /* Copy point */
       for( ; isdigit(*(input+index)) ; index++)           /* For each digit */
         *(number_string+number_length++) = *(input+index);  /* copy it      */
     }
     *(number_string+number_length) = '\0';      /* Append string terminator */
     /* If we have a left operand, the length of number_string */
     /* will be > 0. In this case convert to a double so we    */
     /* can use it in the calculation                          */
     if(number_length>0)
       result = atof(number_string);         /* Store first number as result */
   }
    /* Now look for 'op number' combinations */
    for(;index < input_length;)
    {
      op = *(input+index++);                             /* Get the operator */
      /* Copy the next operand and store it in number */
      number_length = 0;                           /* Initialize the length  */
      /* Check for sign and copy it */
      if(input[index]=='+' || input[index]=='-')           /* Is it + or -?  */
      *(number_string+number_length++) = *(input+index++); /* Yes - copy it. */
      /* Copy all following digits */
      for( ; isdigit(*(input+index)) ; index++)            /* For each digit */
        *(number_string+number_length++) = *(input+index); /* copy it.       */
      /* copy any fractional part */
      if(*(input+index)=='.')                      /* Is it a decimal point? */
      { /* Copy the  decimal point and the following digits */
        /* Copy point     */
        *(number_string+number_length++) = *(input+index++);
        for( ; isdigit(*(input+index)) ; index++)          /* For each digit */
          *(number_string+number_length++) = *(input+index); /* copy it.     */
      }
      *(number_string+number_length) = '\0';             /* terminate string */
      /* Convert to a double so we can use it in the calculation */
      number = atof(number_string);
      /* Execute operation, as 'result op= number' */
      switch(op)
      {
         case '+':                                        /* Addition        */
           result += number;
           break;
         case '-':                                        /* Subtraction     */
           result -= number;
           break;
         case '*':                                        /* Multiplication  */
           result *= number;
           break;
         case '/':                                        /* Division         */
           /* Check second operand for zero */
           if(number == 0)
             printf("\n\n\aDivision by zero error!\n");
           else
             result /= number;
           break;
         case '%':                          /* Modulus operator - remainder  */
           /* Check second operand for zero */
           if((long)number == 0)
             printf("\n\n\aDivision by zero error!\n");
           else
             result = (double)((long)result % (long)number);
           break;
         case '^':                              /* Raise to a power          */
           result = pow(result, number);
           break;
         default:                          /* Invalid operation or bad input */
           printf("\n\n\aIllegal operation!\n");
           break;
       }
    }
    printf("= %f\n", result);                          /* Output the result */
  }
  return 0;
}
有点纠结,看似不起眼的计算器程序让我想了2天,不断调试,不断调试才真正真正全部搞懂!这种程序思路清晰的让我纠结。



                               
登录/注册后可看大图
该贴已经同步到 空手套小白狼的微博
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-30 13:51:12 | 显示全部楼层
啦啦啦啦啦啦啦、、、、
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-30 21:22:12 | 显示全部楼层
还需要顶帖
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-1-30 23:40:57 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-11 00:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表