繁体   English   中英

嗨,我试图使该程序多次运行(使用for(;;)并执行…同时),但是每次它停止读取字符串时

[英]Hi, I tried to make this program run multiple times(using for(;;) and do…while) but every time it stops at reading the string

我创建了这个程序来从用户那里读取一个字符串并按字母顺序对其单词进行排序,最后我尝试添加一个功能来帮助我尽可能多地运行该程序,但从未成功。 我使用do ... while,但是每次程序在读取字符串之前停止。

char *words[L];
char *word;
char sentence[100];
int i = 0, nrCuvinte = 0;
    int j, k, ok, n, lung;


for(j=0;j<L;++j)
{
    words[j] = (char*)malloc(L*sizeof(char));
}
printf("Enter any sentence you want: \n");

fgets(sentence,99,stdin);
lung = strlen(sentence);

if(sentence[lung-1] == '\n')
{
    sentence[lung-1] = '\0';
}
printf("\n");

word = strtok(sentence, " .,-;/?!");
for(j=0;j<(strlen(word)+1);j++)
{
    word[j] = tolower((unsigned char) word[j]);
}
while(word != NULL)
{
    for(j=0;j<(strlen(word)+1);j++)
    {
        word[j] = tolower((unsigned char) word[j]);
    }
    strcpy(words[i],word);
    word = strtok(NULL, " .,-;/?!");
    ++i;
    ++nrCuvinte;

}

n = nrCuvinte-1;
do{
    ok =1;
    for(k=0;k<n;++k)
    {
        if(strcmp(words[k],words[k+1])>0)
        {
            char *aux;
            aux = words[k];
            words[k] = words[k+1];
            words[k+1]= aux;
            ok = 0;
        }
    }
    --n;
}while(n>0&&(ok==0));

for(j=0;j<nrCuvinte;++j)
{
    puts(words[j]);
}
for(j=0;j<L;++j)
{
    free(words[j]);
    words[j]=0;
}

我添加了#includes ,一个#define为L,一个包裹内一切main功能,并放置在while(1)的内部循环main功能。 while(1)循环的开始,我添加了nrCuvinte = 0; i = 0; 为了重置那些变量。 我还添加了通过在要求时键入“是”来突破while(1)循环的功能。

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

#define L 500

int main(){

  char user_input[4];
  char *words[L];
  char *word;
  char sentence[100];
  int i = 0, nrCuvinte = 0;
  int j, k, ok, n, lung;

  while(1){
    i = 0;
    nrCuvinte = 0;
    for(j=0;j<L;++j){
      words[j] = (char*)malloc(L*sizeof(char));
    }
    printf("Enter any sentence you want: \n");

    fgets(sentence,99,stdin);
    lung = strlen(sentence);

    if(sentence[lung-1] == '\n'){
      sentence[lung-1] = '\0';
    }
    printf("\n");

    word = strtok(sentence, " .,-;/?!");
    for(j=0;j<(strlen(word)+1);j++){
      word[j] = tolower((unsigned char) word[j]);
    }
    while(word != NULL)
    {
      for(j=0;j<(strlen(word)+1);j++){
        word[j] = tolower((unsigned char) word[j]);
      }
      strcpy(words[i],word);
      word = strtok(NULL, " .,-;/?!");
      ++i;
      ++nrCuvinte;
    }

    n = nrCuvinte-1;
    do{
      ok =1;
      for(k=0;k<n;++k){
        if(strcmp(words[k],words[k+1])>0)
        {
          char *aux;
          aux = words[k];
          words[k] = words[k+1];
          words[k+1]= aux;
          ok = 0;
        }
      }
      --n;
    }while(n>0&&(ok==0));

    for(j=0;j<nrCuvinte;++j){
      puts(words[j]);
    }
    for(j=0;j<L;++j){
      free(words[j]);
    }
    printf("If you want to exit type 'yes', else press enter.\n");
    fgets(user_input,4,stdin);
    if(strcmp(user_input, "yes") == 0)
      break;
  }
}

我还写了另一个版本,这次将您的代码包装在一个函数中,并在while(1)循环中从主函数调用此函数:

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

#define L 500

void sort_sentence(){
  char *words[L];
  char *word;
  char sentence[100];
  int i = 0, nrCuvinte = 0;
  int j, k, ok, n, lung;

  for(j=0;j<L;++j)
  {
    words[j] = (char*)malloc(L*sizeof(char));
  }
  printf("Enter any sentence you want: \n");

  fgets(sentence,99,stdin);
  lung = strlen(sentence);

  if(sentence[lung-1] == '\n'){
    sentence[lung-1] = '\0';
  }
  printf("\n");

  word = strtok(sentence, " .,-;/?!");
  for(j=0;j<(strlen(word)+1);j++){
    word[j] = tolower((unsigned char) word[j]);
  }
  while(word != NULL){
    for(j=0;j<(strlen(word)+1);j++){
      word[j] = tolower((unsigned char) word[j]);
    }
    strcpy(words[i],word);
    word = strtok(NULL, " .,-;/?!");
    ++i;
    ++nrCuvinte;

  }

  n = nrCuvinte-1;
  do{
    ok =1;
    for(k=0;k<n;++k){
      if(strcmp(words[k],words[k+1])>0){
        char *aux;
        aux = words[k];
        words[k] = words[k+1];
        words[k+1]= aux;
        ok = 0;
      }
    }
    --n;
  }while(n>0&&(ok==0));

  for(j=0;j<nrCuvinte;++j){
    puts(words[j]);
  }
  for(j=0;j<L;++j){
    free(words[j]);
  }

}

int clean_stdin()
{
  while (getchar()!='\n');
  return 1;
}

int main(){

  int user_input;
  char c;

  while(1){
    sort_sentence();
    do{
      printf("If you want to exit type '2', if you want to continue type '1'.\n");
    }while(((scanf("%d%c", &user_input, &c) != 2 || c!='\n') && clean_stdin()) ||( user_input != 2 && user_input != 1));
    if(user_input == 2)
      return 0;
  }
}

编辑:现在在第二个代码示例中,将提示用户输入一条消息,告诉他/他输入1或2,直到他/她输入1或2,其他所有输入都会再次提示相同的消息。 如果输入1,则执行下一个sort_sentence()函数调用;如果读取2,则程序终止。 感谢 MOHAMED在社区Wiki上为他的优雅解决方案 我在这里用了他的想法。

暂无
暂无

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

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