解答私信@被c++折磨头秃的花季美少女 //C++ 利用指针数组输入10个单词,编写函数对10个单词进行排序并输出,要求判断是否有相同的单词,如果有相同的单词在输出时该单词只输出一次。
2023-09-14 09:06:59 时间
利用指针数组输入10个单词,编写函数对10个单词进行排序并输出,要求判断是否有相同的单词,如果有相同的单词在输出时该单词只输出一次。
注意:此代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
#include <iostream>
using namespace std;
void input(char **word, int n);
void output(char **word, int n);
void sort(char **word, int n);
int main()
{
char *word[10];
input(word, 10);
sort(word, 10);
output(word, 10);
system("pause");
return 0;
}
void input(char **word, int n){
cout<<"Enter "<<n<<" words: ";
for(int i=0; i<n; i++){
word[i]=new char[20];
cin>>word[i];
}
}
void sort(char **word, int n){
char **p, **q, *temp;
for(p=word; p<word+n; p++){
for(q=p+1; q<word+n; q++){
if(**p==**q)
*q="\0";
else if(**p>**q){
temp=*p;
*p=*q;
*q=temp;
}
}
}
}
void output(char **word, int n){
int i;
cout<<"Output: ";
for(i=0; i<n; i++){
if(word[i]=="\0")
continue;
cout<<word[i]<<" ";
free(word[i]);
}
cout<<endl;
}
相关文章
- java的输入和输出语句_c++输入输出语句
- C++系列笔记(五)
- 深入理解C++11_c++ string char
- C++构造函数的作用_c++什么是构造函数
- C++11:Random
- 【c++】【基础】【primer_plus】【第九章】内存模型与作用域
- C++stl库_c++库
- C++ 中的基本输入u002F输出
- C++ 新特性学习(一) -- 概述+智能指针(smart_ptr)
- c++的链表-C++链表
- OpenTime做最称心的C++开发时间库
- C/C++ 实现URL路径拆分
- C++ winhttp 实现文件下载器
- C屁屁(c++)万字入门
- c++基础篇之C++ 模板
- 图的存储结构的实现(C/C++实现)详解编程语言
- c++引用和运算符重载思考详解编程语言
- C++ showpoint操作符(详解版)
- 如何选出最适合的C++ STL容器?
- C++非递归遍历磁盘文件和递归遍历磁盘文件的程序示例
- c++显式类型转换示例详解
- C++实现修改函数代码HOOK的封装方法
- C++链表倒序实现方法