曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 2396|回复: 0
打印 上一主题 下一主题

计算器(初级版)

[复制链接]

716

主题

734

帖子

2946

积分

超级版主

Rank: 9Rank: 9Rank: 9

积分
2946
跳转到指定楼层
楼主
发表于 2013-7-8 21:36:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
该计算器程序不支持算数式中有括号的计算。代码如下:
[C++] 纯文本查看 复制代码
// EX6_10.cpp
// A program to implement a calculator

#include <iostream>                   // For stream input/output
#include <cstdlib>                    // For the exit() function
#include <cctype>                     // For the isdigit() function
using std::cin;
using std::cout;
using std::endl;

void eatspaces(char* str);            // 去除输入算数式中的空格
double expr(char* str);               // 计算算数式
double term(char* str, int& index);   // 分析算数式
double number(char* str, int& index); // 将输入字符转换为数字

const int MAX(80);                    // 支持表达式的输入最大长度,包含 '\0'
                                    
int main()
{
   char buffer[MAX] = {0};    // 保存输入的算数式

   cout << endl
        << "Welcome to your friendly calculator."
        << endl
        << "Enter an expression, or an empty line to quit."
        << endl;

   for(;;)
   {
      cin.getline(buffer, sizeof buffer);   // 读入
      eatspaces(buffer);                    // 将输入的算数式中的空白符去除

      if(!buffer[0])                        // 当输入一个空行时退出程序
         return 0;

      cout << "\t= " << expr(buffer)        // 输出算数式的值
           << endl << endl;
   }
}


// 将字串中的空白符去除
void eatspaces(char* str)
{
   int i(0);                             
   int j(0);                            

   while((*(str + i) = *(str + j++)) != '\0') 
                                             
      if(*(str + i) != ' ')                  
         i++;                                
   return;
}

// 计算算数式的值
double expr(char* str)
{
  double value(0.0);                   // 保存结果
  int index(0);                        // 字串的索引

  value = term(str, index);            // 获得第一个操作数

  for(;;)                              // 做加减法运算,直到整个算数式结束。算数式中可能会有加减法和乘除法的混合,先计算乘除部分,剩下的加减使用该循环全部计算
  {
    switch(*(str + index++))           // 查看当前的字符是什么
    {
      case '\0':                       // 如果已经到了算数式末尾,那么返回计算结果
         return value;               

      case '+':                        // 加法
         value += term(str, index);    // term() 得到加数,然后计算结果保存到 value 中,value 在下次的运算中为被加数
         break;

      case '-':                        // 减法
         value -= term(str, index);    // term() 得到减数,然后计算结果保存到 value 中,value 在下次的运算中为被减数
         break;

      default:                         // 输入的是非运算符,
         cout << endl                
              << "Arrrgh!*#!! There's an error"
              << endl;
         exit(1);
    }
  }
}

// 得到算数式的值
double term(char* str, int& index)
{
  double value(0.0);                   // 存储算数式中某段的计算结果,比如 3+2*3*4-5+10 ,那么 value 会存储 2*3*4 这个算数式段的结果 
                                      

  value = number(str, index);          // 将字符转换为数字

  // 该 while 循环用来计算连乘或连除
  while(true)
  {

    if(*(str + index) == '*')          // 乘法运算
      value *= number(str, ++index);   // value 是被乘数, 由 number() 得到乘数

    else if(*(str + index) == '/')     // 除法运算
      value /= number(str, ++index);   // value 是被除数, 由 number() 得到除数
    else
      break;
  }
  return value;                        // 结束,返回计算结果
                                      
}

//将数字字符转换为数字
double number(char* str, int& index)
{
  double value(0.0);                  // 保存结果

  // 必须至少一位是数字
  if(!isdigit(*(str + index)))
  { // There's no digits so input is junk...
    cout << endl                  
         << "Arrrgh!*#!! There's an error"
         << endl;
    exit(1);
  }

  while(isdigit(*(str + index)))       // 转换,比如 "321" 这个字串在这里被转换为整数 321
    value = 10*value + (*(str + index++) - '0');

                                       // 如果不是数字,那么检查是否是小数点
  if(*(str + index) != '.')           
    return value;                     

  double factor(1.0);                  // 计算浮点数
  while(isdigit(*(str + (++index))))  
  {
    factor *= 0.1;                     // 小数点每往后一位就要乘以一个 0.1
    value = value + (*(str + index) - '0')*factor;   // 加上小数部分
  }

  return value;                        // 返回结果
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-5-15 14:35 , Processed in 0.114848 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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