本文介绍了输入线反向器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个尚未正常工作的C程序的目的是输出每个输入行

反转(所以如果用户输入''hello'',程序将输出

''olleh'' - 请假设这个stdout和stdin !!!)。

为什么h ** l不是这个工作,为什么会崩溃? !


#include< stdio.h>

#define MAXINPUT 256

void reverse(char [ ],int);


main()

{

int c;

int number ;

char s [MAXINPUT];

int i;


for(i = 0;(i< MAXINPUT-1) )&&((c = getchar())!=''\ n''); i ++){

s [i] = c;

+ +数字;

}

反向(s,数字);

for(i = 0; i< = number; i ++)

putchar(s [i]);

返回0;

}

void reverse(char s [], int num_elements)

{

int i,j;


for(i = 0,j = num_elements-1;( I< = num_elements-1 )&&(j> = 0); i ++,j--)

s [i] = s [j];

}

解决方案




桌面检查。这意味着:拿出一张纸,假装你是一台b
的电脑。记下所有对象的值,每个对象都在它自己的b / b
列的头部。假设你有五个元素:


代码s [0] s [1] s [2] s [3] s [4] nij

''我是谁?'''''''''''''''5'? ?


放下左栏中的每个代码步骤,查看

代码对所有相关对象的值的影响。例如:


代码s [0] s [1] s [2] s [3] s [4] nij

''h'' ''''''''''''''''''5'? ?

i = 0'''''''''''''''''''''''5 0?

j = n- 1'''''''''''''''''''''''''''5 5 4

i< = 4?是的。

j> = 0?是的。

s [i] = s [j]'''''''''''''''''''''''''''''5 5 4


等等。手动执行此操作 - 桌面检查 - 是一个非常宝贵的帮助来理解你实际写的是什么,而不是你认为的那个

写的。


- -

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999


电子邮件:rjh在上面的域名(但显然放弃了www)




-Arthur



This not-yet-working C program''s aim is to output each input line
reversed (so if the user enters ''hello'', the program will output
''olleh'' - and please assume this stdout and stdin!!!).
Why the h**l isn''t this WORKING AND WHY DOES IT CRASH???!

#include <stdio.h>
#define MAXINPUT 256

void reverse(char[], int);

main()
{
int c;
int number;
char s[MAXINPUT];
int i;

for (i=0; (i<MAXINPUT-1) && ((c = getchar()) != ''\n''); i++) {
s[i] = c;
++number;
}
reverse(s, number);
for (i=0; i<=number; i++)
putchar(s[i]);
return 0;
}
void reverse(char s[], int num_elements)
{
int i, j;

for (i=0,j=num_elements-1; (i<=num_elements-1) && (j>=0); i++,j--)
s[i] = s[j];
}

解决方案



Desk-check it. That means: get out a piece of paper, and pretend you''re a
computer. Write down the values of all objects, each at the head of its own
column. Assuming you have five elements:

Code s[0] s[1] s[2] s[3] s[4] n i j
''h'' ''e'' ''l'' ''l'' ''o'' 5 ? ?

Put down each code step in the left hand column, and look at the effect the
code has on the values of all the relevant objects. For example:

Code s[0] s[1] s[2] s[3] s[4] n i j
''h'' ''e'' ''l'' ''l'' ''o'' 5 ? ?
i = 0 ''h'' ''e'' ''l'' ''l'' ''o'' 5 0 ?
j = n-1 ''h'' ''e'' ''l'' ''l'' ''o'' 5 0 4
i <= 4? Yes.
j >= 0? Yes.
s[i]=s[j] ''o'' ''e'' ''l'' ''l'' ''o'' 5 0 4

and so on. Doing this by hand - "desk-checking" - is an invaluable aid to
understanding what you actually wrote, as opposed to what you thought you
wrote.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)



-Arthur



这篇关于输入线反向器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:17