检查输入是否已输入到数组中

标签 c arrays

我试图在我的代码中包含一个检查,以查看是否已输入值。我的程序以(月日年)的格式对日期列表进行排序,当月份作为字符输入时。

以下是应该发生的情况的示例。

3 - the number of n dates to sort.

followed by n dates in format (month day year)

January 1 01

January 1 00

February 28 99

Then the user checking if a date has been inputted in the format of (month day year)

1 1 00

最终输出:

dates sorted in chronological order

Then telling the user if the check they made is in the data they inputted (This is what I need help with)

这就是我尝试检查是否已输入值的方法。

int k;
int input[j];
scanf("%d", &input[j]);

for (i = 0; k < n; k++) {
    if (ar[i] == input[j]) {

        printf("Yes value is there");
    }
    else {

        printf("No value isn't");
    }
}

下面我发布了我的工作程序,但没有尝试执行检查。运行此命令只会返回按时间顺序排序的日期。

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

/* constants for max chars, max day, max year, max size */
enum { MAXC = 12,
    MAX_DAY = 31,
    MAX_YEAR = 12,
    MAX_SIZE = 1000 };
typedef struct {
    char month[MAXC]; /* either make static or allocate separately */
    unsigned day;
    unsigned year;
    int monthnum;
} date;

/* empty character remaining in stdin */
void empty_stdin()
{
    int c;
    while ((c = getchar()) != '\n' && c != EOF) {
    }
}

int fix_year(int year)
{
    if (year >= 90)
        return 1900 + year;
    return 2000 + year;
}

/* sort struct date on year */
int sort(const void* a, const void* b)
{
    date* date1 = (date*)a;
    date* date2 = (date*)b;

    /*if (date2->year != date1->year)
        return (date1->year > date2->year) - (date1->year < date2->year);*/

    int year1 = fix_year(date1->year);
    int year2 = fix_year(date2->year);
    //return year2 - year1;

    //int month1 = date1->month;
    //int month2 = date2->month;

    if (date2->year != date1->year) {
        return year2 - year1;
    }
    else if (date2->month != date1->month) {
        int month1 = date1->month;
        int month2 = date2->month;

        return month2 - month1;
    }
    else if (date2->day != date1->day) {
        //else sort day
        int day1 = date1->month;
        int day2 = date2->month;

        /*int daycompare = date2->day - date1->day;
    return -daycompare;
    */

        return day2 - day1;
    }

    return 0;
}

/* output n elements of array of struct date */
void output(date* ar, int n)
{
    int i;

    printf("\nOutput sorted by year:\n\n");

    for (i = 0; i < n; i++)
        printf("  %s %d %d\n", ar[i].month, ar[i].day, ar[i].year);
}

int main(void)
{

    int i, n;
    date* ar = NULL;

    while (1) { /* obtain valid 'n', compare with using fgets below */

        int rtn; /* varaible to save return of scanf -- always validate */

        //printf ("Enter number of dates to be entered (between 1 & 1000): ");
        if ((rtn = scanf("%d", &n)) != 1) { /* if conversion failed */
            if (rtn == EOF) { /* test for user cancelation of input */
                fprintf(stderr, "note: user canceled input, exiting.\n");
                return 0;
            } /* otherwise simply an invalid input */
            fprintf(stderr, "error: invalid input.\n");
            goto tryagain;
        }

        if (n < 0) { /* invalid input < 0 */
            fprintf(stderr, "error: invalid input (n < 0).\n");
            goto tryagain;
        }

        if (n > MAX_SIZE) { /* invalid input > MAX_SIZE */
            fprintf(stderr, "error: invalid input (n > %d).\n", MAX_SIZE);
            goto tryagain;
        }

        break; /* if we are here - we have a good value, break */

    tryagain:; /* label for goto to jump over break */

        empty_stdin(); /* empty characters that remain in input buffer */
    }

    empty_stdin(); /* empty characters that remain in input buffer */

    /* allocate array of struct ar, n elements */
    if ((ar = malloc(sizeof *ar * n)) == NULL) {
        fprintf(stderr, "error: virtual memory exhausted.\n");
        return 1;
    }

    /* provide format instructions */
    //printf ("Enter the date (month day year)\n"
    // "  format, e.g.:  Jan 18 2017\n\n");

    for (i = 0; i < n; i++) { /* loop until all elements filled */

        char buf[MAX_DAY + 1] = "", ans[MAXC] = "";

        //printf (" date[%2d] : ", i + 1);    /* prompt for input */

        /* if fgets return is NULL, EOF encountered */
        if (fgets(buf, MAX_DAY + 1, stdin) == NULL) {
            fprintf(stderr, "note: user canceled input, exiting.\n");
            return 0;
        }

        if (*buf == '\n') { /* if first char is '\n', user just hit enter */
            printf("no input provided, quit (y/n)? ");
            if (fgets(ans, MAXC, stdin) && (*ans == 'y' || *ans == 'Y'))
                return 0;
            else if (!*ans) { /* if ans NULL, EOF encountered */
                fprintf(stderr, "note: user canceled input, exiting.\n");
                return 0;
            }
        }

        /* parse with sscanf, validate 3 conversion took place */
        if (sscanf(buf, "%11s %u %u", ar[i].month, &ar[i].day, &ar[i].year) != 3) {
            fprintf(stderr, "error: invalid input.\n");
            continue;
        }

        /* only increment if valid sscanf conversion took place */
    }

    //scanf("%d", &date_list[i].day);
    qsort(ar, n, sizeof(date), sort); /* sort by year */

    output(ar, n); /* output results */

    free(ar); /* free ar - you allocate it, you free it */

    return 0;
}

最佳答案

int cday, cyear, cmonth, i; 
char smonth[MAXC];
scanf("%d %d %d",&month ,&day, &cyear);
month_num_to_string(cmonth,smonth); 
for (i = 0; i < n; i++) {
   if (ar[i].day== cday && ar[i].year== cyear && !strcmp(smonth, ar[i].month) { 
       break;
   }
}
if (i<n) { 
   printf("Yes value is there\n"); 
} else { 
   printf("No value isn't\n"); 
}

你可以做类似的事情, 但是您有一个小任务来编写该函数: month_num_to_string(cmonth,smonth);

关于检查输入是否已输入到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42895749/

相关文章:

java - 为什么这个方法返回null? (从数组中调用一组字符串)

c - C语言实现文件间回调

c - 我从文件中读取一个字符串并将其存储到一个数组中,但是我如何知道数组的长度呢?

c - 返回 C 中带有 const 数组的结构体

javascript - 排队 Freecodecamp

c#计算数组之间的重叠

php - 我们如何计算 aaray 的结束位置?

cArray[][] 指针数组转换

c - 如何在没有参数的情况下将值传递给函数

c - 写一个小程序,困了,从命令行获取循环计数