如何修改程序以使直方图垂直?我无法使其垂直。代码如下

How can I modify my program to make the histogram vertical? I'm having trouble making it vertical. Code is below

本文关键字:垂直 代码 直方图 何修改 修改 程序      更新时间:2024-09-24

无法使直方图垂直。如何修改程序使直方图垂直?请帮帮我谢谢!这就是代码。这个直方图是水平的,但我不知道如何使它垂直。使直方图垂直更具挑战性。我试过很多种方法,但似乎每次尝试都把代码搞砸了,而且都不起作用。

#include<iostream>
#include<string>
using namespace std;

int main()
{
// these 2 arrays will be used to count the letters in string
char letterArray[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z' };
int letterCountArray[26];
string userInputString;
for (int i = 0; i < 26; i++) {
letterCountArray[i] = 0;
}

cout << "****************************************THIS IS MY FREQUENCY PROGRAM*****************************************nn";
cout << "Please input your sentence: ";
getline(cin, userInputString);
int lengthOfString = userInputString.length();
for (int i = 0; i < lengthOfString; i++) {
char letter = tolower(userInputString[i]);
//finding this letter in letterArray so we can get it's position number and increment in counter array
for (int j = 0; j < 26; j++) {
if (letterArray[j] == letter) {

letterCountArray[j] = letterCountArray[j] + 1;
}
}
}
// showing result
cout << "nn***** Showing Result *****n";
cout << "----------------------------------------------------------------------------------------";
cout <<"n"<< "We have calculated the frequency of letters succuessfully and accurately for younn";
cout <<"n"<< "See Belownn";
for (int i = 0; i < 26; i++) {
if (letterCountArray[i] > 0) {
cout << "The letter '" << letterArray[i] << "' occurrs " << letterCountArray[i] << " times.n";
}
}
cout << "----------------------------------------------------------------------------------------n";
// showing histogram
cout << "n***** This is the Histogram: *****n";
// loop to display numbers and *
for (int i = 15; i >0 ; i--) {
cout.width(2);
cout << i <<"|";
for (int j = 0; j < 26; j++) {
if (letterCountArray[j] == i) {
cout << "* ";
letterCountArray[j] = letterCountArray[j] - 1;
}
else {
cout << "  ";
}
}
cout << "n";
}   
cout << "   ";
for (int i = 0; i < 26; i++) {
cout << "--";
}
cout << "n";
cout << "   ";
// displaying letters from letterArray
for (int i = 0; i < 26; i++) {
cout << letterArray[i]<<" ";
}
cout << "n";
// stopPING the screen from dissappearing immideatly 
system("pause");
return 0;
}

我开始改变循环的顺序:先是字母,然后是计数。然后我改变了两个循环的方向。然后我在打印星星和线条时对空格和破折号的数量进行了一些调整。

cout << "n***** This is the Histogram: *****n";
// loop to display numbers and *
for (int j = 25; j >= 0; --j) {
cout << letterArray[j] <<"|";
for (int i = 1; i <= 15 ; ++i) {
if (letterCountArray[j] >= i) {
cout << " * ";
}
else {
cout << "   ";
}
}
cout << "n";
}   
cout << "  ";
for (int i = 1; i <= 15; i++) {
cout << "---";
}
cout << "n";
cout << "  ";
// displaying letters from letterArray
for (int i = 1; i <= 15; i++) {
cout.width(2);
cout << i <<" ";
}
cout << "n";
// stopPING the screen from dissappearing immideatly