#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 255
typedef int Status;
typedef struct
{
char* ch;
int length;
}Str;
//初始化
Status Initstr(Str* str)
{
str->ch = NULL;
str->length = 0;
return 1;
}
//赋值
Status Strassign(Str& str, char* ch)
{
int i = 0, len = 0;
char* c = ch;
if (str.ch)
free(str.ch);
while (*c)
{
++len;
++c;
}
if (len == 0)
{
str.ch = NULL;
str.length = 0;
}
else
{
str.ch = (char*)malloc(sizeof(char) * (len + 1));
if (str.ch == NULL)
return 0;
else
{
c = ch;
for (i; i <= len; i++)
{
str.ch[i] = *c;
++c;
}
str.length = len;
}
}
return 1;
}
//合并
Status Concat(Str& str, Str str1, Str str2)
{
int i = 0, j = 0;
if (str.ch)
{
free(str.ch);
str.ch = NULL;
}
str.ch = (char*)malloc(sizeof(char) * (str1.length + str2.length + 1));
if (str.ch == NULL)
return 0;
while (i < str1.length)
{
str.ch[i] = str1.ch[i];
i++;
}
while (j < str2.length)
{
str.ch[i + j] = str2.ch[j];
++j;
}
str.length = str1.length + str2.length;
return 1;
}
//排序
Status Sort(Str str)
{
int i = 0, j, n = str.length;
j = i + 1;
char temp;
for (i; i < n - 1; i++)
{
if (str.ch[i] > str.ch[j])
{
temp = str.ch[i];
str.ch[i] = str.ch[j];
str.ch[j] = temp;
}
}
return 1;
}
//打印
Status Print(Str str)
{
int i = 0;
for (i; i < str.length; i++)
{
printf("%c", str.ch[i]);
}
return 1;
}
int main()
{
char ch, str1[MAXLEN], str2[MAXLEN];
Str str, s1, s2;
Initstr(&str);
Initstr(&s1);
Initstr(&s2);
//输入值到字符串
gets_s(str1);
Strassign(s1, str1);
gets_s(str2);
Strassign(s2, str2);
Concat(str, s1, s2);
Sort(str);
Print(str);
}
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 255
typedef int Status;
typedef struct {
char* ch;
int length;
}Str;
//初始化
Status Initstr(Str* str) {
str->ch = NULL;
str->length = 0;
return 1;
}
//赋值
Status Strassign(Str& str, char* ch) {
int i = 0, len = 0;
char* c = ch;
if (str.ch)
free(str.ch);
while (*c) {
++len;
++c;
}
if (len == 0) {
str.ch = NULL;
str.length = 0;
} else {
str.ch = (char*)malloc(sizeof(char) * (len + 1));
if (str.ch == NULL)
return 0;
else {
c = ch;
for (i; i <= len; i++) {
str.ch[i] = *c;
++c;
}
str.length = len;
}
}
return 1;
}
//合并
Status Concat(Str& str, Str str1, Str str2) {
int i = 0, j = 0;
if (str.ch) {
free(str.ch);
str.ch = NULL;
}
str.ch = (char*)malloc(sizeof(char) * (str1.length + str2.length + 1));
if (str.ch == NULL)
return 0;
while (i < str1.length) {
str.ch[i] = str1.ch[i];
i++;
}
while (j < str2.length) {
str.ch[i + j] = str2.ch[j];
++j;
}
str.length = str1.length + str2.length;
return 1;
}
//排序
Status Sort(Str str) {
int i = 0, j, n = str.length;
j = i + 1;
char temp;
/*for (i; i < n - 1; i++) {
if (str.ch[i] > str.ch[j]) {
temp = str.ch[i];
str.ch[i] = str.ch[j];
str.ch[j] = temp;
}
}*/
for (i = 0; i < n - 1; i++) {
int index = i;
for (int j = i + 1; j < n; j++) {
if (str.ch[index] > str.ch[j]) {
index = j;
}
}
if (index != i) {
temp = str.ch[i];
str.ch[i] = str.ch[index];
str.ch[index] = temp;
}
}
return 1;
}
//打印
Status Print(Str str) {
int i = 0;
for (i; i < str.length; i++) {
printf("%c", str.ch[i]);
}
return 1;
}
int main() {
char ch, str1[MAXLEN], str2[MAXLEN];
Str str, s1, s2;
Initstr(&str);
Initstr(&s1);
Initstr(&s2);
//输入值到字符串
gets_s(str1);
Strassign(s1, str1);
gets_s(str2);
Strassign(s2, str2);
Concat(str, s1, s2);
Sort(str);
Print(str);
}
你的排序算法有问题,这样写无法起到排序作用!
要想下方这样写:
for (i = 0; i < n - 1; i++) {
int index = i;
for (int j = i + 1; j < n; j++) {
if (str.ch[index] > str.ch[j]) {
index = j;
}
}
if (index != i) {
temp = str.ch[i];
str.ch[i] = str.ch[index];
str.ch[index] = temp;
}
}
另外, 分配内存时,+1会导致输出一些乱码。