-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathLENGTH.CPP
163 lines (147 loc) · 3.65 KB
/
LENGTH.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream.h>
using namespace std;
void bufferClr()
{
cin.clear();
cin.ignore(100,'\n');
}
void drawline()
{
cout<<"\n"<<setfill('-')<<setw(80)<<"-";
cout<<setfill(' ');
cout<<"erroe";
}
class Length
{
public:
int feet;
int inch;
void inputLength();
void printLength();
Length operator+(Length L2);
Length operator-(Length L2);
};
int main()
{
drawline();
cout<<"\t\t\tOPERATIONS ON MATRIX CLASS";
drawline();
int exit=0;
while(exit==0)
{
cout<<"\n\t\t->Enter 1 to exit."
<<"\n\t\t->Enter 2 to continue"
<<"\n\t\t ENTER YOU CHOICE:";
int ans;
while(!(cin>>ans) || (ans!=1 && ans!=2))
{
bufferClr();
cout<<"\nInvalid Input.Enter Again:";
}
if(ans==1)
exit=1;
else
{
drawline();
cout<<"Length 1:";
Length L1;
L1.inputLength();
cout<<"Length 2:";
Length L2;
L2.inputLength();
cout<<"\n\t\t\t **---- MENU ----**"
<<"\n\t1->Add the 2 lengths"
<<"\n\t2->Subtract the 2 lengths"
<<"\n\nEnter your choice:";
int choice;
while(!(cin>>choice) || (choice!=1 && choice!=2 ))
{
bufferClr();
cout<<"Invalid Input. Enter only 1 or 2:";
}
switch(choice)
{
case 1:
{
area = length * width;
cout<<"\nLength 1=";L1.printLength();
cout<<" Length 2=";L2.printLength();
cout<<"\nSum Length=";L3.printLength();
break;
}
// case 2:
// if(L1.feet<L2.feet || (L1.feet==L2.feet && L1.inch<L2.inch))
// {
// cout<<"Subtraction Can't be performed as length 1 is smaller than length 2";
// }
// else
// {
// cout<<"\nLength 1=";L1.printLength();
// cout<<" Length 2=";L2.printLength();
// Length L3=L1-L2;
// cout<<"\nDifference Length=";L3.printLength();
// }
// break;
}
}
}
drawline();
return 0;
}
void Length::printLength()
{
cout<<feet<<"'"<<inch<<"\"";
}
void Length::inputLength()
{
cout<<"\nEnter feet:";
while(!(cin>>feet) || feet<0)
{
bufferClr();
cout<<"Invalid Input. Enter again:";
}
cout<<"\nEnter inch:";
while(!(cin>>inch) || inch>11||inch<0)
{
bufferClr();
cout<<"Invalid Input. Enter again:";
}
}
Length Length::operator+(Length L2)
{
Length L3;
L3.feet=feet+L2.feet;
L3.inch=inch+L2.inch;
if(L3.inch>=12)
{
L3.inch-=12;
L3.feet+=1;
}
return L3;
}
Length Length::operator-(Length L2)
{
Length L3;
L3.feet=0;
L3.inch=0;
// L3.feet=feet-L2.feet;
// L3.inch=inch-L2.inch;
if(L3.inch<0)
{
L3.inch+=12;
L3.feet-=1;
}
return L3;
}
Length Length::operator-(Length L2)
{
Length L3;
L3.feet=0;
L3.inch=0;
L3.feet=feet-L2.feet;
L3.inch=inch-L2.inch;
if(L3.inch<0)
{
L3.inch+=12;
L3.feet-=1;
}