简体   繁体   English

用C处理多个输入的输出

[英]Processing outputs of multiple inputs in C

It's not something trivial but I would like to know the best way to process multiple outputs, for example: 这不是一件小事,但我想知道处理多个输出的最佳方法,例如:

Input 输入项

First line of input will contain a number T = number of test cases. 输入的第一行将包含数字T =测试用例数。 Following lines will contain a string each. 接下来的各行将包含一个字符串。

Output 输出量

For each string, print on a single line, "UNIQUE" - if the characters are all unique, else print "NOT UNIQUE" 对于每个字符串,在一行上打印“ UNIQUE”-如果字符都是唯一的,则打印“ NOT UNIQUE”

Sample Input 样本输入

 3
 DELHI
 london
 #include<iostream>

Sample Output 样本输出

 UNIQUE
 NOT UNIQUE
 NOT UNIQUE

So how can I accomplish outputs like that? 那么我该如何完成这样的输出? My code so far is: 到目前为止,我的代码是:

 int main(int argc, char *argv[]) 
 {

     int inputs, count=0;
     char str[100];
     char *ptr;

     scanf("%d",&inputs);


     while(inputs-- >0)
     {
         scanf("%s",str);

         for(ptr=str; *ptr!='\0';ptr++)
         {
             if( *ptr== *(ptr+1))
             {
                 count++;
             }
         }
         if(count>0)
         {
             printf("NOT UNIQUE");

         }
         else
         {
             printf("UNIQUE");
         }

     }

 }

But the above will obviously print the output after each input, but I want the output only after entering all the inputs, if the user enters 3, then the user have to give 3 strings and after the output will be given whether the given strings are unique or not. 但是上面显然会在每次输入后打印输出,但是我只希望在输入所有输入后才输出,如果用户输入3,则用户必须给出3个字符串,并且在给出输出后是否给出给定的字符串独特与否。 So I want to know how can I achieve the result given in the problem. 所以我想知道如何获得问题中给出的结果。 Also another thing I want to know is, I am using an array of 100 char, which it can hold a string up to 100 characters, but what do I have to do if I want to handle string with no limit? 我还想知道的另一件事是,我使用的是100个字符的数组,它可以容纳最多100个字符的字符串,但是如果我想无限制地处理字符串,该怎么办? Just declaring char *str is no good, so what to do? 仅声明char * str不好,那么该怎么办?

Hope this helps: 希望这可以帮助:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int inputs,count=0;
    char str[20];
    scanf("%d",&inputs);
    char *ptr;
    char *dummy;

    while(inputs-- >0)
    {
        scanf("%s",str);

        for(ptr=str; *ptr!='\0';ptr++)
        {
            for(dummy=ptr+1; *dummy != '\0';dummy++)
            {
                if( *ptr== *dummy)
                {
                    count=1;
                }
            }
            if(count == 1)
                break;
        }
        if(count>0)
        {
            printf("NOT UNIQUE");
        }
        else
        {
            printf("UNIQUE");
        }
    }
}

If you want to save stuff for later use, you must store it somewhere. 如果要保存东西以备后用,则必须将其存放在某个地方。 The example below stores up to 10 lines in buf and then points str to the current line: 下面的示例在buf最多存储10行,然后将str指向当前行:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>    /* for strlen */
#include <ctype.h>     /* for isspace */

int main(int argc, char *argv[]) 
{
    int ninput = 0;
    char buf[10][100];      /* storage for 10 strings */
    char *str;              /* pointer to current string */
    int i;

    printf("Enter up to 10 strings, blank to and input:\n");

    for (i = 0; i < 10; i++) {
        int l;

        str = buf[i];

        /* read line and break on end-of-file (^D) */
        if (fgets(str, 100, stdin) == NULL) break;

        /* delete trailing newline & spaces */
        l = strlen(str);
        while (l > 0 && isspace(str[l - 1])) l--;
        str[l] = '\0';

        /* break loop on empty input */
        if (l == 0) break;
        ninput++;
    }

    printf("Your input:\n");
    for (i = 0; i < ninput; i++) {
        str = buf[i];
        printf("[%d] '%s'\n", i + 1, str);
    }

    return 0;
}

Note the two separate loops for input and output. 注意输入和输出的两个单独的循环。

I've also rejiggled your input. 我也重新调整了您的输入。 I'm not very fond of fscanf ; 我不太喜欢fscanf I prefer to read input line-wise with fgets and then analyse the line with strtok or sscanf . 我更喜欢使用fgets逐行读取输入,然后使用strtoksscanf分析该行。 The advantage over fscanf is that yout strings may contain white-space. fscanf相比,优点是您的字符串可能包含空格。 The drawback is that you have a newline at the end which you usually don't want and have to "chomp". 缺点是您通常不需要并在末尾使用换行符,而必须“换行”。

If you want to allow for longer strings, you should use dynamic allocation with malloc , although I'm not sure if it is useful when reading user input from the console. 如果要允许更长的字符串,则应将动态分配与malloc ,尽管我不确定从控制台读取用户输入时它是否有用。 Tackle that when you have understood the basics of fixed-size allocation on the stack. 当您了解堆栈上固定大小分配的基础时,请解决。

Other people have already pointed you to the error in your check for uniqueness. 其他人已经指出您的唯一性检查错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM