CHAPTER 4—STRUCTURES codes;
Code:/**1. A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this: Enter your area code, exchange, and number: 415 555 1212 My number is (212) 767-8900 Your number is (415) 555-1212 */ #include<iostream.h> #include<conio.h> struct phone{int area_code, exchange, number;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; phone input, mine={212, 767, 8900}; do{ cout<<"Enter your area code, exchange, and number: "; cin >>input.area_code>>input.exchange>>input.number; cout<<"My number is ("<<mine.area_code<<") "<<mine.exchange<<"-"<<mine.number<<endl; cout<<"Your number is ("<<input.area_code<<") "<<input.exchange<<"-"<<input.number; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/**2. A point on the two-dimensional plane can be represented by two numbers: an x coordinate and a y coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5 units up the horizontal axis. The sum of two points can be defined as a new point whose x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the sum of the y coordinates. Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Then set the third point equal to the sum of the other two, and display the value of the new point. Interaction with the program might look like this: Enter coordinates for p1: 3 4 Enter coordinates for p2: 5 7 Coordinates of p1+p2 are: 8, 11 */ #include<iostream.h> #include<conio.h> struct point{int x, y;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; point p1, p2, sum; do{ cout<<"Enter coordinates for p1: "; cin >>p1.x>>p1.y; cout<<"Enter coordinates for p2: "; cin >>p2.x>>p2.y; sum.x=p1.x+p2.x; sum.y=p1.y+p2.y; cout<<"Coordinates of p1+p2 are: "<<sum.x<<", "<<sum.y; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/**3. Create a structure called Volume that uses three variables of type Distance (from the ENGLSTRC example) to model the volume of a room. Initialize a variable of type Volume to specific dimensions, then calculate the volume it represents, and print out the result. To calculate the volume, convert each dimension from a Distance variable to a variable of type float representing feet and fractions of a foot, and then multiply the resulting three numbers.*/ #include<iostream.h> #include<conio.h> struct Distance{int feet; float inches;}; struct Volume{Distance x, y, z;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; Volume dimension; char c; do{ cout<<"Enter x, y & z ...\n(EX: 3.6=3 feet and 6 inches, default: 'x.0 y.0 z.0' x, y & z are in feet) : \n"; cin >>dimension.x.feet>>c>>dimension.x.inches >>dimension.y.feet>>c>>dimension.y.inches >>dimension.z.feet>>c>>dimension.z.inches; /*float result=(dimension.x.feet+dimension.x.inches/12)* (dimension.y.feet+dimension.y.inches/12)* (dimension.z.feet+dimension.z.inches/12);*/ cout<<"the volume is : " <<(dimension.x.feet+dimension.x.inches/12)* (dimension.y.feet+dimension.y.inches/12)* (dimension.z.feet+dimension.z.inches/12); cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*4. Create a structure called employee that contains two members: an employee number (type int) and the employee’s compensation (in dollars; type float). Ask the user to fill in this data for three employees, store it in three variables of type struct employee, and then display the information for each employee.*/ #include<iostream.h> #include<conio.h> #include<iomanip.h> struct employee{int number; float compensation;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; employee e[3]; int i; do{ for(i=0; i<3; i++) { cout<<"Enter the number of employee number "<<i+1<<" : "; cin>>e[i].number; cout<<"Enter the compensation of employee number "<<i+1<<" : "; cin>>e[i].compensation;} cout<<"Employee number"<<" Employee compensation\n"; for(i=0; i<3; i++) cout<<setw(15)<<e[i].number<<setw(24)<<e[i].compensation<<endl; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*5. Create a structure of type date that contains three members: the month, the day of the month, and the year, all of type int. (Or use day-month-year order if you prefer.) Have the user enter a date in the format 12/31/2001, store it in a variable of type struct date, then retrieve the values from the variable and print them out in the same format. */ #include<iostream.h> #include<conio.h> #include<iomanip.h> struct date{int day; int month; int year;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; date x; char c; do{ cout<<"Enter the date : "; cin >>x.day>>c>>x.month>>c>>x.year; cout<<"The date is : "; cout<<x.day<<c<<x.month<<c<<x.year; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
-
Code:
/*6. We said earlier that C++ I/O statements don’t automatically understand the data types of enumerations. Instead, the (>>) and (<<) operators think of such variables simply as integers. You can overcome this limitation by using switch statements to translate between the user’s way of expressing an enumerated variable and the actual values of the enumerated variable. For example, imagine an enumerated type with values that indicate an employee type within an organization: enum etype { laborer, secretary, manager, accountant, executive, researcher }; Write a program that first allows the user to specify a type by entering its first letter (‘l’, ‘s’, ‘m’, and so on), then stores the type chosen as a value of a variable of type enum etype, and finally displays the complete word for this type. Enter employee type (first letter only) laborer, secretary, manager, accountant, executive, researcher): a Employee type is accountant. You’ll probably need two switch statements: one for input and one for output.*/ #include<iostream> using namespace std; #include<conio.h> enum etype{laborer, secretary, manager, accountant, executive, researcher}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; etype x; char *ret; do{ cout<<"Enter employee type (first letter only)"<<endl <<"(laborer, secretary, manager, accountant, executive, researcher): "; switch(getche()){ case 'l': x=laborer ; break; case 's': x=secretary ; break; case 'm': x=manager ; break; case 'a': x=accountant; break; case 'e': x=executive ; break; case 'r': x=researcher;} switch(x){ case 0: ret = "laborer" ; break; case 1: ret = "secretary" ; break; case 2: ret = "manager" ; break; case 3: ret = "accountant"; break; case 4: ret = "executive" ; break; case 5: ret = "researcher";} cout<<"\nEmployee type is "<<ret<<"."; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*7. Add a variable of type enum etype (see Exercise 5), and another variable of type struct date (see Exercise 3) to the employee class of Exercise 4. Organize the resulting program so that the user enters four items of information for each of three employees: an employee number, the employee’s compensation, the employee type, and the date of first employment. The program should store this information in three variables of type employee, and then display their contents.*/ #include<iostream> #include<conio.h> #include<iomanip> using namespace std; enum etype{laborer, secretary, manager, accountant, executive, researcher}; struct date{int day; int month; int year;}; struct employee{int number; float compensation; date d; char *ret;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; employee e[3]; int i; char c; etype x; do{ for(i=0; i<3; i++) { cout<<"\nEnter the number of employee number "<<i+1<<" : "; cin>>e[i].number; cout<<"Enter the compensation of employee number "<<i+1<<" : "; cin>>e[i].compensation; cout<<"Enter employee type (first letter only) of employee number "<<i+1<<" : "<<endl <<"(laborer, secretary, manager, accountant, executive, researcher): "; switch(getche()){ case 'l': x=laborer ; break; case 's': x=secretary ; break; case 'm': x=manager ; break; case 'a': x=accountant; break; case 'e': x=executive ; break; case 'r': x=researcher;} switch(x){ case 0: e[i].ret = "laborer" ; break; case 1: e[i].ret = "secretary" ; break; case 2: e[i].ret = "manager" ; break; case 3: e[i].ret = "accountant"; break; case 4: e[i].ret = "executive" ; break; case 5: e[i].ret = "researcher"; break; default: e[i].ret = "Unknow";} cout<<"\nEnter the date of employee number "<<i+1<<" : "; cin >>e[i].d.day>>c>>e[i].d.month>>c>>e[i].d.year;} cout<<"\nEmployee number"<<" compensation"<<" type"<<" date of first employment"<<endl; for(i=0; i<3; i++) cout<<setw(15)<<e[i].number <<setw(15)<<e[i].compensation <<setw(15)<<e[i].ret <<setw(21)<<e[i].d.day<<c<<e[i].d.month<<c<<e[i].d.year<<endl; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*8. Start with the fraction—adding program of Exercise 9 in Chapter 2, “C++ Programming Basics.” This program stores the numerator and denominator of two fractions before adding them, and may also store the answer, which is also a fraction. Modify the program so that all fractions are stored in variables of type struct fraction, whose two members are the fraction’s numerator and denominator (both type int). All fraction-related data should be stored in structures of this type.*/ #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; fraction equ[2]; char operation; do{ cout<<"Enter first fraction: "; cin >>equ[0].numerator>>operation>>equ[0].denominator; //if (operation != '/') {raise error event} cout<<"Enter second fraction: "; cin >>equ[1].numerator>>operation>>equ[1].denominator; //if (operation != '/') {raise error event} cout<<"Sum = "<<(equ[0].numerator*equ[1].denominator+equ[0].denominator*equ[1].numerator) <<operation<<(equ[0].denominator*equ[1].denominator)<<endl; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*9. Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter hours:”, and so forth). The program should then store the time in a variable of type struct time, and finally print out the total number of seconds represented by this time value: long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds */ #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; time t1; char c; do{ cout<<"Enter a time value in hours, minutes, and seconds [hh:mm:ss] format: "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"The total number of seconds is: "<<t1.hours*3600 + t1.minutes*60 + t1.seconds; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*10. Create a structure called sterling that stores money amounts in the old-style British system discussed in Exercises 8 and 11 in Chapter 3, “Loops and Decisions.” The members could be called pounds, shillings, and pence, all of type int. The program should request the user to enter a money amount in new-style decimal pounds (type double), convert it to the old-style system, store it in a variable of type struct sterling, and then display this amount in pounds-shillings-pence format.*/ #include<iostream> #include<conio.h> using namespace std; struct sterling{int pounds; int shillings; int pence;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; float decpounds; float decfrac; //From old programme. sterling s1; do{ cout<<"Enter a money amount in new-style decimal pounds: "; cin >>decpounds; s1.pounds = static_cast<int>(decpounds); decfrac = 240*(decpounds-s1.pounds); s1.shillings = (static_cast<int>(decfrac))%12; //Ignore fracions in pence. decfrac = static_cast<int>((decfrac-s1.shillings)/12); //Ignore fracions in pence. cout<<"Equivalent in old notation = \x9c"<<s1.pounds<<"."<<decfrac<<"."<<s1.shillings<<endl; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
- 819
Code:/*11. Use the time structure from Exercise 9, and write a program that obtains two time values from the user in 12:59:59 format, stores them in struct time variables, converts each one to seconds (type int), adds these quantities, converts the result back to hours-minutes-seconds, stores the result in a time structure, and finally displays the result in 12:59:59 format.*/ #include<iostream> #include<conio.h> using namespace std; struct time{int hours; int minutes; int seconds;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; time t1, t2, t3; char c; long tmp; //t3 coz he want to store the result in variable. do{ cout<<"In [hh:mm:ss] format;\n"; cout<<"Enter first time value : "; cin >>t1.hours>>c>>t1.minutes>>c>>t1.seconds; cout<<"Enter second time value: "; cin >>t2.hours>>c>>t2.minutes>>c>>t2.seconds; tmp=t1.hours*3600+t1.minutes*60+t1.seconds+t2.hours*3600+t2.minutes*60+t2.seconds; t3.seconds=tmp%60; t3.minutes=((tmp-t3.seconds)%3600)/60; t3.hours=tmp/3600; //those lines for true input at first then true output. if(t3.seconds>59) {t3.seconds-=59; t3.minutes++;} //Check seconds over. if(t3.minutes>59) {t3.minutes-=59; t3.hours++;} //Check minutes over. //hours check not needed .. at 25 hours I haven't a format for days. cout<<"The result is: "<<t3.hours<<":"<<t3.minutes<<":"<<t3.seconds; cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
Code:/*12. Revise the four-function fraction calculator program of Exercise 12 in Chapter 3 so that each fraction is stored internally as a variable of type struct fraction, as discussed in Exercise 8 in this chapter.*/ #include<iostream> #include<conio.h> using namespace std; struct fraction{int numerator; int denominator;}; void main(void) { cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"-------------------------------------------------------------------------------\n"<<endl; fraction f[2]; char c, op; do{ cout<<"Enter your task : "; cin >>f[0].numerator>>c>>f[0].denominator>>op>>f[1].numerator>>c>>f[1].denominator; if(!f[0].denominator || !f[1].denominator) {cout<<"Illeagle fraction !"<<endl; op=false;} switch(op) { case '+': cout<<"Answer = "<<(f[0].numerator*f[1].denominator+f[0].denominator*f[1].numerator)<<c <<(f[0].denominator*f[1].denominator); break; case '-': cout<<"Answer = "<<(f[0].numerator*f[1].denominator-f[0].denominator*f[1].numerator)<<c <<(f[0].denominator*f[1].denominator); break; case '*': cout<<"Answer = "<<f[0].numerator*f[1].numerator<<c<<f[0].denominator*f[1].denominator; break; case '/': if(f[0].numerator != 0) cout<<"Answer = "<<f[0].numerator*f[1].denominator<<c <<f[1].numerator*f[0].denominator; else cout<<"Math error !"<<endl; break; default: cout<<"Unknow operator please try again !"<<endl;} cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl; }while(getch()=='c'); }
thank you Hekmat Amin
ReplyDeleteIts really very helpfull thanksss
ReplyDeleteperfect
ReplyDelete:))
DeleteIts amazing to have such a good guider like you
ReplyDelete👌👌👌
ReplyDeleteQ5) #include
ReplyDelete#include
using namespace std;
struct date
{
int day; int month; int year;
};
int main()
{
date x; char c;
do{
cout<<"Enter the day,month,year: ";
cin >>x.day;
cin>>x.month;
cin>>x.year;
cout<<"The date is : ";
cout<<x.day<<"//"<<x.month<<"//"<<x.year;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
it will give true output
Delete