實驗2
驗證性實驗部分
①函數聲明和函數定義各自的作用及二者的區別
函數聲明一定要放在主函數之前,因為系統要先識別才能調用,函數定義則是對函數的解釋,是函數算法的說明,可以放在主函數后面。
②什么是形參?什么是實參?函數參數和返回值在函數中起到什么作用?
函數定義時寫的參數叫做形參,這個只是告訴你函數需要這些參數才能運行,這些參數只是給你看的,沒有分配內存,沒有具體的值。
函數調用時寫的參數叫做實參,這些參數要有意義,即分配了內存,有具體的值。
③函數參數傳遞過程中,值傳遞和引用傳遞區別
值傳遞僅僅傳遞的是值。
引用傳遞,傳遞的是內存地址,修改后會改變內存地址對應儲存的值。
編程實驗部分
1
#include<iostream> using namespace std; int main() { cout << "Menu: A(dd) D(elete) S(ort) Q(uit),Select one:" << endl; while (true){ char t;cin >> t; if (t == 'A') { cout << "數據已經增加。" << endl; continue; } else if (t == 'D') { cout << "數據已經刪除。" << endl; continue; } else if (t == 'S') { cout << "數據已經排序。" << endl; continue; } if (t =='Q') break; return 0; } }
#include<iostream> using namespace std; int main() { while(true) { cout<<"Menu:A(dd) D(elete) S(ort) Q(uit), Select One:"<<endl; char t; cin>>t; switch(t) { case 'A':cout<<"數據已經增加"<<endl;break; case 'D':cout<<"數據已經刪除"<<endl;break; case 'S':cout<<"數據已經排序"<<endl;break; case 'Q':return 0; } } }

2
#include<iostream> using namespace std; int main(){ int a,i=2,j; while (i<=100) { a=1,j=2; while (j<=i) { if (i%j == 0) { a++; }j++; } if (a==2){ cout << i << " "; } i++; } return 0; }
#include<iostream> using namespace std; int main() { int i, j, a; for (i = 2; i <=100; i++) { a = 1; for (j = 2; j <= i; j++){ if (i%j == 0) { a++; } } if (a == 2) { cout << i << " "; } }return 0; }
#include<iostream> using namespace std; int main() { int i=2, j, a; do { a = 1, j = 2; for (j = 2; j <= i; j++) { if (i%j == 0) { a++; } } if (a == 2) { cout << i << " "; } i++; } while (i <= 100); return 0; }

3
#include <iostream> using namespace std; int main() { int i,n=65;//n為要猜的數,可修改n的值 cout << "猜這個數:"; cin >> i; while (true) { if (i != n) { if (i < n) cout << "猜小了" << endl; else cout << "猜大了" << endl; } else { cout << "猜對了"; break; } cin >> i; } return 0; }
#include <iostream> using namespace std; int main() { int i, n = 65;//n為要猜的數,可修改n的值 cout << "猜這個數:"; cin >> i; do { if (i != n) { if (i < n) cout << "猜小了" << endl; else cout << "猜大了" << endl; } else { cout << "猜對了"; break; } cin >> i; } while (true); return 0; }

4
#include<iostream> using namespace std; int main() { int i,j,k,s=0; for(i=0;i<5;i++) for(j=i+1;j<5;j++) for(k=j+1;k<5;k++) if(i!=k&&j!=k&&i!=j) s+=1; cout<<s<<endl; return 0; }

浙公網安備 33010602011771號