首页 > 解决方案 > 我得到正确的输出,但我使用的测试是说:

问题描述

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct Student {
    char name[15];
    char lastname[50];
    int age;
} student;
int findYoungest(struct Student students[10], int length) {
    int currentYoungestIndex = 0;
    for (int i = 0; i < length; i++) {
        if (students[currentYoungestIndex].age > students[i].age) {
            currentYoungestIndex = i;
        }
    }
    return currentYoungestIndex;
}

int findOldest(struct Student students[10], int length) {
    int currentOldestIndex = 0;
    for (int i = 1; i < length; i++) {
        if (students[currentOldestIndex].age < students[i].age) {
            currentOldestIndex = i;
        }
    }
    return currentOldestIndex;
}

void printStudentInformation(struct Student students[10], int countOfStudents) {
    if(countOfStudents <= 0) {
        printf("No students were given\n");
    }

    printf("Count: %i\n", countOfStudents);
    for (int i = 0; i < countOfStudents; ++i) {
        printf("Name = %s%s, Age = %d\n", students[i].name, students[i].lastname, students[i].age);
    }

    printf("Youngest: %s%s\n",
           students[findYoungest(students, countOfStudents)].name,
           students[findYoungest(students, countOfStudents)].lastname);
    printf("Oldest: %s%s\n",
           students[findOldest(students, countOfStudents)].name,
           students[findOldest(students, countOfStudents)].lastname);
}


int main() {
    char name[50];
    char lastname[50];
    int age;
    char stop[] = "stop";
    int wroteStop = 1;
    int countOfStudents = 0;
    student * students = NULL;

    while (wroteStop) {
        scanf("%s", name);
        fgets(lastname, 49, stdin);
        name[strcspn(name, "\n")] = 0;
        lastname[strcspn(lastname, "\n")] = 0;

        if (strcmp(name, stop) != 0) {
            scanf("%i", &age);

            // Create dynamic array
            if (!students) {
                students = (student *) malloc(sizeof(student *));
            } else {
                students = (student *) realloc(students, sizeof(countOfStudents + 1));
            }
            // Add student to list.
            strcpy(students[countOfStudents].name, name);
            strcpy(students[countOfStudents].lastname, lastname);
            students[countOfStudents].age = age;
            countOfStudents++;
        } else {
            wroteStop = 0;
        }

    }
    // Print student information
    printStudentInformation(students, countOfStudents - 1);

    free(students);
    students = NULL;
    return 0;

}

示例输入:

Erin Smith
10
Matthew Berg
90
Alexandria Garcia
60
Michelle Lewis
85
Amanda Best
34
Michelle Cook
47
Todd Vazquez
13
Erica Moss
50
Sarah Holder
41
Charles Vincent Jr.
29
Joshua Rivera
25
Katherine Peters
56
Natasha Lee
32
Emily Carter
79
Bryan Ingram
87
Gavin Lewis
53
Richard Ellis
51
Nicole Payne
55
Brian Cantrell
20
William Jackson
16
stop

等待程序退出时超时。程序可能正在等待输入或有无限循环。”

用户输入:名字、姓氏和年龄。

这被附加到一个列表中,稍后会对其进行迭代,以便我们在列表中获得非常学生和列表中最年轻/最年长的学生。如果用户键入“stop”,printStudentInformation()则执行名为的输出函数

标签: arrayscoutput

解决方案


推荐阅读