1 / 20

資料型態 陣列名稱 [ 長度 ]; 資料型態 陣列名稱 [ 長度 ]={ 值 1, 值 2,….};

陣列. 資料型態 陣列名稱 [ 長度 ]; 資料型態 陣列名稱 [ 長度 ]={ 值 1, 值 2,….};. int a[3]; a[0]=2; a[1]=1; a[2]=5;. int a[3]={2,1,5};. // 排序 ; 由小排到大 # include <iostream.h> void main() { int i,j,b; int a[5]={2,6,3,1,5}; for (i=0;i<(5-1);i++) { for(j=i+1;j<5;j++) {

kalyca
Télécharger la présentation

資料型態 陣列名稱 [ 長度 ]; 資料型態 陣列名稱 [ 長度 ]={ 值 1, 值 2,….};

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 陣列 • 資料型態 陣列名稱[長度]; • 資料型態 陣列名稱[長度]={值1,值2,….}; int a[3]; a[0]=2; a[1]=1; a[2]=5; int a[3]={2,1,5};

  2. //排序;由小排到大 # include <iostream.h> void main() { int i,j,b; int a[5]={2,6,3,1,5}; for (i=0;i<(5-1);i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) {b=a[i];a[i]=a[j];a[j]=b;} } } for(i=0;i<5;i++)cout<<a[i]; cout<<“\n”; }

  3. 數列{2,4,1,3,5}計算各數列中各數值所佔的百分比數列{2,4,1,3,5}計算各數列中各數值所佔的百分比 # include <iostream.h> void main() { int i,sum=0; int a[5]={2,6,3,1,5}; double p; for (i=0;i<(5-1);i++)sum=sum+a[i]; for(i=0;i<4;i++) { p=double(a[i])/sum; cout<<a[i]<<“百分比”<<p<<“\n”; } }

  4. 字串 • char letter; • char string[4]; letter=‘a’; string=“C++”;//錯誤寫法 char string[4]=“C++”;//字串陣列的長度要大 於字串至少1 char x[4]; cin>>x;

  5. 字串 char x[4]=“C++”; //字串陣列的長度要大 於字串至少1 X[0] x[1] x[2] x[3] //’\0’字串結束字元 …’0’是零

  6. 輸入一字串,將此字串以反向輸出。例如:輸入steven輸出nevets輸入一字串,將此字串以反向輸出。例如:輸入steven輸出nevets #include<iostream.h> void main() { char x[50]; int i,L=0; cin>>x; for(i=0;i<50;i++) { if(x[i]==‘\0’)break; L=L+1; } for(i=L-1;i>=0;i--)cout<<x[i]; }

  7. 輸入身份證字號 //判斷身分字號的長度 #include<iostream.h> void main() { char x[20]; int i,L,right; L=0;right=0; while(right!=1){ cin >>x; for(i=0;i<20;i++){ if (x[i]=='\0')break; cout<<x[i]; L=L+1;} if(L==10))right=1; else cout<<“長度輸入錯誤\n”;} }

  8. 輸入身份證字號 //判斷輸入的身份證字號第一個是否為英文字母 #include<iostream.h> void main() { char x[20]; int right; right=0; while(right!=1) { cin >>x; if((x[0]<='z'&& x[0]>='a') || (x[0]<='Z' && x[0]>='A'))right=1; else cout<<“輸入錯誤\n”; } }

  9. 輸入身份證字號 //判斷輸入的身份證字號的阿拉伯數 #include<iostream.h> void main() { char x[20]; int right=0,L=10; while(right!=1) { cin >>x; for(i=1;i<L;i++) { if(x[i]<=‘9’&& x[i]>=‘0’)right=1; else {right=0;cout<<“輸入錯誤\n”;break;} } } }

  10. 輸入身份證字號 //判斷輸入的身份證字號的阿拉伯數 #include<iostream.h> void main() { char x[20]; int right=0,L,i; while(right!=1){ L=0; cin >>x; for(i=0;i<20;i++){if (x[i]=='\0')break;cout<<x[i];L=L+1;} if(L==10)right=1; if(right==1){right=0; if((x[0]<='z'&& x[0]>='a') || (x[0]<='Z' && x[0]>='A'))right=1;} if(right==1){right=0; for(i=1;i<L;i++)if(x[i]<=‘9’&& x[i]>=‘0’)right=1; else{right=-1;break;}} if(right!=1)cout<<“輸入錯誤\n”;} }

  11. ‘A’,’B’,…..’Z’→10,11,……33 沒有’I,O’ W的位置在Y的後面 身分證字號A123456789 A→10拆成x1=1;x2=0 A1=1;A2=2……A9=9; Y=X1+9*X2+8*A1+7*A2+….+2*A7+A8+A9 Y能被10整除 RIGHT ASCII 0(零)→48 A→65 a→97

  12. ASCII 0(零)→48 A→65 a→97 if(right==1){right=0; if(x[0]>=97)z=x[0]-87;else x[0]=z-55; z1=z/10; z2=z%10; y=z1+z2*9; for(i=1;i<(L-1);i++){y=y+(x[i]-48)*(L-1-i);} y=y+x[L-1]-48; if(y%10==0)right=1;}

  13. # include <iostream.h> void main() { int i,j,k,n; int d[12]={31,28,31,30,31,30,31,31,30,31,30,31}; char m[12][7]={"一月","二月","三月","四月","五月","六月","七月","八月“,"九月“,"十月","十一月","十二月"}; k=1;//2007年一月一日星期 一 for(i=0;i<12;i++) { cout<<m[i]<<"\n"; cout<<" 日 一 二 三 四 五 六\n"; for(n=1;n<=k;n++) cout<<" "; for(j=1;j<=d[i];j++) { if (j<10)cout<<" "<<j; else cout<<" "<<j; k++; k=k%7; if(k==0) cout<<"\n"; } cout<<"\n\n"; } }

  14. 函數的定義int checkLen(char x[20]){int Len;return Len;}void main(){L=checkLen(x);}

  15. int checkLen(char x[20]){ int i,L=0; for(i=0;i<20;i++) {if (x[i]=='\0') break; cout<<x[i]; L=L+1;} if(L==10)right=1; return right; }int checkFir(char x[20],int right){if((x[0]<='z'&& x[0]>='a') || (x[0]<='Z' && x[0]>='A'))right=1;return right;}void main(){.right=checkLen(x,right);if(right==1){right=0;right=checkFir(x,right);}if(right==1){right=0; right=checkNum(x,right);}if(right==1){right=0; right=checkID(x,right); } }

  16. 數值分析 Taylor’s series Euler’s Method

  17. 檔案的輸出(寫入檔案)#include<fstream.h>ofstream fop(“test.dat”);// 用ofstream宣告一物件fopfop<<x<<‘ ‘<<y<<‘\n’; fop.close();

  18. #include<iostream.h> #include<fstream.h> void main() { double x0,y0,y1,f,h=0.01; x0=0; y0=1; ofstream fop("test.txt"); while(x0<=2.0) { fop<<x0<<' '<<y0<<'\n'; f=-2*x0*y0; y1=y0+h*f; x0=x0+h; y0=y1; } fop.close(); }

  19. y1=y y2=y1’ y3=y2’=-2*y2-90*y1 Euler’s Method

  20. #include<iostream.h> #include<fstream.h> void main() { double x0,y10,y20,y30,h=0.01,y11,y21; x0=0; y10=0.15; y20=0; ofstream fop("test.txt"); while(x0<=5.0) { y30=-2*y20-90*y10; fop<<x0<<' '<<y10<<'\n'; y11=y10+h*y20; y21=y20+h*y30; x0=x0+h; y10=y11; y20=y21; } fop.close(); }

More Related