zl程序教程

您现在的位置是:首页 >  其他

当前栏目

练习 1-13 编写一个程序,打印输入中单词长度的直方图(垂直)

输入程序 一个 编写 练习 13 打印 长度
2023-09-14 09:06:57 时间

C语言程序设计(第二版) 练习1-13 个人设计

练习 1-13 编写一个程序,打印输入中单词长度的直方图(垂直)

代码块:

方法1:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int nc, ns, nw, i, m, n;                                    /*定义字符个数,单词结束判断条件,单词个数,循环变量*/
	int max;                                                    /*定义最长单词的字符数*/
	int p[50];                                                  /*定义单词长度数组*/
	char a[50][50];                                             /*定义单词字符数组*/
	char c;                                                     /*定义输入字符*/
	max=0;                                                    /*最长单词字符数初始为0*/
	i=1;                                                      /*单词长度数组下标初始值为1*/
	ns=nc=nw=0;                                           /*单词判断初始条件,字符个数初始值,单词个数初始值都为0*/
	m=n=1;                                                  /*单词数组下标初始值为1*/
	while ((c=getchar())!=EOF){                             /*当输入字符不是文件结束符*/
		if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')){  /*当输入字符是字母时*/			
		    ++nc;                                               /*字符个数增加1个*/
			a[m][n]='*';                                      /*单词数组赋值为星号*/
			++n;                                                /*本单词数组的二元下标增加1位*/
			ns=1;                                             /*单词判断为单词内*/
		}
		else if (ns==1){                                      /*如果输入字符不是字母并且单词判断条件是在单词内*/
			++nw;                                               /*单词个数增加1位*/
			++m;                                                /*单词数组一元下标增加1位*/			
			p[i]=nc;                                          /*字符个数赋值给单词长度数组*/
			if (nc>=max)                                      /*如果字符个数大于目前单词长度最大值*/
				max=nc;                                       /*当前字符个数赋值给单词长度最大值*/
			++i;                                                /*单词长度数组下标增加1位*/
			n=1;                                              /*单词数组二元下标回到初始值*/
			ns=nc=0;                                        /*单词判断条件和字符个数回到初始值*/
		}
	}
	for(n=max; n>=1; --n){                                   /*以下循环体是打印单词的垂直直方图,如果单词数组没有赋值星号,则打印空格*/
		for(m=1; m<=nw; ++m){
			if(a[m][n]=='*'){
				printf("%c  ", a[m][n]);
			}
			else
				printf("   ");
		}
		printf("\n");
	}
	for(i=1; i<=nw; ++i)                                     /*以下循环体是打印单词个数*/
		printf("%d  ", p[i]);
	printf("\n");
    system("pause");
	return 0;
}

方法2:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int c, word_length=0, word_num=0, word=0, big=0, i, j, s[10];
	char a[10][10];
	while ((c=getchar())!=EOF){
		if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
			a[word_num][word_length++]='*';
			word=1;
		}
		else if (word==1){
			if (word_length>big)
				big=word_length;
			s[word_num++]=word_length;
			word=0;
			word_length=0;
		}
	}
	for (i=big; i>=0; i--){
		for (j=0; j<word_num; j++){
			if (a[j][i]!='*')
				printf("  ");
			else
				printf("%c ", a[j][i]);
		}
		printf("\n");
	}
	for (i=0; i<word_num; i++)
		printf("%d ", s[i]);
	printf("\n");
	system("pause");
	return 0;
}

####如果程序设计有错误或更简洁的方法,欢迎并感谢您指正出示,谢谢!