看板 C_and_CPP 關於我們 聯絡資訊
#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main(){ int A[2001]={}; int tmp; for(int k=0;k<10;k++){ cin>>A[k]; } int n=10; for(int i=0;i<n-1;i++){ for(int j=0;j<n-1-i;j++){ if(A[j]>A[j+1]){ tmp=A[j]; A[j]=A[j+1]; A[j+1]=tmp; } } } cout<<"排序後"<<endl; for(int i=0;i<10;i++){ cout<<"A["<<i<<"]="<<A[i]<<endl; } return 0; } 以上是在 codeblocks 測試的 氣泡排序 問題:1 請問如何寫 從外部讀一個 test.txt 檔 裡面有數字(彼此間隔一空白) 寫入到 程式內的 矩陣A[2001]={} 之內呢 模仿 書上的寫法是 ifstream in ("test.txt") while (getline(in, A[])){ out<< A[] << endl; } 但矩陣 可以這樣直接寫入嗎?? A[]沒有明確index 好像不行 問題: 2 如果在未知 txt檔案內 總共有幾個數字前 怎麼來表示 排序的n值 嘗試用 n=sizeof(A[])/sizeofA[0] sizeof(A[]) 這寫法 好像不行@@ 要怎麼改正 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.32.117.144 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1552914834.A.13D.html
sarafciel: 我比較好奇你是看哪一本書學的......XD 03/18 22:40
tyjh: 書上只有文字檔的輸出入 上面是我自己亂想的 03/18 22:51
jerryh001: 把cin換成in就好 03/19 00:11
yilanP: istream 有一些狀態的函數,像是eof(),在檔案讀完的時候 03/20 08:29
yilanP: 會return 1,詳細可以自己查查看 03/20 08:29