Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个月提醒程序中理解strcmp()函数_C_Arrays - Fatal编程技术网

在一个月提醒程序中理解strcmp()函数

在一个月提醒程序中理解strcmp()函数,c,arrays,C,Arrays,下面是打印一个月提醒列表的程序。这是K.N.金书中的一个例子。我的问题是我不能理解strcmp函数在这个程序中是如何工作的 #include <stdio.h> #include <string.h> #define MAX_REMIND 50 /* Maximum number of reminders */ #define MSG_LEN 60 /* max length of reminders message */ int rea

下面是打印一个月提醒列表的程序。这是K.N.金书中的一个例子。我的问题是我不能理解strcmp函数在这个程序中是如何工作的

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

#define MAX_REMIND 50       /* Maximum number of reminders */
#define MSG_LEN 60          /* max length of reminders message */

int read_line(char str[], int n);

int main(void) {
    char reminders[MAX_REMIND][MSG_LEN+3];
    char day_str[3], msg_str[MSG_LEN+1];
    int day, i, j, num_remind = 0;

    for(;;) {
        if(num_remind == MAX_REMIND) {
            printf("--No space left--\n");
            break;
        }

        printf("Enter day and reminder: ");
        scanf("%2d", &day);
        if(day == 0)
            break;
        sprintf(day_str, "%2d", day);
        read_line(msg_str, MSG_LEN);

        for(i = 0; i < num_remind; i++)
            if(strcmp(day_str, reminders[i]) < 0)
                break;

        for(j = num_remind; j > i; j--) 
            strcpy(reminders[j], reminders[j - 1]);

        strcpy(reminders[i], day_str);
        strcat(reminders[i], msg_str);

        num_remind++;
    }

    printf("\nDay Reminder\n");
    for(i = 0; i < num_remind; i++)
        printf(" %s\n", reminders[i]);

    return 0;
}

int read_line(char str[], int n) {
    int ch, i = 0;
    while((ch = getchar()) != '\n')
        if (i < n)
            str[i++] = ch;

    str[i] =  '\0';
    return i;
}
#包括
#包括
#定义最大提醒50/*最大提醒次数*/
#定义MSG_LEN 60/*提醒消息的最大长度*/
int read_行(字符str[],int n);
内部主(空){
字符提醒[最大提醒][MSG_LEN+3];
char day_str[3],msg_str[msg_LEN+1];
整数天,i,j,num_=0;
对于(;;){
如果(num\u提醒==MAX\u提醒){
printf(“--没有剩余空间--\n”);
打破
}
printf(“输入日期和提醒:”);
扫描频率(“%2d”和天);
如果(天==0)
打破
sprintf(第二天,“%2d”,第二天);
读线(msg_str,msg_LEN);
对于(i=0;ii;j--)
strcpy(提醒[j],提醒[j-1]);
strcpy(提醒[i],第天);
strcat(提醒[i]、信息和str);
num_++;
}
printf(“\n日期提醒\n”);
对于(i=0;i
我的理解是,字符串存储在2D数组中,其中每一行都接受来自用户的字符串。程序首先获取日期(用户输入的两个十进制数),然后使用sprintf()函数将其转换为字符串。然后将转换后的字符串日期与存储在提醒[]数组中的字符串进行比较


我不明白它是如何比较日期和字符串的。(在这种情况下,它总是返回true,每次在i=0时语句都会中断)。

此代码中使用的strcmp用于排序。添加一些调试代码(在第27行之后),您将看到strcmp生成的结果:

for(i = 0; i < num_remind; i++) {
    printf("%s comparing to %s is %d \n", day_str, reminders[i], strcmp(day_str, reminders[i]));
    if(strcmp(day_str, reminders[i]) < 0) {
            break;
    }
}

看看这个,了解这种排序。

day变量是一个整数,它被组成一个格式为“%2d”的字符串,因此数字6变成了“6”

然后程序在提醒中循环,并在此情况下中断:

if(strcmp(day_str, reminders[i]) < 0)
if(strcmp(day_str,提醒[i])<0)
这里使用strcmp来确定day_str中的字符串排序是否早于提醒[i]中的字符串


因此,提醒[]数组按升序天数排序。此循环在新数据的插入点处中断。然后,它将所有现有提醒在阵列中向下移动一个位置,为插入新数据腾出空间。

事实并非如此。这不是解释代码的正确论坛。我们应该把OP转移到一个论坛,在那里他可以得到很好的专家建议。代码审查在优秀的C代码编写者中没有得到充分的体现,因此他可能无法在这里得到一个完美的答案。事实并非如此。这不是解释代码的正确论坛。我们应该把OP转移到一个论坛,在那里他可以得到很好的专家建议。代码评审在优秀的C代码编写者中代表性不足,因此他可能无法在这里得到完美的答案。
if(strcmp(day_str, reminders[i]) < 0)