[英]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.