首页计算机计算机软件C语言程序设计 16 C语言第四次上机作业.docx

16 C语言第四次上机作业.docx

页数:7页文件大小:33.7 KB来源: C语言程序设计ppt课件

本课件布置第四次上机作业,要求编写transpose函数实现方阵转置,编写input、sort、display、search四个函数完成学生姓名与高考总分的录入排序显示与查找,并通过fmain.c与fstring.c两个源程序文件实现字符串反转与删除字符功能,随后给出完整的参考答案代码与运行示例。

第四次上机作业页,列出数组转置学生信息与多文件字符串处理三题
参考答案页,给出transpose函数用二重循环交换上三角元素实现转置
参考答案页,给出input函数读入姓名与总分的主程序框架
查看本课件的文字版(共 7 页,供检索与复制)

第1页:第四次上机作业题目

第四次上机作业

  • 1. 数组转置:在主函数中定义一个 n(1<=n<=6)维方阵并从键盘读入数组元素;编写函数 transpose(a, n) 实现数组 a 转置;在主函数中输出转置后的方阵
  • 2. 学生信息:编写 4 个函数实现下列功能:input() 输入 20 个学生姓名和高考总分;sort() 按高考总分从高到低排序,姓名顺序也随之调整;display() 显示学生姓名及其高考总分;search() 用顺序查找方法找出该学生,成功则显示姓名与总分,否则显示查找失败
  • 3. 字符串处理:定义两个 C 源程序文件 fmain.c 和 fstring.c。fmain.c 中包含主函数,实现字符串处理功能选项和字符串的输入与结果的输出;fstring.c 中包含 delchar(s,c) 和 strreverse(s),实现删除字符和字符串反转

第2页:参考答案:数组转置

#include <stdio.h> #define N 6 void transpose(int a[][N], int n); void main() { int n, i, j; int a[N][N]; printf("Input n(n<=6):"); scanf("%d", &n); printf("Input the array:\n"); for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d", &a[i][j]); printf("Display the array:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%-4d", a[i][j]); printf("\n"); } transpose(a, n); printf("Display the transposed array:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%-4d", a[i][j]); printf("\n"); } }

第3页:参考答案:学生信息

#include <stdio.h> #include <string.h> #define N 3 #define LEN 80 void input(char name[][LEN], float s[]); void display(char name[][LEN], float s[]); void sort(char name[][LEN], float s[]); int search(char name[][LEN], char s[]); void main() { int num; char name[N][LEN], student[LEN]; float score[N]; input(name, score); puts("Before sorting:"); display(name, score); sort(name, score); puts("After sorting:"); display(name, score); printf("Input student name you want to search:"); gets(student); num = search(name, student); ... } void input(char name[][LEN], float s[]) { int i; printf("Input name and score:\n"); for (i = 0; i < N; i++) { scanf("%s", name[i]); scanf("%f", &s[i]); } }

第4页:参考答案:排序与查找

void sort(char name[][LEN], float s[]) { int i, j, k; float t; char string[LEN]; for (i = 0; i < N-1; i++) { k = i; for (j = i+1; j < N; j++) if (s[j] > s[k]) k = j; if (k != i) { t = s[i]; s[i] = s[k]; s[k] = t; strcpy(string, name[i]); strcpy(name[i], name[k]); strcpy(name[k], string); } } } int search(char name[][LEN], char s[]) { int i; for (i = 0; i < N; i++) if (strcmp(name[i], s) == 0) return i; return -1; }

第5页:字符串处理运行示例与fmain.c

SAMPLE OUTPUT

3. Reverse a string. 4. Delete a character in a string. Please input your choice: 1 Enter a string: holiday Reversed string is:yadiloh Would you like to continue?(y/n)Y Please input your choice: 2 Enter a string: one world, one dream Enter a character you want to delete: o Deleted string is:ne wrld, ne dream Would you like to continue?(y/n)N
 #include <stdio.h> #include "e:\temp\fstring.c" void main() { ... switch (choice1) { case 1: printf("Enter a string:"); fflush(stdin); gets(s); strreverse(s); puts(s); printf("Reversed string is:"); break; case 2: printf("Enter a string:"); fflush(stdin); gets(s); printf("Enter a character you want to delete:"); fflush(stdin); c = getchar(); delchar(s, c); printf("Deleted string is:"); puts(s); break; } printf("Would you like to continue?(y/n)"); fflush(stdin); choice2 = getchar(); if (choice2 == 'N' || choice2 == 'n') break; }

第6页:参考答案:fstring.c函数

 #include <string.h> void strreverse(char s[]) { int i, j; char t; j = strlen(s); j--; for (i = 0; i < j; i++, j--) { t = s[i]; s[i] = s[j]; s[j] = t; } } void delchar(char s[], char c) { int i, j; char temp[80]; for (i = 0, j = 0; s[i] != '\0'; i++) if (s[i] != c) temp[j++] = s[i]; temp[j] = '\0'; strcpy(s, temp); }

第7页:求最大值函数与宏

 void main() { int a, b, c; printf("Input 3 integers:\n"); scanf("%d%d%d", &a, &b, &c); printf("Max of %d,%d,%d is %d\n", a, b, c, max(a, b, c)); } int max(int a, int b, int c) { int t; t = a < b ? a : b; return t > c ? t : c; } #define MAX(a,b,c) ((a)>(b)?(a):(b))>c?((a)>(b)?(a):(b)):c

宏方式在预编译时展开为字符置换,函数方式在运行时求值。

顶部目录