比较的数量?

时间:2015-12-17 16:04:22

标签: c comparison-operators

我最近参加了一个提供这些表达式的C编程考试:

int a = 3, b = 10, c = 4, d = 6;
  1. <击> (c >= d) || (a > b)
  2. 更正:第一个表达式为: (c <= d) || (a > b)

    1. (a <= b) && (c == d)
    2. 和一个问题,询问每个表达式中进行了多少次比较。我说每个表达中有三个比较。在第一个是大于或等于,或,大于。在第二个小于或等于,和,等于。但是根据评分者的说法,第一个表达式只有一个比较,第二个表达式只有两个。

      任何人都可以解释原因吗?

      整数是否相关?

3 个答案:

答案 0 :(得分:4)

由于short-circuiting运算符||&&,整数与上下文非常相关。

对于||,如果第一个表达式为真,则永远不会进行第二次比较。

对于&&,如果第一个表达式为假,则永远不会进行第二次比较。

此外,AND和OR运算符本身不是比较。

答案 1 :(得分:4)

鉴于数字是:

<?php
/*
Plugin Name: WooCommerce Shipping Email
Plugin URI: http://www.kathyisawesome.com/
Description: Add a shipping email field to checkout and notify of new orders
Version: 1.0
Author: Kathy Darling
Author URI: http://kathyisawesome.com
Requires at least: 4.0
Tested up to: 4.0

Copyright: © 2014 Kathy Darling.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html

*/


/**
 * The Main WC_Shipping_Email class
 **/
if ( ! class_exists( 'WC_Shipping_Email' ) ) :

class WC_Shipping_Email {

    /**
     * @var WC_Shipping_Email - the single instance of the class
     * @since 1.0
     */
    protected static $_instance = null;           

    /**
     * Main WC_Shipping_Email Instance
     *
     * Ensures only one instance of WC_Shipping_Email is loaded or can be loaded.
     *
     * @static
     * @see WC_Shipping_Email()
     * @return WC_Shipping_Email - Main instance
     * @since 1.0
     */
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Cloning is forbidden.
     *
     * @since 1.0
     */
    public function __clone() {
        _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce-mix-and-match' ), '2.0' );
    }

    /**
     * Unserializing instances of this class is forbidden.
     *
     * @since 1.0
     */
    public function __wakeup() {
        _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'mix-and-match' ), '2.0' );
    }

    /**
     * WC_Shipping_Email Constructor
     *
     * @access public
     * @return WC_Shipping_Email
     * @since 1.0
     */

    public function __construct() { 

        $this->id = 'email';
        $this->meta = '_shipping_email';
        $this->label = __( 'Shipping Email', 'woocommerce-shipping-email' );

        // add email field to checkout
        add_filter( 'woocommerce_shipping_fields' , array( $this, 'add_shipping_fields' ) );
        add_filter( 'woocommerce_admin_shipping_fields' , array( $this, 'admin_shipping_fields' ) );

        // add recipient to specific emails
        add_filter( 'woocommerce_email_recipient_customer_processing_order' , array( $this, 'add_recipient' ), 20, 2 );
        add_filter( 'woocommerce_email_recipient_customer_completed_order' , array( $this, 'add_recipient' ), 20, 2 );
        add_filter( 'woocommerce_email_recipient_customer_note' , array( $this, 'add_recipient' ), 20, 2 );

        // display meta key in order overview
        add_action( 'woocommerce_order_details_after_customer_details' , array( $this, 'after_customer_details' ) );

        // display meta key in email
        add_action( 'woocommerce_before_template_part' , array( $this, 'before_email_addresses' ), 10, 4 );

    }


    /*-----------------------------------------------------------------------------------*/
    /* Plugin Functions */
    /*-----------------------------------------------------------------------------------*/

    /**
     * Add email to front-end shipping fields
     *
     * @var  array $fields
     * @return  array
     * @since 1.0
     */

    function add_shipping_fields( $fields ) {
        $fields['shipping_' . $this->id] = array(
            'label'         => $this->label,
            'required'      => true,
            'class'         => array( 'form-row-first' ),
            'validate'      => array( 'email' ),
        );
        return $fields;
    }

    /**
     * Add email to Admin Order overview
     *
     * @var  array $fields
     * @return  array
     * @since 1.0
     */

    function admin_shipping_fields( $fields ) {
        $fields[$this->id] = array(
            'label'         => $this->label
        );
        return $fields;
    }

    /**
     * Add recipient to emails
     *
     * @var  mixed $email
     * @return  mixed
     * @since 1.0
     */
    function add_recipient( $email, $order ) {
        $additional_email = get_post_meta( $order->id, $this->meta, true );
        if( $additional_email && is_email( $additional_email )){
            $email = explode( ',', $email );
            array_push( $email, $additional_email );
        }
        return $email;
    }

    /**
     * Display meta in my-account area Order overview
     *
     * @var  object $order
     * @return  null
     * @since 1.0
     */

    public function after_customer_details( $order ){

        $value = get_post_meta( $order->id, $this->meta, true );

        if( $value ){
            echo '<dt>' . $this->label . ':</dt><dd>' . $value . '</dd>';
        }

    }

    /**
     * Display meta in my-account area Order overview
     *
     * @var  array $fields
     * @return  array
     * @since 1.0
     */

    public function before_email_addresses( $template_name, $template_path, $located, $args ){

        if( $template_name == 'emails/email-addresses.php' && isset( $args['order' ] ) && ( $value = get_post_meta( $args['order']->id, $this->meta, true ) ) ){ 

            if ( isset( $args['plain_text'] ) && $args['plain_text'] ){

                echo $this->label . ': ' . $value . "\n";

            } else {

                echo '<p><strong>' . $this->label . ':</strong> ' . $value . '</p>';

            }

        }

    }



} //end class: do not remove or there will be no more guacamole for you

endif; // end class_exists check


/**
 * Returns the main instance of WC_Shipping_Email to prevent the need to use globals.
 *
 * @since  2.0
 * @return WooCommerce
 */
function WC_Shipping_Email() {
  return WC_Shipping_Email::instance();
}

// Launch the whole plugin
WC_Shipping_Email();

对于第一个表达式(更新后):

int a = 3, b = 10, c = 4 , d = 6;

变量(c <= d) || (a > b) 小于c(4小于6),因此无论第二部分的值是什么,整个表达式都是真的,因此只有1次比较。

第二个:

d

变量(a <= b) && (c == d) 小于a,但程序也必须检查其他部分(如果b不等于c并且它们实际上是不同),所以这里也有2个比较。

答案 2 :(得分:3)

这个问题的关键在于确定你是否理解C中||&&的短路概念。这些是逻辑连词,而不是比较。

在您的示例中,您将获得:

int a = 3, b = 10, c = 4, d = 6;

然后会询问您执行了多少次比较:

1.  (c <= d) || (a > b)
2.  (a <= b) && (c == d)

这两个表达式都包含两个比较操作,但这并不意味着实际执行这些比较。

在案例(1)中,它首先使用c比较d<=。结果是真的(即1)。这就是确定||操作的结果为真(即1)所需的全部内容,因此它跳过(a > b)的第二次比较,因为不需要获得最终结果。这称为短路。

在情况(2)中,它首先使用a比较b<=。结果是真的(即1),所以它进行到(c == d)的第二次比较。其结果为假(即0),因此最终结果为假(即0)。

因此对于(1)它只需要进行一次比较,但对于(2)它需要进行两次比较。如果变量的值发生变化,那么比较的数量也会发生变化。