我的直方图程序有问题,我正在打印出我想要它的确切内容,但它有一些不寻常的间距问题,我想知道是否有人可以帮我解决如何避免它。 这就是我的输出的样子,因为你可以看到它在90-100支架之前的所有部分都有错误的间距。
我希望它看起来如此,所以数字和星号之间的底部没有空格:
*
*
* *
* *
* *
* *
* *
* *
* * *
* * * *
* * * *
0 1 2 3
也可以推荐使用的内容代替system("Clear")
或system("PAUSE")
吗?
代码:
#include <iostream>
#include <iomanip>
using namespace std;
void readExamMarks(int examMarks[], int sizeOfArray, int& counter1, int& counter2, int& counter3, int& counter4,int& counter5, int& counter6,int& counter7, int& counter8, int& counter9, int& counter10){
cout << "Please enter a set of exam marks to see a histogram for:" << endl;
int x = 0;
for( int idx = 0; idx < sizeOfArray; idx++){
cin >> x;
if((x >=0) && (x <= 100)){
x = x/10;
switch(x){
case 1:
counter1++;
break;
case 2:
counter2++;
break;
case 3:
counter3++;
break;
case 4:
counter4++;
break;
case 5:
counter5++;
break;
case 6:
counter6++;
break;
case 7:
counter7++;
break;
case 8:
counter8++;
break;
case 9:
counter9++;
break;
case 10:
counter9++;
break;
}
examMarks[idx] = x;
}
else{
cout << "ERROR: Value must be in range [0...100], please enter a valid value\n";
}
}
}
void printExamMarksDecade(){
cout << setw(5) << "10-20 " << "21-30 " << "31-40 " <<"41-50 " << "51-60 " << "61-70 " << "71-80 " << "81-90 " << "91-100 ";
}
void printHisto(int examMarks[], int sizeOfArray,int counter1, int counter2, int& counter3, int& counter4,int& counter5, int& counter6,int& counter7, int& counter8, int& counter9, int& counter10){
system("cls");
while(counter1 != 0 ){
cout << setw(3) << "*" << endl;
counter1--;
}
while(counter2 != 0 ){
cout << setw(10) << "*" << endl;
counter2--;
}
while(counter3 != 0 ){
cout << setw(17) << "*" << endl;
counter3--;
}
while(counter4 != 0 ){
cout << setw(24) << "*" << endl;
counter4--;
}
while(counter5 != 0 ){
cout << setw(31) << "*" << endl;
counter5--;
}
while(counter6 != 0 ){
cout << setw(38) << "*" << endl;
counter6--;
}
while(counter7 != 0 ){
cout << setw(45) << "*" << endl;
counter7--;
}
while(counter8 != 0 ){
cout << setw(52) << "*" << endl;
counter8--;
}
while(counter9 != 0 ){
cout << setw(59) << "*" << endl;
counter9--;
}
}
int main()
{
int examMarks[30];
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;
int counter9 = 0;
int counter10 = 0;
readExamMarks(examMarks, 30, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9, counter10);
printHisto(examMarks, 30, counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8, counter9, counter10);
printExamMarksDecade();
system("PAUSE");
}
答案 0 :(得分:0)
每个范围的变量counter
实际上并不是您想要做的事情,您只需要一个数组:
#include <iostream>
using namespace std;
int main() {
// count array for brackets 0 to 10
int *count = new int[11];
// variable to store each mark read
int mark;
// Read in marks
while (cin >> mark) {
// Increment the count for the bracket the mark falls in
count[mark/10]++;
}
for (int i=0;i<11;i++) {
cout << i << "\t| " << string(count[i],'*') << endl;
}
}
这绘制了水平直方图,但显示了应该采用的简单方法。
$ ./histogram.out < marks
0 | ******
1 | *********
2 | **********
3 | ************
4 | *********
5 | *****************
6 | ********
7 | *********
8 | ***
9 | ****
10 | *
您可以减少修改此代码的麻烦,尝试将原始代码中的括号数增加到1000!