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

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

目录

实例36 变量的指针

实例37 一维数组指针

实例38 二维数组指针

实例39 字符串指针

实例40 函数指针

实例41 指针数组

实例42 二维指针

实例43 指针的初始化

实例44 综合实例


实例36 变量的指针

varpoint.c

/* 输入x、y和z三个整数,按大小顺序输出 */
# include <stdio.h>

void swap(int *pt1, int *pt2);
void exchange(int *q1, int *q2, int *q3);

void main()
{
	int x, y, z;
	int *p1, *p2, *p3;
	printf("请输入三个整数:");
	scanf("%d, %d, %d", &x, &y, &z);    /* 输入整数时以逗号隔开 */
	p1 = &x;
	p2 = &y;
	p3 = &z;
	exchange(p1, p2, p3);
	printf("按大小排序后的三个整数为:");
	printf("%d, %d, %d\n", x, y, z);
}

void swap(int *pt1, int *pt2)
{
	int p;
	p = *pt1;
	*pt1 = *pt2;
	*pt2 = p;
}

void exchange(int *q1, int *q2, int *q3)
{
	if(*q1 < *q2)
		swap(q1, q2);
	if(*q1 < *q3)
		swap(q1,q3);
	if(*q2 < *q3)
		swap(q2,q3);
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam036$ ./a.out 
请输入三个整数:30,50,70 
按大小排序后的三个整数为:70, 50, 30

实例37 一维数组指针

arr1point.c

# include <stdio.h>

void inv(int *x, int n);

void main()
{
	int i;
	int array[10] = {1, 3, 9, 11, 0, 8, 5, 6, 14, 98};
	printf("原始数组是:\n");
	for(i=0; i<10; i++)
		printf("%d " , array[i]);
	printf("\n");
	inv(array, 10);
	printf("按相反次序存放后的数组为:\n");
	for(i=0; i<10; i++)
		printf("%d ", array[i]);
	printf("\n");
}

void inv(int *x, int n)
{
	int *p, *i, *j;
	int t;
	int m = (n-1)/2;
	i = x; 
	j = x + n - 1;
	p = x + m;
	for(; i<=p; i++, j--)
	{
		t = *i;
		*i = *j;
		*j = t;
	}
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam037$ ./a.out 
原始数组是:
1 3 9 11 0 8 5 6 14 98 
按相反次序存放后的数组为:
98 14 6 5 8 0 11 9 3 1 


实例38 二维数组指针

arr2point.c

# include <stdio.h>

void main()
{
	int num;
	/* 声明子函数 */
	void average(float *point, int n);
	void search(float(*point)[4], int n);

	/* 定义一个静态存储数组并赋初值 */
	static float score[4][4] = {{76, 90, 92, 87}, {68, 78, 69, 94},  
	                            {89, 82, 81, 60}, {81, 68, 60, 97}};

	printf("班级的总平均分:");
	average(*score, 16);    /* 调用函数average求12个分数的平均分 */
	printf("请输入学生的学号(0-3):");
	scanf("%d", &num);
	search(score, num);    /* 求出第四个学生的成绩 */
}

/* 子函数定义 */
void average(float *point, int n)
{
	float *p_end;
	float aver;
	float sum = 0;
	p_end = point + n -1;
	for(; point<=p_end; point++)
		sum = sum + (*point);
	aver = sum/n;
	printf("%5.2f\n", aver);
}

void search(float(*point)[4], int n)
{
	int i;
	for(i=0; i<4; i++)
		printf("%5.2f ", *(*(point+n)+i));
	printf("\n");
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam038$ ./a.out 
班级的总平均分:79.50
请输入学生的学号(0-3):1
68.00 78.00 69.00 94.00 


实例39 字符串指针

strpoint.c

/* 将字符串a复制到字符串b */
# include <stdio.h>

void main()
{
	int i;
	char a[] = "I am a student.";
	char b[20];
	char *p1, *p2;
	
	p1 = a; p2 = b;
	for(; *p1!='\0'; p1++, p2++)
		*p2 = *p1;
	*p2 = '\0';
	printf("string a is: %s\n", a);
	printf("string b is: ");
	for(i=0; b[i]!='\0'; i++)
		printf("%c", b[i]);
	printf("\n");
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam039$ ./a.out 
string a is: I am a student.
string b is: I am a student.


实例40 函数指针

funcpoint.c

# include <stdio.h>
# include <string.h>

void check(char *a, char *b, int(*cmp)(const char *, const char *));

void main()
{
	char s1[80], s2[80];
	int(*p)(const char *, const char *);    /* 函数指针 */

	p = strcmp;    /* 将函数strcmp的地址赋给函数指针p */

	printf("输入两个字符串:\n");
	gets(s1);    /* 输入字符串1 */
	gets(s2);    /* 输入字符串2 */

	check(s1, s2, p);    /* 通过指针变量p传递函数strcmp的地址 */
}

void check(char *a, char *b, int(*cmp)(const char *, const char *))
{
	printf("测试是否相等\n");
	if(!(*cmp)(a, b))
		printf("结果:相等\n");
	else
		printf("结果:不相等\n");
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam040$ ./a.out 
输入两个字符串:
hello
world
测试是否相等
结果:不相等


实例41 指针数组

half.c

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

void main()
{
	/* 声明子函数 */
	int binary(char *ptr[], char *str, int n);    /* 查找函数声明 */
	void insert(char *ptr[], char *str, int n, int i);    /* 插入函数声明 */

	char *temp, *ptr1[6];
	int i;
	printf("请为字符形指针数组赋初值:\n");
	for (i=0; i<5; i++)
	{
		ptr1[i] = (char *)malloc(20);    /* 为指针分配地址后 */
		gets(ptr1[i]);    /* 输入字符串 */
	}
	ptr1[5] = (char *)malloc(20);
	printf("\n");

	printf("original string:\n");
	for(i=0; i<5; i++)    /* 输出指针数组各字符串 */
		printf("%s\n", ptr1[i]);

	printf("\ninput search string:\n");
	temp = (char *)malloc(20);
	gets(temp);    /* 输入被插字符串 */

	i=binary(ptr1, temp, 5);    /* 寻找插入位置i */
	printf("i = %d\n", i);

    insert(ptr1, temp, 5, i);    /* 在插入位置i处插入字符串 */
	printf("output strings:\n");

	for(i=0; i<6; i++)    /* 输出指针数组的全部字符串 */
		printf("%s\n", ptr1[i]);
}

int binary(char *ptr[], char *str, int n)
{ 
	/* 折半查找插入位置 */
	int hig, low, mid;
	low = 0;
	hig = n-1;
	if(strcmp(str,ptr[0]) < 0) 
		return 0;
    /* 若插入字符串比字符串数组的第0个小,则插入位置为0 */
	if(strcmp(str,ptr[hig]) > 0) 
		return n;
    /* 若插入字符串比字符串数组的最后一个大,则应插入字符串数组的尾部 */
    while(low <= hig)
	{
		mid = (low + hig)/2 ;
		if (strcmp(str,ptr[mid]) < 0)
			hig = mid - 1;
		else if(strcmp(str,ptr[mid]) > 0)
			low = mid + 1;
		else 
			return mid;    /* 插入字符串与字符串数组的某个字符串相同 */
	}
	return low;    /* 插入的位置在字符串数组中间 */
}

void insert(char *ptr[], char *str, int n, int i)
{
	int j;
	for(j=n; j>i; j--)    /* 将插入位置之后的字符串后移 */
		strcpy(ptr[j], ptr[j-1]);
	strcpy(ptr[i], str);    /* 将被插字符串按字典顺序插入字符串数组 */
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam041$ ./a.out 
请为字符形指针数组赋初值:
hello
world
you
and
me

original string:
hello
world
you
and
me

input search string:
you
i = 5
output strings:
hello
world
you
and
me
you


实例42 二维指针

plapoint.c

/* 用指向指针的指针变量访问一维和二维数组 */
# include <stdio.h>
# include <stdlib.h>

void main()
{
	int a[10], b[3][4];
	int *p1, *p2, **p3;    /* p3是指向指针的指针变量 */
	int i,j; 

	printf("请输入一维数组(10个元素):\n");
	for(i=0; i<10; i++)
		scanf("%d", &a[i]);    /* 一维数组的输入 */

	printf("请输入二维数组(三行四列):\n");
	for(i=0; i<3; i++)
		for(j=0; j<4; j++)
			scanf("%d", &b[i][j]);    /* 二维数组输入 */
	printf("\n");

	for(p1=a, p3=&p1, i=0; i<10; i++)
		printf("%4d", *(*p3+i));    /* 用指向指针的指针变量输出一维数组 */
	printf("\n");

	for(p1=a; p1-a<10; p1++)    /* 用指向指针的指针变量输出一维数组 */
	{
		p3 = &p1;
		printf("%4d", **p3);
	}
	printf("\n");

	for(i=0; i<3; i++)    /* 用指向指针的指针变量输出二维数组 */
	{
		p2 = b[i];
		p3 = &p2;
		for(j=0; j<4; j++)
			printf("%4d", *(*p3+j));
		printf("\n");
	}

	for(i=0; i<3; i++)    /* 用指向指针的指针变量输出二维数组 */
	{
		p2 = b[i];
		for(p2=b[i]; p2-b[i]<4; p2++)
		{
			p3 = &p2;
			printf("%4d", **p3);
		}
	printf("\n");
	}
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam042$ ./a.out 
请输入一维数组(10个元素):
1 
1
5
2
0
0
11 
9
11
0
请输入二维数组(三行四列):
12
13
14
15
16
17
18
19
20
21
22
23

   1   1   5   2   0   0  11   9  11   0
   1   1   5   2   0   0  11   9  11   0
  12  13  14  15
  16  17  18  19
  20  21  22  23
  12  13  14  15
  16  17  18  19
  20  21  22  23


实例43 指针的初始化

initialize.c

# include <stdio.h>
# include <string.h>

int search(char* p[], char* name);

/* 给字符型的指针数组赋初值 */
char* names[] = {
	"Herb",
	"Rex",
	"Dennis",
	"John",
	NULL};    /* NULL指针标志数组内容的结束 */ 
 
void main()
{
	if(search(names, "Herb") != -1)
		printf("Herb is in list.\n");
  
    if(search(names, "Mary") == -1)
		printf("Mary not found.\n");
}
 
int search(char* p[], char* name)
{
	register int t;
   
	for(t = 0; p[t]; ++t)
		if(!strcmp(p[t], name)) return t;
    return -1;
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam043$ ./a.out 
Herb is in list.
Mary not found.


实例44 综合实例

intepoint.c

# include <stdio.h>

void avsco(float *psco, float *pave);
void avcour5(char *pcou, float *psco);
void fali2(char *pcou, int *pnum, float *psco, float *pave);
void excellence(char *pcou, int *pnum, float *psco, float *pave);

void main()
{
	/* 数组num用于存放每位学生的学号 */
	int i, j, *pnum, num[4];

    /* 数组aver用于存放每位学生的平均分,二维数组score用于存放学生成绩 */
	float score[4][5], aver[4], *psco, *pave;

	/* 数组course存放5门课程的名称 */
	char course[5][10], *pcou;  

	printf("请按行输入5门功课的名称:\n");
	pcou = course[0];  /* 指针变量pcou用来存放数组course的首地址 */
	                   /* 从首地址开始,每十个字节存放一个课程的名称 */
	for(i=0; i<5; i++)
		scanf("%s", pcou+10*i);  /* 以空格为间隔输入五门课程的名称 */    

	printf("请按下面的格式输入4个学生的学号和各科成绩:\n");
	printf("学号");
	for(i=0; i<5; i++)
		printf(",%s", pcou+10*i);  /* 输出各门课程的名称 */
	printf("\n");
	psco = &score[0][0];  /* 指针psco指向数组score中的第一个元素 */
	                      /* 即指向第一个学生第一门课程的成绩 */
	pnum = &num[0];
	for(i=0; i<4; i++)
	{
		scanf("%d", pnum+i);  /* 输入学号 */
		for(j=0; j<5; j++)
			scanf(",%f", psco+5*i+j);  /* 以逗号为间隔输入学生成绩 */
	}

	pave = &aver[0];  /* 将数组aver的首地址赋给指针pave */
	printf("\n");  /* 空行 */

	avsco(psco, pave);
	avcour5(pcou, psco);
	printf("\n");  /* 空行 */

	fali2(pcou, pnum, psco, pave);
	printf("\n");  /* 空行 */

	excellence(pcou, pnum, psco, pave);
}

void avsco(float *psco, float *pave)  /* 求每个学生的平均成绩 */
{
	int i, j;
	float sum, average;
	for(i=0; i<4; i++)  /* i代表学生的序号,表示第i个学生 */
	{
		sum = 0.0;
		for(j=0; j<5; j++)  /* j代表课程的序号,表示第j门课程 */
			sum = sum + (*(psco+5*i+j));  /* 累计每个学生的各科成绩 */
		average = sum/5;  /* 计算第i个学生平均成绩 */
		*(pave+i) = average;
	}
}

void avcour5(char *pcou, float *psco)  /* 求第五门课程的平均成绩 */
{
	int i;
	float sum, average5;
	sum = 0.0;
	for(i=0; i<4; i++)
		sum = sum + (*(psco+5*i+4));  /* 累计每个学生第五门课的得分 */
	average5 = sum/4;    /* 计算第五门课程的平均成绩 */
	printf("第5门课程%s的平均成绩为%5.2f.\n", pcou, average5);
}

void fali2(char *pcou, int *pnum, float *psco, float *pave)
{
	int i, j, k, label;
	printf("      =====两门以上课程不及格的学生=====     \n");
	printf("  学号  ");
	for(i=0; i<5; i++)
		printf(" %-8s", pcou+10*i);  /* 输出课程名称 */
	printf("  平均分\n");
	for(i=0; i<4; i++)
	{
		label = 0;
		for(j=0; j<5; j++)
			if(*(psco+5*i+j) < 60.0)
				label++;  /* 计算第i个学生不及格课程的门数 */
		if(label >= 2)
		{
			printf("%-8d", *(pnum+i));  /* 输出学号 */
			for(k=0; k<5; k++)
				printf("  %-8.2f", *(psco+5*i+k));  /* 输出符合条件学生的各科成绩 */
			printf("  %-8.2f\n", *(pave+i));  /* 输出符合条件学生的平均分 */
		}
	}
}

/* 程序结构和上一个子函数fali2类似 */
void excellence(char *pcou, int *pnum, float *psco, float *pave)    
{
	int i, j, k, label;
	printf("      =====成绩优秀学生=====\n");
	printf("  学号  ");
	for(i=0; i<5; i++)
		printf("  %-8s", pcou+10*i);
	printf("  平均分\n");
	for(i=0; i<4; i++)
	{
		label = 0;
		for(j=0; j<5; j++)
			if(*(psco+5*i+j) >= 85.0)
				label++;
		if((label>=5)||(*(pnum+i)>=90))
		{
			printf("%-8d", *(pnum+i));
			for(k=0; k<5; k++)
				printf("  %-8.2f", *(psco+5*i+k));
			printf("  %-8.2f\n", *(pave+i));
		}
	}
}

运行

guo@ubuntu:~/test/Clanguage/1/Exam044$ ./a.out 
请按行输入5门功课的名称:
english
chinese
qt
math
c
请按下面的格式输入4个学生的学号和各科成绩:
学号,english,chinese,qt,math,c
1,88,99,100,98,99
2,90,80,99,89,94
3,60,89,55,66,79
4,55,88,90,77,80

第5门课程english的平均成绩为88.00.

      =====两门以上课程不及格的学生=====     
  学号   english  chinese  qt       math     c         平均分

      =====成绩优秀学生=====
  学号    english   chinese   qt        math      c         平均分
1         88.00     99.00     100.00    98.00     99.00     96.80 
发布了201 篇原创文章 · 获赞 46 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/rong11417/article/details/104530928