黑马程序员--养成程序规范书写的习惯

本文强调了在C语言编程中遵循代码规范的重要性,包括变量定义、括号使用、声明与语句分离、代码对齐等,以提高代码的可读性和可维护性。通过遵循这些规则,可以减少错误并提高代码质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. ------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

 

养成程序的规范化书写的好习惯

       在C语言中我们不按编译器的规则书写就会报错,为了减少甚至避免在编程时出错,我们在书写程序时就要注意代码的规范化书写。

比如:

一、变量要先定义后使用,不定义就直接使用就会出错。

int a = 4;

a+=5;

正确,但是如果没有定义a变量,直接用

a+=7;

则是错误的,因为变量先定义后使用就是规则。

再如

二、的使用,在编程的时候一定要一对一对的书写,这样就不会漏掉右边的}。每对{}的列要对齐,且{和}要独占一行,这样程序的可读性和美观

性都会更好,让人觉得条理很清晰。

三、声明和语句要分开写。

# include <stdio.h>

int main(viod)

{                                         //独占一行

     int mystrcmy(char*,char*);

       char  str1[20];

       char  str2[20];           //声明与语句空一行

 

        clrscr();

         gets(str1);

        gets(str2);

       printf("%d\n",mystrcmp(str1,str2));

       

return 0;

}

 

int mystrcmp(char*p, char*q)  //函数空一行分开

{

while(*p==*q && *p!='\0')

       {

            p++;

            q++;

       }

return (*p-*q);

}

四、地位相等的语句按列对齐功能嵌套按3个空格缩进。

当然还有很多要注意的细节,这里就不一一说出,我也是一个刚开始学习编程的新手,希望和大家共同学习,共同进步,今后能编出高质量的代码。

### Spring Security入门案例与黑马程序员教程 Spring Security 是一个强大的安全框架,用于保护基于 Java 的应用程序。它提供了认证、授权和防止常见攻击的功能。以下是一个简单的入门案例,结合了 Spring Boot 和 Spring Security 的基本配置。 #### 1. 引入依赖 在 `pom.xml` 文件中添加 Spring Security 的启动器依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 引入此依赖后,Spring Security 将自动为项目提供基础的安全保护[^2]。 #### 2. 配置安全规则 创建一个配置类来定义访问规则。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许所有用户访问 /public 路径 .anyRequest().authenticated() // 其他路径需要认证 .and() .formLogin() .loginPage("/login") // 自定义登录页面 .permitAll() .and() .logout() .permitAll(); } } ``` 上述代码定义了访问规则:`/public/**` 路径对所有用户开放,而其他路径需要经过身份验证才能访问[^2]。 #### 3. 创建用户认证信息 可以通过内存中的用户数据进行简单认证。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class AuthConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN"); } } ``` 这里定义了两个用户:`user` 和 `admin`,分别拥有不同的角色权限[^2]。 #### 4. 创建控制器 为了测试安全规则,可以创建一个简单的控制器: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/public/hello") public String publicHello() { return "Hello, this is a public resource!"; } @GetMapping("/private/hello") public String privateHello() { return "Hello, this is a private resource!"; } } ``` 访问 `/public/hello` 不需要认证,而访问 `/private/hello` 则需要认证。 #### 5. 测试应用 运行 Spring Boot 应用程序后,尝试访问以下 URL: - `/public/hello`:可以直接访问。 - `/private/hello`:将被重定向到登录页面,输入用户名和密码后可以访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值