Is this from a good coder? or a rubbish one? Whichever way, it was paid for hisservices, just want to know if its worth while....ThanksJen x#include <iostream.h>#include <conio.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fstream.h>#include <direct.h>void main(){char current_dir[50],tokens[10][20],input[300]; // for storing currentdirectory , tokens , and inputint i,j,k;char x;char dir_ex[30];ifstream fin;cout<<"\n\n\t\t *** COMMAND PROMPT *** \n\n ";do // repeat till token = EXIT{_getcwd(current_dir,50); // gets current directorycout<<"\n\n\n"<<current_dir;cout<<"> "; // puts it as a prompt//cin>>input;cin>>x;gets(input); // reads command//tokens[0][0] =NULL;int length=strlen(input);j=0;for(i=0;i< length;i++) // do till end of command{k=0;while( (input[i]== '' '' )&&(i<strlen(input)) ) // parse command to tokensbased upon spaces{i++;}while( (input[i]!= '' '' )&&(i<strlen(input)) ){tokens[j][k] = input[i];k++;i++;}tokens[j][k] = NULL;cout<<"\n Token "<<j+1<<" : "<<tokens[j]; // show tokenj++;}if( strcmpi(tokens[0],"DIR")==0) // if token is DIR{strcat(dir_ex,"DIR ");strcat(dir_ex,tokens[1]);strcat(dir_ex,NULL);system( dir_ex);cout<<"\n\n\t Listing directory : ";cout<<" "<<current_dir<<"\n\n";}if( strcmpi(tokens[0],"PWD")==0) // if token is PWD{cout<<"\n\n\t Current directory : ";cout<<" "<<current_dir<<"\n\n"; // puts it as a prompt}if( strcmpi(tokens[0],"CD")==0) // if token is CD{chdir(tokens[1]); // change directorycout<<"\n\n\t Directory changed to : ";getcwd(current_dir,50);cout<<" "<<current_dir<<"\n\n"; // shows changed directory}if( strcmpi(tokens[0],"TYPE")==0) // if token is TYPE{fin.open(tokens[1]);if(!fin){cout<<"\n\n\n FILE NOT FOUND ! \n\n ";}else{cout<<"\n\n\n\t *** PRINTING FILE *** \n\n ";while(!fin.eof()) // shows file contents{fin.read((char*)&x,1);cout<<x;}}fin.close();}if( strcmpi(tokens[0],"EXIT")==0) // if command is exit{exit(1);}}while(strcmpi(tokens[0],"EXIT")!=0);} 解决方案exit(1) is guaranteed never to return, so there is no way that the while test above ever is false.Not only does this person not know C or C++ adequately, he doesn''t seem to understandhow to program at all. His gaffs and clutter go far beyond lack of knowledge of C++.If this code actually ran, it''s purely coincidental.Well it depends on his constraints - the compiler to use, etc. It looks OK,but it looks like a C programmer only dipping his toes in C++ code. I wouldsay that his "EXIT" strategy is a bit dubious. for his Well it depends on his constraints - the compiler to use, etc. It looks OK, but it looks like a C programmer only dipping his toes in C++ code. I would say that his "EXIT" strategy is a bit dubious.It''s lousy C code too. In addition to the horrendous control constructs andinefficiencies, there are over a half a dozen invocations of undefined behavioreven in the C constructs. 这篇关于关于这段代码的意见请大家......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 11:03