获取数字序列的功能
Function for getting the ordinal of a number?
i即将编写一个C 函数来执行以下操作:
1 ---> "1st"
2 ---> "1nd"
3 ---> "3rd"
...
17657 --> "17657th"
...
即。生成该数字的序数扩展字符串(它不必执行数字本身的itoa()
)。但是后来我以为"标准库中的某些东西或提升已经做到了吗?"
注意:
- 我知道写这篇文章并不难,这里有一个实现,因此我只是不想复制代码。
- 显然,我需要英语。多语言版本对政治校正的考虑是很好的,而不仅仅是……
这是我最终写的:
const char* ordinal_suffix(int n)
{
static const char suffixes [][3] = {"th", "st", "nd", "rd"};
auto ord = n % 100;
if (ord / 10 == 1) { ord = 0; }
ord = ord % 10;
if (ord > 3) { ord = 0; }
return suffixes[ord];
}
代码高尔夫解决方案很可爱,但是 - 它们确实为简洁而不是其他任何东西都进行了优化。这是更快的(尽管可以通过将后缀放在功能主体中并使代码不限于代码来使其更快,但比这里大多数其他答案更清晰,更简短。
// Returns numbers with ordinal suffix as string
// Based on https://stackoverflow.com/questions/3109978/display-numbers-with-ordinal-suffix-in-php
std::string NumberToOrdinal(size_t number) {
std::string suffix = "th";
if (number % 100 < 11 || number % 100 > 13) {
switch (number % 10) {
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
}
}
return std::to_string(number) + suffix;
}
我敢肯定,您可以在PHP中使用Ordinal后缀以显示四行解决方案。不幸的是,我认为在常见的C lib中没有这样的事情。
尝试这个...
#include <iostream>
using namespace std;
void suffix(int n, char suff[]);
// creates the ordinal suffix
// for a given number
int main()
{
char s[5];
int x;
cout << "Enter a number to find the ordinal suffix for ";
cin >> x;
suffix(52111,s);
}
void suffix(int n, char suff[])
{
if(n%100 == 11 || n%100 == 12 || n%100 == 13)
{
cout << "suffix is: " << n << "th";
cout << endl;
}
else
{
if(n%10 == 1)
{
cout << "Suffix is: " << n << "st";
cout << endl;
}
else
{
if(n%10 == 2)
{
cout << "Suffix is: " << n << "nd";
cout << endl;
}
else
{
if(n%10 == 3)
{
cout << "Suffix is: " << n << "rd";
cout << endl;
}
else
{
if(n%10 == 4 || n%10 == 5 || n%10 == 6 || n%10 == 7 || n%10 == 8 || n%10 == 9 || n%10 == 0)
{
cout << "Suffix is: " << n << "th";
cout << endl;
}
}
}
}
}
}
我使用以下字符串函数来完成它。
#include <string>
#include <iostream>
using namespace std;
string ordinal(int i)
{
if(i==1)
{
return "First";
}
if(i==2)
{
return "Second";
}
if(i==3)
{
return "Third";
}
if(i==4)
{
return "Fourth";
}
if(i==5)
{
return "Fifth";
}
if(i==6)
{
return "Sixth";
}
if(i==7)
{
return "Seventh";
}
if(i==8)
{
return "Eighth";
}
}
int main()
{
for(int i=0; i<8; i++)
{
cout << ordinal(i+1) << " number: ";
}
return 0;
}
#include <iostream>
#include <string>
std::string number_to_ordinal(int number)
{
// Convert number to string
std::string ordinal = std::to_string(number);
// Get the last character of the number to later determine ordinal indicator
char last_char = ordinal.back();
// Get the last two characters of the number to deal with ordinal indicator conditions
std::string last_two_char;
if(ordinal.size() > 1)
last_two_char = ordinal.substr(ordinal.size() - 2, ordinal.size());
// Determine ordinal indicator. Each number with a last character ending in '1', '2',
// and '3' require ordinal indicators of 'st', 'nd', and 'rd', respectively. However,
// numbers with the last two characters ending in '11', '12', and '13' require 'th'
// as the ordinal indicator.
if(last_two_char != "11" && last_char == '1')
ordinal += "st";
else if (last_two_char != "12" && last_char == '2')
ordinal += "nd";
else if (last_two_char != "13" && last_char == '3')
ordinal +="rd";
else
ordinal += "th"; // All other numbers require 'th' as the ordinal indicator
return ordinal;
}
///////////////////////////////////////////////////////////////////////
// Main Program
///////////////////////////////////////////////////////////////////////
int main()
{
// Test number to ordinal
for(int i = 17657; i < 17725; i++)
std::cout << number_to_ordinal(i) << std::endl;
return 0;
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 是否有与 C# Structs/StructLayout 等效的功能,C++中的字段偏移量?
- 基于不同字段的对象向量的排序功能
- 如何使用 recv 或 C++ 中的读取功能从 tcp 套接字读取大请求
- 派生类中的成员字段别名(无访问器功能)
- 套接字连接在主功能内部但不在功能内部
- C 模板功能在任何集合成员字段上迭代
- 如何设置超时以在C套接字上"connect()"功能?
- std :: async在成员字段的成员功能上
- 我不能在不使用指针的情况下返回 2d 数组!我正在制作一个井字游戏类型游戏,这是我的向上移动功能
- 套接字的pty功能
- 好友功能 - 无法访问私人字段C++
- 我的井字游戏的吸气机和二传手功能不起作用
- 井字游戏 BIG 板使用功能和 2D 阵列
- boost套接字读写功能是否线程安全
- 在编译时根据字节序定义位字段
- 我如何重播我的主要功能?(读描述,难字标题)
- 在非阻塞套接字中选择功能
- offsetof和sizeof类似的位字段功能