实验4

实验任务1;

1.1:

1.int型数组a,在内存中 连续存放

#include <stdio.h>
#define N 4

void test1()
{
    int a[N]={1,98,4};
    int i;

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

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

    printf("a=%p\n",a);
}

void test2()
{
    char b[N]={'1','9','8','4'};
    int i;

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

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

    printf("b=%p\n",b);
}

int main()
{
    printf("测试1:int类型一维数组\n");
    test1();

    printf("测试2:char类型一维数组\n");
    test2();

    return 0;
}

 1.2

#include <stdio.h>
#define N 2
#define M 4

void test1()
{
    int a[N][M]={{1,9,8,4},{2,0,4,9}};
    int i,j;

    printf("sizeof(a)=%d\n",sizeof(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");

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

void test2()
{
    char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}};
    int i,j;

    printf("sizeof(b)=%d\n",sizeof(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");

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

int main()
{
    printf("测试1:int类型一维数组\n");
    test1();

    printf("测试2:char类型一维数组\n");
    test2();

    return 0;
}

 

 

 

 

实验任务2:

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

#define N 80

void swap_str(char s1[N],char s2[N]);
void test1();
void test2();

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

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

    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:

3.1:

 

 

 

3.2:

 

 

 

 

思考:

利用ASCII码的范围来解决字母以外的影响

实验任务4:

#include <stdio.h>
#include <math.h>
#define N 100
void dec_to_n(int x,int n);

int main()
{
    int x;

    printf("输入一个十进制整数:");
    while(scanf("%d",&x)!=EOF){
        dec_to_n(x,2);
        dec_to_n(x,8);
        dec_to_n(x,16);
        printf("输入一个十进制整数:");
    }

    return 0;
}

void dec_to_n(int x,int n)
{
    char s[10]={0};
    int a[10];
    int cnt=0;

    while(x>0){
        a[cnt]=x%n;
        x/=n;
        cnt++;
    }

    for(int i=0;i<cnt;i++){
        s[i]=a[i]+48;
        if(s[i]==':'){
            s[i]='A';
        }else if(s[i]==';'){
            s[i]='B';
        }else if(s[i]=='<'){
            s[i]='C';
        }else if(s[i]=='='){
            s[i]='D';
        }else if(s[i]=='>'){
            s[i]='E';
        }else if(s[i]=='?'){
            s[i]='F';
        }
    }

    for(int cntt=cnt-1;cntt>=0;cntt--){
        printf("%c",s[cntt]);
    }
    printf("\n");
}

 实验任务5:

#include <stdio.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);
    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)
{
    double ave;
    double sum = 0;
    for(int cnt = 0;cnt<n;cnt++){
        sum+=x[cnt];
    }
    ave=1.0*sum/n;
    return ave;
}

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


}

 实验任务6:

#include <stdio.h>
#include <string.h>
#define N 5
#define M 20
void output(char str[][M],int n);
void bubble_sort(char str[][M],int n);

int main()
{
    char name[][M]={"Bob","Bill","Joseph","Taylor","George"};
    int i;
    printf("输出初始名单:\n");
    output(name,N);
    printf("\n排序中...\n");
    bubble_sort(name,N);
    printf("\n按字典序输出名单:\n");
    output(name,N);
    return 0;
}

void output(char str[][M],int n)
{
    int i;
    for(i=0;i<n;i++)
       printf("%s\n",str[i]);
}

void bubble_sort(char str[][M],int n)
{
    int round;
    for(int cnt=0;cnt<n;cnt++){
        round=n-cnt;
        for(int cnt=0;cnt<round;cnt++){
            if(strcmp(str[cnt],str[cnt+1])>0){
                char i[M];
                strcpy(i,str[cnt+1]);
                strcpy(str[cnt+1],str[cnt]);
                strcpy(str[cnt],i);
            }
        }
    }
}

 

实验任务7:

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

int main()
{
    char a[110];
    while (scanf("%s", a) != EOF) {
          int flag = 0;
          for (int n = 0; n < strlen(a); n++) {
              for (int s = n + 1; s < strlen(a); s++) {
                if (a[n] == a[s]) {
                     flag = 1;
                     break;
                }
             }
            if (flag == 1) {
                 break;
            }
         }
         if (flag == 1) {
             printf("YES\n\n");
         }
         else if (flag == 0) {
             printf("NO\n\n");
         }
     }
     return 0;
}

 

实验任务8:

#include <stdio.h>
#include <string.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);
    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 mid[M];
    for (int j = 0; j < n; j++) {
        mid[j] = x[j][M - 1];
    }
    for (int j = 0; j < n; j++) {
        for (int i = n-1; i >= 1; i--) {
            x[j][i] = x[j][i - 1];
        }
    }
    for (int i = 0; i < n; i++) {
        x[i][0] = mid[i];
    }
}

 

posted @ 2023-11-19 20:05  zy050101-  阅读(1)  评论(0编辑  收藏  举报