如何修改程序以使直方图垂直?我无法使其垂直。代码如下
How can I modify my program to make the histogram vertical? I'm having trouble making it vertical. Code is below
无法使直方图垂直。如何修改程序使直方图垂直?请帮帮我谢谢!这就是代码。这个直方图是水平的,但我不知道如何使它垂直。使直方图垂直更具挑战性。我试过很多种方法,但似乎每次尝试都把代码搞砸了,而且都不起作用。
#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
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 如何在c++中仅使用for循环打印此代码的垂直直方图
- 如何在以下代码中使用垂直条