0% found this document useful (0 votes)
13 views

LabStruct

The document provides multiple examples of using structures and enumerations in C++. It demonstrates how to define and manipulate structures for various applications, such as parts inventory, distances, and playing cards. Additionally, it includes an example of using an enumeration to represent days of the week and perform arithmetic and comparisons.

Uploaded by

naeemnoorawan23
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

LabStruct

The document provides multiple examples of using structures and enumerations in C++. It demonstrates how to define and manipulate structures for various applications, such as parts inventory, distances, and playing cards. Additionally, it includes an example of using an enumeration to represent days of the week and perform arithmetic and comparisons.

Uploaded by

naeemnoorawan23
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

STRUCT/ENUM LAB

Dated 09/10/2017
Example 1:

// parts.cpp
// uses parts inventory to demonstrate structures
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct part //declare a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{
part part1; //define a structure variable
part1.modelnumber = 6244; //give values to structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
return 0;
}

Example 2:

#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////

struct part //specify a structure


{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};

////////////////////////////////////////////////////////////////

int main()
{ //initialize variable
part part1 = { 6244, 373, 217.55F };
part part2; //define variable

//display first variable


cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
part2 = part1; //assign first variable to second

//display second variable


cout << “Model “ << part2.modelnumber;
cout << “, part “ << part2.partnumber;
cout << “, costs $” << part2.cost << endl;
return 0;
}
Example 3:
struct Distance {
int feet;
float inches;
};

int main ()
{
Distance d1, d3;
Distance d2 = {3, 9.0F};

cout<<"Enter Feet = ";


cin>>d1.feet;

cout<<"Enter Inches = ";


cin>>d1.inches;

d3.inches = d1.inches + d2.inches;


d3.feet=0;

if(d3.inches>=12)
{
d3.inches-=12.0;
d3.feet++;
}

d3.feet += d1.feet + d2.feet;

cout<<d1.feet<<"\'-"<<d1.inches<<"\"+";
cout<<d2.feet<<"\'-"<<d2.inches<<"\"=";
cout<<d3.feet<<"\'-"<<d3.inches<<"\"";
return 0;
}

Example 4:
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct Distance //English distance
{
int feet;
float inches;
};
////////////////////////////////////////////////////////////////
struct Room //rectangular area
{
Distance length; //length of rectangle
Distance width; //width of rectangle
};
////////////////////////////////////////////////////////////////
int main()
{
Room dining; //define a room
dining.length.feet = 13; //assign values to room
dining.length.inches = 6.5;
dining.width.feet = 10;
dining.width.inches = 0.0;
//convert length & width
float l = dining.length.feet + dining.length.inches/12;
float w = dining.width.feet + dining.width.inches/12;
//find area and display it
cout << “Dining room area is “ << l * w
<< “ square feet\n” ;
return 0;
}

Example 5:
CARDS.CPP
// cards.cpp
// demonstrates structures using playing cards
#include <iostream>
using namespace std;
const int clubs = 0; //suits
const int diamonds = 1;
const int hearts = 2;
const int spades = 3;
const int jack = 11; //face cards
const int queen = 12;
const int king = 13;
const int ace = 14;
////////////////////////////////////////////////////////////////
struct card
{
int number; //2 to 10, jack, queen, king, ace
int suit; //clubs, diamonds, hearts, spades
};
////////////////////////////////////////////////////////////////
int main()
{
card temp, chosen, prize; //define cards
int position;
card card1 = { 7, clubs }; //initialize card1
cout << “Card 1 is the 7 of clubs\n”;
card card2 = { jack, hearts }; //initialize card2
cout << “Card 2 is the jack of hearts\n”;
card card3 = { ace, spades }; //initialize card3
cout << “Card 3 is the ace of spades\n”;
prize = card3; //copy this card, to remember it
cout << “I’m swapping card 1 and card 3\n”;
temp = card3; card3 = card1; card1 = temp;
cout << “I’m swapping card 2 and card 3\n”;
temp = card3; card3 = card2; card2 = temp;
cout << “I’m swapping card 1 and card 2\n”;
temp = card2; card2 = card1; card1 = temp;
cout << “Now, where (1, 2, or 3) is the ace of spades? “;
cin >> position;
switch (position)
{
case 1: chosen = card1; break;
case 2: chosen = card2; break;
case 3: chosen = card3; break;
}
if(chosen.number == prize.number && // compare cards
chosen.suit == prize.suit)
cout << “That’s right! You win!\n”;
else
cout << “Sorry. You lose.\n”;
return 0;
}

Example 6:

#include <iostream>
using namespace std;

//specify enum type


enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

int main()
{
days_of_week day1, day2; //define variables of type days_of_week

day1 = Mon; //give values to


day2 = Thu; //variables
int diff = day2 - day1; //can do integer arithmetic
cout << “Days between = “ << diff << endl;
if(day1 < day2) //can do comparisons
cout << “day1 comes before day2\n”;
return 0;
}

You might also like