该声明的工作原理> [英] How this statement work>

查看:63
本文介绍了该声明的工作原理>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.//Program for find large number
2.#include<iostream>
3.using namespace std;
4.main()
5.{
6.int a,b,c;
7.cout<<"Enter three numbers (A,B,C): ";
8.cin>>a>>b>>c;
9.if(a>b)
10.{
11.    if(a>c)
12.        cout<<a<<" is large";
13.    else
14.        cout<<c<<" is large";
15.}
16.else
17.   if(b>c)
18.    cout<<b<<" is large";
19.    else
20.    cout<<c<<" is large";
21.}



如果a = 10 b = 20 c = 30那么

当a比b大时,只有第11行才会执行,但即使这段代码也能正常工作并且显示c更大但它是如何工作的?


If a=10 b=20 c=30 then
When a is grater than b then only line no 11 will execute but even this code work properly and show c is grater but how it works?

推荐答案

想想你有三个数字,所以(假设没有相同的数字)他们的订单必须是以下之一:

Think about it you have three numbers, so (assuming none are the same) their order must be one of:
a > b > c
a > c > b
b > a > c
b > c > a
c > a > b
c > b > a



因此,如果a大于b,那么就是以下情况之一:


So, if a is greater than b, then it's one of these cases:

a > b > c
a > c > b
c > a > b

因为在所有其他方面,b大于a。

前两个在a和c之间具有相同的关系,所以你可以认为是相同的。

所以你需要做的就是通过比较a和c来决定这两种情况中的哪一种。

同样适用于所有其他情况。



如果允许重复,那么您报告的内容无关紧要 - 即使它说C而不是A也是正确的,因为它们具有相同的值。 br />


澄清添加到这两个中的哪一个部分 - OriginalGriff [/ edit]

Because in all the other, b is greater than a.
And the first two have the same relationship between a and c, so thy can be considered as the same.
So all you need to do is decide which of those two cases it is by comparing a and c.
The same thing works for all the other cases.

If duplicates are allowed, then it doesn't matter which you report - it's right even if it says "C" rather than "A" because they have the same value.

[edit]Clarification added to the "which of these two" part - OriginalGriff[/edit]


它完全像你把它。



它通过将此代码转换为机器代码,CPU 指令来完成相同(将C ++代码编译到目标文件中,而不是将目标文件链接到可执行文件中)。要理解它是如何工作的,你应该了解CPU是如何工作的。



可执行代码使用jump 指令:如果值中有一些条件一些寄存器发生,这样的指令跳转指令指针(定义下一条指令的寄存器)到标签中指定的指令,即指令的参数。这样,当某些条件为假时执行下一条指令,并且执行跳转到某条其他指令,即条件指令的参数。为了允许两个命令的替代执行,写入另一个无条件跳转。



一些伪代码:



It works exactly as you put it.

It works by translating this code into machine code, CPU instructions which do exactly the same (as a result of compilation C++ code into object file and than linking object files into executable file). To understand how it works, you should understand how a CPU works.

The executable code uses jump instructions: if some condition in the values of some registers takes place, such instruction jumps instruction pointer (the register which defines which instruction will be executed next) to the instruction specified in the label, a parameter of the instruction. This way, the next instruction is executed when some condition is false, and the execution jumps to some other instruction, a parameter of conditional instruction. To allow for alternative execution of the two commands, another unconditional jump is written.

Some pseudo-code:

Check condition and jump to CALL_WITH_C
   call OUTPUT_A_IS_LARGER
   jump to END_IF
CALL_WITH_C:
   call OUTPUT_C_IS_LARGER
END_IF:
...





对于最常用的指令集架构,x86,它可能是像 Jcc 或 JCXZ ,指令指针注册表名为 IP 。请参阅:

http://en.wikipedia.org/wiki/X86_instruction_listings [< a href =http://en.wikipedia.org/wiki/X86_instruction_listingstarget =_ blanktitle =New Window> ^ ]。



另请参阅:

http://en.wikipedia.org/wiki/Compiler [ ^ ],

http://en.wikipedia.org/wiki/Object_file [ ^ ],

http://en.wikipedia.org/wiki/Machine_code [ ^ ],

http://en.wikipedia.org/维基/ Linker_%28comput %29 [ ^ ],

http://en.wikipedia.org/wiki/Executable_file [ ^ ]。



尽管许多程序员并不了解所有这些机器,但我确信每个体面的开发人员都应该很好地理解它,即使这样的开发人员也不能使用汇编语言。



-SA



For the most usually used instruction-set architecture, x86, it could be the instruction like Jcc or JCXZ, and instruction pointer registry is called IP. Please see:
http://en.wikipedia.org/wiki/X86_instruction_listings[^].

See also:
http://en.wikipedia.org/wiki/Compiler[^],
http://en.wikipedia.org/wiki/Object_file[^],
http://en.wikipedia.org/wiki/Machine_code[^],
http://en.wikipedia.org/wiki/Linker_%28computing%29[^],
http://en.wikipedia.org/wiki/Executable_file[^].

Even though many "programmers" don't understand all this machinery, I'm sure that every decent developer should understand it all very well, even it such developer does not work in Assembly Language.

—SA


这篇关于该声明的工作原理&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆