Sei sulla pagina 1di 15

Assignment 9

Data Files

SKCH

Assignment 9 Data Files


Akshay Tiwary XII A 03 SKCH

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

1. Consider the following data below. Each line represents the shipping records of a company ABC. The columns represent the month, no of red crates, no of green crates, no of yellow crates and the no of blue crates respectively. Create a structure to represent this data. Jan 13 25 15 115 Feb 15 32 24 226 Mar 15 24 34 228 Apr 31 52 63 420 May 16 34 29 208 Jun 31 42 75 492 Jul 24 34 67 436 Aug 15 34 47 316 Sep 13 55 37 277 Oct 29 54 68 525 Nov 20 87 82 577 Dec 17 35 61 401 Write a menu based program which a. Writes the above data into a data file. b. Reads only those records where number of number of yellow crates is lesser than number of green crates c. Prints the total number of crates shipped for each month d. Obtains the month from the user and prints the record for that month. 2. Consider the following table which shows the weekend movie collection data o MovieName Distributor Genre Cloudy with a Chance of Meatballs 2 2 Prisoners 3 Rush Universal Drama 2,297 $4,490 10 4 Insidious Chapter 2 FilmDistrict Horror 3,120 $2,163 17 5 The Family Relativity Black Comedy 2,894 $1,270 17 6 Lee Daniels' The Butler 7 Enough Said 8 Battle of the Year 3D Percy Jackson: Sea of

Akshay Tiwary

XII A

03

Assignment 9
Monsters 10 Despicable Me 2 Universal Comedy 748 $1,035 90 11 Disney Planes

Data Files

SKCH

1. Create a class called as movie with the above column headings as 2. The constructor should populate the data 3. Create a member function to retrieve values Sony Pictures Adventure 4,001 $8,748 3 Warner Bros. Thriller/ Suspense 3,290 $3,435 10 Weinstein Co. Drama 2,062 $1,172 45 Fox Searchlight Comedy 227 $9,317 12 Sony Pictures Drama 2,008 $996 10 20th Century Fox Adventure 1,007 $770 54 Walt Disney Comedy 1,426 $518 52 its data members. 4. Write a non member function which will write the data to a file 5. Write a non member function which asks for a genre From the user 6. Create a report with movie name and gross earnings. Gross earnings 3. Consider the following table with data about social networking sites Site % of Men % of Women Number of and reports all movies details of the genre = earnings per theatre * no of theatres * no of days. users in millions Facebook 40 60 901 405 Google+ 63 37 170 3

Akshay Tiwary

XII A

03

Assignment 9
Twitter 43 57 555 21 LinkedIn 55 45 150 17 Pinterest 32 68 12 89 1. Create the file

Data Files

SKCH

2. Create another file with records in the following format Site name, no of men users in million, no of women users in million, time spent in hours per month 3. Ask for the site name and modify the record with new details without opening a new file. 4. Create class called as books with the following details of classic books which have sold more than 100 million copies Book Author(s) Original language A Tale of Two Cities Charles Dickens English 1859 200 million The Lord of the Rings J. R. R. Tolkien English 1954 150 million Le Petit Prince (The Little Prince) Antoine de SaintExupry French[Note 1] 1943 140 million[3] The Hobbit J. R. R. Tolkien English 1937 100 million[4] (Dream of the Red Chamber) Cao Xueqin Chinese 1754 100 million[6] Book Author(s) Original language And Then There Were None Agatha Christie English 1939 100 million[7] 1. Create the file with the class objects. 2. Ask user input and add a record to the end of the file 3. Create a third file where all records are arranged in ascending order of the year published . 4. Ask for author name and display details of the record . 5. All data fields must be private in the class.

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

1.
#include<iostream> #include<fstream> using namespace std; struct shipping_record {char month[4]; int red; int green; int yellow; int blue; }s; int main() { fstream f; cout << "Enter choice" << endl << "1. Write" << endl << "2. Read." << endl; cout << "3. Print total number of crates" << endl "4. Record of the month" << endl; cout << "5. Exit. " << endl; int choice; do { cin>>choice; switch(choice) {case 1: f.open("Shipping.dat", ios::out|ios::app); for(int i=0;i<12;i++) {cout<<"Enter month: "; cin>>s.month; cout<<"Enter red crates: "; cin>>s.red; cout<<"Enter green crates: "; cin>>s.green; cout<<"Enter yellow crates: "; cin>>s.yellow; cout<<"Enter blue crates: "; cin>>s.blue; f.write((char*)&s, sizeof(s)); } f.close(); break; case 2: f.open("data.dat", ios::in); while(f.read((char*)&s, sizeof(s))) {if(s.yellow<s.green) {cout<<"Month: "<<s.month<<endl; cout<<"Red: "<<s.red<<endl; cout<<"Green: "<<s.green<<endl; cout<<"Yellow: "<<s.yellow<<endl; cout<<"Blue: "<<s.blue<<endl; cout<<"\n---------------\n"; }

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

} f.close(); break; case 3: f.open("data.dat", ios::in); while(f.read((char*)&s, sizeof(s))) {cout<<"Month: "<<s.month<<endl; int sum; sum=s.red+s.green+s.yellow+s.blue; cout<<"Total crates: "<<sum<<endl; } f.close(); break; case 4: f.open("data.dat", ios::in); cout<<"Enter month: "; char mon[4]; cin>>mon; while(f.read((char*)&s, sizeof(s))) { if(strcmp(mon, s.month)==0) {cout<<"Month: "<<s.month<<endl; cout<<"Red: "<<s.red<<endl; cout<<"Green: "<<s.green<<endl; cout<<"Yellow: "<<s.yellow<<endl; cout<<"Blue: "<<s.blue<<endl; } } f.close(); break; case 5: cout << "The system will now exit" << endl; break; default: cout << "Erroneous input." << e } }while(choice!=5); return 0; }

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

2.
#include<iostream> #include<fstream> #include<string> class movie { char name[30],dist[20],genre[15]; int no_of_theatres,earning,no_of_days; public: movie() { cout<<"Enter name of the movie\n"; gets(name); cout<<"Enter the distributor\n"; gets(dist); cout<<"Enter the genre of the movie\n"; gets(genre); cout<<"Enter the number of theatre\n"; cin>>no_of_theatres; cout<<"Enter the earnings per theatre\n"; cin>>earning; cout<<"Enter the no of days it has been running\n"; cin>>no_of_days; } char* get_genre() { return genre; } char* get_name() { return name; } char* get_dist() { return dist; } int get_no() { return no_of_theatres; } int get_earn() { return earning; } int get_num() { return no_of_days; } }; void write_data(movie this_movie[11])

Akshay Tiwary

XII A

03

Assignment 9
{

Data Files

SKCH

fstream f1("movies.dat",ios::in|ios::out); int i; for(i=0;i<11;i++) { f1.write((char*)&this_movie[i],sizeof(this_movie[i])); } f1.close(); } void search() { movie temp; char s[15]; fstream f ("movies.dat",ios::in|ios::out); while(!f.eof()) { f.read((char*)&temp,sizeof(temp)); if(strcmp(temp.get_genre(),s)==0) { cout<<"\nMovie Name:"<<temp.get_name(); cout<<"\nDistributor:"<<temp.get_dist(); cout<<"\nNumber of theatres:"<<temp.get_no(); cout<<"\nEarning per theatre:"<<temp.get_earn(); cout<<"\nNo of days:"<<temp.get_num(); } } f.close(); } int main() { movie list[11]; int input; int a,b,c; do { cout<<"1.Write data " << endl << "2.Search in the file" << endl << "3.Get Report and print report" << endl << "4.EXIT" << endl; cin>>input; switch(input) { case 1:write_data(m); break; case 2:search(); break; case 3:movie temp;

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

fstream f2("movies.dat",ios::in|ios::out); cout<<"THE REPORT:\n\n\n"; while(!f2.eof()) { f2.read((char*)&temp,sizeof(temp)); cout<<temp.get_name(); a=temp.get_earn(); b=temp.get_no(); c=temp.get_num(); cout<<a*b*c;; } f2.close(); break; case 4: cout << "The system will exit now." << endl; default:cout << "Erroneous Input" << endl; } }while(ch!=4); return 0; }

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

3.
# include<iostream> # include<fstream> using namespace std; class site_name { char site[10]; int men,wmen,tot,time; public: void read() { cout<<"Enter the gets(site); cout<<"Enter the cin>>wmen; cout<<"Enter the cin>>men; cout<<"Enter the endl; cin>>tot; cout<<"Enter the cin>>time; } char* get_site() { return site; } int get_men() { return men; } int get_wmen() { return wmen; } int get_tot() { return tot; } int get_time() { return time; } void editc() { cout<<"Enter new cin>>men; cout<<"Enter new cin>>wmen;

name of the site" << endl; percentage of women" << endl; percentage of men" << endl; total number of user in millions" <<

time spent per user per month"

percentage of men" << endl; percentage of women" << endl;

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

cout<<"Enter new total number of users in millions" << endl; cin>>tot; cout<<"Enter new time spent per user per month" << endl; cin>>time; } }; void create(site_name current_site5]) { fstream f("social.dat",ios::in|ios::out); int i; for(i=0;i<5;i++) { f.write((char*)&current_sitei],sizeof(current_sitei])); } f.close(); } void ncreate(site_name current_site5]) { fstream f1("social1.dat",ios::in|ios::out); double m,w; int i; for(i=0;i<5;i++) { m=(current_sitei].get_men()/100)*(current_sitei].get_tot()); w=(current_sitei].get_wmen()/100)*(current_sitei].get_tot()); f1<<current_sitei].get_site(); f1<<m; f1<<w; f1<<current_sitei].get_time(); } f1.close(); } void edit() { char s[10]; cout<<"Enter site name whose records are to be modified" << endl; gets(s); site_name temp; fstream f("social.dat",ios::in|ios::out); while(!f.eof()) { f.read((char*)&temp,sizeof(temp)); if(strcmp(temp.get_site(),s)==0) { temp.editc(); f.seekg(-1*sizeof(temp),ios::cur); f.write((char*)&temp,sizeof(temp));

Akshay Tiwary

XII A

03

Assignment 9
} } f.close();

Data Files

SKCH

} int main() { site_name s[5]; char ch; int i; for(i=0;i<5;i++) s[i].read(); do { cout << "Enter input choice. " << endl; cin>>ch; switch(ch) { case 1:create(s); break; case 2:ncreate(s); break; case 3:edit(); break; case 4: cout << "The system will exit now." << endl; break; default: cout << "Erroneous input." << endl; } }while(ch!=4); return 0; }

Akshay Tiwary

XII A

03

Assignment 9

Data Files

SKCH

4.
# include<iostream > # include<fstream > using namespace std; class books { char book[30],author[20],lang[10]; int pub,sale; public: void read() { cout<<"Enter the name of the book" << endl; gets(book); cout<<"Enter the author's name" << endl; gets(author); cout<<"Enter the original language in which the book was written" << endl; gets(lang); cout<<"Enter the year of publishing. " << endl; cin>>pub; cout<<"Enter sales. " << endl; cin>>sale; } char* get_book() { return book; } char* get_author() { return author; } char* get_lang() { return lang; } int get_pub() { return pub; } int get_sale() { return sale; } }; void create(books cur[6]) { fstream f1("books.dat",ios::in|ios::out); int i; for(i=0;i<6;i++)

Akshay Tiwary

XII A

03

Assignment 9
{

Data Files

SKCH

if(cur[i].get_pub()>100) f1.write((char*)&cur[i],sizeof(cur[i])); } f1.close(); } void add() { fstream f1("books.dat",ios::in|ios::out); books temp; cout<<"Enter details of new addition\n"; temp.read(); f1.seekg(0,ios::end); f1.write((char*)&temp,sizeof(temp)); f1.close(); } void rearrange() { fstream f("book1.dat",ios::in|ios::out); fstream f1("books.dat",ios::in|ios::out); int i,min=2000; books temp,temp1; for(i=0;i<6;i++) { while(!f1.eof()) { f1.read((char*)&temp,sizeof(temp)); if(temp.get_pub()<min) { min=temp.get_pub(); temp1=temp; } } f.write((char*)&temp1,sizeof(temp1)); f1.seekg(0,ios::beg); } } void display() { char s[20]; cout<<"Enter the suthor to be searched\n"; gets(s); books temp; fstream f1("books.dat",ios::in|ios::out); while(!f1.eof()) { f1.read((char*)&temp,sizeof(temp)); if(strcmp(temp.get_author(),s)==0); {

Akshay Tiwary

XII A

03

Assignment 9

Data Files
cout<<"Book

SKCH

Name:"<<temp.get_book()<<endl; cout<<"Original Language:"<<temp.get_lang()<<endl; cout<<"First Published:"<<temp.get_pub()<<endl; cout<<"Approx sales:"<<temp.get_sale()<<endl; } } f1.close(); } int main() { books b[6]; int i; for(i=0;i<6;i++) b[i].read(); char ch; do { cout<<"1.Create the file\n2.Add a record\n3.rearrange the records\n4.Search and display\n5.Exit" << endl; cin>>ch; switch(ch) { case 1:create(b); break; case 2:add(); break; case 3:rearrange(); break; case 4:display(); break; case 5: cout << "The system will exit now." << endl; break; default:cout<<"Erroneous input." << endl; } }while(ch!=5); return 0; }

Akshay Tiwary

XII A

03

Potrebbero piacerti anche