c - 嗨,我试图让这个程序运行多次(使用 for(;;) 和 do...while)但每次它都停止读取字符串
问题描述
我创建了这个程序来从用户那里读取一个字符串并按字母顺序对它的单词进行排序,最后我尝试添加一个函数来帮助我尽可能多地运行这个程序,但从来没有工作过。我使用 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
, a #define
for 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 上的优雅解决方案。我在这里使用了他的想法。
推荐阅读
- asp.net-core - ASP.NET Core / HTTP 错误 404.13 - 未找到
- sharepoint-2013 - SPWeb.CurrentUser.RegionalSettings.TimeZone 为空
- haskell - 是否可以为在同一个单子中具有动作的单子创建的 ConduitT 组合序列和连接?
- sql-server - VBA 将 QueryDef 传递到 SQL Server 中的错误 3134
- ruby-on-rails - 用户属于 Rails 中的另一个用户
- java - 整数对象前导零改变值
- hyperledger-fabric - 不支持 Ubuntu 宇宙
- python - 将列表存储在文件中
- python - 可视化时间序列序列的一维 CNN 特征重要性
- angular - 如果在刷新令牌后未经授权,则重新调度操作并获取新的访问令牌 angular ngrx-store