4.试完成如下几个程序设计步骤: (1)编写一个子两数实现月份输出,其输出效果如下: 33 C:\Windows\system32\cmd.exe 锋出月份? January lebruary March April May June July August September Detober November 清校崔意健牲缘 (2)再编写一个子两数实现月份按宇符串顺序排列,并将排序结果进行输出, 其程序结构如下: #include <iostream> using namespace std; void DispMonths (char p[J [30]) //打印12个月份 void SortStrings (char p[] [30], int n) int main () { 1/months的定义及用字符串初始化的过程 char months [12] [30]=("January", "February", "*) cout<<“排序前:”<<endl; DispMonths (months); SortStrings (months, 12); cout<Cendl<<〞排序后:“<<endl: DispMonths (months); return 0; 其效果图为
时间: 2024-02-01 08:02:36 浏览: 130
(1) 子函数实现月份输出:
```cpp
#include <iostream>
using namespace std;
void DispMonths() {
char months[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for(int i = 0; i < 12; i++) {
cout << months[i] << " ";
}
}
int main() {
DispMonths();
return 0;
}
```
输出结果:
```
January February March April May June July August September October November December
```
(2) 子函数实现月份按字母顺序排列并输出:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
void DispMonths(char p[][30], int n) {
for(int i = 0; i < n; i++) {
cout << p[i] << " ";
}
cout << endl;
}
void SortStrings(char p[][30], int n) {
for(int i = 0; i < n - 1; i++) {
for(int j = i + 1; j < n; j++) {
if(strcmp(p[i], p[j]) > 0) {
char temp[30];
strcpy(temp, p[i]);
strcpy(p[i], p[j]);
strcpy(p[j], temp);
}
}
}
}
int main() {
char months[12][30] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
cout << "排序前:" << endl;
DispMonths(months, 12);
SortStrings(months, 12);
cout << "排序后:" << endl;
DispMonths(months, 12);
return 0;
}
```
输出结果:
```
排序前:
January February March April May June July August September October November December
排序后:
April August December February January July June March May November October September
```
阅读全文