Preface
In the collation of documents accidentally found large-time C language big job, so suddenly whim want to see once spent one weeks of results.
Although it has only one file, but a full 829 lines, I can see how much effort. Recall that the requirements of the job at that time, roughly also is to achieve a can be deleted and changed records of the student performance management system, why should write 829 line? Find the source documentation, think of those I racked my brains out of the many fancy features, I can not help feeling: Young is able to toss Ah!
And when I'm ready to read it, I'm not happy-I don't understand or want to read my code! Although I have read a lot of bad code, but when I found that my pride was a piece of rubbish, I am still very uncomfortable.
Remember the teacher when the big homework to encourage us to write more code, add more features, so I think the more code the better, and add a lot of weird features. And now I am very clear: The code is not the number of spelling, but the quality of the work, the function of the design is not the richer the better, for a less meaningful function is not worth the effort.
A good code?
Looking back at my little experience of C, I have had too many misconceptions about programming languages:
- Crazy about language skills rote operator precedence, the behavior of dead-drilling language characteristics is to go crazy, language is for us to serve, the use of the characteristics used, bad features should be avoided. I used to think that the more language features a person knows, the more important it is to understand how to use a language than to explore the language itself.
- mistakenly think the code is more difficult to understand the better once thought that only their own can understand the program is tall, unscrupulous use of "," "," "
#
##
#@
preprocessing operators, abuse of those little-known macro replacement skills. Not only reduces the efficiency of writing code, but also increases the time others read, to the final written code even if they do not understand.
- The blind pursuit of operational efficiency has participated in the intelligent car competition, in order to improve the speed, mistaken for variable name change, the array expanded into simple variables, the loop unfold ... And so on can improve the speed of the program, simply stupid. (Imagination is too rich not necessarily good, to read more, less blind to think)
- Do not want to share code once thought code is their own hard work income, if given to others there is no value. Now that the code is like a work of art, no one is looking at it and it is worthless.
I think that writing code should strive to achieve a poetic simplicity and elegance:
- Do not pursue tonal alternation, only to express emotion, Express thought
- Not the pursuit of rhetoric gorgeous, just plain and easy to understand, Shinong
Such a good code to be People clone, star, to be recited.
Rewrite the code!
Thousands of feelings so that I can not help wandering for a while, back to God, think of senior No class, also is a little idle time, so want to change this code. But I changed my mind when I found out that there was something wrong in almost every place and I didn't want to see the logic clutter again.
Finally, more than 800 lines into more than 200 lines, the program to see my GitHub blog, I hope to have just finished learning C language students have some help. My ability is limited, if the predecessor is willing to say, please comment after this article, greatly appreciated!
Finally, attach the code:
/ * * Student information Manager * https://baidut.github.io/| Released under MIT license * Copyright (c) zhenqiang.ying <[email protected]> * *#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX#define Name_lentypedef struct{CharName[name_len];unsigned intscore[3];unsigned intsum;} stu_t; stu_t Stu[max];intNum stu_t Input_stu (void);//By prompting the user to correctly enter a student record and return this recordvoidOutput_stu (inti);//Print out the corresponding student information according to the array indexintFind_stu (void);//Prompts the user to find the record, if found, displays the discovered record and returns the array index, otherwise returns-1voidRead_stu (Const Char*filename);//Read the student data in the file according to the filenamevoidWrite_stu (Const Char*filename);//write student data to the specified fileintConfirmConst Char*words);//Request user confirmation, enter Y or Y to return 1, enter n or N to return 0, other prompts the user to re-entervoidSafe_flush (FILE *FP);///Safe_flush (stdin) cleans up excess input to prevent the next gain of inputintMainvoid) {Const Char*filename ="Stud_info.txt"; Read_stu (filename);//Import data printf("Welcome to student performance Management system \ n" "-There are currently%d records, this program limits%d records \ n", num, MAX); for(;;) {CharChoiceintI, J;printf("-Menu: \ t" "1. Find \ t" "2. Append \ t" "3. Delete \ t" "4. Modify \ t" "5. Sort \ t" "6. Show \ T" "7. Save \ T" "0. exit \ n" "Enter serial number:"); Choice = GetChar (); Safe_flush (stdin);Switch(choice) { Case ' 1 '://FindFind_stu (); Break; Case ' 2 '://Append if(Num < MAX) {Stu[num] = Input_stu (); Output_stu (num); num++;puts("added success!"); }Else puts("The record is full and cannot be appended!" "); Break; Case ' 3 '://Deletei = Find_stu ();if(-1! = i) {if(Confirm ("Confirm the deletion?" ")) { while(i++! = num) {stu[i-1] = Stu[i]; } num--;puts("Delete succeeded!"); } } Break; Case ' 4 '://Modifyi = Find_stu ();if(-1! = i) {Stu[i] = Input_stu (); Output_stu (i);puts("Modified successfully!" "); } Break; Case ' 5 '://sorted by total score for(i =0; I < num-1; i++)//bubble sort is already lined up for(j=0; J < num-1-I.; J + +)//Traversal not in line if(Stu[j].sum < stu[j+1].sum) {stu_t t = stu[j]; STU[J] = stu[j+1]; stu[j+1] = t; }puts("Sort of successful!" ");/ * Fall through * / //automatic display after finishing sorting Case ' 6 '://Display for(i =0; i < num; i++) {printf("%4d:\t", i+1); Output_stu (i); } Break; Case ' 7 '://SaveWrite_stu (filename); Break; Case ' 0 '://Exit if(Confirm ("Save before rollout?" (y/n) ") {write_stu (filename); }Exit(0);default:puts("not the serial number!" Please re-enter "); Break; } }//END for return 0;}voidSafe_flush (FILE *fp) {intCh while(ch = fgetc (FP), ch! = EOF && ch! =' \ n ');}intConfirmConst Char*words) {CharChoiceputs(words); for(;;) {choice = GetChar (); Safe_flush (stdin);Switch(choice) {default:puts("input illegal, re-enter!" ");Continue; Case ' Y ': Case ' y ':puts("Confirmed! ");return 1; Case ' N ': Case ' n ':puts("Canceled!" ");return 0; }}}stu_t Input_stu (void) {stu_t ret;puts("Enter name, language, math, foreign language scores, space separation, the score requirement is an integer:"); while(4!=scanf("%s%d%d%d", Ret.name, &ret.score[0], &ret.score[1], &ret.score[2]) {Safe_flush (stdin);puts("wrong input, please re-enter!"); } safe_flush (stdin); Ret.sum = ret.score[0] + ret.score[1] + ret.score[2];returnRET;}voidOutput_stu (inti) {if(I <0|| i > MAX)return;printf("%s\t:%d\t:%d\t out:%d\t score:%d\n", Stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].sum);}intFind_stu (void) {CharName[name_len];intIprintf("Enter a name (%d characters):", Name_len);scanf('%s ', name); Safe_flush (stdin); for(i =0; i < num; i++) {if(0==strcmp(stu[i].name, name)) {Output_stu (i);returnI } }puts("no record found!");return-1;}voidRead_stu (Const Char*filename) {FILE *FP = fopen (filename,"RT");if(NULL! = FP) {Fread (&num,sizeof(int),1, FP); Fread (Stu,sizeof(stu_t), NUM, FP); } fclose (FP);}voidWrite_stu (Const Char* filename) {FILE *FP = fopen (filename,"WT");if(NULL! = FP) {fwrite (&num,sizeof(int),1, FP); Fwrite (Stu,sizeof(stu_t), NUM, FP); Fclose (FP);puts("saved successfully!"); }Else puts("failed to open file!" ");}
What is the experience of rewriting a freshman's C-language job?