经典编程900例(c语言)(第一篇)

专升本考上,打算学习新的东西,偶然找到这些例子,算是给c语言画上一个句号

先做几个格式约定声明吧

  1. 代码上方注释是解释说明
  2. 代码后面注释是输出结果
  3. 同一篇博客的重复知识点不做第二次解释
  4. 博客例子无知识点顺序

例1:打印输出

#include <stdio.h>

int main(int argc, char const *argv[])
{
	// 调用pritnf函数,需包含stdio.h
	printf ("1001 ");			
    printf ("C and C++ ");
    printf ("Tips!");
    
	return 0;
}

例2:打印0-100的数字

#include <stdio.h>

int main(int argc, char const *argv[])
{
	// 定义变量
	int value = 0;				
	
	// while循环, value值小于100循环
    while (value <= 100)		
	{
		// 打印数字并换行
		printf("%d\n", value);		// 0<换行>...
		// value自增
		value++;				
	}
	return 0;
}

例3:按位与的使用

#include <stdio.h>
/**
 * 按位与运算(&)
 * 将数转为二进制码,对位比较
 * 两者都为1才为1,否则位0
 */
int main(int argc, char const *argv[])
{
	// %d打印十进制
	printf("0 & 0 is %d\n", 0 & 0);			// 0 & 0 is 0
	printf("0 & 1 is %d\n", 0 & 1);			// 0 & 1 is 0
	printf("1 & 1 is %d\n", 1 & 1);			// 0 & 1 is 0
	printf("1 & 2 is %d\n", 1 & 2);			// 1 & 2 is 0
	printf("15 & 127 is %d\n", 15 & 127);	// 15 & 127 is 15

	return 0;
}

例4:按位取反的使用

#include <stdio.h>
/**
 * 按位取反运算(~)
 * 将数转为二进制码,按位取反
 * 1为0,0为1
 */
int main(int argc, char const *argv[])
{
	// 定义变量, 用十六进制字面量赋值
	int value = 0xFF;

	// %X打印十六进制
	printf("The inverse of %X is %X\n", value, ~value);	// The inverse of FF is FFFFFF00

	return 0;
}

例5:按位或的使用

#include <stdio.h>
/**
 * 按位或运算(|)
 * 将数转为二进制码,对位比较
 * 两者有一个为1就为1,都为0才为0
 */
int main(int argc, char const *argv[])
{
	printf("0 | 0 is %d\n", 0 | 0);			// 0 | 0 is 0
	printf("0 | 1 is %d\n", 0 | 1);			// 0 | 1 is 1
	printf("1 | 1 is %d\n", 1 | 1);			// 1 | 1 is 1
	printf("1 | 2 is %d\n", 1 | 2);			// 1 | 2 is 3
	printf("128 | 127 is %d\n", 128 | 127); // 128 | 127 is 255

	return 0;
}

例6:按位异或的使用

#include <stdio.h>
/**
 * 按位异或运算(^)
 * 将数字转为二进制码,对位比较
 * 数码相同为0,数码不同为0,
 */
int main(int argc, char const *argv[])
{
	printf("0 ^ 0 is %d\n", 0 ^ 0);			// 0 ^ 0 is 0
	printf("0 ^ 1 is %d\n", 0 ^ 1);			// 0 ^ 1 is 1
	printf("1 ^ 1 is %d\n", 1 ^ 1);			// 1 ^ 1 is 0
	printf("1 ^ 2 is %d\n", 1 ^ 2);			// 1 ^ 2 is 3
	printf("15 ^ 127 is %d\n", 15 ^ 127);	// 15 ^ 127 is 112

	return 0;
}

例7:输出ASCII码字符

#include <stdio.h>

int main(int argc, char const *argv[])
{
	// ASCII码表中字符A对应的值是65
	printf("The letter is %c\n", 'A');	// The letter is A
	printf("The letter is %c\n", 65);	// The letter is A

	return 0;
}

例8:计算输出元音字母和辅音字母的个数

#include <stdio.h>

int main(int argc, char const *argv[])
{
  char letter;

  // 元音字母个数
  int vowel_count = 0;
  // 辅音字母个数
  int consonant_count = 0;   

  // 循环变量从A到Z
  for (letter = 'A'; letter <= 'Z'; letter++)
    // switch case中有所有的元音字母,
    switch (letter) {
     case 'A':
     case 'E':
     case 'I':
     case 'O':
     case 'U': 
        // 是元音字母就+1并跳出switch
        vowel_count++;
          break;
      // 没有跳出switch就是辅音字母
      default: consonant_count++;
    }; 

  printf("The number of vowels is %d\n", vowel_count);    // The number of vowels is 5
  printf("The number of vowels is %d\n", consonant_count);// The number of vowels is 21

  return 0;
}

例9:实现一个简单的命令行程序,可以查看当前路径的文件目录,可以检查磁盘,可以修改系统日期

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
  char letter;
   
  do {
    // 提示用户功能
    printf("A Display directory listing\n");
    printf("B Display disk information\n");
    printf("C Change system date\n");
    printf("Q Quit\n");
    printf("Choice: ");

    // 获取用户输入的字符(需包含conio.h)
    letter = getch();     
    // 将字符转为大写(达到不区分输入大小写的目的,需包含ctype.h)
    letter = toupper(letter);

    // 根据字符调用对应系统命令
    if (letter == 'A')
     // system函数调用需包含stdlib.h
     system("DIR");
    else if (letter == 'B')
     system("CHKDSK");
    else if (letter == 'C')
     system("DATE");
  }
  // 只要在输入Q时才退出
  while (letter != 'Q');

  return 0;
}

例10:判断相等的运算符

#include <stdio.h>

int main(int argc, char const *argv[])
{
  int age = 21;
  int height = 73;

  // age等于21时输出
  if (age == 21)
    printf("User's age is 21\n");

  // age不等于21时输出
  if (age != 21)
    printf("User's age is not 21\n");
  
  // height等于73时输出
  if (height == 73) 
    printf("User's height is 73\n");

  // height不等于73时输出
  if (height != 73)
    printf("User's height is not 73\n"); 
  
  return 0;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值