vc6.03、输入三个字符串,按照由大到小的顺序输出,要求用指针方法处理。

时间: 2023-12-10 10:39:51 浏览: 33
以下是用指针方法处理的示例代码: ```c++ #include <iostream> #include <cstring> using namespace std; void sortStrings(char** strings) { char* temp; for (int i = 0; i < 2; i++) { for (int j = i + 1; j < 3; j++) { if (strcmp(strings[i], strings[j]) < 0) { temp = strings[i]; strings[i] = strings[j]; strings[j] = temp; } } } } int main() { char* strings[3]; char buffer[100]; for (int i = 0; i < 3; i++) { cout << "Enter string " << i + 1 << ": "; cin.getline(buffer, 100); strings[i] = new char[strlen(buffer) + 1]; strcpy(strings[i], buffer); } sortStrings(strings); cout << "Sorted strings:" << endl; for (int i = 0; i < 3; i++) { cout << strings[i] << endl; delete[] strings[i]; } return 0; } ``` 在这个示例中,我们使用了一个字符串指针数组 `char** strings` 来存储输入的三个字符串。然后我们使用 `cin.getline()` 函数从输入流中读取每个字符串,并使用 `new` 运算符为每个字符串分配内存空间。 接着,我们使用 `sortStrings()` 函数对字符串进行排序。这个函数使用两层循环来遍历所有字符串,并使用 `strcmp()` 函数比较它们的大小。如果前面的字符串比后面的字符串小,就交换它们的指针。 最后,我们遍历排序后的字符串指针数组,并依次输出每个字符串。由于我们使用了 `new` 运算符为每个字符串分配了内存空间,所以在程序结束时需要使用 `delete[]` 运算符释放这些内存空间。

相关推荐

最新推荐

recommend-type

DB Query Analyzer specification_Version6.03

From 6.01, DB Query Analyzer provides SQL Execute Schedule function to ... Without changing any Windows OS settings or configurations, DB Query Analyzer 6.03 can run on any Microsoft Windows OS directly.
recommend-type

DB Query Analyzer 6.03 on Microsoft Windows 8

The latest version of DB Query Analyzer is 6.03, which resolves the BUG which is caused by the default setting of short date separator character ‘/’ in Windows OS such as Windows 7, Windows 8 and ...
recommend-type

How to easily create DB2 user function by DB Query Analyzer 6.03

This essay gives a demonstration of creating DB2 user function easily by DB Query Analyzer. And also the creating process of stored procedure has no difference with that of function What’s more, the ...
recommend-type

DB Query Analyzer 6.03, the most excellent UDA tools on any Microsoft Windows OS

Without changing any Windows OS settings or configurations, DB Query Analyzer 6.03 can run on any Microsoft Windows OS directly. DB Query Analyzer is one of the few excellent Client Tools in the ...
recommend-type

Windows 8.1 环境下 Canopus Edius 6.03-6.08 安装实验图文教程

在 Windows 8.1 环境下 Canopus Edius 6.03-6.08 安装图文详细教程,让你在新系统中照旧使用Edius软件,已经经过测试
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。