第四次上机作业

task1_1

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

int main() {
    int a[N] = {2, 0, 2, 3};          
    char b[N] = {'2', '0', '2', '3'}; 
    int i;

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

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

    printf("\n");

    for (i = 0; i < N; ++i)
        printf("%p: %c\n", &b[i], b[i]);
    
    printf("\n");
    
    printf("a = %p\n", a); 
    printf("b = %p\n", b); 
    
    system("pause");
    return 0;
}

验证结果:

1.int 型数据在数组a是连续存放的,每个元素占4个内存字节单元

2.char型数据在数组b中是连续存放的,每个元素占1个内存字节单元

3.数组a对应的值和&a[0]一样,数组b对应的值和&b[0]一样

task1_2

#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;

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

    printf("a = %p\n", a);
    printf("a[0] = %p\n", a[0]);
    printf("a[1] = %p\n", a[1]);
    printf("\n");


    for (i = 0; i < N; ++i)
        for (j = 0; j < M; ++j)
            printf("%p: %c\n", &b[i][j], b[i][j]);
    printf("\n");

    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;
}

验证结果:

1.int 型二维数组a,在内存中是按行连续存放的,每个元素占4个字节

2.int 型二维数组a,数组名a和&a[0][0]的值,在数字字面上是一样的

3.char 型二维数组b,在内存中是按行连续存放的,每个元素占1个字节

4.char 型二维数组b,数组名b和&b[0][0]的值,在数字字面是是有一点

5.二维数组a[0]与a[1],b[0]和b[1]都是指的该行第一个元素地址

task2

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

#define N 80

void swap_str(char s1[N], char s2[N]);
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);
}

思考:如果数组是一维数组调用时可以不加[ ] 而数组如果是二维数组则调用的时候需要加[ ] 

 

task3_1

#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);
    }
    
    system("pause");
    return 0;
}

int count(char x[]) 
{
    int i;
    int word_flag = 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++;
        }
    }

    return number;
}

task3_2

#include <stdio.h>
#include<stdlib.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;
                i++;
            }

            while(line[i] != '\0' && line[i] != ' ') {
                word_len++;
                i++;
            }
        
            if(max_len < word_len) {
                max_len = word_len;
                end = i;
            }

            if(line[i] == '\0')
                break;
        }

        printf("最长单词: ");
        for(i = end - max_len; i < end; ++i)
            printf("%c", line[i]);
        printf("\n\n");
    }
    
    system("pause");
    return 0;
}

 

1.在C语言里,char类型的变量存储的是ASCII码,而在askII码中a~z以及A~Z之间是连续的,所以可以用!(line[i] <= 'z '&&line[i]>='a' ||line[i] <= 'z '&&line[i]>='a')替换原先只判断是否为空格

2.统计最长单词数可以定义一个int型的数组将每一个单词的长度存入其中,可以用b[i]数组存储单词长度,然后使用a[b[i++]]=a[b[i++]]+1  (边界条件利用单词的个数),因为max_len记录了单词的最大长度,所以最后的a[max_len]就存储了最长单词的个数

task4

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

void input(int x[], int n);
void output(int x[], int n);
double average(int x[], int n);
void bubble_sort(int x[], int n);

int main() {
    int scores[N];
    double ave;
    
    printf("录入%d个分数:\n", N);
    input(scores, N);
    
    printf("\n输出课程分数: \n");
    output(scores, N);
    
    printf("\n课程分数处理: 计算均分、排序...\n");
    ave = average(scores, N);
    bubble_sort(scores, N);
    
    printf("\n输出课程均分: %.2f\n", ave);
    printf("\n输出课程分数(高->低):\n");
    output(scores, N);
    
    system("pause");
    return 0;
}

void input(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        scanf("%d", &x[i]); 
}

void output(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}

double average(int x[], int n)
{
    int i;
    double ave = 0.0;
    for (i=0;i<n;i++)
    {
        ave+= x[i];
    }
    return ave/n;
 } 

void bubble_sort(int x[], int n)
{
    int i,j,t;
    for(i=0;i<n-1;i++){
        for(j=0;j<n-1-i;j++)
        if(x[j+1]>x[j]){
            t = x[j];
            x[j] = x[j+1];
            x[j+1] = t;
        }
    }
}

task5

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
void dec2n(int x, int n);

int main() {
    int x;

    printf("输入一个十进制整数: ");
    while(scanf("%d", &x) != EOF) {
        dec2n(x, 2);
        printf("\n");
        dec2n(x, 8);
        printf("\n");
        dec2n(x, 16);
        printf("\n");
        printf("\n输入一个十进制整数: ");
    }
    system("pause");
    return 0;
}

void dec2n(int x, int n)
{
    char s[]="0123456789ABCDEF";
    char ans[100];
    int i,r;
    for (i=0;x!=0;i++)
    {
        r = x%n;
        x = x/n;
        ans[i] = s[r];
    }
    for(i;i>=0;i--)
    {
        printf("%c",ans[i]);
    }
}

task6

#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);

    printf("变换后矩阵:\n");
    output(t, M);
    
    system("pasue");
    return 0;
}

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");
    }
}

void rotate_to_right(int x[][N], int n)
{
    int i,j,t;
    for (i = 0 ;i < n; i++){
        t = x[i][n-1];
        for (j = 0 ;j < n; j++)
        {
            x[i][n-1-j] = x[i][n-2-j];
        }
        x[i][0] = t;
}
        
}

task7_1

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

void replace(char x[], char old_char, char new_char);
int main() {
    char text[N] = "The c programming Language is very difficlut for me,but i like it";

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

    replace(text, 'i', '*');

    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)
        if (x[i] == old_char)
            x[i] = new_char;
}

问题1:replace()实现的功能是将数组中之前的某一个字符换成一个指定的新字符。

问题2:'\0'在ASCLL字符中对应空字符NULL,是字符串的结束标识,因此可以做为line24行代码结束循环条件。

task7_2

#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++;
    }
    str[i] = '\0';

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

task8

#include <stdio.h>
#include<stdlib.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(i=0;i<n-1;i++)
    for(j=0;j<n-i-1;j++)
    if(strcmp(str[j],str[j+1])>=0)
    {
    strcpy(t,str[j]);
    strcpy(str[j],str[j+1]);
    strcpy(str[j+1],t);
    }
}

 

posted @ 2023-04-14 18:22  chen,,  阅读(20)  评论(0编辑  收藏  举报