C语言代码风格

个人比较喜欢的代码风格,仅供参考

文章目录

空格

1、导入头文件时,include和头文件之间加空格

#include <cstdio>

2、逗号(,)后面加空格。

int i, j;

顿号(;)如果不是一行的结束时,后面加空格

int p = que.front(); que.pop();

3、双目运算符(+、-、*、/、=、==、+=、^、&、&&、|、||、>>等)前后加空格

int res = a + b;

单目运算符(!、~、++、- -等)前后不加空格

while (t--) {
   
	// operate
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以了解到KTV点歌系统需要实现的功能包括管理员对歌曲进行管理、统计等操作,以及用户根据关键字或风格等对歌曲进行查询等操作。下面是一个简单的KTV点歌系统的C语言代码示例,实现了管理员和用户的基本功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SONGS 1000 #define MAX_NAME 50 #define MAX_SINGER 50 #define MAX_STYLE 20 typedef struct { char name[MAX_NAME]; char singer[MAX_SINGER]; char style[MAX_STYLE]; int score; } Song; Song songs[MAX_SONGS]; int num_songs = 0; void add_song() { if (num_songs >= MAX_SONGS) { printf("The song list is full.\n"); return; } Song song; printf("Please enter the name of the song: "); scanf("%s", song.name); printf("Please enter the name of the singer: "); scanf("%s", song.singer); printf("Please enter the style of the song: "); scanf("%s", song.style); song.score = 0; songs[num_songs++] = song; printf("The song has been added successfully.\n"); } void delete_song() { char name[MAX_NAME]; printf("Please enter the name of the song you want to delete: "); scanf("%s", name); int i; for (i = 0; i < num_songs; i++) { if (strcmp(songs[i].name, name) == 0) { int j; for (j = i; j < num_songs - 1; j++) { songs[j] = songs[j + 1]; } num_songs--; printf("The song has been deleted successfully.\n"); return; } } printf("The song is not found.\n"); } void search_song() { char keyword[MAX_NAME]; printf("Please enter the keyword you want to search: "); scanf("%s", keyword); int i; for (i = 0; i < num_songs; i++) { if (strstr(songs[i].name, keyword) != NULL || strstr(songs[i].singer, keyword) != NULL || strstr(songs[i].style, keyword) != NULL) { printf("%s - %s (%s) [%d]\n", songs[i].name, songs[i].singer, songs[i].style, songs[i].score); } } } void rate_song() { char name[MAX_NAME]; printf("Please enter the name of the song you want to rate: "); scanf("%s", name); int i; for (i = 0; i < num_songs; i++) { if (strcmp(songs[i].name, name) == 0) { int score; printf("Please enter your score (0-10): "); scanf("%d", &score); if (score < 0 || score > 10) { printf("Invalid score.\n"); return; } songs[i].score += score; printf("Your score has been added successfully.\n"); return; } } printf("The song is not found.\n"); } void show_statistics() { int i; int total_score = 0; int num_rated_songs = 0; for (i = 0; i < num_songs; i++) { if (songs[i].score > 0) { total_score += songs[i].score; num_rated_songs++; } } if (num_rated_songs == 0) { printf("No song has been rated yet.\n"); } else { printf("The average score of rated songs is %.2f.\n", (float)total_score / num_rated_songs); } } int main() { while (1) { printf("Welcome to KTV song system.\n"); printf("Please select your role:\n"); printf("1. Administrator\n"); printf("2. User\n"); printf("3. Exit\n"); int role; scanf("%d", &role); if (role == 1) { printf("Please enter the password: "); char password[10]; scanf("%s", password); if (strcmp(password, "admin") != 0) { printf("Incorrect password.\n"); continue; } while (1) { printf("Administrator menu:\n"); printf("1. Add song\n"); printf("2. Delete song\n"); printf("3. Show statistics\n"); printf("4. Back to the previous menu\n"); int choice; scanf("%d", &choice); switch (choice) { case 1: add_song(); break; case 2: delete_song(); break; case 3: show_statistics(); break; case 4: goto end; default: printf("Invalid choice.\n"); break; } } } else if (role == 2) { while (1) { printf("User menu:\n"); printf("1. Search song\n"); printf("2. Rate song\n"); printf("3. Back to the previous menu\n"); int choice; scanf("%d", &choice); switch (choice) { case 1: search_song(); break; case 2: rate_song(); break; case 3: goto end; default: printf("Invalid choice.\n"); break; } } } else if (role == 3) { break; } else { printf("Invalid role.\n"); } } printf("Goodbye.\n"); end: return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值