这种情况有其他方法替代goto语句吗?

wokaoyan1981 2016-12-03 02:07:50
编写一个程序,读取输入,直到读入了10个字符串或遇到EOF,由二者中最先被满足的那个终止读取过程。这个程序可以为用户提供一个有5个选项的菜单:输出初始字符串列表、按ASCII顺序输出字符串、按长度递增顺序输出字符串、按字符串中第一个单词的长度输出字符串和退出。菜单可以循环,直到用户输入退出请求。当然,程序要能真正完成菜单中的各项功能。
我的代码已经完成,但是出现了goto语句,能够用其他方法替代吗?
#include <stdio.h>
#include <string.h>
#define LIM 10
#define SIZE 81
void output(char * strings[], int num);
void asc_output(char * strings[], int num);
void len_output(char * strings[], int num);
void word_len_output(char * strings[], int num);
int first_word_len(char * strings);
char * s_gets(char * st, int n);
int main(void)
{
int ct;
char input[LIM][SIZE];
char * ptstr[LIM];
char ch;
statement:while(1)
{
printf("Options:\n");
printf("a)original strings; b)ASCII;\n");
printf("c)lengths of lines; d)length of first word;\n");
printf("q)quit.\n");
while((ch=getchar())!='q')
{
ct=0;
printf("Input up to %d lines.\n",LIM);
while( ct<LIM&&s_gets(input[ct],SIZE)!=NULL&&input[ct][0]!=EOF)
{

ptstr[ct]=input[ct];
ct++;
}
switch (ch)
{
case 'a' :output(ptstr,ct); goto statement;
case 'b' :asc_output(ptstr,ct); output(ptstr,ct);goto statement;
case 'c' :len_output(ptstr,ct); output(ptstr,ct);goto statement;
case 'd' :word_len_output(ptstr,ct);output(ptstr,ct); goto statement;
default : printf("only a,b,c or d is available, please try again!\n");
continue;
}

}
return 0;
}
}

void output(char * strings[], int num)
{
int i;
for(i=0;i<num;i++)
{
fputs(strings[i],stdout);
printf("\n");
}
return;
}

void asc_output(char * strings[], int num)
{
char * temp;
int i;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(strcmp(strings[top],strings[seek])>0)
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}

void len_output(char * strings[], int num)
{
char * temp;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(strlen(strings[top])>strlen(strings[seek]))
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}

void word_len_output(char * strings[], int num)
{
char * temp;
int top,seek;
for(top=0;top<num-1;top++)
for(seek=top+1;seek<num;seek++)
if(first_word_len(strings[top])>first_word_len(strings[seek]))
{
temp=strings[top];
strings[top]=strings[seek];
strings[seek]=temp;
}
}

int first_word_len(char * strings)
{
int i=0,j=0;
for(i=0;isblank(strings[i]);i++)
{}
for(j=i;isalpha(strings[j]);j++)
{}
return j-i;
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i=0;

ret_val=fgets(st,n,stdin);
if(ret_val)
{
while (st[i]!='\n'&&st[i]!='\0')
i++;
if(st[i]=='\n')
st[i]='\0';
else
while( getchar()!='\n')
continue;
}
}
...全文
360 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2016-12-03
  • 打赏
  • 举报
回复
引用 4 楼 wokaoyan1981 的回复:
也就是说循环嵌套比较深的时候还是可以用goto。楼上修改的代码只执行完一次功能就退出了,貌似没有解决问题。
是的,如果你不用goto,就检查一个变量判断是否继续循环
ID870177103 2016-12-03
  • 打赏
  • 举报
回复
状态机式写法适合一切字符扫描,而且逻辑清晰,不容易出错
wang0635 2016-12-03
  • 打赏
  • 举报
回复
从来没人说要禁用goto。在容易理解的前提下使用goto,比用一些机械的标志更高效
wokaoyan1981 2016-12-03
  • 打赏
  • 举报
回复
也就是说循环嵌套比较深的时候还是可以用goto。楼上修改的代码只执行完一次功能就退出了,貌似没有解决问题。
ck2333 2016-12-03
  • 打赏
  • 举报
回复
continue接在while((ch=getchar())!='q')循环体后面。
ck2333 2016-12-03
  • 打赏
  • 举报
回复
switch (ch) { case 'a' :output(ptstr,ct); break; case 'b' :asc_output(ptstr,ct); output(ptstr,ct);break; case 'c' :len_output(ptstr,ct); output(ptstr,ct);break; case 'd' :word_len_output(ptstr,ct);output(ptstr,ct); break; default : printf("only a,b,c or d is available, please try again!\n"); continue; } continue;
paschen 2016-12-03
  • 打赏
  • 举报
回复
如果循环比较深,用goto也可以,如果要替换,就这样写吧;
#include <stdio.h>
#include <string.h>
#define LIM 10
#define SIZE 81 
void output(char * strings[], int num); 
void asc_output(char * strings[], int num);
void len_output(char * strings[], int num);
void word_len_output(char * strings[], int num);
int first_word_len(char * strings);
char * s_gets(char * st, int n);
int main(void)
{
	int ct;
	char input[LIM][SIZE];
	char * ptstr[LIM];
	char ch;
	while(1)
		  {
			  printf("Options:\n");
			  printf("a)original strings;              b)ASCII;\n");
			  printf("c)lengths of lines;              d)length of first word;\n");
			  printf("q)quit.\n");
			  bool flag = true;
			  while(flag && (ch=getchar())!='q')
			  {
				  ct=0;
				  printf("Input up to %d lines.\n",LIM);
				  while( ct<LIM&&s_gets(input[ct],SIZE)!=NULL&&input[ct][0]!=EOF)
				  {

					  ptstr[ct]=input[ct];
					  ct++;
				  }
				  switch (ch)
				  {
				  case 'a' :output(ptstr,ct); flag = false; break;
				  case 'b' :asc_output(ptstr,ct); output(ptstr,ct); flag = false; break;
				  case 'c' :len_output(ptstr,ct); output(ptstr,ct); flag = false; break;
				  case 'd' :word_len_output(ptstr,ct);output(ptstr,ct);  flag = false; break;
				  default : printf("only a,b,c or d is available, please try again!\n");
					  continue;
				  } 

			  }
			  return 0;   		
		  }
}

void output(char * strings[], int num)
{
	int i;
	for(i=0;i<num;i++)
	{
		fputs(strings[i],stdout);
		printf("\n");
	}
	return;
}

void asc_output(char * strings[], int num)
{
	char * temp;
	int i;
	int top,seek;
	for(top=0;top<num-1;top++)
		for(seek=top+1;seek<num;seek++)
			if(strcmp(strings[top],strings[seek])>0)
			{
				temp=strings[top];
				strings[top]=strings[seek];
				strings[seek]=temp;
			}
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧