c++ - 使用 switch 将整数更改为字符串
问题描述
我需要将整数转换为字符串。该整数是 most_occurred_number ,我无法确定将哪个整数插入代码。我已将 most_occurred_number 作为整数,但 cout 中没有任何变化。我尝试放置month1 [i],但即使它在代码中,它也会出现未声明标识符的错误。
#include <string>
#include <algorithm>
#include <map>
#include <array>
#include <iomanip>
using namespace std;
int dayofweek(int d, int m, int y)
{
static int t[] = { 0, 3, 2, 5, 0, 3,
5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y / 4 - y / 100 +
y / 400 + t[m - 1] + d) % 7;
}
std::string to_date_string1(int most_occurred_number1)
{
switch(most_occurred_number1)
{
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
}
}
std::string to_date_string2(int most_occurred_number2)
{
switch(most_occurred_number2)
{
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
}
}
void most_occurred_number1(int month1[], int size1)
{
int max_count1 = 0;
cout << "\nMost occurred number: ";
for (int i=0; i<size1; i++)
{
int count1=1;
for (int j=i+1;j<size1;j++)
if (month1[i]==month1[j])
count1++;
if (count1>max_count1)
max_count1 = count1;
}
for (int i=0;i<size1;i++)
{
int count1=1;
for (int j=i+1;j<size1;j++)
if (month1[i]==month1[j])
count1++;
if (count1==max_count1)
cout << month1[i] << endl;
}
}
void most_occurred_number2(int month2[], int size2)
{
int max_count2 = 0;
cout << "\nMost occurred number: ";
for (int i=0; i<size2; i++)
{
int count2=1;
for (int j=i+1;j<size2;j++)
if (month2[i]==month2[j])
count2++;
if (count2>max_count2)
max_count2 = count2;
}
for (int i=0;i<size2;i++)
{
int count2=1;
for (int j=i+1;j<size2;j++)
if (month2[i]==month2[j])
count2++;
if (count2==max_count2)
cout << month2[i] << endl;
}
}
int main()
{
int day1 = dayofweek(03, 02, 2020); //month 1 days
int day2 = dayofweek(04, 02, 2020);
int day3 = dayofweek(04, 02, 2020);
int day4 = dayofweek(05, 02, 2020);
int day5 = dayofweek(11, 02, 2020);
int day6 = dayofweek(12, 02, 2020);
int day7 = dayofweek(13, 02, 2020);
int day8 = dayofweek(20, 02, 2020);
int day9 = dayofweek(21, 02, 2020);
int day10 = dayofweek(27, 02, 2020);
int day11 = dayofweek(02, 03, 2020); //month 2 days
int day12 = dayofweek(02, 03, 2020);
int day13 = dayofweek(05, 03, 2020);
int day14 = dayofweek(10, 03, 2020);
int day15 = dayofweek(11, 03, 2020);
int day16 = dayofweek(11, 03, 2020);
int day17 = dayofweek(17, 03, 2020);
int day18 = dayofweek(19, 03, 2020);
int day19 = dayofweek(24, 03, 2020);
int day20 = dayofweek(25, 03, 2020);
int month1 [] = {day1, day2, day3, day4, day5, day6, day7, day8, day9, day10};
int n1 = sizeof(month1)/sizeof(month1[0]);
cout << "Original array: ";
for (int i=0; i < n1; i++)
cout << month1[i] <<" ";
most_occurred_number1(month1, n1);
int month2 [] = {day11, day12, day13, day14, day15, day16, day17, day18, day19, day20};
int n2 = sizeof(month2)/sizeof(month2[0]);
cout << "Original array: ";
for (int i=0; i < n2; i++)
cout << month2[i] <<" ";
most_occurred_number2(month2, n2);
}```
解决方案
我需要将整数转换为字符串。
然后使用std::to_string
https://en.cppreference.com/w/cpp/string/basic_string/to_string
整数是 most_occurred_number
int
这段代码中没有这种类型的变量。我可以看到most_occurred_number1
和most_occurred_number2
,它们是函数参数——它们似乎(几乎)在switch
语句中得到了正确处理。几乎 - 因为你to_date_string1
并不to_date_string2
总是返回一个值 - 这是 c++ 中的未定义行为 - 你可以添加return "?"
或抛出异常,以防函数没有在适当范围内调用值。
我已将 most_occurred_number 作为整数,但 cout 中没有任何变化
您应该在代码中显示您尝试放置的位置most_occurred_number
我尝试放置month1 [i],但即使它在代码中,它也会出现未声明标识符的错误。
Arraymonth1
是 的一个参数,most_occurred_number1
也是 的一个变量main
,这些是它唯一可见的地方。
推荐阅读
- android - 如何使用 Android 将文本设置为 Spinner
- unity3d - Unity & Leap Motion - 如何获取实时手部数据?
- c - 如何在控制台应用程序中设置 WM_KEYDOWN 消息拦截器?
- django - 如何在 Django ORM 中分组和聚合条件
- html - 使用 Flex 尝试根据图像获取框
- javascript - 如何在服务器端 Javascript (OrientDB Studio) 中获取 OConcurrentModificationException
- reporting-services - SSRS 多值参数 - 空白值重置参数
- gradle - Gradle Wrapper - 如何为本地和 CI 构建使用不同的 distributionUrls?
- html - 如何正确旋转表格单元格内的文本
- c++ - 区分jsoncpp中的int和uint类型