📜 ⬆️ ⬇️

Working with structures or how I learned to write readable code

How it all began


I am a student of a technical university and I study in the direction: "Higher Mathematics, Computer Science and Mathematical Modeling". Since I study only in the second year - it’s very difficult to call my code perfect. Last semester, we studied such discipline as “Modern programming paradigms”. In one of the lectures, we looked at OOP using C ++ as an example and were given the task to write a pseudo-music library using structures.

Task and the first version of the program


The program was supposed to use a structure of 5 fields:
Field number 1: record number;
Field number 2: track name;
Field number 3: the name of the artist;
Field number 4: playing time;
Field number 5: year of entry.
This was supposed to be a console program, all the data into which is entered from the keyboard or from a text file (at the request of the writer). Input data is stored in computer memory or in a file, respectively. Also, the program should execute the following commands:
Search: by number, title, artist, time and year of recording as well as display of all records on the screen;
Change data: delete, edit and add new records;
Editing: the ability to change all fields except the record number.

After almost two weeks of laziness, such a code was written (most of the output text is written in transliteration, because the school taught German, not English):

What can make you horrified!
#include <cstdlib> #include <iostream> #include <string> struct Muslib{ int num; std::string name; std::string artist; std::string time; short jear;}; using namespace std; int main(int argc, char *argv[]) { Muslib muslib[100]; int kil; cout<<"Vvedit' kil'kist pisen' yaku bazhaete dodaty u bazu dannyh: "; cin>>kil; for(int i=1; i<=kil; i++){ muslib[i].num=i; cout<<"Input name: "; cin>>muslib[i].name; cout<<"Input artist: "; cin>>muslib[i].artist; cout<<"Enter time: "; cin>>muslib[i].time; cout<<"Enter jear: "; cin>>muslib[i].jear; cout<<endl;} string command; int n=-1; string str[15]; while(command!="exit"){ cout<<"Enter command (num, name, artist, time, jear, del, add, all, re). Dlya vyhoda vvedite 'exit'!"<<endl; cin>>command; if(command=="num"){ cout<<"Enter num: "; cin>>n; cout<<"Number "<<muslib[n].num<<endl; cout<<"Name "<<muslib[n].name<<endl; cout<<"Artist "<<muslib[n].artist<<endl; cout<<"Time "<<muslib[n].time<<endl; cout<<"Jear "<<muslib[n].jear<<endl; } if(command=="name"){ cout<<"Enter name: "; cin>>str[15]; for(int i=0; i<kil; i++){ if(str[15]==muslib[i].name){ n=i; cout<<"Number "<<muslib[n].num<<endl; cout<<"Name "<<muslib[n].name<<endl; cout<<"Artist "<<muslib[n].artist<<endl; cout<<"Time "<<muslib[n].time<<endl; cout<<"Jear "<<muslib[n].jear<<endl; } if(i==kil-1&&n==-1) cout<<"Element not found!"<<endl; } } if(command=="artist"){ cout<<"Enter artist: "; cin>>str[10]; for(int i=0; i<kil; i++){ if(str[10]==muslib[i].artist){ n=i; cout<<"Number "<<muslib[n].num<<endl; cout<<"Name "<<muslib[n].name<<endl; cout<<"Artist "<<muslib[n].artist<<endl; cout<<"Time "<<muslib[n].time<<endl; cout<<"Jear "<<muslib[n].jear<<endl; } if(i==kil-1&&n==-1) cout<<"Element not found!"<<endl; } } if(command=="time"){ cout<<"Enter time: "; cin>>str[10]; for(int i=0; i<kil; i++){ if(str[10]==muslib[i].time){ n=i; cout<<"Number "<<muslib[n].num<<endl; cout<<"Name "<<muslib[n].name<<endl; cout<<"Artist "<<muslib[n].artist<<endl; cout<<"Time "<<muslib[n].time<<endl; cout<<"Jear "<<muslib[n].jear<<endl; } if(i==kil-1&&n==-1) cout<<"Element not found!"<<endl; } } int jahr; if(command=="jear"){ cout<<"Enter jear: "; cin>>jahr; for(int i=0; i<kil; i++){ if(jahr==muslib[i].jear){ n=i; cout<<"Number "<<muslib[n].num<<endl; cout<<"Name "<<muslib[n].name<<endl; cout<<"Artist "<<muslib[n].artist<<endl; cout<<"Time "<<muslib[n].time<<endl; cout<<"Jear "<<muslib[n].jear<<endl; } if((i==kil-1)&&(n==-1)) cout<<"Element not found!"<<endl; } } if(command=="add"){//new elem int kill; cout<<"Vvedit' kilkist' novyh elementiv: "; cin>>kill; for(int i=kil+1; i<=kil+kill; i++){ muslib[i].num=i; cout<<"Input name: "; cin>>muslib[i].name; cout<<"Input artist: "; cin>>muslib[i].artist; cout<<"Enter time: "; cin>>muslib[i].time; cout<<"Enter jear: "; cin>>muslib[i].jear;} kil=kil+kill;//kilkist elem } if(command=="all"){ for(int i=1; i<=kil; i++){ cout<<"Number "<<muslib[i].num<<endl; cout<<"Name "<<muslib[i].name<<endl; cout<<"Artist "<<muslib[i].artist<<endl; cout<<"Time "<<muslib[i].time<<endl; cout<<"Jear "<<muslib[i].jear<<endl; cout<<endl; } } if(command=="re"){//perepysat pole cout<<"Enter num: "; cin>>n; cout<<"Number "<<muslib[n].num<<endl; cout<<"Name "<<muslib[n].name<<endl; cout<<"Artist "<<muslib[n].artist<<endl; cout<<"Time "<<muslib[n].time<<endl; cout<<"Jear "<<muslib[n].jear<<endl; cout<<endl; string pole; cout<<"Vvedit' imya polya: "; cin>>pole; if(pole=="name") cin>>muslib[n].name; if(pole=="artist") cin>>muslib[n].artist; if(pole=="time") cin>>muslib[n].time; if(pole=="jear") cin>>muslib[n].jear; if(pole!="name"&pole!="artist"&pole!="time"&pole!="jear") cout<<"Pole not found :("<<endl; } if(command=="del"){ cout<<"Enter num: "; cin>>n; muslib[n].num=0; for(int i=1; i<=kil; i++){ if(muslib[i].num!=i){ muslib[i].num=i; muslib[i].name=muslib[i+1].name; muslib[i].artist=muslib[i+1].artist; muslib[i].time=muslib[i+1].time; muslib[i].jear=muslib[i+1].jear; muslib[i+1].num=0; } } kil=kil-1; } } system("PAUSE"); return EXIT_SUCCESS; } 

In spite of everything, this my govnokod tried to pass off as the majority of the students of our group. The result was a finding of more than 10 bugs and a clear misunderstanding of what they are trying to get an assessment for. But its main task - to get an assessment, this program performed perfectly.
')

Full upgrade and version 2.0


It has been more than 2 months after passing this program and a month after passing the exam in this subject. Sitting on vacation at home and missing the fast internet in the hostel, a brilliant idea was born to rewrite this program using functions. It was my sore point. For a year and a half, I never learned to use them. Armed with a 3G modem and an eternal google , I began to look for examples of using functions in C ++. After 3 hours of surfing the internet and writing code in Dev-C ++ in parallel, I could understand how it works. It took another 7 hours and the program was almost completely rewritten using functions. Now it was divided into 3 files and had a code of 100 lines more:

muslibrary.h
 #ifndef MUSLIBRARY_H #define MUSLIBRARY_H #include <cstdlib> #include <iostream> #include <string> using namespace std; struct MusLib{ int num; std::string name; std::string artist; std::string time; short jear;}; void AddPositionf(int,int); void helpf(void); void helpref(void); void numf(int,int); void namef(std::string,int); void artistf(std::string,int); void timef(std::string,int); void jearf(short,int); void ref(int,int); void allf(int); int delf(int,int); #endif /* MUSLIBRARY_H */ 

MusikLibrary.cpp
 #include "muslibrary.h" int position; int main(int argc, char *argv[]) { cout<<"Enter the number of positions: "; cin>>position; AddPositionf(0,position); //   string cmd; while(cmd!="exit"){ cout<<"Enter command (Enter 'help' to help): "; cin>>cmd; if(cmd=="help") helpf(); //  else if(cmd=="help_re") helpref(); //    else if(cmd=="num") { //   int number; cin>>number; numf(number,position); } else if(cmd=="name"){ //   std::string name; cin>>name; namef(name,position); } else if(cmd=="artist"){ //   std::string artist; cin>>artist; artistf(artist,position); } else if(cmd=="time"){ //   std::string time; cin>>time; timef(time,position); } else if(cmd=="jear"){ //   short jear; cin>>jear; jearf(jear,position); } else if(cmd=="re"){ // int re; cin>>re; ref(re,position); } else if(cmd=="add"){ // int add; cin>>add; AddPositionf(position,position+add); position=position+add; } else if(cmd=="all") //  allf(position); else if(cmd=="del"){ //  int del; cin>>del; if(del>0&&del<position) { delf(del,position); position=position-1; } } else cout<<"Command not found!"<<endl; //   } //system("PAUSE"); return EXIT_SUCCESS; } 

MusFunc.cpp
 #include "muslibrary.h" MusLib muslib[100]; void AddPositionf(int epos,int pos){ if(epos>=0&&epos<pos){ for(int i=epos; i<pos; i++){ muslib[i].num=i+1; cout<<"Enter name: "; cin>>muslib[i].name; cout<<"Enter artist: "; cin>>muslib[i].artist; cout<<"Enter time: "; cin>>muslib[i].time; cout<<"Enter jear: "; cin>>muslib[i].jear; cout<<endl; } } } void helpf(void){ cout<<"__________________________________"<<endl; cout<<"| command | action |"<<endl; cout<<"|--------------------------------|"<<endl; cout<<"| Find |"<<endl; cout<<"|--------------------------------|"<<endl; cout<<"|num <number> |find position |"<<endl; cout<<"|name <name> |find name |"<<endl; cout<<"|artist <artist> |find artist |"<<endl; cout<<"|time <hh:mm:ss> |find time |"<<endl; cout<<"|jear (jear<9999)|find jear |"<<endl; cout<<"|--------------------------------|"<<endl; cout<<"| Operation of library |"<<endl; cout<<"|--------------------------------|"<<endl; cout<<"|re <num> |rename num |"<<endl; cout<<"|del <num> |delete num |"<<endl; cout<<"|all |print all |"<<endl; cout<<"|add <number> |add position |"<<endl; cout<<"|--------------------------------|"<<endl; cout<<"|exit |Exit |"<<endl; cout<<"|________________________________|"<<endl; } void helpref(void){ cout<<"__________________________________"<<endl; cout<<"| Help Rename |"<<endl; cout<<"|--------------------------------|"<<endl; cout<<"|Not rename for \"Name\"-enter NaN |"<<endl; cout<<"| ------ for \"Artist\"-enter NaN |"<<endl; cout<<"| ------ for \"Time\"-enter NaN |"<<endl; cout<<"| ------ for \"Jear\"-enter 0000 |"<<endl; cout<<"|________________________________|"<<endl; } void numf(int n, int pos){ if(pos>n-1&&n!=0){ cout<<"Position "<<muslib[n-1].num<<endl; cout<<"Name "<<muslib[n-1].name<<endl; cout<<"Artist "<<muslib[n-1].artist<<endl; cout<<"Time "<<muslib[n-1].time<<endl; cout<<"Jear "<<muslib[n-1].jear<<endl; cout<<endl;} else cout<<"Item position "<<n<<" is missing!\n"<<endl; } void namef(std::string str,int pos){ int notfound=0; for(int i=0; i<pos; i++){ if(str==muslib[i].name){ cout<<"Position "<<muslib[i].num<<endl; cout<<"Name "<<muslib[i].name<<endl; cout<<"Artist "<<muslib[i].artist<<endl; cout<<"Time "<<muslib[i].time<<endl; cout<<"Jear "<<muslib[i].jear<<"\n"<<endl; notfound=notfound+1; } } if(notfound==0) cout<<"Elements named \""<<str<<"\" not found!\n"<<endl; } void artistf(std::string str,int pos){ int notfound=0; for(int i=0; i<pos; i++){ if(str==muslib[i].artist){ cout<<"Position "<<muslib[i].num<<endl; cout<<"Name "<<muslib[i].name<<endl; cout<<"Artist "<<muslib[i].artist<<endl; cout<<"Time "<<muslib[i].time<<endl; cout<<"Jear "<<muslib[i].jear<<"\n"<<endl; notfound=notfound+1; } } if(notfound==0) cout<<"Artist \""<<str<<"\" not found!\n"<<endl; } void timef(std::string str,int pos){ int notfound=0; for(int i=0; i<pos; i++){ if(str==muslib[i].time){ cout<<"Position "<<muslib[i].num<<endl; cout<<"Name "<<muslib[i].name<<endl; cout<<"Artist "<<muslib[i].artist<<endl; cout<<"Time "<<muslib[i].time<<endl; cout<<"Jear "<<muslib[i].jear<<"\n"<<endl; notfound=notfound+1; } } if(notfound==0) cout<<"Elements for time \""<<str<<"\" not found!\n"<<endl; } void jearf(short jear, int pos){ int notfound=0; for(int i=0; i<pos; i++){ if(jear==muslib[i].jear){ cout<<"Position "<<muslib[i].num<<endl; cout<<"Name "<<muslib[i].name<<endl; cout<<"Artist "<<muslib[i].artist<<endl; cout<<"Time "<<muslib[i].time<<endl; cout<<"Jear "<<muslib[i].jear<<"\n"<<endl; notfound=notfound+1; } } if(notfound==0) cout<<"Elements for jear \""<<jear<<"\" not found!\n"<<endl; } void ref(int num,int pos){ if(num>0&&num<=pos){ std::string str; short jear; cout<<"Rename help - enter command \"help_re\"!"<<endl; cout<<"Position "<<muslib[num-1].num<<endl; cout<<"Enter new Name: "; cin>>str; if(str!="NaN") muslib[num-1].name=str; cout<<"Enter new Artist: "; cin>>str; if(str!="NaN") muslib[num-1].artist=str; cout<<"Enter new Time: "; cin>>str; if(str!="NaN") muslib[num-1].time=str; cout<<"Enter new Jear: "; cin>>jear; if(jear!=0000) muslib[num-1].jear=jear; } else cout<<"Position not found!\n"<<endl; } void allf(int pos){ for(int i=0; i<pos; i++){ cout<<"Position "<<muslib[i].num<<endl; cout<<"Name "<<muslib[i].name<<endl; cout<<"Artist "<<muslib[i].artist<<endl; cout<<"Time "<<muslib[i].time<<endl; cout<<"Jear "<<muslib[i].jear<<"\n"<<endl; } } int delf(int num,int pos){ muslib[num-1].num=0; for(int i=0; i<pos; i++){ if(muslib[i].num!=i+1){ muslib[i].num=i+1; muslib[i].name=muslib[i+1].name; muslib[i].artist=muslib[i+1].artist; muslib[i].time=muslib[i+1].time; muslib[i].jear=muslib[i+1].jear; muslib[i+1].num=0; } } } 


The second option is also not perfect, but compared to the first one it will be much better. In addition, here I was armed with a translator and tried to write as beautifully as possible. All criticism and the possibility of simplification or improvement will accept with understanding.

Source: https://habr.com/ru/post/168075/


All Articles