1.深夜飞来杭州粉丝的第一个提示
我是c++小白。只有一个星期的学过的经验。2020-7.31在粉丝群里深夜十二点时问了这个问题:
“这个main函数我觉得非常糟糕。我需要d在2到9之间。但是这里怎么也看不出会是2-9”
意外的得到了精准的回答。“这里是抽取字符0123456789 ,通过ASCII表0123456789字符对应48 49 50…来处理的”。杭州的粉丝。
2.来自CSDN博客的助攻
接着我上网搜索”main函数 argc,argv“。搜到了牧野的csdn博客。他解释的果然很专业。难怪有几千个粉丝。感兴趣的人可以搜一下。我把他举的几个例子一一在VS里面运行。
3.main函数长这样,广州粉丝的进一步透彻答疑
我的原程序是这样的。这是丹麦的一位学者分类2维到8维的多面体的一个730行代码的main函数部分。
int main(int argc, char *argv[])
{
// Read arguments.
// Only one argument allowed: an integer between 2 and 9.
if (argc != 2) //判断参数个数。必须是两个,多了少了都不行.//取第二个参数,这里所谓第二个参数是指argv[1],因为argv[0]是系统自己加的,不是你输的,你输的是第二个,这个参数是你运行程序是加的,比如sfp.exe 3.加的是3.
return 0;
string st(argv[1]);//argv[1]数据类型是字符串,而且程序注释说了是0-9.程序先将argv[1]字符串转成string对象string(argv[1]).string对象有很多字符串操作的函数,比较方便,所以他要这样转,不然用指针不方便.
if (st.size() != 1) //if(st.size()!=1)是说输入的只是一个字符,多了不行
return 0;
if (isdigit(st[0]) == false)// if(isdigit(st[0]))是说第一个字符是不是数字
return 0;
int d_char = st[0];
d = d_char - 48;//int d_char=st[0]是将第一个字符转成整形,所以后面减了个48得到实际值.48的ascii吗是字符"0".字符"2"与字符0相差2,所以d就等于2了
if (d < 2)
return 0;
// Open output files
char outfile1[] = “fanovertd”;//文件名字符串最后一个字符是d,用d加上48就是字符2了,这样文件名就确定了.说错,文件名中的d是被换了,换成数字
outfile1[8] = d_char;
fanovert.open(outfile1);
char outfile2[] = “msgd”;
outfile2[3] = d_char;//是说outfilt2[3]=d_char,outfile2一共有4个字符,所以你输几,文件名就会有几。用调试器运行的话,要修改命令行参数。可能默认是2,你没有改,就按2运行的。
msg.open(outfile2);
msg << endl
<< "SFP-algorithm: dimension " << d << endl
<< endl;
number_of_equivalence_classes.assign(3 * d + 1, 0);
// Initialize and commence construction
sfp();
// Final output
msg << “Construction finished!” << endl
<< endl;
msg << “Up to isomorphism, the program found” << endl
<< endl;
for (int i = d + 1; i < 3 * d + 1; ++i)
msg << number_of_equivalence_classes[i] << " with " << i << " vertices." << endl << endl;msg<< endl;
msg << "Total number of smooth Fano " << d << "-polytopes: " << (int)accumulate(number_of_equivalence_classes.begin(), number_of_equivalence_classes.end(), 0) << endl
<< endl;
// Close files
fanovert.close();
msg.close();
}
作者:千条数学课收藏夹分类
https://www.bilibili.com/read/cv6993583
出处: bilibili