问题描述
我需要将整数转换为字符串。整数是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,1,4,6,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,2020);
int day3 = dayofweek(04,2020);
int day4 = dayofweek(05,2020);
int day5 = dayofweek(11,2020);
int day6 = dayofweek(12,2020);
int day7 = dayofweek(13,2020);
int day8 = dayofweek(20,2020);
int day9 = dayofweek(21,2020);
int day10 = dayofweek(27,2020);
int day11 = dayofweek(02,03,2020); //month 2 days
int day12 = dayofweek(02,2020);
int day13 = dayofweek(05,2020);
int day14 = dayofweek(10,2020);
int day15 = dayofweek(11,2020);
int day16 = dayofweek(11,2020);
int day17 = dayofweek(17,2020);
int day18 = dayofweek(19,2020);
int day19 = dayofweek(24,2020);
int day20 = dayofweek(25,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],但即使在代码中,它也会出现未声明标识符的错误。
数组month1
是most_occurred_number1
的参数,也是main
中的变量,这是唯一可见的地方。