c++ 获取数字序数的函数?

l5tcr1uw  于 2023-02-10  发布在  其他
关注(0)|答案(6)|浏览(176)

我准备写一个C++函数来完成以下任务:
也就是说,为那个数字生成有序扩展字符串(它不必对数字本身进行itoa()操作),但我想"标准库或boost中肯定已经有东西做了这件事吧?"

    • 注:**
  • 我知道写这个并不难,这里有一个implementation in Python,所以,我只是不想重复代码。
  • 我需要这个在英语,显然。一个多语言版本将是很好的政治正确性的考虑,而不是更多...
esyap4oy

esyap4oy1#

以下是我最后写的:

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];
}

代码高尔夫解决方案很可爱,但是--他们确实优化了简洁性,而不是其他的东西。这比这里的大多数其他答案更快(尽管通过将.cpp中的后缀从函数体中取出并使代码可内联可以使其更快),更清晰,更简洁。

jchrr9hc

jchrr9hc2#

// 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;
}
j8ag8udp

j8ag8udp3#

我非常肯定你可以在Display numbers with ordinal suffix in PHP中修改四行的解决方案,不幸的是,我不认为在普通的C++库中有这样的东西。

zqdjd7g9

zqdjd7g94#

试试这个...

#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;
              }
        }
        }
    }
    }
}
5gfr0r5j

5gfr0r5j5#

我使用下面的字符串函数来完成它。

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

x33g5p2x6#

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

相关问题