C++ 使用枚举和结构?
我有一个关于在作业中使用C++ 使用枚举和结构?,c++,struct,enums,typedef,C++,Struct,Enums,Typedef,我有一个关于在作业中使用enum和struct的问题,我理解起来有点困难 首先,这是老师给我们的代码示例 enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; struct WeatherType { int avgHiTemp; int avgLoTemp; float actualRain; float recordRain; }; 然后,作业要求声明
enum
和struct
的问题,我理解起来有点困难
首先,这是老师给我们的代码示例
enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
struct WeatherType
{
int avgHiTemp;
int avgLoTemp;
float actualRain;
float recordRain;
};
然后,作业要求声明一个一维数组类型,WeatherListType
,由WeatherType
组件组成,由类型MonthType
的值索引。当我试图通过编码来实现这一点时:
typedef WeatherType WeatherListType[MonthType];
我犯了以下错误:
problem5.cpp:19:47: error: expected primary-expression before ']' token
typedef WeatherType WeatherListType[MonthType];
^
problem5.cpp:21:2: error: 'WeatherListType' was not declared in this scope
WeatherListType yearlyWeather;
我只是搞不清楚这个问题在问什么,如果我在正确的轨道上,我到底做错了什么 这里的问题是
typedef WeatherType WeatherListType[MonthType];
括号中的东西必须是一个数字(元素的数量),但是MonthType
不是一个数字,而是一个类型
这里有两个主要选项:
enum MonthType{…,NOV,DEC,NUM_MONTHS}代码>
然后使用NUM_月
这是枚举的一个常见技巧-因为它们从0开始并向上计数,NUM_MONTHS
将是第13个值,因此它得到值12
这样做的原因是,如果您添加另一个值,编译器将自动为您调整NUM_MONTHS
。我认为我们不可能很快迎来新的几个月,所以在这种情况下,这并不重要
(旁注:数组在C中总是由整数索引的。你不能用一个枚举来索引一个数组。但是枚举可以转换为整数并返回,所以这不是一个问题)这里的问题是
typedef WeatherType WeatherListType[MonthType];
括号中的东西必须是一个数字(元素的数量),但是MonthType
不是一个数字,而是一个类型
这里有两个主要选项:
enum MonthType{…,NOV,DEC,NUM_MONTHS}代码>
然后使用NUM_月
这是枚举的一个常见技巧-因为它们从0开始并向上计数,NUM_MONTHS
将是第13个值,因此它得到值12
这样做的原因是,如果您添加另一个值,编译器将自动为您调整NUM_MONTHS
。我认为我们不可能很快迎来新的几个月,所以在这种情况下,这并不重要
(旁注:数组在C中总是由整数索引的。你不能用枚举索引一个数组。但是枚举可以转换为整数并返回,所以这不是问题)我在这里给出了完整程序供您澄清,因为我没有提到我在输出中给出的输入值,因为在12个月内,每个月4个数据总共需要给出48个数据。假设值是给定的。但在运行时,必须给出所有48个值。在使用JAN时,它是常数0;在二月时,它是常数1;在DEC时,它是常数11。希望您理解,这将对您有用
#include<iostream>
using namespace std;
enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
struct WeatherType
{
int avgHiTemp;
int avgLoTemp;
float actualRain;
float recordRain;
};
int main()
{
WeatherType WeatherListType[12];
for (int i=JAN;i<=DEC;i++)
{
cout<<"\n High temperature :";
cin>>WeatherListType[i].avgHiTemp;
cout<<"\n Low temperature :";
cin>>WeatherListType[i].avgLoTemp;
cout<<"\n Actual Rain :";
cin>>WeatherListType[i].actualRain;
cout<<"\n Record Rain :";
cin>>WeatherListType[i].recordRain;
}
cout<<"\n Month \t HiTemp\t LoTemp\t ActualRain\t RecordRain\n";
for (int i=JAN;i<=DEC;i++)
{
cout<<i<<"\t"<<WeatherListType[i].avgHiTemp;
cout<<"\t"<<WeatherListType[i].avgLoTemp;
cout<<"\t"<<WeatherListType[i].actualRain;
cout<<"\t"<<WeatherListType[i].recordRain<<"\n";
}
}
OUTPUT
High temperature :40
Low temperature :28
Actual Rain :34
Record Rain :56
.
.
.
Month HiTemp LoTemp ActualRain RecordRain
0 40 28 34 56
1 45 32 45 67
2 34 23 56 76
3 34 32 45 43
4 67 12 45 43
5 78 65 45 32
6 56 54 34 23
7 45 34 23 23
8 3 1 45 43
9 34 45 23 23
10 12 3 34 43
11 56 54 43 34
#包括
使用名称空间std;
枚举月类型{1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月、12月};
结构天气类型
{
int AVGHP;
int AVGLOTEM;
浮力;
浮雨;
};
int main()
{
WeatherType WeatherListType[12];
对于(int i=JAN;i这里我给出了完整程序供您澄清,因为我没有提到我在输出中给出的输入值,因为每个月12个月共有4个数据,共有48个数据必须给出。假设给出了这些值。但在运行时,您必须给出所有48个值。在使用JAN时,它是conatant 0,在2月是常数1,12月是常数11。希望你理解,这将对你有用
#include<iostream>
using namespace std;
enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
struct WeatherType
{
int avgHiTemp;
int avgLoTemp;
float actualRain;
float recordRain;
};
int main()
{
WeatherType WeatherListType[12];
for (int i=JAN;i<=DEC;i++)
{
cout<<"\n High temperature :";
cin>>WeatherListType[i].avgHiTemp;
cout<<"\n Low temperature :";
cin>>WeatherListType[i].avgLoTemp;
cout<<"\n Actual Rain :";
cin>>WeatherListType[i].actualRain;
cout<<"\n Record Rain :";
cin>>WeatherListType[i].recordRain;
}
cout<<"\n Month \t HiTemp\t LoTemp\t ActualRain\t RecordRain\n";
for (int i=JAN;i<=DEC;i++)
{
cout<<i<<"\t"<<WeatherListType[i].avgHiTemp;
cout<<"\t"<<WeatherListType[i].avgLoTemp;
cout<<"\t"<<WeatherListType[i].actualRain;
cout<<"\t"<<WeatherListType[i].recordRain<<"\n";
}
}
OUTPUT
High temperature :40
Low temperature :28
Actual Rain :34
Record Rain :56
.
.
.
Month HiTemp LoTemp ActualRain RecordRain
0 40 28 34 56
1 45 32 45 67
2 34 23 56 76
3 34 32 45 43
4 67 12 45 43
5 78 65 45 32
6 56 54 34 23
7 45 34 23 23
8 3 1 45 43
9 34 45 23 23
10 12 3 34 43
11 56 54 43 34
#包括
使用名称空间std;
枚举月类型{1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月、12月};
结构天气类型
{
int AVGHP;
int AVGLOTEM;
浮力;
浮雨;
};
int main()
{
WeatherType WeatherListType[12];
例如:我认为我们不可能很快迎来新的几个月。哈……谁知道呢:)我认为我们不可能很快迎来新的几个月。哈……谁知道呢:)