1

我是这里的新手,有一些问题想从你们那里得到一些教训。例如:

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

void main()
{
    char name[51],selection;

    do
    {

    printf("Enter name: ");
    fflush(stdin);
    fgets(name,51,stdin);
    printf("Enter another name?(Y/N)");
    scanf("%c",&selection);
    selection=toupper(selection);
    }while (selection=='Y');
                     //I want to printout the entered name here but dunno the coding
printf("END\n");
system("pause");
}

据我所知,循环何时执行会覆盖变量,那么我如何执行编码以打印出用户输入的所有名称?我已经问过我的导师,他要求我使用指针,在这种情况下有人可以指导我吗?

4

3 回答 3

1

您基本上有两个选项,创建所有名称的列表,在最后循环遍历每个名​​称,或者在读取它们时将所有名称连接成一个字符串,并在最后打印整个缓冲区。第一个大致是这样的:

 char ch[2]="Y", names[100][52]; //store up to 100 50-char names (leaving space for \n and \0)
 int x, nnames=0;
 while(nnames<100 && *ch=='Y'){
     fgets(names[nnames++], sizeof names[0], stdin); //since names is a 2d array, look
     //only at the length of a row, (in this case, names[0])
     fgets(ch, 2, stdin);
     *ch = toupper(*ch);
 }
 for(x=0; x<nnames; x++)
     //print names[x].

第二个大致是这样的:

 char names[100*52], *np=names, *ep=names+sizeof names; //an array, the position, and the end
 char ch[2]="Y";
 while(np<ep && *ch=='Y'){
      fgets(np, ep-np, stdin); //read a name, and a newline into the buffer
      //note how I use the difference between the end and the position to tell
      //fgets the most it can possibly read
      np+=strlen(np); //advance the position to the end of what we read.
      //same deal with the y/n stuff...
 }
 printf("%s", names);

注意最后没有循环,因为整个字符串都存储在名称中。

于 2012-07-07T05:31:31.463 回答
0

您可以使用链接列表来存储所有输入的名称。所有的数据结构书籍都大量谈论链表。

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

struct name_node
{
    char name[100];
    struct name_node *next;
};

struct name_node* add_name(struct name_node *first, const char *name)
{
    struct name_node *p;

    /* create our node */
    p = (struct name_node*)malloc(sizeof(struct name_node));
    strcpy(p->name, name);
    p->next = NULL;

    /* link our node to the tail */
    if (first) {
        for (; first->next; first = first->next);
        first->next = p;
    }

    return p;
}

void print_names(struct name_node *first)
{
    /* print names stored in the list */
    for (; first; first = first->next) {
        printf("%s\n", first->name);
    }
}

/* free the memory we used */
void destroy_list(struct name_node *first)
{
    if (first) {
        if (first->next)
            destroy_list(first->next);

        free(first);
    }
}

int main(void)
{
    struct name_node *head = NULL;
    struct name_node *node;
    char name[100];
    char selection;

    do {
        printf("Enter name: ");
        fflush(stdin);
        fgets(name, 51, stdin);

        /* save the user input to a linked list */
        /* save head if we don't have head */
        node = add_name(head, name);
        if (!head)
            head = node;

        printf("Enter another name?(Y/N)");
        scanf("%c", &selection);
        selection = toupper(selection);
    }
    while (selection == 'Y');

    /* print the list if we have any data */
    if (head)
        print_names(head);

    /* free the memory we used */
    destroy_list(head);

    printf("END\n");
    system("pause");

    return 0;
}
于 2012-07-07T03:07:03.280 回答
0

使用单个字符串获取所有名称:

char allnames[1000] = "";

do {
    //get name in fgets
    selection = toupper(selection);
    strcat(allnames, selection);

    //Check for repetion

} while (/*there is repetion*/);

printf("%s", allnames);

您必须适当地选择所有名称的大小以存储所有名称。

于 2012-07-07T04:40:17.150 回答