【C】【吐血整理】你们要的C语言实例大全-基础知识篇二

你们要的C语言实例大全-基础知识篇二

目录

你们要的C语言实例大全-基础知识篇二

实例11 嵌套if语句

实例12 switch语句

实例13 for语句

实例14 while语句

实例15 do-while语句

实例16 break和continue语句

实例17 exit()语句

实例18 综合实例

实例19 一维数组

实例20 二维数组

实例21 字符数组

实例22 数组初始化

实例23 数组应用

下一篇:【C】【吐血整理】你们要的C语言实例大全-基础知识篇三


实例11 嵌套if语句

nesting.c

# include <stdio.h>
void main()
{
	/* sex代表输血者的性别,weight代表输血者的体重,cubage代表输血量 */
	int sex, weight, cubage;
	printf("请给出输血者的性别和体重:");
	scanf("%d,%d", &sex, &weight);

	if(sex >= 0)    /* 若变量sex的数值为非负数,则表示为男性 */
	{
		if(weight >= 120)
		{
            cubage = 200;
			printf("此人应该输血:%d毫升\n", cubage);
		}
		else
		{
			cubage = 180;
			printf("此人应该输血:%d毫升\n", cubage);
		}
	}
	else   /* 否则,表示为女性 */
	{
		if(weight >= 100)
		{
            cubage = 150;
			printf("此人应该输血:%d毫升\n", cubage);
		}
		else
		{
			cubage = 120;
			printf("此人应该输血:%d毫升\n", cubage);
		}
	}
}

演示

请给出输血者的性别和体重:1,125
此人应该输血:200毫升
请给出输血者的性别和体重:-1,90
此人应该输血:120毫升

实例12 switch语句

switch.c

#include <stdio.h>

void main()
{
	int num;
	/* 下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 */
	int indiv, ten, hundred, thousand; 
	int ten_thousand, hundred_thousand, place;

	printf("请输入一个整数(0~999999):");
	scanf("%d", &num);

	/* 判断变量num的位数 */
	if(num > 99999)
		place = 6;
	else if(num > 9999)
		place = 5;
	else if(num > 999)
		place = 4;
	else if(num > 99)
		place = 3;
	else if(num > 9)
		place = 2;
	else
		place = 1;
	printf("place = %d\n", place);
	
	printf("每位数字为:");

	/* 求出num在各位上的值 */
	hundred_thousand = num/100000;
	ten_thousand = (num - hundred_thousand*100000)/10000;
	thousand = (num - hundred_thousand*100000 - ten_thousand*10000)/1000;
	hundred = (num - hundred_thousand*100000 - ten_thousand*10000 
		      - thousand*1000)/100;
	ten = (num - hundred_thousand*100000 - ten_thousand*10000 
		  - thousand*1000 - hundred*100)/10;
	indiv = num - hundred_thousand*100000 - ten_thousand*10000 
		    - thousand*1000 - hundred*100 - ten*10;

	/* 判断变量num的位数,并根据位数做出相应的输出 */
	switch(place)
	{
	case 1: printf("%d", indiv);
		    printf("\n反序数字为:");
			printf("%d\n", indiv);
			break;
    case 2: printf("%d, %d", ten, indiv);
		    printf("\n反序数字为:");
			printf("%d%d\n", indiv, ten);
			break;
	case 3: printf("%d, %d, %d", hundred, ten, indiv);
		    printf("\n反序数字为:");
			printf("%d%d%d\n", indiv, ten, hundred);
			break;
	case 4: printf("%d, %d, %d, %d", thousand, hundred, ten, indiv);
		    printf("\n反序数字为:");
			printf("%d%d%d%d\n", indiv, ten, hundred, thousand);
			break;
	case 5: printf("%d, %d, %d, %d, %d", ten_thousand, thousand, 
				   hundred, ten, indiv);
		    printf("\n反序数字为:");
			printf("%d%d%d%d%d\n", indiv, ten, hundred, 
				    thousand, ten_thousand);
			break;
	case 6: printf("%d, %d, %d, %d, %d, %d", hundred_thousand, 
				   ten_thousand, thousand, hundred, ten, indiv);
		    printf("\n反序数字为:");
			printf("%d%d%d%d%d%d\n", indiv, ten, hundred, thousand,
				    ten_thousand, hundred_thousand);
			break;
	default: printf("Not find.\n");
		     break;
	}
}

演示

guo@ubuntu:~/test/Clanguage/1/Exam012$ ./a.out
请输入一个整数(0~999999):999
place = 3
每位数字为:9, 9, 9
反序数字为:999
guo@ubuntu:~/test/Clanguage/1/Exam012$ ./a.out
请输入一个整数(0~999999):520
place = 3
每位数字为:5, 2, 0
反序数字为:025

实例13 for语句

asterisk.c

# include <stdio.h>

void main()
{
	int i, j, k;
	/* 变量i从0到4,表示所画菱形图的第一至第五行 */
	for(i = 0; i <= 4; i++)
	{
		/* 当行数为i时,空格数是i的函数,为4-i个 */
		for(j = 0; j <= 3-i; j++)
			printf(" ");
		/* 星号数也是i的函数,为2i+1个 */
		for(k = 0; k <= 2*i; k++)
			printf("*");
		printf("\n");
	}
	/* 变量i从0到3,表示所画菱形图的第六至第九行 */
	for(i = 0; i <= 3; i++)
	{
		/* 当行数为i时,空格数是i的函数,此时为i个 */
		for(j = 0; j <= i; j++)
			printf(" ");
		/* 星号数也是i的函数,此时为7-2i个 */
		for(k = 0; k <= 6-2*i; k++)
			printf("*");
		printf("\n");
	}
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam013# ./a.out 
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

实例14 while语句

common.c

# include <stdio.h>

void main()
{
	int x, y, num1, num2, temp;
	printf("请输入两个正整数:\n");
	scanf("%d %d", &num1, &num2);

	if(num1 < num2)
	{
		temp = num1;
		num1 = num2;
		num2 = temp;
	}
	x = num1;
	y = num2;
	while(y != 0)
	{
		temp = x%y;
		x = y;
		y = temp;
	}
	printf("它们的最大公约数为:%d\n", x);
	printf("它们的最小公倍数为:%d\n", num1*num2/x);
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam014# ./a.out 
请输入两个正整数:
34
59
它们的最大公约数为:1
它们的最小公倍数为:2006

实例15 do-while语句

count.c

# include <math.h>
# include <stdio.h>    /* 数学函数库 */

void main()
{
	/* 用s表示多项式的值,用t表示每一项的值 */
	double s, t, x;
	int n;
	printf("please input x: ");
	scanf("%lf", &x);
    /* 符初值 */
	t = x;
	n = 1;
	s = x;
    /* 进行叠加运算 */
	do
	{
		n = n + 2 ;
		t = t * (-x*x)/((float)(n)-1)/(float)(n);
		s = s + t;
	} while (fabs(t)>=1e-8);
	printf("sin(%f) = %lf\n", x, s);
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam015# ./a.out 
please input x: 43
sin(43.000000) = -24.157247

实例16 break和continue语句

area.c

# include <stdio.h>

void main( )
{
	int radius;
	double area;
	for(radius = 1; radius <= 10 ; radius++)
	{
		area = 3.1416 * radius * radius;
		/* 若圆面积超过120,则跳出for循环,不予输出 */
		if(area >= 120.0)
			break;
		printf("square = %f\n", area);
	}
	printf("now radius=%d\n\n", radius-1);

	for(radius = 1; radius <= 10 ; radius++)
	{
		area = 3.1416 * radius * radius;
		/* 若圆面积没有超过60,则不输出而是从新开始循环 */
		if(area < 120.0)
			continue;
		printf("square = %f\n", area);
	}
	printf("now radius=%d\n", radius-1);
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam016# ./a.out 
square = 3.141600
square = 12.566400
square = 28.274400
square = 50.265600
square = 78.540000
square = 113.097600
now radius=6

square = 153.938400
square = 201.062400
square = 254.469600
square = 314.160000
now radius=10

实例17 exit()语句

exit.c

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

void main()
{
	int month;
	int day;
	
	printf("please input the month number: ");
	scanf("%d", &month);
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12: day=31;
			 break;
	case 4:
	case 6:
	case 9:
	case 11: day=30;
             break;
	case 2:  day=28;
             break;
	default: exit(0);
	}
	printf("1999.%d has %d days.\n", month, day);
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam017# ./a.out 
please input the month number: 11
1999.11 has 30 days.

实例18 综合实例

integration.c

# include <math.h>
# include <stdio.h>

void main( )
{
	int i, j, num;
	int p, q, flagp, flagq;
	printf("Please input a plus integer: ");
	scanf("%d", &num);

	/* 代码 (num%2)!=0) 表示num不能被2整除 */
	if(((num%2)!=0) || (num<=4))
		printf("input data error!\n");
	else
	{
		p = 1;
		/* do-while循环体 */
		do {
				p = p + 1;
				q = num - p;
				flagp = 1;
				flagq = 1;
				/* for循环体 */
				for(i=2; i<=(int)(floor(sqrt((double)(p)))); i++)
				{
					if((p%i) == 0)
					{
						flagp = 0;
						break;
					}
				}
				/* while循环体 */
				j = 2;
				while(j <= (int)(floor(sqrt((double)(q)))))
				{
					if ((q%j) == 0)
					{
						flagq = 0;
						break;
					}
					j++;
				}
			} while (flagp*flagq == 0);
		printf("%d = %d + %d \n", num, p, q);
	}
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam018# ./a.out 
Please input a plus integer: 48
48 = 5 + 43 

实例19 一维数组

onedim.c

/* 使用选择法排序 */
# include <stdio.h>

void main()
{
	int i, j, min, temp;
	/* 定义一个整型得一维数组 */
	int array[10];
	/* 输入数据 */
	printf("Please input ten integer: \n");
	for(i=0; i<10; i++)
	{
		printf("array[%d] = ", i);
		scanf("%d", &array[i]);
	}
	printf("The array is: ");
	for(i=0; i<10; i++)
		printf("%d ", array[i]);
	printf("\n");
	/* 排序 */
	for(i=0; i<9; i++)
	{
		min = i;
		for(j=i; j<10; j++)
			if(array[min]>array[j]) min = j;
		temp = array[i];
		array[i] = array[min];
		array[min] = temp;
	}
	/* 输出 */
	printf("\nThe result: \n");
	for(i=0; i<10; i++)
		printf("%d ", array[i]);
	printf("\n");
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam019# ./a.out 
Please input ten integer: 
array[0] = 22
array[1] = 34
array[2] = 56
array[3] = 76
array[4] = 4
array[5] = 31
array[6] = 15
array[7] = 88
array[8] = 999
array[9] = 100
The array is: 22 34 56 76 4 31 15 88 999 100 

The result: 
4 15 22 31 34 56 76 88 100 999 

实例20 二维数组

twodim.c

# include <stdio.h>

void main()
{
	int array[16][16];
	int i, j, k, m, n;
	
	/* 变量初始化 */
	m = 1;
	while(m == 1)
	{
		printf("请输入n(0<n<=15),n是奇数)\n");
		scanf("%d", &n);
		/* 判断n是否是大于0小于等于15的奇数 */
		if((n!=0) && (n<=15) && (n%2!=0))
		{
			printf("矩阵阶数是 %d\n", n);
			m = 0;
		}
	}
	/* 数组赋初值为0 */
	for(i=1; i<=n; i++)
		for(j=1; j<=n; j++)
			array[i][j] = 0;

	/* 建立魔方阵 */
	j = n/2 + 1;
	array[1][j] = 1;
	for(k=2; k<=n*n; k++)
	{
		i = i - 1;
		j = j + 1;
		if((i<1) && (j>n))
		{
			i = i + 2;
			j = j - 1;
		}
		else
		{
			if(i < 1)
				i = n;
			if(j > n)
				j = 1;
		}
		if(array[i][j] == 0)
			array[i][j] = k;
		else
		{
			i = i + 2;
			j = j - 1;
			array[i][j] = k;
		}
	}

	/* 输出魔方阵 */
	for(i=1; i<=n; i++)
	{
		for(j=1; j<=n; j++)
			printf("%5d", array[i][j]);
		printf("\n");
	}
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam020# ./a.out 
请输入n(0<n<=15),n是奇数)
3
矩阵阶数是 3
    8    1    6
    3    5    7
    4    9    2

实例21 字符数组

chararr.c

# include <stdio.h>
/* 宏定义 */
# define MAX 100
# define LEN 80

/* 一个非常简单的文本编辑器 */
void main()
{
	char text[MAX][LEN];
	register int t, i, j;    /* 定义三个寄存器变量 */
	/* 逐行输入字符串 */
    for(t=0; t<MAX; t++)    
	{
		printf("%d: ", t);
		gets(text[t]);
		if(!text[t][0])
			break;  /* 空行退出 */
	}
	
	/* 按行,逐个字符输出字符串 */
	for(i=0; i<t; i++)  
	{
		for(j=0; text[i][j]; j++)
			putchar(text[i][j]);
		putchar('\n');
	}
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam021# ./a.out 
0: 22
1: ff
2: tt
3: 5t
4: you
5: me
6: for
7: int
8: log
9: float
10: ls
11: ln
12: ll
13: la
14: python
15: java
16: c
17:  
18: 
22
ff
tt
5t
you
me
for
int
log
float
ls
ln
ll
la
python
java
c

实例22 数组初始化

init.c

# include <stdio.h>

void main()
{
	/* 有尺寸 */
	/* 一维整形数组初始化 */
	int  array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

	/* 一维字符型数组初始化 */
	char array2[13] = "How are you!";    /* 方式一 */
	char array3[13] = {'H','o','w',' ','a','r','e',' ','y','o','u','!'};    /* 方式二 */

	/* 二维整形数组初始化 */
	int array4[4][4] = 
	{
		12, 18,  6, 25,
	    23, 10, 32, 16,
		25, 63,  1, 63,
		 0,  0, 27, 98
	};    /* 矩阵的形式 */

	/* 无尺寸 */
	/* 一维字符型数组初始化 */
	char array5[] = "How are you!";
	int array6[][2] = {
		{ 1,50},
		{45, 2},
		{ 2, 0},
		{12,32},
		{42,33},
		{15,18}
	};
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam022# ./a.out 
root@ubuntu:/home/guo/test/Clanguage/1/Exam022# 

实例23 数组应用

query.c

/* 学生成绩查询系统 */
# include <stdio.h>
# include <stdlib.h>

void main( )
{
	int select;
	int i, j;
	int score[5][7];
	int average = 0;
	int sum = 0;
    do{
		printf("本程序有4项功能:\n");
		printf(" 1. 根据学号查询学生成绩\n");
		printf(" 2. 根据考试号统计成绩\n") ;
		printf(" 3. 根据考试号和学号查询成绩\n");
		printf(" 4. 成绩录入\n");
		printf(" 0. 退出\n");
		printf(" 请输入选择(0 - 4): ");
		scanf("%d", &select);
		switch(select)
		{
		case 0:
			printf("OK\n");
			exit(0);
			break;
		case 1:
			printf("输入学号:");
			scanf("%d\n", &i);
			for(j=1; j<7; j++)
			{
				printf("第%d科成绩是%d\n", j, score[i][j]);
				sum += score[i][j];
			}
			average = sum/6;
			printf("学生的平均成绩是%d\n", average);
			break;
		case 2:
			printf("输入考试号:");
			scanf("%d\n", &j);
			for(i=1; i<5; i++)
			{
				printf("第%d号学生本科成绩是%d\n", i, score[i][j]);
				sum += score[i][j];
			}
			average = sum/4;
			printf("本科平均成绩是%d\n", average);
			break;
		case 3:
			printf("输入学号和考试号:");
			scanf("%d %d\n", &i, &j);
			printf("第%d号学生的第%d科考试成绩是%d\n", i, j, score[i][j]);
			break;
		case 4:
			printf("请输入成绩\n");
			for(i=1; i<5; i++)
				for(j=1; j<7; j++)
					scanf("%d\n", &score[i][j]);
			break;
		default:
			break;
		}
	}while(1);
}

演示

root@ubuntu:/home/guo/test/Clanguage/1/Exam023# ./a.out 
本程序有4项功能:
 1. 根据学号查询学生成绩
 2. 根据考试号统计成绩
 3. 根据考试号和学号查询成绩
 4. 成绩录入
 0. 退出
 请输入选择(0 - 4): 4
请输入成绩
76
87
89
98
100
99
97
96
95
79
87
86
85
84
83
66
60
59
67
87
91
92
93
94
95
本程序有4项功能:
 1. 根据学号查询学生成绩
 2. 根据考试号统计成绩
 3. 根据考试号和学号查询成绩
 4. 成绩录入
 0. 退出
 请输入选择(0 - 4): 本程序有4项功能:
 1. 根据学号查询学生成绩
 2. 根据考试号统计成绩
 3. 根据考试号和学号查询成绩
 4. 成绩录入
 0. 退出
 请输入选择(0 - 4): 2
输入考试号:2
2
第1号学生本科成绩是87
第2号学生本科成绩是96
第3号学生本科成绩是84
第4号学生本科成绩是87
本科平均成绩是88
本程序有4项功能:
 1. 根据学号查询学生成绩
 2. 根据考试号统计成绩
 3. 根据考试号和学号查询成绩
 4. 成绩录入
 0. 退出
 请输入选择(0 - 4): 输入考试号:

下一篇:【C】【吐血整理】你们要的C语言实例大全-基础知识篇三

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GLL_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值