几个简单程序怎么写

时间:2025-01-27 00:11:29 单机游戏

菜单选择程序

```cpp

include

using namespace std;

void BubbleSort() {

int iTemp;

int* pData, Count;

cin >> Count;

pData = new int[Count];

for (int i = 0; i < Count; i++) {

cin >> pData[i];

}

for (int i = 0; i < Count; i++) {

for (int j = 1; j < Count - i; j++) {

if (pData[j - 1] > pData[j]) {

iTemp = pData[j - 1];

pData[j - 1] = pData[j];

pData[j] = iTemp;

}

}

}

for (i = 0; i < Count; i++) {

cout << pData[i] << " ";

}

cout << endl;

delete[] pData;

}

void SelectSort() {

int iTemp;

int* pData, Count;

cin >> Count;

pData = new int[Count];

for (int i = 0; i < Count; i++) {

cin >> pData[i];

}

for (int i = 0; i < Count; i++) {

int minIndex = i;

for (int j = i + 1; j < Count; j++) {

if (pData[j] < pData[minIndex]) {

minIndex = j;

}

}

iTemp = pData[i];

pData[i] = pData[minIndex];

pData[minIndex] = iTemp;

}

for (i = 0; i < Count; i++) {

cout << pData[i] << " ";

}

cout << endl;

delete[] pData;

}

int main() {

int choice;

cout << "请选择排序方法:" << endl;

cout << "1. 冒泡排序法" << endl;

cout << "2. 选择排序法" << endl;

cout << "3. 快速排序法" << endl;

cout << "4. 退出" << endl;

cin >> choice;

switch (choice) {

case 1:

BubbleSort();

break;

case 2:

SelectSort();

break;

case 3:

// 快速排序法的实现

break;

case 4:

return 0;

default:

cout << "无效选择,请重新输入。" << endl;

}

return 0;

}

```

计算月份天数

```cpp

include

using namespace std;

int main() {

int year, month, days;

printf("请输入年份: ");

scanf("%d", &year);

printf("请输入月份: ");

scanf("%d", &month);

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

days = 31;

break;

case 4: case 6: case 9: case 11:

days = 30;

break;

case 2:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

days = 29;

} else {

days = 28;

}

break;

default:

printf("输入有误,请重新输入!");

return 0;

}

printf("%d 年%d 月共有 %d 天。\n", year, month, days);

return 0;

}

```

九九乘法表