LINL631 2021-04-11 15:15 采纳率: 75%
浏览 31
已采纳

合并两个字符串,排序后输出,但排序功能未实现

#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);
}

  • 写回答

2条回答 默认 最新

  • cpp_learners 2021-04-11 21:18
    关注
    #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);
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘