你能告诉我如何找到这个程序的输出吗? [英] Can you please describe me how to find the outputs of this programme?

查看:100
本文介绍了你能告诉我如何找到这个程序的输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到这个程序的输出,但我不能。我不明白,请帮帮我。



我的尝试:



I tried to find the outputs of this programme but I can't. I don't understand it, please help me.

What I have tried:

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
	char v[] = {'A', 'B', 'C', 'D', 'Z'};
	int m=0, k=0, x;
	while (v[k] != 'Z')
	{
		m += v[k] - 'A';
		k++;
	}
	printf("%d\n", m);
	for (m=5, k=7, x=0; --m, k>m; k-=2) x++;
	printf("%d\n", x);

system("pause");
return 0;
}

推荐答案

编译并运行它。



要知道在每个步骤中发生了什么,请插入显示循环内所有变量内容的打印语句。



或者通过将其写在纸上或在文本文件中:

Compile and run it.

To know what happens at each step insert print statements showing the content of all variables inside the loops.

Or do it manually by writing it down on a piece of paper or in a text file:
m = 0, k = 0
v[k] = v[0] = 'A' = 64
v[k] - 'A' = 'A' - 'A' = 0
m += v[k] - 'A' -> m = 0
k++ -> k = 1
v[k] = v[1] = 'B' = 65
v[k] - 'A' = 'B' - 'A' = 65 - 64 = 1
m += v[k] - 'A' -> m = 1
...

类似于第二个循环:

Similar for the second loop:

m = 5, k = 7, x = 0
--m -> m = 4
k > m / 7 > 4 ? Yes, move on
x++ -> x = 1
k -= 2 -> k = 5

--m -> m = 3
k > m / 5 > 3? Yes, move on
x++ -> x = 2
k -= 2 -> k = 3

...

你只需知道的执行顺序为循环以及逗号是什么运算符(例如,参见 for loop - cppreference.com [ ^ ]和其他运营商 - cppreference.com [ ^ ]。。

All you have to know is the execution order of for loops and what the comma operator does (see for example for loop - cppreference.com[^] and Other operators - cppreference.com[^] ).


引用:

我试图找到输出这个程序,但我不能。我不明白,请帮帮我。

I tried to find the outputs of this programme but I can't. I don't understand it, please help me.



没有必要了解这个程序来找到它的输出。简单的方法就是运行它,你就会得到输出。


There is no need to understand this program to find its output. The easy way is simply to run it, and you will get the outputs.

引用:

你能描述一下吗?我如何找到这个程序的输出?

Can you please describe me how to find the outputs of this programme?



这个程序是专门设计的,如果只是阅读代码很难理解,但你可以从你的PC,工具到使用是调试器

-----

有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪个期望符合现实。

只需设置断点并查看代码执行情况,调试器允许你逐行执行1行和在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

那里在调试器中没有魔力,它没有找到bug,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


This program is specifically crafted to be difficult to understand if just reading the code, but you can get help from your PC, the tool to use is the debugger.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


你最好写清洁代码(更容易理解)并扩展输出更好的理解:

you better write cleaner code (more understandable) and extend the output for a better understanding like that:
m=5, x=0;//not for the loop
printf("m = %d\n", m);
for ( k=7; k>m; k-=2) //with brace for loop body
{
  x++;
  --m;// in loop operation
  printf("x = %d\n", x);
}
printf("final x = %d\n", x);

输出位于控制台(黑色窗口)。

The output is at the console (the black window).


这篇关于你能告诉我如何找到这个程序的输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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