參考黑馬程式csdn
第1個程式:封裝函數利用冒泡排序:
第2個程式:案例1
第3個程式:案例2
函數
作用:将一段經常使用的代碼封裝起來,減少重複代碼
文法:
傳回值類型 函數名 (參數清單)
{
函數體語句
return表達式
}
傳回值類型 :一個函數可以傳回一個值。在函數定義中
函數名:給函數起個名稱
參數清單:使用該函數時,傳入的資料
函數體語句:花括号内的代碼,函數内需要執行的語句
return表達式: 和傳回值類型挂鈎,函數執行完後,傳回相應的資料
函數的調用
使用定義好的函數
文法:
函數名(參數)
//函數定義
int add(int num1, int num2) //定義中的num1,num2稱為形式參數,簡稱形參
{
int sum = num1 + num2;
return sum;
}
int main() {
int a = 10;
int b = 10;
//調用add函數
int sum = add(a, b);//調用時的a,b稱為實際參數,簡稱實參
cout << "sum = " << sum << endl;
a = 100;
b = 100;
sum = add(a, b);
cout << "sum = " << sum << endl;
system("pause");
return 0;
}
指針
作用:可以通過指針間接通路記憶體
文法:
資料類型 * 變量名
#include<iostream>
#include<string>
using namespace std;
int main() {
//1、指針的定義
int a = 10; //定義整型變量a
//指針定義文法: 資料類型 * 變量名 ;
int * p;
//指針變量指派
p = &a; //指針指向變量a的位址
cout << &a << endl; //列印資料a的位址
cout << p << endl; //列印指針變量p
//2、指針的使用
//通過*操作指針變量指向的記憶體
cout << "*p = " << *p << endl;
system("pause");
return 0;
}
運作結果:
const修飾指針
const修飾指針有三種情況:
const修飾指針 — 常量指針
const修飾常量 — 指針常量
const即修飾指針,又修飾常量
int main() {
int a = 10;
int b = 10;
//const修飾的是指針,指針指向可以改,指針指向的值不可以更改
const int * p1 = &a;
p1 = &b; //正确
//*p1 = 100; 報錯
//const修飾的是常量,指針指向不可以改,指針指向的值可以更改
int * const p2 = &a;
//p2 = &b; //錯誤
*p2 = 100; //正确
//const既修飾指針又修飾常量
const int * const p3 = &a;
//p3 = &b; //錯誤
//*p3 = 100; //錯誤
system("pause");
return 0;
}
指針和數組
定義:利用指針通路數組中元素
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int * p = arr; //指向數組的指針
cout << "第一個元素: " << arr[0] << endl;
cout << "指針通路第一個元素: " << *p << endl;
for (int i = 0; i < 10; i++)
{
//利用指針周遊數組
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
指針和函數
利用指針作函數參數,可以修改實參的值
//值傳遞
void swap1(int a ,int b)
{
int temp = a;
a = b;
b = temp;
}
//位址傳遞
void swap2(int * p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a = 10;
int b = 20;
swap1(a, b); // 值傳遞不會改變實參
swap2(&a, &b); //位址傳遞會改變實參
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
第1個程式:封裝函數利用冒泡排序
封裝一個函數,利用冒泡排序,實作對整型數組的升序排序int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
#include<iostream>
#include<string>
using namespace std;
/*
封裝一個函數,利用冒泡排序,
實作對整型數組的升序排序
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
*/
int main(){
void Bubble_Sort_Method(int arr1[], int len);
void printarray(int arr1[], int len);
int arr[10] = { 4, 3, 6, 9, 1, 2, 10, 8, 7, 5 };
int length = sizeof(arr) / sizeof(arr[0]);
Bubble_Sort_Method( arr, length);
printarray(arr, length);
system("pause");
return 0;
}
void Bubble_Sort_Method(int arr1[],int len){
for (int i = 0; i <len-1; i++)
{
for (int j = 0; j < len-1-i; j++)
{
if (arr1[j] > arr1[j + 1])
{
int temp = arr1[j];
arr1[j] = arr1[j + 1];
arr1[j + 1] = temp;
}
}
}
}
//排序之後的數組
void printarray(int arr1[], int len)
{
for (int k = 0; k <len; k++)
{
cout << arr1[k] << endl;
}
}
運作結果:
結構體
定義:結構體屬于使用者自定義的資料類型,允許使用者存儲不同的資料類型,
文法:
struct 結構體名 { 結構體成員清單 };
通過結構體建立變量的方式有三種:
struct 結構體名 變量名
struct 結構體名 變量名 = { 成員1值 , 成員2值…}
定義結構體時順便建立變量
//結構體定義
struct student
{
//成員清單
string name; //姓名
int age; //年齡
int score; //分數
}stu3; //結構體變量建立方式3
int main() {
//結構體變量建立方式1
struct student stu1; //struct 關鍵字可以省略
stu1.name = "張三";
stu1.age = 18;
stu1.score = 100;
cout << "姓名:" << stu1.name << " 年齡:" << stu1.age << " 分數:" << stu1.score << endl;
//結構體變量建立方式2
struct student stu2 = { "李四",19,60 };
cout << "姓名:" << stu2.name << " 年齡:" << stu2.age << " 分數:" << stu2.score << endl;
stu3.name = "王五";
stu3.age = 18;
stu3.score = 80;
cout << "姓名:" << stu3.name << " 年齡:" << stu3.age << " 分數:" << stu3.score << endl;
system("pause");
return 0;
}
結構體數組
定義:将自定義的結構體放入到數組中友善維護
文法:
struct 結構體名 數組名[元素個數] = { {} , {} , ... {} }
//結構體定義
struct student
{
//成員清單
string name; //姓名
int age; //年齡
int score; //分數
}
int main() {
//結構體數組
struct student arr[3]=
{
{"張三",18,80 },
{"李四",19,60 },
{"王五",20,70 }
};
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << arr[i].name << " 年齡:" << arr[i].age << " 分數:" << arr[i].score << endl;
}
system("pause");
return 0;
}
運作結果:
結構體指針
定義:通過指針通路結構體中的成員
//結構體定義
struct student
{
//成員清單
string name; //姓名
int age; //年齡
int score; //分數
};
int main() {
struct student stu = { "張三",18,100, };
struct student * p = &stu;
p->score = 80; //指針通過 -> 操作符可以通路成員
cout << "姓名:" << p->name << " 年齡:" << p->age << " 分數:" << p->score << endl;
system("pause");
return 0;
}
結構體做函數參數
定義:将結構體作為參數向函數中傳遞
//學生結構體定義
struct student
{
//成員清單
string name; //姓名
int age; //年齡
int score; //分數
};
//值傳遞
void printStudent(student stu )
{
stu.age = 28;
cout << "子函數中 姓名:" << stu.name << " 年齡: " << stu.age << " 分數:" << stu.score << endl;
}
//位址傳遞
void printStudent2(student *stu)
{
stu->age = 28;
cout << "子函數中 姓名:" << stu->name << " 年齡: " << stu->age << " 分數:" << stu->score << endl;
}
int main() {
student stu = { "張三",18,100};
//值傳遞
printStudent(stu);
cout << "主函數中 姓名:" << stu.name << " 年齡: " << stu.age << " 分數:" << stu.score << endl;
cout << endl;
//位址傳遞
printStudent2(&stu);
cout << "主函數中 姓名:" << stu.name << " 年齡: " << stu.age << " 分數:" << stu.score << endl;
system("pause");
return 0;
}
第2個程式:案例1 學校正在做畢設項目,每名老師帶領5個學生,總共有3名老師,需求如下
設計學生和老師的結構體,其中在老師的結構體中,有老師姓名和一個存放5名學生的數組作為成員
學生的成員有姓名、考試分數,建立數組存放3名老師,通過函數給每個老師及所帶的學生指派
最終列印出老師資料以及老師所帶的學生資料
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
/*
學校正在做畢設項目,每名老師帶領5個學生,總共有3名老師,需求如下
設計學生和老師的結構體,其中在老師的結構體中,有老師姓名和一個存放5名學生的數組作為成員
學生的成員有姓名、考試分數,建立數組存放3名老師,通過函數給每個老師及所帶的學生指派
最終列印出老師資料以及老師所帶的學生資料*/
struct Student
{
string name;
int score;
};
struct Teacher
{
string name;
Student sArray[5];
};
void allocateSpace(Teacher tArray[], int len)
{
string tName = "教師";
string sName = "學生";
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++)
{
tArray[i].name = tName + nameSeed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].name = sName + nameSeed[j];
tArray[i].sArray[j].score = rand() % 61 + 40;
}
}
}
void printTeachers(Teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << tArray[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t姓名:" << tArray[i].sArray[j].name << " 分數:" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
srand((unsigned int)time(NULL)); //随機數種子 頭檔案 #include <ctime>
Teacher tArray[3]; //老師數組
int len = sizeof(tArray) / sizeof(Teacher);
allocateSpace(tArray, len); //建立資料
printTeachers(tArray, len); //列印資料
system("pause");
return 0;
}
運作結果:
第3個程式:案例2 設計一個英雄的結構體,包括成員姓名,年齡,性别;建立結構體數組,數組中存放5名英雄。
通過冒泡排序的算法,将數組中的英雄按照年齡進行升序排序,最終列印排序後的結果。
五名英雄資訊如下:
{"劉備",23,"男"},
{"關羽",22,"男"},
{"張飛",20,"男"},
{"趙雲",21,"男"},
{"貂蟬",19,"女"},
運作結果:
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
/*
設計一個英雄的結構體,包括成員姓名,年齡,性别;建立結構體數組,數組中存放5名英雄。
通過冒泡排序的算法,将數組中的英雄按照年齡進行升序排序,最終列印排序後的結果。
五名英雄資訊如下:
{"劉備",23,"男"},
{"關羽",22,"男"},
{"張飛",20,"男"},
{"趙雲",21,"男"},
{"貂蟬",19,"女"},*/
struct hero
{
string name;
int age;
string sex;
};
int main(){
void age_sort(hero arr[], int len);
void print_message(hero arr[], int len);
hero arr[5] = { { "劉備", 23, "男" }, { "關羽", 22, "男" }, { "張飛", 20, "男" },
{ "趙雲", 21, "男" },{ "貂蟬", 19, "女" }};
int length = sizeof(arr) / sizeof(arr[0]);
age_sort(arr, length);
print_message(arr, length);
system("pause");
return 0;
}
void age_sort(hero arr[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
if (arr[j].age>arr[j + 1].age)
{
hero temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void print_message(hero arr[], int len)
{
for (int k = 0; k < len; k++)
{
cout << "姓名: "<<arr[k].name << "年齡:"<<arr[k].age << "性别: "<<arr[k].sex << endl;
}
}