c-如何垂直打印f



这是我的代码。我的重点是输入字符串,并计算每个字母中有多少出现

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

int main()
{
int c1;
int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0, n=0, o=0, p=0, q=0, r=0, s=0, t=0, u=0, v=0, w=0, x=0 , y=0, z=0;
while (( c1=getchar()) != EOF)
if (isalpha(tolower(c1)) != 0) {
if (tolower(c1) == 97) {  // Character = 'a'
a += 1;
}
else if (tolower(c1) == 98) { // Character = 'b'
b += 1;
}
else if (tolower(c1) == 99) { // Character = 'c'
c += 1;
}
.
.
.
}
return 0;
}

下一步,我想打印垂直的结果。你能给我一些提示吗。例如,

输入:ABC---Ddhhh

输出:

*
*   *
****   *
abcdefghijklmnopqrstuvwxyz

虽然学习可能很困难,但重要的是要花时间学习,而不是简单地复制/粘贴代码行来拥有一个大程序。最好的代码是实现目标的最简短的代码。

正如其他人所说,连续字母表应该使您使用计数器的连续数组,而不是单个变量。(一个拼写错误,您的结果将是错误的。(不需要嵌套isalpha(tolower(c))。而且,您已经展示了如何计数,但没有展示可以输出所需结果的代码。

这是为你学习,学习和成长为一个编码器。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Try to write functions; blocks of code that do what you want in compartments
void countAndGraph( char *str ) {
int max = 0, cnt[26] = { 0 }; // initialises 'max' and 26 counters
for( char *p = str; *p; p++ ) // as long as there is a character
if( isalpha( *p ) ) {     // if it is alphabetic
int i = tolower( *p ) - 'a'; // convert ASCII character to value 0-25
if( ++cnt[ i ] > max ) // increment the right counter and test against max
max = cnt[ i ]; // 'max' - the ceiling - keeps getting higher
}
for( ; max; max-- ) { // lower the ceiling
for( int i = 0; i < 26; i++ ) // for 26 alphabetic characters
putchar( " *"[cnt[i]>=max] ); // output ' ' or '*' if count at/above ceiling
putchar( 'n' ); // newline
}
puts( "abcdefghijklmnopqrstuvwxyz" ); // reference alphabet string
puts( str ); // the string that's been analysed
puts( "" );
}
int main() {
char *tst[] = { // three test strings to demonstrate
"The quick brown fox jumps over the lazy dogs",
"Sally sells seashells by the sea shore",
"I wish I was what I was when I wished I was what I am now",
};
const size_t nTst = sizeof tst/sizeof tst[0];
for( size_t i = 0; i < nTst; i++ ) // count and graph each of the strings
countAndGraph( tst[i] );
return 0;
}
*
*         *
*  *      *  ****
**************************
abcdefghijklmnopqrstuvwxyz
The quick brown fox jumps over the lazy dogs
*
*
*      *      *
*      *      *
*      *      *
*   *  *   *      *
*   *  *   *      *     *
**  *  *   *  *  ***    *
abcdefghijklmnopqrstuvwxyz
Sally sells seashells by the sea shore
*
*             *
*             *
*       *             *
*      **         *   *
*      **         *   *
*      **         *   *
*   *  **    *    **  *
*  **  **   ***   **  *
abcdefghijklmnopqrstuvwxyz
I wish I was what I was when I wished I was what I am now

既然您有了这些代码,那么一个练习就是按升序/降序打印条形图。不要止步于这个版本;把它推向一种新的教育体验。

*
*
*
*
**
***
****
*********
*************
**************
****************
*****************
******************
fzcxqpjkuvywnbmdgrloshiate
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.

任何时候,只要有一个或两个以上的变量做相同的事情,就应该使用数据结构;在本例中为数组。你会想要类似的东西

int counts[26];

而不是二十六个不同的变量,并且只有一个语句而不是26个不同的if子句。然后你可以说

counts[lower(c1) - 97]

代替ab。。。CCD_ 5(在检查CCD_。

打印输出的方式基于以下见解:

  • 字母表上方的行数是counts中的最高数(我们称之为maxcount(。在您的示例中,它将是3
  • 对于每一行,如果该字母的计数至少为maxcount - line,则存在一个*(即,在第0行中,如果该字符的计数至少有3 - 0,则存在*(,否则存在一个空格

我认为您可以一次打印一个。

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

int main()
{
int c1;
int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0, n=0, o=0, p=0, q=0, r=0, s=0, t=0, u=0, v=0, w=0, x=0 , y=0, z=0;
// I might do an array here, so you can loop through later.
int letters[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

while (( c1=getchar()) != EOF) {
if (isalpha(tolower(c1)) != 0) {
if (tolower(c1) == 97) {  // Character = 'a'
a += 1;
letters[0]++;
}
else if (tolower(c1) == 98) { // Character = 'b'
b += 1;
letters[1]++;
}
else if (tolower(c1) == 99) { // Character = 'c'
c += 1;
letters[2]++;
}
.
.
.
}
}
int ii;
for (ii = 0; ii < 26; ii++) {
printf("%dn", letters[ii]);
}
return 0;
}

最新更新