页数:7页文件大小:33.7 KB来源: C语言程序设计ppt课件
本课件布置第四次上机作业,要求编写transpose函数实现方阵转置,编写input、sort、display、search四个函数完成学生姓名与高考总分的录入排序显示与查找,并通过fmain.c与fstring.c两个源程序文件实现字符串反转与删除字符功能,随后给出完整的参考答案代码与运行示例。
第四次上机作业
transpose(a, n) 实现数组 a 转置;在主函数中输出转置后的方阵#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"); } }#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]); } }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; }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; } #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); } 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宏方式在预编译时展开为字符置换,函数方式在运行时求值。
下载地址:{{downurl}}
{{{a}}}
{{#b}}{{{b}}}
{{/b}} {{/title}} {{#nolog}}登陆注册
{{/nolog}} {{#djsx}}点我刷新
{{/djsx}} {{#gocz}} {{/gocz}} {{#ffqr}} {{/ffqr}}