LabStruct
LabStruct
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;
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
int main()
{ //initialize variable
part part1 = { 6244, 373, 217.55F };
part part2; //define variable
int main ()
{
Distance d1, d3;
Distance d2 = {3, 9.0F};
if(d3.inches>=12)
{
d3.inches-=12.0;
d3.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;
int main()
{
days_of_week day1, day2; //define variables of type days_of_week