检查输入是否已输入数组

时间:2017-03-20 04:14:22

标签: c arrays

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

以下是应该发生的事情的一个例子。

  

3 - 要排序的n个日期数。

     

后跟n个日期格式(月份日)

     

1月1日

     

1月1日

     

99年2月28日

     

然后用户检查是否以(月日)的格式输入日期

     

1 1 00

最终输出:

  

日期按时间顺序排序

     

然后告诉用户他们所做的检查是否在数据中   输入(这是我需要帮助的)

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

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;
}

1 个答案:

答案 0 :(得分:0)

@api.one

你可以这样做, 但是你有一个小任务来编写函数: 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"); }