实验四

任务1
#include <stdio.h>
#include <stdlib.h>
#define N 4
int main()
{
int a[N]={2, 0, 2, 3};
char b[N]={'2','O','2','3'};
int i;

printf("sizeof(int)= %d\n", sizeof(int));
printf("sizeof(char)= %d\n", sizeof(char));
printf("\n");

//输出int型数组a中每个元素的地址、值

for (i = 0; i < N;++i)
printf("%p: %d\n",&a[i], a[i]);

printf("\n");
//输出char型数组b中每个元素的地址、值
for (i = 0; i < N;++i)

printf("%p: %c\n",&b[i], b[i]);
printf("\n");

//输出数组名a和b对应的值
printf("a = %p\n", a);
printf("b = %p\n", b);
system("pause");
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define N 2
#define M 3
int main(){
int a[N][M]={{1,2,3},{4,5,6}};
char b[N][M]={{'1','2','3'},{'4','5','6'}};
int i, j;
//输出int型二维数组a中每个元素的地址、值
for (i = 0; i < N;++i)
for (j = 0;j < M;++j)
printf("%p: %d\n",&a[i][j], a[i][j]);
printf("\n");
//输出int型二维数组名a,以及,a[0],a[1]的值
printf("a= %p\n", a);
printf("a[0]= %p\n", a[0]);
printf("a[1]= %p\n", a[1]);
printf("\n");
//输出char型二维数组b中每个元素的地址、值
for (i = 0; i< N;++i)
for (j = 0; j < M;++j)
printf("%p:%c\n", &b[i][j], b[i][j]);
printf("\n");
//输出char型二维数组名b,以及,b[O],b[1]的值
printf("b= %p\n", b);
printf("b[0]= %p\n", b[0]);
printf("b[1]= %p\n", b[1]);
printf("\n");
system("pause");
return 0;
}

2

void test1();
void test2();

int main() {
    printf("测试1: 用两个一维维数组,实现两个字符串交换\n");
    test1();

    printf("\n测试: 用二维数组,实现两个字符串交换\n");
    test2();

    system("pause");
    return 0;
}

void test1() {
    char views1[N] = "hey, C, I hate u.";
    char views2[N] = "hey, C, I love u.";

    printf("交换前: \n");
    puts(views1);
    puts(views2);

    swap_str(views1, views2);

    printf("交换后: \n");
    puts(views1);
    puts(views2);
}

void test2() {
    char views[2][N] = {"hey, C, I hate u.", "hey, C, I love u."};

    printf("交换前: \n");
    puts(views[0]);
    puts(views[1]);

    swap_str(views[0], views[1]);

    printf("交换后: \n");
    puts(views[0]);
    puts(views[1]);
}

void swap_str(char s1[N], char s2[N]) {
    char tmp[N];

    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);
}

3

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

#define N 80

int count(char x[]);

int main() {
    char words[N+1];
    int n;

    while(gets(words) != NULL) {
        n = count(words);
        printf("单词数: %d\n\n", n);
    }

    return 0;
}

int count(char x[]) {
    int i;
    int word_flag = 0;  // 用作单词标志,一个新单词开始,值为1;单词结束,值为0
    int number = 0;  // 统计单词个数

    for(i = 0; x[i] != '\0'; i++) {
        if(x[i] == ' ')
            word_flag = 0;
        else if(word_flag == 0) {
            word_flag = 1;
            number++;
        }
    }
    system("pause");
    return number;
}

#include <stdio.h>
#define N 1000

int main() {
    char line[N];
    int word_len;   // 记录当前单词长度
    int max_len;    // 记录最长单词长度
    int end;        // 记录最长单词结束位置
    int i;

    while(gets(line) != NULL) {
        word_len = 0;
        max_len = 0;
        end = 0;

        i = 0;
        while(1) {
            // 跳过连续空格
            while(line[i] == ' ') {
                word_len = 0;  // 单词长度置0,为新单词统计做准备
                i++;
            }

            // 在一个单词中,统计当前单词长度
            while(line[i] != '\0' && line[i] != ' ') {
                word_len++;
                i++;
            }
        
            // 更新更长单词长度,并,记录最长单词结束位置
            if(max_len < word_len) {
                max_len = word_len;
                end = i;   // end保存的是单词结束的下一个坐标位置
            }

            // 遍历到文本结束时,终止循环
            if(line[i] == '\0')
                break;
        }

        // 输出最长单词
        printf("最长单词: ");
        for(i = end - max_len; i < end; ++i)
            printf("%c", line[i]);
        printf("\n\n");
    }

    return 0;
}

4

    printf("\n输出课程分数(高->低):\n");
    output(scores, N);
    
    system("pause");
    return 0;
}

// 函数定义
// 输入n个整数保存到整型数组x中 
void input(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        scanf("%d", &x[i]); 
}

// 输出整型数组x中n个元素 
void output(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}

// 计算整型数组x中n个元素均值,并返回 
// 补足函数average()实现

double average(int x[], int n){
    int i,a=0;
    double b;
    
    for(i = 0; i < n; ++i)
        a=a+x[i];

    b=a/n;
return b;
}



// 对整型数组x中的n个元素降序排序 
// 补足函数bubble_sort()实现
void bubble_sort(int x[], int n){
    int i,j,t;
    for(i=0;i<=n;i++)
        for(j=0;j<=n;j++)
            if (x[i]>=x[j]){
                t=x[i];
                x[i]=x[j];
                x[j]=t;
            }
return x[i];
}

5

#include <stdio.h>
#define N 100
void dec2n(int x, int n); // 函数声明

int main() {
    int x;

    printf("输入一个十进制整数: ");
    while(scanf("%d", &x) != EOF) {
        dec2n(x, 2);  // 函数调用: 把x转换成二进制输出
        dec2n(x, 8);  // 函数调用: 把x转换成八进制输出
        dec2n(x, 16); // 函数调用: 把x转换成十六进制输出

        printf("\n输入一个十进制整数: ");
    }

    return 0;
}

// 函数定义
// 功能: 把十进制数x转换成n进制,打印输出
// 补足函数实现
void dec2n(int x, int n)
{
char h[N];
int i, j = 0, k = 0, l = 0;
for (i = 0; x!= 0;i++,j++)
{
    k = x % n;
    if (k <10)
    {

       h[i]='0'+ k;
    }
     if (k>= 10)
     {
         h[i]='A'+ k - 10;
     }
      x = x/ n;
}
for (l = j - 1; l>= 0; l--)
{
    printf("%c", h[l]);
}
printf("\n");
}

6

#include <stdio.h>
#include <stdlib.h>
#define N 100
#define M 4

void output(int x[][N], int n);          // 函数声明
void rotate_to_right(int x[][N], int n); // 函数声明


int main() {
    int t[][N] = {{21, 12, 13, 24},
                  {25, 16, 47, 38},
                  {29, 11, 32, 54},
                  {42, 21, 33, 10}};

    printf("原始矩阵:\n");
    output(t, M); // 函数调用
rotate_to_right(t, M);
 output(t, M); // 函数调用
    system("pause");
    return 0;
}

// 函数定义
// 功能: 输出一个n*n的矩阵x
void output(int x[][N], int n) {
    int i, j;

 

for (i = 0; i < n; ++i) {
        for (j = 0; j < n; ++j)
            printf("%4d", x[i][j]);

        printf("\n");
    }
}

// 待补足3:函数rotate_to_right()定义
// 功能: 把一个n*n的矩阵x,每一列向右移, 最右边被移出去的一列绕回左边
void rotate_to_right(int x[][N], int n){
    int i,j,m,temp=0;

for(i=0;i<n;i++)//第i行
{    
        temp=x[i][n-1];

        for(j=n-1;j>0;j--){//n=3,j=2,j2=j1;j1=j0

            x[i][j]=x[i][j-1];}

            x[i][0]=temp;
}
printf("\n");
}

7

#include <stdio.h>
#include <stdlib.h>
#define N 80

void replace(char x[], char old_char, char new_char); // 函数声明

int main() {
    char text[N] = "c programming is difficult or not, it is a question.";

    printf("原始文本: \n");
    printf("%s\n", text);

    replace(text, 'o', '*'); // 函数调用 注意字符形参写法,单引号不能少

    printf("处理后文本: \n");
    printf("%s\n", text);
    system("pause");
    return 0;
}

// 函数定义
void replace(char x[], char old_char, char new_char) {
    int i;

    for (i = 0; x[i] != '\0'; ++i) // 思考: '\0'是什么,为什么能作为循环结束条件
        if (x[i] == old_char)
            x[i] = new_char;
}

#include <stdio.h>
#include <stdlib.h>
#define N 80

int main() {
char str[N], ch;
int i;

printf("输入字符串: ");
gets(str);

printf("输入一个字符: ");
ch = getchar();

printf("截断处理......");

i = 0;
while (str[i] != '\0') {
if (str[i] == ch)
str[i]='\0';
else
i++;

}

printf("\n截断处理后字符串: %s\n", str);
system("pause");
return 0;
}

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

#define N 5
#define M 20

void bubble_sort(char str[][M], int n);

int main() {
char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"};
int i;

printf("输出初始名单:\n");
for (i = 0; i < N; i++)
printf("%s\n", name[i]);

printf("\n排序中...\n");
bubble_sort(name, N);

printf("\n按字典序输出名单:\n");
for (i = 0; i < N; i++)
printf("%s\n", name[i]);
system("pause");
return 0;
}

void bubble_sort(char str[][M],int n){
int i,j;
char t[20];

for(j=0;j<n;j++)
{for(i=0;i<n-j-1;i++)
if(strcmp(str[i],str[i+1])>0)
{strcpy(t,str[i]);
strcpy(str[i],str[i+1]);
strcpy(str[i+1],t);}
}
}

 

posted on 2023-04-19 23:07  楼璟  阅读(17)  评论(0)    收藏  举报