C++/Data Type/enum
Содержание
demonstrates enumerations
#include <iostream>
using namespace std;
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;
enum Suit { clubs, diamonds, hearts, spades };
struct card{
int number;
Suit suit;
};
int main()
{
card temp, chosen, prize;
int position;
card card1 = { 7, clubs };
cout << "Card 1 is the seven of clubs\n";
card card2 = { jack, hearts };
cout << "Card 2 is the jack of hearts\n";
card card3 = { ace, spades };
cout << "Card 3 is the ace of spades\n";
prize = card3;
if(chosen.number == prize.number && chosen.suit == prize.suit)
cout << "That"s right! You win!\n";
else
cout << "Sorry. You lose.\n";
return 0;
}
demonstrates enums, counts words in phrase
#include <iostream>
using namespace std;
#include <conio.h>
enum itsaWord { NO, YES };
int main(){
itsaWord isWord = NO;
char ch = "a";
int wordcount = 0;
cout << "Enter a phrase:\n";
do {
ch = getche();
if(ch==" " || ch=="\r"){
if( isWord == YES ){
wordcount++;
isWord = NO;
}
}else if( isWord == NO ){
isWord = YES;
}
} while( ch != "\r" );
cout << wordcount;
return 0;
}
demonstrates enum types
#include <iostream>
using namespace std;
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main() {
days_of_week day1, day2;
day1 = Mon;
day2 = Thu;
int diff = day2 - day1;
cout << "Days between = " << diff << endl;
if(day1 < day2)
cout << "day1 comes before day2\n";
return 0;
}
Using bit fields.
#include <iostream>
#include <string.h>
using namespace std;
enum STATUS { FullTime, PartTime } ;
enum GRADLEVEL { UnderGrad, Grad } ;
enum HOUSING { Dorm, OffCampus };
enum FOODPLAN { OneMeal, AllMeals, WeekEnds, NoMeals };
class student
{
public:
student():
myStatus(FullTime),
myGradLevel(UnderGrad),
myHousing(Dorm),
myFoodPlan(NoMeals)
{}
~student(){}
STATUS GetStatus();
void SetStatus(STATUS);
unsigned GetPlan() { return myFoodPlan; }
private:
unsigned myStatus : 1;
unsigned myGradLevel: 1;
unsigned myHousing : 1;
unsigned myFoodPlan : 2;
};
STATUS student::GetStatus()
{
if (myStatus)
return FullTime;
else
return PartTime;
}
void student::SetStatus(STATUS theStatus)
{
myStatus = theStatus;
}
int main()
{
student Jim;
if (Jim.GetStatus()== PartTime)
cout << "Jim is part time" << endl;
else
cout << "Jim is full time" << endl;
Jim.SetStatus(PartTime);
if (Jim.GetStatus())
cout << "Jim is part time" << endl;
else
cout << "Jim is full time" << endl;
cout << "Jim is on the " ;
char Plan[80];
switch (Jim.GetPlan())
{
case OneMeal: strcpy(Plan,"One meal"); break;
case AllMeals: strcpy(Plan,"All meals"); break;
case WeekEnds: strcpy(Plan,"Weekend meals"); break;
case NoMeals: strcpy(Plan,"No Meals");break;
default : cout << "Something bad went wrong!\n"; break;
}
cout << Plan << " food plan." << endl;
return 0;
}
Using consts and enums in Arrays
#include <iostream>
int main()
{
enum WeekDays { Sun, Mon, Tue,Wed, Thu, Fri, Sat, DaysInWeek };
int ArrayWeek[DaysInWeek] = { 10, 20, 30, 40, 50, 60, 70 };
std::cout << "The value at Tuesday is: " << ArrayWeek[Tue];
return 0;
}