我正在学习 C 编程并正在完成学校作业。由于向老师或同学询问的可能性有限(由于新冠病毒爆发和在家中保持距离),我正在与您联系。非常感谢您的帮助。
我的挑战:编写一个程序,使用不同的函数对 11 个文本字符串按照长度和字母顺序进行排序。告诉用户该程序的限制是什么。
Desired output, for instance:
bc
bcb
bcebc
cbbcac
dbccac
(...)
Run Code Online (Sandbox Code Playgroud)
首先,用户必须输入 11 个文本字符串。我已经用以下代码解决了这个问题:
#include <stdio.h>
#include <string.h>
#define NUMBER_OF_STRINGS 11
int main (void)
char textstrings[NUMBER_OF_STRINGS][15];
int i;
(...)
printf("Enter 11 textstrings, one by one:\n");
for(i=0;i<NUMBER_OF_STRINGS-1;i++)
{
printf("Enter string %d: ", i + 1);
gets(textstrings[i]);
}
Run Code Online (Sandbox Code Playgroud)
现在我们在“textstrings[i]”中得到了字符串,必须对它们进行排序并打印。我认为最好看的方法是为每个排序条件提供单独的函数。例如,一个单独的函数用于“长度”,然后另一个函数用于“字母顺序”。我知道我应该使用“strlen”来比较长度,使用“strcmp”来比较字母顺序。
我还了解按多个条件排序的策略是为每个返回比较函数的适当返回类型的条件编写比较表达式,并按所需的排序顺序(例如,首先是长度,然后是字母顺序)对它们进行评估。
这就是我迷失的地方......
但是,我确实理解按长度排序的逻辑可能如下所示:
char* temp;
int step
int length=20; //Not allowing text longer than 20 characters.
for(step=0;step<length-1;step++)
for(i=0;i<length-step-1;i++)
{
if(strlen(textstrings[i])>strlen(textstrings[i+1]))
{
temp=textstrings[i];
textstrings[i]=textstrings[i+1];
textstrings[i+1]=temp;
}
Run Code Online (Sandbox Code Playgroud)
我还了解到,按照字母顺序排序可能看起来像这样:
int k, …Run Code Online (Sandbox Code Playgroud) 我需要显示我从解析器中获取的名称列表.我正在获取列表的NSMutable列表,然后我需要按字母顺序显示它们.
我尝试做的是:
NSArray *myArtistArray=[[NSArray alloc]init];
myArtistArray=[artistsList sortUsingSelector:@selector(compare:) ];
// error void value not ignored as it outght to be
[myArtistArray sortedArrayUsingSelector:@selector(compare:)];
Run Code Online (Sandbox Code Playgroud) 好的,所以我需要的是相当不言自明的.
使用相同的方式.sort,为了按字母顺序/按字典顺序排序基于拉丁语的字符串数组,我正在寻找一种方法来排序非拉丁文UTF-8字符串.
特别:
通过"排序",我的意思与通常在字典中找到它们的方式相同.(我知道对于中国人/日本人来说这可能会更棘手,所以让我们先坚持其余部分)
有任何想法吗?
PS我对音译不感兴趣(这就是我目前正在做的事情),因为结果与"正确"相差甚远 - 按词汇表来说......
注意:它与RoR无关.只是纯粹的Ruby.
我有 2 个变量,想要检查其按字母顺序排序。
这是我的代码
cy.get('.list-item').eq(1)
.find('.activity-name span')
.invoke('text')
.then(text => {
const first = text;
cy.get('.activity').click();
cy.get('.list-item').eq(1)
.find('.activity-name span')
.invoke('text')
.then(text => {
const second = text;
// Here I want to check if the result of first element is equal second-variable
});
});
Run Code Online (Sandbox Code Playgroud)
请帮助我。我怎样才能用柏树做到这一点
我有一个文件名列表,每个文件名都以一个前导号码开头:
10_file
11_file
1_file
20_file
21_file
2_file ...
Run Code Online (Sandbox Code Playgroud)
我需要按顺序排列:
1_file
10_file
11_file
2_file
21_file
22_file ...
Run Code Online (Sandbox Code Playgroud)
如果它们只是数字作为字符串('1')而没有下划线,我可以使用sorted()对它们进行排序.我已尝试使用不同的键属性的不同排序方法,也模块"natsort",但没有结果.我必须为此编写自己的算法吗?也许我可以提取前导数字并用它们进行排序?
更新所需列表以更正列表
我一直在尝试按字母顺序对姓名列表进行排序,例如:
list=['Bob','Alice','Charlie']
print(list.index('Alice'))
1
Run Code Online (Sandbox Code Playgroud)
但是我也想跟踪原始索引,所以这行不通:
list.sort()
print(list)
['Alice','Bob','Charlie']
print(list.index('Alice'))
0
Run Code Online (Sandbox Code Playgroud)
排序后索引改变;有没有办法跟踪原始索引?我检查了其他类似的问题,numpy 有一个解决方案,但对 str 变量没有用。
我正在尝试使用C#编写一个程序,该程序对包含familyname的文本文件进行排序,后跟逗号,后跟名字.我如何根据姓氏按字母顺序获取文本文件.
sorting ×7
python ×2
alphabetical ×1
arrays ×1
c ×1
c# ×1
cypress ×1
dictionary ×1
indexing ×1
objective-c ×1
ruby ×1
string ×1
testing ×1
text ×1