[問題] 怎麼讓次數顯示星號

看板C_and_CPP (C/C++)作者 (President)時間15年前 (2011/06/01 01:17), 編輯推噓1(107)
留言8則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Visual C++6.0 問題(Question): 從TXT檔讀入資料後然後計算每個數字出現的次數,並且用*號顯示出來 例如:讀入1 5 4 9 9 4 5 8 2 3 6 2 7 然後顯示1 * 2 ** 3 * 4 ** 5 ** 6 * 7 * 8 * 9 ** 程式碼(Code):(請善用置底文網頁, 記得排版) #include<iostream> #include<fstream> using namespace std; void main() { int i=0,j,k,temp,count=1; int data[100]; ifstream fip("b.txt"); while(fip.eof()!=1) { fip>>data[i]; cout<<data[i]<<" "; i++; } for(j=0;j<i-1;j++) { for(k=j+1;k<i;k++) { if(data[j]>data[k]) { temp=data[j]; data[j]=data[k]; data[k]=temp; } } } cout<<endl<<"Ans="<<endl; for(int y=0;y<i;y++) { if(data[y]==data[y+1]) { count++; } else { cout<<data[y]<<" "<<count<<endl; count=1; } } fip.close(); } 補充說明(Supplement): 目前的程式都已經將數字排序好並且顯示出現的次數,請問現在要怎麼把出現次數改成 星號顯示呢? 麻煩知道的人解決一下 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.231.221.67

06/01 01:58, , 1F
用for 裡面塞printf就好了
06/01 01:58, 1F

06/01 02:09, , 2F
文字檔內容有範圍限制嗎?
06/01 02:09, 2F

06/01 02:34, , 3F
把原本印'次數'的那行, 變成一個loop跑'次數'次印'*'XD
06/01 02:34, 3F

06/01 03:11, , 4F
cout<<setfill('x')<< setw(data[y]) << "x" << endl;
06/01 03:11, 4F

06/01 10:22, , 5F
在else{ cout << data[y]<< " " << count << endl;
06/01 10:22, 5F

06/01 10:23, , 6F
後面加上下面這一段
06/01 10:23, 6F

06/01 10:23, , 7F
for (int a=0 ; a<count ; a++) std::cout << "*";
06/01 10:23, 7F

06/06 11:26, , 8F
謝謝 問題已解決
06/06 11:26, 8F
文章代碼(AID): #1DvIAZyV (C_and_CPP)
文章代碼(AID): #1DvIAZyV (C_and_CPP)