林锐博士的《高质量C++/C编程指南》摘录

需积分: 48 0 下载量 69 浏览量 更新于2024-09-26 收藏 637KB PDF 举报
"林博士的《CC++编程指南》是一本针对C++和C语言的高质量编程书籍,适合面试准备和提升编程技能。本书由林锐博士编写,经过朱洪海的审查,旨在提供精确和规范的编程指导。书中涵盖了从文件结构、程序版式、命名规则、表达式和基本语句,到常量、函数设计、内存管理等多个核心知识点。" 在《CC++编程指南》中,林博士详细讲解了以下内容: 1. **文件结构**:书中强调了版权和版本声明的重要性,以及头文件和定义文件的结构。头文件用于包含常量、类型定义、函数原型等,而定义文件则包含了实际的函数实现。此外,还提到了合理的目录结构对于项目维护的重要性。 2. **程序的版式**:版式是代码可读性的关键,包括空行的使用、代码行的长度限制、空格的恰当使用、对齐方式、长行的拆分、修饰符的位置以及注释的编写规则。良好的版式能让代码更易读,提高团队合作效率。 3. **命名规则**:书中设定了通用的命名规则,并针对不同的操作系统环境(如Windows和UNIX)提供了具体的命名建议,以保持代码的一致性和可读性。 4. **表达式和基本语句**:这部分深入讨论了运算符的优先级、复合表达式、条件语句(如IF)、循环语句的效率优化(如FOR和WHILE)、SWITCH语句和GOTO语句的使用。理解这些基础能提升程序的逻辑清晰度和执行效率。 5. **常量**:解释了常量在代码中的作用,比较了CONST和#DEFINE的用法,制定了常量定义的规则,并探讨了在类中定义常量的方法。 6. **函数设计**:林博士详细阐述了函数参数、返回值、函数内部实现的规则,提倡使用断言以增强代码的健壮性,并对比了引用和指针的区别,提供了关于何时选择使用它们的建议。 7. **内存管理**:这一部分详细介绍了内存分配的方式(如malloc和new),以及如何避免常见的内存错误。讨论了指针与数组的差异,指针参数如何传递内存,free和delete的正确使用,动态内存的生命周期,防止野指针,以及为何在有malloc/free的情况下仍需要new/delete。 这本书不仅是初学者理解C++和C编程基础的优秀教程,也是资深开发者提高代码质量和遵循最佳实践的宝贵参考。通过学习这些内容,读者将能够编写出更加高效、可维护且易于理解的C++和C代码。

function [num,Period, Frequency, Density, CL95]=spectrum(x,mLAG) %%% function for power spectral analysis % usage: [num,Period, Frequency, density, cl95]=spectrum(x,mLAG) % Gong Daoyi 2003.12 xLEN=length(x); SER=x;N=xLEN;mLAGWK=mLAG;mLEN=N;J=mLAG;J1=J+1; %c calculating auto-connection coefficient A=0.0; C=0.; for I=1:N A=A+SER(I);end % I A=A/N; for I=1:N SER(I)=SER(I)-A; C=C+SER(I).^2; end % I C=C/N; for L=1:J CC(L)=0.0; for I=1:N-L CC(L)=CC(L)+SER(I)*SER(I+L); end %I CC(L)=CC(L)./(N-L); CC(L)=CC(L)/C; end %L C=1.0; %c estimating rude power spectra SPE(1)=0.0; for L=1:J-1 SPE(1)=SPE(1)+CC(L); end %L SPE(1)=SPE(1)./J+(C+CC(J))./(2*J); for L=1:J-1 % DO 210 L=1,J-1 SPE(L+1)=0.; for I=1:J-1 SPE(L+1)=SPE(L+1)+CC(I)*cos(pi*L*I/J); end % I SPE(L+1)=2*SPE(L+1)./J+C./J+(-1).^L*CC(J)./J; end % 210 L SPE(J1)=0.0; for I=1:J-1 SPE(J1)=SPE(J1)+(-1).^I*CC(J); end %I SPE(J1)=SPE(J1)/J+(C+(-1).^J*CC(J))/(2*J); %c smoothing power spectra PS(1)=.54*SPE(1)+.46*SPE(2); for L=2:J PS(L)=.23*SPE(L-1)+.54*SPE(L)+.23*SPE(L+1); end %L PS(J1)=.46*SPE(J)+.54*SPE(J1); %c statistical significence of PS W=0.0; for L=1:J-1 W=W+SPE(L+1); end %L W=W/J+(SPE(1)+SPE(J1))/(2*J); if (J > fix(N/2)) W=2.57*W; end if(J == fix(N/2)) W=2.49*W; end if(J < fix(N/2) & J > fix(N/3)) W=2.323*W; end if (J == fix(N/3)) W=2.157*W; end if (J < fix(N/3)) W=1.979*W; end %c the red noice examination for L=1:J1 SK(L)=W*(1-CC(1).^2)/(1+CC(1).^2-2*CC(1)*cos(3.14159*(L-1)/J)); end % L if (CC(1) > 0 & CC(1) >= CC(2) ) %c the white noice examination else for L=1:J1 SK(L)=W; end %L end % if %c calculating the length of cycle T(1)=NaN; for L=2:J1 T(L)=(2.0*J)/(L*1.0-1.0); end % L num=1:J+1;num=num(:)-1; Period=T(:); Frequency=1./T(:); Density=PS(:); CL95=SK(:);

2023-06-01 上传

while any(openSet(:) > 0) % Find the minimum fScore within the open set [~, current] = min(fScore(:)); % If we've reached the goal if current == goal % Get the full path and return it final = get_path(cameFrom, current); return end % Linear index -> row, col subscripts rc = rem(current - 1, mapSize(1)) + 1; cc = (current - rc) / mapSize(1) + 1; % Remove CURRENT from openSet openSet(rc, cc) = false; % Place CURRENT in closedSet closedSet(rc, cc) = true; fScore(rc, cc) = inf; gScoreCurrent = gScore(rc, cc) + costs(rc, cc); % Get all neighbors of CURRENT. Neighbors are adjacent indices on % the map, including diagonals. % Col 1 = Row, Col 2 = Col, Col 3 = Distance to the neighbor n_ss = [ ... rc + 1, cc + 1, S2 ; ... rc + 1, cc + 0, 1 ; ... rc + 1, cc - 1, S2 ; ... rc + 0, cc - 1, 1 ; ... rc - 1, cc - 1, S2 ; ... rc - 1, cc - 0, 1 ; ... rc - 1, cc + 1, S2 ; ... rc - 0, cc + 1, 1 ; ... ]; % keep valid indices only valid_row = n_ss(:,1) >= 1 & n_ss(:,1) <= mapSize(1); valid_col = n_ss(:,2) >= 1 & n_ss(:,2) <= mapSize(2); n_ss = n_ss(valid_row & valid_col, :); % subscripts -> linear indices neighbors = n_ss(:,1) + (n_ss(:,2) - 1) .* mapSize(1); % only keep neighbors in the map and not in the closed set ixInMap = map(neighbors) & ~closedSet(neighbors); neighbors = neighbors(ixInMap); % distance to each kept neighbor dists = n_ss(ixInMap, 3); % Add each neighbor to the open set openSet(neighbors) = true; % TENTATIVE_GSCORE is the score from START to NEIGHBOR. tentative_gscores = gScoreCurrent + costs(neighbors) .* dists; % IXBETTER indicates where a better path was found ixBetter = tentative_gscores < gScore(neighbors); bestNeighbors = neighbors(ixBetter); % For the better paths, update scores cameFrom(bestNeighbors) = current; gScore(bestNeighbors) = tentative_gscores(ixBetter); fScore(bestNeighbors) = gScore(bestNeighbors) + compute_cost(mapSize, bestNeighbors, gr, gc); end % while end

2023-05-25 上传