编程实现从键盘输入5个英文单词(每个单词不超过20个字母),然后将它们按照字典序(即字母从小到大的顺序)排序后打印输出。要求:编写assign函数、sort函数、show函数,分别用于实现单词的键盘输入、单词的排序、单词的输出。目前,已完成main函数的编写,请编写assign、sort和show函数,对这三个函数的定义及要求如下所示。
时间: 2024-01-22 10:18:18 浏览: 37
函数定义及要求:
1. assign函数
```c++
void assign(string words[]);
```
实现从键盘输入5个英文单词,并将它们存储在words数组中。
2. sort函数
```c++
void sort(string words[]);
```
实现对words数组中的单词进行字典序排序,排序后的单词存储在原数组中。
3. show函数
```c++
void show(string words[]);
```
实现将排序后的words数组中的单词按照顺序输出到屏幕上。
完整程序如下:
相关问题
编程实现从键盘输入5个英文单词(每个单词不超过20个字母),然后将它们按照字典序(即字母从小到大的顺序)排序后打印输出。要求:编写assign函数、sort函数、show函数,分别用于实现单词的键盘输入、单词的排序、单词的输出。目前,已完成main函数的编写,请编写assign、sort和show函数
下面是一个可能的实现:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void assign(string words[]) {
for (int i = 0; i < 5; i++) {
cin >> words[i];
}
}
void sort(string words[]) {
std::sort(words, words + 5);
}
void show(string words[]) {
for (int i = 0; i < 5; i++) {
cout << words[i] << endl;
}
}
int main() {
string words[5];
assign(words);
sort(words);
show(words);
return 0;
}
```
在这个实现中,assign函数使用循环从键盘输入5个单词,保存在一个字符串数组中。sort函数使用了STL库中的sort函数对字符串数组进行排序。show函数使用循环打印排好序的字符串数组中的每个单词。最后,在main函数中调用这三个函数,完成整个程序的执行。
编写一个python程序,从键盘输入一个英文句子,按照字母序输出每个单词及其重复次数,英文句子中只用空格分割单词,不包含其他标点符号
好的,这是一个比较常见的编程问题,可以使用Python中的字典来解决。以下是实现代码:
```
sentence = input("请输入一个英文句子:")
words = sentence.split() # 按照空格分割单词
word_dict = {} # 用于记录每个单词出现的次数的字典
# 遍历每个单词,并记录出现次数
for word in words:
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
# 将字典按照单词排序,并输出每个单词及其出现次数
for word, count in sorted(word_dict.items()):
print(word, count)
```
如果您还有其它问题,请随时问我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)