当前位置: 首页 > news >正文

cs106x-lecture9(Autumn 2017)-SPL实现

打卡cs106x(Autumn 2017)-lecture9

(以下皆使用SPL实现,非STL库,后续课程结束会使用STL实现)

1、evaluateMathExpression

Write a recursive function named evaluateMathExpression that accepts a string parameter representing a math expression on integers and returns the result of that math expression. The expression will consist of single-digit integers and possible operators. All operators will be surrounded by parentheses; we would say that the expression is fully parenthesized. The operators will be either + or *. For example, the call of evaluateMathExpression("((1+2)*(3+1)+(1*(2+2)))") should return xxxx . You may assume that the string is non-empty and does not contain any other characters.

Constraints: Your code must be recursive and not use any loops. Do not declare any global variables or any auxiliary data structures. You can declare as many primitive variables and strings as you like.

解答:

#include <iostream>
#include <cctype>
#include <string>
#include "console.h"

using namespace std;

int evaluateMathExpressionHelper(const string& exp, int& index) {
    if (isdigit(exp[index])) {
        return exp[index++] - '0';
    } else {
        index++;
        int left = evaluateMathExpressionHelper(exp, index); // if '(' else 'number'
        char op = exp[index++];
        int right = evaluateMathExpressionHelper(exp, index);
        index++;

        if (op == '+') {
            return left + right;
        } else { // op == '*'
            return left * right;
        }
    }
}

int evaluateMathExpression(string exp) {
    int index = 0;
    return evaluateMathExpressionHelper(exp, index);
}

int main() {
    cout << evaluateMathExpression("(2+3)") << endl; // 5
    cout << evaluateMathExpression("((2+3)*(1+1))") << endl; // 10
    cout << evaluateMathExpression("(((1+2)*(3+1))+(1*(2+2)))") << endl; // 16
    cout << evaluateMathExpression("(((3*(1+(2+1)))+((9+2)*4))*((1+1)+(2+(2+2))))") << endl; // 448
    cout << evaluateMathExpression("8") << endl; // 8
    return 0;
}

2、evenDigits

Write a recursive function named evenDigits that accepts an integer parameter n and returns a new integer containing only the even digits from n, in the same order. If n does not contain any even digits, return 0.

For example, the call of evenDigits(8342116) should return 8426 and the call of evenDigits(35179) should return 0.

解答:

#include <iostream>
#include <string>
#include "console.h"

using namespace std;

int evenDigitsHelper(int n, int& t) {
    if (n < 10) {
        if (n % 2 == 0) {
            n *= t;
            t *= 10;
            return n;
        } else {
            return 0;
        }
    } else {
        return evenDigitsHelper(n % 10, t) + evenDigitsHelper(n / 10, t);
    }
}
int evenDigits(int n) {
    int t = 1;
    if (n >= 0) {
        return evenDigitsHelper(n, t);
    } else {
        return -evenDigitsHelper(-n, t);
    }
}

int main() {
    
    cout << evenDigits(8342116) << endl; // 8426
    cout << evenDigits(40109) << endl; // 400 
    cout << evenDigits(8) << endl; // 8   
    cout << evenDigits(-163505) << endl; // -60 
    cout << evenDigits(35179) << endl; // 0   
    cout << evenDigits(-2) << endl; // -2
    cout << evenDigits(0) << endl; // 0
    return 0;
}

http://www.dtcms.com/a/23300.html

相关文章:

  • PLC的集成RAM,存储器卡,用户程序存储空间,数据存储容量分别指的什么,有什么关联?
  • set的使用(c++)
  • JVM基础---java类加载机制(类的生命周期,类加载器,双亲委派模型)
  • XSS攻击(跨站脚本攻击)详解与实战
  • 零基础入门机器学习 -- 第五章决策树与随机森林
  • BSD协议栈:多播
  • Visual Basic语言的数据类型
  • Logo语言的图形用户界面
  • jar命令解压jar包及更新jar的配置文件
  • RTMP(Real-Time Messaging Protocol)
  • 网工项目理论1.11 网络出口设计
  • seata基本使用
  • 【Java】Mongodb
  • UI自动化教程 —— 元素定位技巧:精确找到你需要的页面元素
  • Kafka偏移量管理全攻略:从基础概念到高级操作实战
  • 如何在yolov8系列运行自己的数据集
  • NAT(网络地址转换)技术详解:网络安全渗透测试中的关键应用与防御策略
  • 嵌入式人工智能应用-第四章 决策树 6
  • PostgreSQL 创建数据库
  • 一个基于Spring Boot和Vue.js的web商城系统-邻家小铺
  • SyntaxError: invalid syntax
  • MYSQL中的性能调优方法
  • Mac 安装Ollama和llama3,本地部署LobeChat和刘皇叔聊三国
  • Rust编程语言入门教程(四)猜数游戏:一次猜测
  • HarmonyOS4-工具安装
  • 【JavaScript】《JavaScript高级程序设计 (第4版) 》笔记-Chapter15-DOM 扩展
  • STM32 CubeMx配置串口收发使用DMA并调用Idle模式(一)
  • 嵌入式编程——数据结构与linux编程
  • 测试常见问题汇总-检查表(持续完善)
  • 【C++游戏开发-五子棋】