zoukankan      html  css  js  c++  java
  • scip习题(1) scheme和c实现的对比

    习题1.3

    定义一个过程,它以三个数为参数,返回其中较大的两个数的平方和.

    (Define a procedure thats three numbers as argument and return the sum of the square of two large number.)

    scheme实现

    (define (square x)
      (* x x))
    (define (sum_of_square x y)
      (+ (square x)
         (square y)))
    (define (bigger x y)
      (if (> x y)
          x
          y))
    (define (smaller x y)
      (if (< x y)
          x
          y))
    (define (sum_square x y z)
      (sum_of_square (bigger x y)
                     (bigger (smaller x y) z)))
                     
    
    
            
          
            

    c语言实现

    //用C语言实现scip(计算机程序构造与解释)的一些习题。对比并进一步了解scheme的函数式思想
    //Define a procedure thats three numbers as argument and return the sum of the square of two large 
    //numbers.
    //C language implementation
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    int square(int x);
    int sum_of_square(int a, int b);
    int bigger_num(int a, int b);
    int two_more_bigger_sum_of_square(int a, int b, int c);
    int smaller_num(int a, int b);
    
    //check the code
    int main()
    {
        cout<<two_more_bigger_sum_of_square(1,2,3);
        return 0;
    }
    int square(int x) {
        return x*x;
    }
    
    //retun the sum of the num of square
    int sum_of_square(int a, int b) {
        return square(a)+square(b);
    
    }
    
    //return the bigger num
    int bigger_num(int a, int b) {
        if (a > b)
            return a;
        else
            return b;
    }
    
    //takes the two num as agrument and return the smaller num
    int smaller_num(int a, int b) { //return the bigger num
        if (a > b)
            return b;
        else
            return a;
    }
  • 相关阅读:
    抽象类与抽象方法
    简单工厂模式
    面向对象的七种基本设计原则
    HashTable集合遍历的三种方法
    继承(父类为虚方法以及子类的重写)
    继承(is与as)
    Chrome OS 更新新版本可让Linux访问USB连接的Android设备
    谷歌对Intel 10nm进度不满
    盖茨对没能做好手机系统对抗苹果表示遗憾
    微软内部封杀 Slack
  • 原文地址:https://www.cnblogs.com/everydaykeepgoing/p/6128320.html
Copyright © 2011-2022 走看看