# 实验四捏

Task1_1

#include <stdio.h>
#define N 4
int main()
{
int a[N] = {2, 0, 2, 2};
char b[N] = {'2', '0', '2', '2'};
int i;
printf("sizeof(int) = %d\n", sizeof(int));
printf("sizeof(char) = %d\n", sizeof(char));
printf("\n");
/*输出数组a中每个元素的地址、值*/
for (i = 0; i < N; ++i)
printf("%p: %d\n", &a[i], a[i]);
printf("\n");
/*输出数组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);
return 0;
}

1 不是，4字节

2 是 ，1字节

3 一样，一样

Task1_2

#include <stdio.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;
/*输出二维数组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");
/* 输出二维数组a中每个元素的地址和值*/
for (i = 0; i < N; ++i)
for (j = 0; j < M; ++j)
printf("%p: %c\n", &b[i][j], b[i][j]);
return 0;
}

1 是 4个

2 是 1个

Task 2

#include <stdio.h>
int days_of_year(int year, int month, int day); /*函数声明*/
int main()
{
int year, month, day;
int days;
while(scanf("%d%d%d", &year, &month, &day) != EOF) /*按下Ctrl+Z终止*/
{
days = days_of_year(year, month, day); /*函数调用*/
printf("%4d-%02d-%02d是这一年的第%d天.\n\n", year, month, day, days);
}
return 0;
}

int days_of_year(int year,int month,int day)
{
int sum;
if(year%4==0&&year%100!=0||year%400==0)
{
if(month==1) sum=day;
if(month==2) sum=31+day;
if(month==3) sum=31+29+day;
if(month==4) sum=31+29+31+day;
if(month==5) sum=31+29+31+30+day;
if(month==6) sum=31+29+31+30+31+day;
if(month==7) sum=31+29+31+30+31+30+day;
if(month==8) sum=31+29+31+30+31+30+31+day;
if(month==9) sum=31+29+31+30+31+30+31+30+day;
if(month==10) sum=31+29+31+30+31+30+31+30+30+day;
if(month==11) sum=31+29+31+30+31+30+31+30+30+31+day;
if(month==12) sum=31+29+31+30+31+30+31+30+30+31+30+day;
}
else
{
if(month==1) sum=day;
if(month==2) sum=31+day;
if(month==3) sum=31+28+day;
if(month==4) sum=31+28+31+day;
if(month==5) sum=31+28+31+30+day;
if(month==6) sum=31+28+31+30+31+day;
if(month==7) sum=31+28+31+30+31+30+day;
if(month==8) sum=31+28+31+30+31+30+31+day;
if(month==9) sum=31+28+31+30+31+30+31+30+day;
if(month==10) sum=31+28+31+30+31+30+31+30+30+day;
if(month==11) sum=31+28+31+30+31+30+31+30+30+31+day;
if(month==12) sum=31+28+31+30+31+30+31+30+30+31+30+day;
}
return sum;
}

Task 3

#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 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);
sort(scores, N);
printf("\n输出课程均分: %.2f\n", ave);
printf("\n输出课程分数(高->低):\n");
output(scores, N);
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 a;
for(a=0; a<n; ++a)
printf("%d ", x[a]);
printf("\n");
}
/*计算整型数组x中n个元素均值，并返回*/
/*补足函数average()实现*/
double average(int x[], int n){
double sum=0;
double ave=0;
int i;
for(i=0;i<n;i++)
sum=sum+x[i];
ave=sum/N;
return ave;
}
/*对整型数组x中的n个元素降序排序*/
/* 补足函数sort()实现*/
void sort(int x[], int n)
{
int i,t,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(x[j]<x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
}

Task 4

#include <stdio.h>
void dec2n(int x, int n);
int main()
{
int x;
printf("输入一个十进制整数: ");
scanf("%d", &x);
dec2n(x, 2);
dec2n(x, 8);
dec2n(x, 16);
return 0;
}

void dec2n(int x, int n)
{
int i;
int a[20];
for(i=0;x!=0;i++)
{
a[i]=x%n;
x=x/n;
}
for(i=i-1;i>=0;i--)
{
if(a[i]<10)
{
printf("%d",a[i]);
}
if(a[i]>=10)
{
if(a[i]==10) printf("A");
if(a[i]==11) printf("B");
if(a[i]==12) printf("C");
if(a[i]==13) printf("D");
if(a[i]==14) printf("E");
if(a[i]==15) printf("F");
}
}
printf("\n");
}

Task 5

#include<stdio.h>
int main()
{
int a[100][100];
int n;
int i, j, k;
scanf("%d", &n);
for(i=0;i<n;++i)
{
for(k=0;k<i;k++)
{
a[i][k]=k+1;
}
for(j=i;j<n;j++)
{
a[i][j]=i+1;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}

Task 6

#include <stdio.h>
#define N 80
int main()
{
char views1[N] = "hey, c, i hate u.";
char views2[N] = "hey, c, i love u.";
printf("original\n");
puts(views1);
puts(views2);

char temp[N];
int i;

for(i=0;i<N;i++)
{
temp[i]=views1[i];
}
for(i=0;i<N;i++)
{
views1[i]=views2[i];
}
for(i=0;i<N;i++)
{
views2[i]=temp[i];
}
printf("swapping\n");
puts(views1);
puts(views2);

return 0;
}

Task 7

#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]);
return 0;
}

void bubble_sort(char str[][M], int n)
{
int i, j;
char t[10];
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 @ 2022-05-09 17:18  铭啾同志拜托了  阅读(3)  评论(0编辑  收藏  举报