C语言课后习题练习(五)

1.编写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为strcmp(char *p1, char *p2)

int main() {
    char *month_name[13] = {"illegal month", "January","February","March","April","May","June","July","August","September","October","November","December"};
    int n;
    printf("input month:\n");
    scanf("%d",&n);
    if ((n <= 12) && (n >= 1))
        printf("It is %s.\n",*(month_name + n));
    else
        printf("It is wrong. \n");
    
    return 0;
}

运行结果:
c1.png

2.用指向指针的方法对n个整数排序并输出。要求将排序单独写成一个函数。n个整数在主函数中输入,最后在主函数中输出。

int main() {
    void sort(int **p, int n);
    int i, n, data[20], **p, *pstr[20];
    printf("input n:\n");
    scanf("%d",&n);
    for (i = 0; i < n; i++)
        pstr[i] = &data[i];       //将第i个整数的地址赋予指针数组pstr的第i个元素
    printf("input %d integer numbers :",n);
    for (i = 0; i < n; i++)
        scanf("%d",pstr[i]);
    p = pstr;
    sort(p, n);
    printf("Noew,the sequence is:\n");
    for (i = 0; i < n; i++)
        printf("%d",*pstr[i]);
    printf("\n");
    return 0;
}

void sort(int **p, int n) {
    int i, j, *temp;
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if (**(p + 1) > **(p + j)) {       //比较后交换整数地址
                temp = *(p + i);
                *(p + i) = *(p + j);
                *(p + j) = temp;
            }
        }
    }
}

运行结果:
C语言课后习题练习(五)_第1张图片
c2.png

3.定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题

  • 解法一:
struct {
    int year;
    int month;
    int day;     //结构体变量date中的成员对应于年、月、日
}date;
int main() {
    int days = 0;     //days为天数
    printf("input year, month, day:");
    scanf("%d,%d,%d",&date.year,&date.month,&date.day);
    switch (date.month) {
        case 1:
            days = date.day;
            break;
        case 2:
            days = date.day + 31;
            break;
        case 3:
            days = date.day + 59;
            break;
        case 4:
            days = date.day + 90;
            break;
        case 5:
            days = date.day + 120;
            break;
        case 6:
            days = date.day + 151;
            break;
        case 7:
            days = date.day + 181;
            break;
        case 8:
            days = date.day + 212;
            break;
        case 9:
            days = date.day + 243;
            break;
        case 10:
            days = date.day + 273;
            break;
        case 12:
            days = date.day + 304;
            break;
        case 13:
            days = date.day + 334;
            break;
    }
    if (((date.year%4 == 0 && date.year%100 != 0) || date.year % 4 == 0) && date.month >= 3) {
        days+=1;
    }
    printf("%d/%d is the %dth day in %d.\n",date.month,date.day,days,date.year);
    return 0;
}

运行结果:
c3.png
  • 解法二:
struct {
    int year;
    int month;
    int day;     //结构体变量date中的成员对应于年、月、日
}date;
int main() {
    int i, days;
    int day_tab[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    printf("input year, month,day:");
    scanf("%d,%d,%d",&date.year,&date.month,&date.day);
    days = 0;
    for (i = 1; i < date.month; i++)
        days = days + day_tab[i];
    days = days + date.day;
    if ((date.year % 4 == 0) && (date.year % 100 != 0 || date.year % 400 == 0) && date.month >= 3)
        days = days + 1;
    printf("%d%d is the %dth day in %d.\n",date.month,date.day,days,date.year);
    return 0;
}

运行结果:
c3_2.png

你可能感兴趣的:(C语言课后习题练习(五))