How to Write C code -- crazy guy C exercise answer 1 -- Yu Xiang

Source: Internet
Author: User

Related essays: How to Write C code -- the crazy C Exercise answers 1 http://www.cnblogs.com/KBTiller/archive/2011/03/14/1983964.html #

"How to Write C code -- crazy man C exercise answer 1" was a bit uneasy before it was published. There is no uncertain truth in it, no dazzling skills, and no complicated or difficult problems. It's just a bit of plain knowledge like boiled water. Although people who leave the plain boiled water cannot survive, I am still worried that the rest of the people in the blog Park are clear and there is no need to say that.
However, to my expectation, I can find from this comment that my essay is definitely not without any loss, but necessary. Zzd's comments are really awesome. I should say thank you.

One of my points in this article is to carefully examine the question, to find out what the problem is, and to clarify the functions of the program.

Zdd users responded:
# 5th floor | zdd
Aside from many other issues in this Code, the question "How many three numbers can be composed of different and no repeated numbers" is obviously ignored by programmers. Such code has no value. What does this mean? Aside from the performance of this code, I can't see anything that doesn't match the meaning of the question. When guiding others to seriously examine the question, I 'd better take it seriously.

He said that he "couldn't see anything that doesn't match the meaning of the question" and obviously ignored the requirement of "how many items can be made up". Even if he didn't see the code, he could not answer the question at all. The primary school teacher taught us not to answer questions.
Zzd users told me that "I 'd better be more careful when guiding others to seriously examine Questions." I am very grateful. But I don't know why he did not answer questions carefully. Why did he turn a blind eye to the requirement of "how many can be made up. So I am willing to share with him.
 
# | Zdd
The structure of the program in this question is a little weak, because it is too simple to talk about any structure. Just write it directly. It is a little troublesome for you to fill it out step by step. Furthermore, writing code directly in the main function is not a good habit. It is better to separate it into a function, and your program is not well written, without any scalability. I did not write a ugly, print any layer of Triangle
01 # include <stdio. h>
02
03 void printtriangle (INT level)
04 {
05 for (INT I = 1; I <= level; ++ I)
06 {
07 for (Int J = 0; j <level-I; ++ J)
08 printf ("% C ",'');
09
10 printf ("% C ",'/');
11
12 For (int K = 1; k <= I * 2-2; ++ K)
13 printf ("% C ",'_');
14
15 printf ("% C \ n ",'\\');
16}
17}
18
19 int main (void)
20 {
21 printtriangle (10 );
22
23 getchar ();
24 return 0;
25}

Zdd netizens think that "the program structure in this question is a little weak ".
In fact, where am I talking about the program structure? I did not solve this problem at all. In addition to having to carefully examine the questions, develop comments, grasp the general information first, pay attention to the details, and test these good programming habits at any time, another main point is that "programmers should not only complete the correct code, and the code should be completed in the correct way ". I really don't understand why the zzd user pulled the "program structure. Where is this? Is it totally a different thing?
More seriously, the zzd Code does not meet the question requirements at all. The question requires output.
/\
/\
/\
/\
/________\
But the output of zzd code is
/\
/__\
/____\
/______\
/________\
/__________\
/____________\
/______________\
/________________\
/__________________\
He probably thinks that he has the right to arbitrarily misinterpret the user's needs, or determine the user's needs on behalf of the user. Or, as I warned beginners, I did not pay attention to the questions and did not know what functions the program should have.

"Printing triangles of any number of layers" is basically an out-of-the-box conversation. There is no "arbitrary" concept in computer solutions. All the problems that computers can solve are limited. Otherwise, there will be no resource depletion. The so-called "arbitrary" is a process of taking for granted that does not completely get rid of mathematical thinking.
Speaking of the program structure, I think the greedy and cheap style of writing the function definition in front of it to omit the function declaration is actually very ugly. I also mentioned the reason in crazy man C.
"Just write it directly. It is a little difficult to fill it out in one step." I can only say that he did not understand my point of view at all: "programmers should not only complete the correct code, and the code should be completed in the correct way ". However, he did not give an appropriate objection. It can only be said that he does not understand the meaning of a good "process" and the importance of programming habits. Do not know how to design "processes" and how to cultivate habits.
In addition, "printf (" % C ", '/');" in his code is actually simpler: "putchar ('/');". I don't know why he used such an implementation (I should also write it as "printf ("/") even if he was not familiar with it, as for "printf (" % C \ n ", '\');", it's hard to see ). It should not be as concise as the code or program efficiency.
The Code contains two for statements that are almost identical. Repeated statements appear in the Code. programmers with a little programming skills cannot write them at all. The true meaning of the sentence "we should separate them to make functions better" does not know whether he really understands it.
In addition, he thinks that the "System () function is ultimately a system call? The overhead of system calls is much higher than that of library function calls. Getchar () is the best choice here !". I don't know whether he noticed the meaningless and ugly character Echo brought by using getchar. It is a sin to accomplish a function other than a program requirement, which is the basic knowledge of software testing. In addition, system ("pause"); is not written by me. It is automatically generated by Dev C ++ to help programmers observe the running results.
"Writing code directly in the main function is not a good habit," Of course not. However, the question I answered is the question in chapter 1 of "Mad Man C". "Mad Man C" has no programming knowledge for readers. Do they need to use functions from the very beginning? Is the first C program of zzd implemented by self-defined functions?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.