20 21 22 23 24 sum5?110 sum?300
(2) 下面的程序用来对数组元素进行升序排序,并输出排序前后各元素的值。其中包含了一些错误(错误处均标有found的注释),请上机调试正确。
#include ?stdio.h?
/* 定义一个排序函数,采用冒泡法 */ void sort(int p[], int n){
int i, j, temp; for (i?0; i?n?1; i??)
for (j?i?1; j?n; j??)
if(p[i]?p[j]) { p[i]?p[j]; p[j]?p[i];} /* found */
}
/*主函数*/ void main() {
int num(9), i; /*found*/ void sort(); /*found*/ printf(?enter 9 numbers: \\n?); for (i?0; i?9; i??)
scanf(??d?, &num[i]); printf(?before the sorting: \\n?);
for (i?0; i??9; i??) /* found */
/* 每行输出三个数组元素 */
printf(?num(?d)??2d?2c?, i, num[i], i?3??0??\\n?: ? ?); /* found */ sort(num, 9);
printf(?\\nafter the sorting: \\n?); for (i?0; i??9; i??) /* found */
printf(?num(?d)??2d?2c?, i, num[i], i?3??0??\\n?: ? ?); /* found */
}
(3) 下面的程序实现对四个串按字典次序排序,其中有一些错误,请上机调试正确。 #include ?stdio.h??#include ?string.h? void main() {
char temp[4], str[4][]?{?asd?, ?abd?, ?wkl?, ?fgh?}; /* found */ int i, j, k;
printf(?\\nbefore sorting: \\n?); for (i?0; i?4; i??)
printf(??c\\n?, str[i]); /* found */ /* 选择排序法 */ for (i?0; i?3; i??) {
k?i;
for (j?i?1; j?4; j??)
? 307 ?
if (strcmp(str[k], str[j])?0) k?j; if (k!?i) {
temp?str[i]; /* found */ str[i]?str[k]; /* found */ str[k]?temp; /* found */ } }
printf(?\\nafter sorting: \\n?); for (i?0; i?4; i??)
printf(??s\\n?, str[i]);
}
(4) 下面是实现十进制数向二进制数转换的程序,请在do?while循环中的三个括号内填入正确的表达式,并上机调试运行该程序。
#include ?stdio.h? void main() {
int bin[64], i; long num;
printf(?\\nenter a number: \\n?); scanf(??ld?, &num); i?0; do {
bin[i]?(___________); i??;
num?(__________); } while (_________); for (??i; i??0; i??)
printf(??d?, bin[i]);
}
(5) 下面是用数组来实现栈操作的程序。栈是数据的一种线性表结构,对表内的数据访问一般是按后进先出的原则来进行。对栈的基本操作有两种,出栈和压栈。压栈是把数据放到该表的尾部,出栈是把表尾的数据取走。上机调试该程序。
#include ?stdio.h??
#include ?conio.h?
#define MAX 10 /* 定义栈的大小 */ float stack[MAX];
int top?0; /* top表示栈内数据个数 */ /* 定义压栈函数,即向栈内存放一个数据 */ void push(float x) {
if (top??MAX)
{ printf(?\\n\\t\\t\\tstack full! OK! \\n?); getch();}
? 308 ?
else
{ stack[top]?x; top??;} }
/* 定义出栈函数,取走并显示最后放入栈中的数据 */ void pop() {
if (top??0) printf(?\\n\\t\\t\\tstack empty! OK! \\n?); else { top??; printf(??.2f\\n?, stack[top]);} getch(); }
/* 定义按后进先出的原则打印栈内各元素的函数,不改变栈的状况 */ void prnstack() {
int i?top;
if (i??0) printf(?\\n\\t\\t\\tstack empty! OK!?); else
for (; i?0; i??)
printf(??.2f?s?, stack[i?1], (i?1)?0??????:??); printf(?\\n?); }
void main() { char ch; float f;
for (; ;) {
clrscr(); /* 清屏 */ printf(?\\n\\t\\t\\t1---压栈---push\\n?); printf(?\\t\\t\\t2---出栈---pop\\n?); printf(?\\t\\t\\t3---打印---print\\n?); printf(?\\t\\t\\t4---退出---exit\\n?); printf(?\\t\\t\\tinput your choice: ?); scanf(??c?, &ch); getchar(); switch (ch) {
case ?1?: /* 输入数据并压到栈中 */
printf(?\\t\\t\\tinput a number: ?); scanf(??f?, &f); getchar(); push(f); break;
case ?2?: /* 取走栈顶元素,并显示该元素 */
pop(); break;
case ?3?:
prnstack(); getch(); break; case ?4?: exit(0);
? 309 ?
} } }
实验八 指 针
1.目的及要求
(1) 掌握指针的基本概念及其定义方法,学会使用指针变量。 (2) 学习掌握数组的指针和指向数组的指针变量。 (3) 学习掌握字符串的指针和指向字符串的指针变量。 (4) 学习了解指向函数的指针变量。 (5) 学习了解指向指针的指针。 2.实验内容
(1) 下面两个小程序都是按逆序输出数组元素的值,试上机调试并比较异同。 /* 程序一:用数组下标变量直接处理 */ #include ?stdio.h? void main() {
int a[6]?{1, 2, 3, 4, 5, 6}, i, temp; printf(?\\nbefore: \\n?); for (i?0; i?6; i??) printf(??2d?, a[i]);
for (i?0; i?3; i??) {
temp?a[i]; a[i]?a[5?i]; a[5?i]?temp; }
printf(?\\nafter: \\n?); for (i?0; i?6; i??)
printf(??2d?, a[i]);
}
/* 程序二:用指针变量处理 */ #include ?stdio.h? void main() {
int a[6]?{1, 2, 3, 4, 5, 6}, i, temp; int *p?a, *q?a?5; printf(?\\nbefore: \\n?); for (i?0; i?6; i??)
printf(??2d?, *(p?i)); for (; p?q; p??, q??) {
temp?*p; *p?*q; *q?temp; }
printf(?\\nafter: \\n?); for (i?0; i?6; i??)
? 310 ?
printf(??2d?, a[i]);
}
(2) 下面的程序用二维数组的行指针处理数组元素的输出,其中包含了一些错误,请上机调试正确(标有found的注释语句为有错误语句)。
#include ?stdio.h?
void main() {
int a[3][]?{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; /* found */ int *p[4]?a, i, j; /* found */ for (i?0; i?3; i??) {
for (j?0; j?4; j??)
printf(??5d?, *(p?i?j)); /* found */ printf(?\\n?); } }
(3) 上机调试运行例题8.13?8.16。
(4) 下面的程序中分别用函数名和函数的指针来调用函数max(),试上机运行。 #include ?stdio.h?
int max(int x, int y) { /* 定义函数max */
return (x?y?x:y); }
void main() {
int a, b, m1, m2;
int (*p)(); /* 定义一个指向函数的指针变量 */ scanf(??d?d?, &a, &b);
m1?max(a, b); /* 用函数名直接调用max()函数 */ printf(?max output 1: ?5d\\n?, m1); p?max;
m2?*p(a, b); /* 通过函数指针形式来调用函数 */ printf(?max output 2: ?5d\\n?, m2); }
(5) 下面的程序用指向指针的指针来访问数组元素,试上机调试运行。 #include ?stdio.h??#include ?stdlib.h? void main() {
int i, j, a[2][4]?{1, 2, 3, 4, 5, 6, 7, 8};
int **p; /* 定义指向指针的指针变量 */ p?(int **)malloc(2*sizeof(int *)); /* 语句1 */ *p?*a; *(p?1)?*(a?1); /* 语句2 */ printf(?\\n?); for (i?0; i?2; i??)
? 311 ?