当前位置:文档之家› C语言程序设计—指针—实验报告

C语言程序设计—指针—实验报告

实验报告专业软件工程班级X 班学号_ _ 姓名实验日期:201X年X月X日报告退发(订正、重做)课程C程序设计实验实验名称指针一、实验目的二、实验环境(描述实验的软件、硬件环境)①软件环境:windows xp/win7等操作系统,Microsoft Visual C++ 6.0编译器;②硬件环境:PC机一台三、实验内容、步骤和结果分析题目一:输入3个整数,按由小到大的顺序输出要求:使用指针方法实现;#include <stdio.h>void function(int *a, int *b){int temp;if (*a<*b){temp = *a;*a = *b;*b = temp;}}int main(){int a, b, c;int *p1, *p2, *p3;p1 = &a;p2 = &b;p3 = &c;scanf("%d%d%d", &a, &b, &c);function(p1, p2);function(p1, p3);function(p2, p3);printf("%d %d %d\n", *p3, *p2, *p1);return 0;题目二:将长度为10的整型数组arr中的元素按照从小到大排列并输出要求:使用指针方法实现;#include <stdio.h>int main(){struct METRIC {float m;float cm;} m1, m2;struct BRITISH{float foot;float inches;} b1, b2;printf("Enter the info of m1(米,厘米):");scanf("%f%f", &m1.m, &m1.cm);printf("Enter the info of m2(米,厘米):");scanf("%f%f", &m2.m, &m2.cm);printf("\nEnter the info of m2(英尺,英寸):");scanf("%f%f", &b1.foot, &b1.inches);printf("Enter the info of m2(英尺,英寸):");scanf("%f%f", &b2.foot, &b2.inches);printf("\nSum of m1 and m2 is:%.2f(厘米)\n", (m1.m + m2.m) * 100 + m1.cm + m2.cm);printf("Sum of b1 and b2 is:%.2f(厘米)\n\n", (b1.inches + b2.inches)*30.48 + (b1.foot + b2.foot)*2.54);return 0;题目三:已知一个长度为10的一维数组arr,编写函数,求出第m个数到第n个数的和要求:使用指针方法实现。

#include <stdio.h>int main(){int arr[] = { 9, 1, 5, 6, 8, 4, 2, 7, 0, 3 };int *p1, *p2, a, m, n, temp = 0;scanf("%d%d", &m, &n);p1 = arr + m - 1;for (a = 0; a <= (n - m); a++){temp += *(p1 + a);}printf("%d\n", temp);return 0;}题目四:写一函数实现以上功能,在主函数中输入n个整数和输出调整后的的n个数要求:Arr是一个包含n个整数的一维数组。

现将数组中的每个元素向后移m个位置,使最后m个数变成最前面的m个数。

指针作为函数参数。

#include <stdio.h>#include <stdlib.h>void fun(int *arr, int n, int m){int a, b, temp;for (a = 0; a<m; a++){temp = *(arr + n - 1);for (b = n; b>0; b--){*(arr + b) = *(arr + b - 1);}*arr = temp;}}int main(){int *arr, n, m, a;scanf("%d%d", &n, &m);fflush(stdin);arr = (int *)calloc(n, sizeof(int));for (a = 0; a<n; a++){scanf("%d", arr + a);}fun(arr, n, m);for (a = 0; a<n; a++){printf("%d ", *(arr + a));}printf("\n");return 0;}题目五:编写一个函数实现将字符串str1和字符串str2合并。

要求:①合并后的字符串中的字符按其ASCII码值从小到大进行排序)②相同的字符在新字符串中只出现一次#include <stdio.h>#include <string.h>#include <stdlib.h>void fun(char str1[], char str2[]){int a, b = strlen(str1), c = strlen(str2), d, f, g;char *str = (char *)calloc(b + c + 1, sizeof(char)), e;for (a = 0; a < (b + c); a++){if (a < b){*(str + a) = str1[a];}else{*(str + a) = str2[a - b];}}f = b + c;for (a = 0; a < f; a++) //排序{for (d = a + 1; d < f; d++){if (*(str + a) > *(str + d)){e = *(str + a);*(str + a) = *(str + d);*(str + d) = e;}}}for (a = 0; a < f - 1;){if (*(str + a) == *(str + a + 1)){for (d = a; d < f - 1; d++){*(str + d) = *(str + d + 1);}f--;continue;}a++;}realloc(str, f + 1);for (a = 0; a < f; a++){putchar(*(str + a));}putchar('\n');}int main(){char str1[100], str2[100];gets(str1);gets(str2);fun(str1, str2);return 0;}题目六:编程模拟实现比较两个字符串大小的函数strcmp()要求:①已知strcmp函数的原型如下:int strcmp(char *p1, char *p2)②设p1指向字符串s1,p2指向字符串s2,要求当s1==s2时,返回值为0;若s1>s2,则返回值是一个正整数;若s1<s2,则返回值是一个负整数。

#include <stdio.h>int f_strcmp(char *p1, char *p2){int i, check;for (i = 0;; i++){if (*(p1 + i) > *(p2 + i))return 1;else if (*(p1 + i) < *(p2 + i))return -1;else if ((*(p1 + i) == *(p2 + i)) && (*(p1 + i) == '\0'))return 0;}}int main(){int check;char str1[100], str2[100];gets(str1);gets(str2);check = f_strcmp(str1, str2);switch (check){case -1:printf("str1 < str2.\n"); break;case 0:printf("str1 < str2.\n"); break;case 1:printf("str1 < str2.\n"); break;}return 0;}四、讨论。

相关主题