第一个项目评估 [英] first program evaluation

查看:60
本文介绍了第一个项目评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我刚刚制作了我的第一个c ++程序,我想知道是否有更好的东西可以做得更好。另外,exe大约500 kb,我可以把它变成
更小吗?


谢谢!

Pieter



#include< string>

#include< fstream>

使用命名空间std;


int main()

{

字符串缓冲区;

string includebuffer;

string bestand;

string configin;

string configout;


ifstream config(" config.txt");

getline(config,configin);

getline(config,configout);

config.close();


ifstream infile(configin.c_str());

ofstream outfile(configout.c_str());


while(getline(infile,buffer) ))

{

if(buffer =="")

{

outfile<< ; " " <<结束;

}

其他

{

if(buffer.substr(1,7)==" ;包括)

{

bestand = buffer.substr(9,(buffer.find_first_of("}) - ) - 9));

bestand + =" .tex" ;;

ifstream includefile(bestand.c_str());

while(getline(includefile,includebuffer))

{

outfile<< includebuffer<< endl;

}

includefile.close();

}

else

{

outfile<<缓冲区<<结束;

}

}

}


infile.close();

outfile.close();

返回0;

}

Hi,

I just made my first c++ program, and I would like to know if there are
things I can do better. Also, the exe is about 500 kb, can I make it
smaller?

Thanks!
Pieter


#include <string>
#include <fstream>
using namespace std;

int main()
{
string buffer;
string includebuffer;
string bestand;
string configin;
string configout;

ifstream config("config.txt");
getline(config, configin);
getline(config, configout);
config.close();

ifstream infile(configin.c_str());
ofstream outfile(configout.c_str());

while (getline(infile, buffer))
{
if (buffer == "")
{
outfile << " " << endl;
}
else
{
if (buffer.substr(1,7) == "include")
{
bestand = buffer.substr(9, (buffer.find_first_of("}") - 9));
bestand += ".tex";
ifstream includefile(bestand.c_str());
while (getline(includefile, includebuffer))
{
outfile << includebuffer << endl;
}
includefile.close();
}
else
{
outfile << buffer << endl;
}
}
}

infile.close();
outfile.close();
return 0;
}

推荐答案

Pieter Provoost写道:
Pieter Provoost wrote:
我刚刚制作了我的第一个c ++程序,我想知道是否有更好的事情。


为每个功能编写单元测试。

此外,exe大约500 kb,我可以使它更小吗?


这是这个新闻组的常见问题解答。好消息是,当您的程序获得更大的可执行文件大小不会按比例增长时。你可以在达到501 kb之前添加很多

的代码。可执行文件很大,因为许多功能

你没有使用链接。

#include< string>
#include< fstream>
using namespace std;


不要使用''使用命名空间''。只邀请您需要的标识符,例如使用std :: string

。随着程序变得庞大,许多''使用命名空间''

语句将导致重大问题,包括编译器中的错误,以及

名称冲突。

int main()
{> string string;
string includebuffer;
string bestand;
string configin;
string configout;


将每一个尽可能接近使用它们的代码。

ifstream config(" config.txt");
getline(config,configin);
getline(config,configout);
config.close();


将这些功能切换成功能。但是......

ifstream infile(configin.c_str());
ofstream outfile(configout.c_str());


....这些可能是命令行参数。研究argv和argc。

而(getline(infile,缓冲区))


很好。此时有些人使用infile>>字符串或sc​​anf。

都是用户敌对的。

{
if(buffer =="")
{
outfile<< " " << ENDL;


为什么小空间?

}

{
if(buffer.substr(1,7) ==" include")
{
bestand = buffer.substr(9,(buffer.find_first_of("}") - 9));


该行太多了。拉出第一个find_first_of,并将其

结果放入变量中,并为该变量命名。

bestand + =" .tex";
ifstream includefile(bestand.c_str());
while(getline(includefile,includebuffer))
{
outfile<< includebuffer<< endl;
}
includefile.close();
}
其他
{
outfile<<缓冲区<< ENDL;


如果你没有那个if(buffer =="")那么,那么控制流将会是
降到这里,然后做同样的事情。

}
}

infile.close();
outfile.close();
返回0;
}
I just made my first c++ program, and I would like to know if there are
things I can do better.
Write unit tests for every function.
Also, the exe is about 500 kb, can I make it
smaller?
That is a FAQ for this newsgroup. The good news is as your program gets
larger the executable size will not grow proportionally. You can add a lot
of code before you hit 501 kb. The executable is big because many features
you did not use got linked in.
#include <string>
#include <fstream>
using namespace std;
Don''t use ''using namespace''. Invite only the identifiers you need in, such
as using std::string. As a program gets huge, many ''using namespace''
statements will cause major problems, including bugs in your compiler, and
name collisions.
int main()
{
string buffer;
string includebuffer;
string bestand;
string configin;
string configout;
Put each of these as close as possible to the code that uses them.
ifstream config("config.txt");
getline(config, configin);
getline(config, configout);
config.close();
Cut these up into functions. But...
ifstream infile(configin.c_str());
ofstream outfile(configout.c_str());
....these could have been command line arguments. Research argv and argc.
while (getline(infile, buffer))
Nice. Some folks at this point use either infile >> string, or scanf. Both
are user hostile.
{
if (buffer == "")
{
outfile << " " << endl;
Why the little space?
}
else
{
if (buffer.substr(1,7) == "include")
{
bestand = buffer.substr(9, (buffer.find_first_of("}") - 9));
That line does too much. Pull out the first find_first_of, and put its
result in a variable, and give that variable a name.
bestand += ".tex";
ifstream includefile(bestand.c_str());
while (getline(includefile, includebuffer))
{
outfile << includebuffer << endl;
}
includefile.close();
}
else
{
outfile << buffer << endl;
If you did not have the if(buffer == "") up there, then control flow would
drop to here, and do the same thing.
}
}
}

infile.close();
outfile.close();
return 0;
}




祝你好运!


-

Phlip
http://industrialxp.org/community/bi。 ..UserInterfaces




" Phlip" < pH值******* @ yahoo.com> schreef in bericht

news:vw ***************** @ newssvr16.news.prodigy.co m ...

"Phlip" <ph*******@yahoo.com> schreef in bericht
news:vw*****************@newssvr16.news.prodigy.co m...
Pieter Provoost写道:
Pieter Provoost wrote:
我刚刚制作了我的第一个c ++程序,我想知道是否有更好的东西。
I just made my first c++ program, and I would like to know if there are
things I can do better.



为每个函数编写单元测试。



Write unit tests for every function.

此外,exe大约500 kb,我可以使它更小吗?
Also, the exe is about 500 kb, can I make it
smaller?



这是这个新闻组的常见问题解答。好消息是,当您的程序变得更大时,可执行文件大小不会按比例增长。你可以在达到501 kb之前添加很多代码。可执行文件很大,因为您没有使用的许多功能已被链接。



That is a FAQ for this newsgroup. The good news is as your program gets
larger the executable size will not grow proportionally. You can add a lot
of code before you hit 501 kb. The executable is big because many features
you did not use got linked in.

#include< string>
#include< fstream>
using namespace std;
#include <string>
#include <fstream>
using namespace std;



不要使用''using namespace''。仅邀请您需要的标识符,例如使用std :: string。随着程序变得庞大,许多''使用命名空间''
语句将导致重大问题,包括编译器中的错误和名称冲突。



Don''t use ''using namespace''. Invite only the identifiers you need in, such
as using std::string. As a program gets huge, many ''using namespace''
statements will cause major problems, including bugs in your compiler, and
name collisions.




好​​的,我必须要查看它(不知道如何使用它)。



Ok, I''ll have to look that up (don''t know how to use it).

int main()
{> string string;
string includebuffer;
string bestand;
string configin;
string configout;
int main()
{
string buffer;
string includebuffer;
string bestand;
string configin;
string configout;



将每个这些尽可能接近使用它们的代码。



Put each of these as close as possible to the code that uses them.

ifstream config(" config.txt");
getline( config,configin);
getline(config,configout);
config.close();
ifstream config("config.txt");
getline(config, configin);
getline(config, configout);
config.close();



将它们切换成函数。但是......



Cut these up into functions. But...

ifstream infile(configin.c_str());
ofstream outfile(configout.c_str());
ifstream infile(configin.c_str());
ofstream outfile(configout.c_str());



......这些可能是命令行参数。研究argv和argc。



...these could have been command line arguments. Research argv and argc.




它们最初是命令行参数,但我更喜欢使用

配置文件,然后点击图标。我停止使用dos命令提示符

,因为他们引入了名称为documents and

settings的目录......



They were command line arguments at first, but I prefer to work with the
config file and jut click the icon. I stopped using the dos command prompt
since they introduced directories with names such as "documents and
settings"...

while(getline(infile,buffer))
while (getline(infile, buffer))



很好。此时有些人使用infile>>字符串或sc​​anf。两者都是用户敌对的。



Nice. Some folks at this point use either infile >> string, or scanf. Both
are user hostile.

{
if(buffer =="")
{
outfile<< " " << endl;
{
if (buffer == "")
{
outfile << " " << endl;



为什么小空间?



Why the little space?




好​​吧,当我没有这条线时,我曾经当程序

试图处理空行时出错。也许是因为我不能将子串应用于

一个空字符串?



Well, when I didn''t have this line, I used to get an error when the program
tried to process a blank line. Maybe it''s because I can''t apply substring to
an empty string?

}

{
if(buffer.substr(1,7)==" include")
{
bestand = buffer.substr(9,( buffer.find_first_of("}") - 9));
}
else
{
if (buffer.substr(1,7) == "include")
{
bestand = buffer.substr(9, (buffer.find_first_of("}") - 9));



这一行做得太多了。拉出第一个find_first_of,并将其结果放在变量中,并为该变量命名。



That line does too much. Pull out the first find_first_of, and put its
result in a variable, and give that variable a name.

bestand + =" .tex" ;;
ifstream includefile(bestand.c_str());
while(getline(includefile,includebuffer))
{
outfile<< includebuffer<< endl;
}
includefile.close();
}
其他
{
outfile<<缓冲区<< endl;
bestand += ".tex";
ifstream includefile(bestand.c_str());
while (getline(includefile, includebuffer))
{
outfile << includebuffer << endl;
}
includefile.close();
}
else
{
outfile << buffer << endl;



如果你没有if(buffer =="")那么,那么控制流将会落到这里,并做同样的事情。



If you did not have the if(buffer == "") up there, then control flow would
drop to here, and do the same thing.




请参阅上面的评论。



See my comments above.

} }

infile.close();
outfile.close();
返回0;
}
}
}
}

infile.close();
outfile.close();
return 0;
}

祝你好运!

-
Phlip
http://industrialxp.org/community/bi...UserInterfaces



非常感谢!

Pieter


Thank you very much!
Pieter




" Pieter Provoost" < PI ************ @ tiscali.be> schreef in bericht

news:40 ********************** @ news.skynet.be ...

"Pieter Provoost" <pi************@tiscali.be> schreef in bericht
news:40**********************@news.skynet.be...

Phlip < pH值******* @ yahoo.com> schreef in bericht
新闻:vw ***************** @ newssvr16.news.prodigy.co m ...

"Phlip" <ph*******@yahoo.com> schreef in bericht
news:vw*****************@newssvr16.news.prodigy.co m...
Pieter Provoost写道:
Pieter Provoost wrote:
#include< string>
#include< fstream>
使用命名空间std;
#include <string>
#include <fstream>
using namespace std;



不要使用''使用命名空间''。仅邀请您需要的标识符,
,例如使用std :: string。随着程序变得庞大,许多''使用命名空间''
语句将导致重大问题,包括编译器中的错误,
和名称冲突。



Don''t use ''using namespace''. Invite only the identifiers you need in, such as using std::string. As a program gets huge, many ''using namespace''
statements will cause major problems, including bugs in your compiler, and name collisions.



好的,我必须要查看(不知道如何使用它)。



Ok, I''ll have to look that up (don''t know how to use it).




找到它。这样做:


使用std :: string;

使用std :: endl;

使用std :: ifstream ;

使用std :: ofstream;

使用std :: getline;



Found it. This does the job:

using std::string;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::getline;


这篇关于第一个项目评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆