1 #pragma once 2 3 #ifndef GAME_H_ 4 #define GAME_H_ 5 6 #include<string> 7 using namespace std; 8 9 struct Game 10 { 11 string gameName; 12 string gameScore; 13 }; 14 15 void inputGame(Game games[], const int size); 16 void sort(Game games[], const int size); 17 void display(const Game games[], const int size); 18 19 const int Size = 5; 20 #endif
1 #include "stdafx.h" //引入系统默认的头文件 2 #include "game.h" //引入自定义的头文件 3 #include <iostream>; 4 5 void inputGame(Game games[], const int size) 6 { 7 for (int i = 0; i < size; i++) 8 { 9 cout << "请输入喜爱的游戏的名称:"; 10 cin >> games[i].gameName; 11 cout << "请输入游戏评分(10以内的小数):"; 12 cin >> games[i].gameScore; 13 } 14 } 15 void sort(Game games[], const int size) 16 { 17 Game temp; 18 for (int i = 0; i < size - 1; i++) //第一个数 19 { 20 for (int j = i+1; j < size; j++) //后面所有数 21 { 22 if (games[i].gameScore < games[j].gameScore) 23 { 24 temp = games[i]; 25 games[i] = games[j]; 26 games[j] = temp; 27 } 28 } 29 } 30 } 31 void display(const Game games[], const int size) 32 { 33 for (int i = 0; i < size; i++) 34 { 35 cout << i + 1 << ":" << games[i].gameName << "(" << games[i].gameScore << ")" << endl; 36 } 37 }
1 // C++函数和类 15-分离式编译.cpp: 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "game.h" 6 #include <iostream> 7 8 int main() 9 { 10 cout << "请输入五个你喜爱的游戏的名称,并给她们评分:" << endl; 11 Game games[Size] = {}; 12 inputGame(games, Size); 13 sort(games, Size); 14 display(games, Size); 15 return 0; 16 }