Vous êtes sur la page 1sur 15

C++ File Stream

Exercise: Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes per semester), mid-term score, final score, and total score. The program will prompt the user to choose the operation of records from a menu as shown below: =================================================== MENU =================================================== 1. 2. 3. 4. 5. 6. 7. 8. Add student records Delete student records Update student records View all student records Calculate an average of a selected students scores Calculate total scores of a selected student Display the highest and lowest scores Sort students records by ID

9. Sort students' records by total score Enter your choice:1

Note: All students records store in a file stream C++ code solution: #include <cstdlib> #include <iostream>

#include<iomanip.h> #include<fstream.h> using namespace std;

struct student {

int stnumber; char stname[20]; char sex; float quizz1; float quizz2; float assigment; float midterm; float final; float total; int numberOfitem; };

student st[20]; int itemcount=0;

void displayheading();

void Showmax(); void Showmin(); void Sortbyid(); bool Searchduplicate(int); //Menu contruction

void displaymenu(){ cout<<"============================================"<<"\n"; cout<<" MENU "<<"\n";

cout<<"============================================"<<"\n"; cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" } void Add(){ 1.Add student records"<<"\n"; 2.Delete student records"<<"\n"; 3.Update student records"<<"\n"; 4.View all student records"<<"\n"; 5.Sort student records by ID"<<"\n"; 6.Sort student records by Total score"<<"\n"; 7.Display average score of a selected student"<<"\n"; 8.Display the highest and the lowest scores"<<"\n"; 9.Search student by ID"<<"\n";

again:

cout<<"Enter student's ID(1-1000):"; cin>>st[itemcount].stnumber; if(Searchduplicate((int)st[itemcount].stnumber)==true){ cout<<"This ID already exists\n";goto again; }

cout<<"\n"; cout<<"Enter student's Name:"; cin>>st[itemcount].stname; cout<<"\n"; cout<<"Enter student's Sex(F or M):";cin>>st[itemcount].sex; cout<<"\n"; cout<<"Enter student's quizz1 score:";cin>>st[itemcount].quizz1; cout<<"\n"; cout<<"Enter student's quizz2 score:";cin>>st[itemcount].quizz2; cout<<"\n"; cout<<"Enter student's assigment score:";cin>>st[itemcount].assigment; cout<<"\n"; cout<<"Enter student's mid term score:";cin>>st[itemcount].midterm; cout<<"\n"; cout<<"Enter student's final score:";cin>>st[itemcount].final; st[itemcount].total=st[itemcount].quizz1+st[itemcount].quizz2+ s[itemcount].assigment+st[itemcount].midterm+st[itemcount].final;

++itemcount;

} bool Searchduplicate(int id){ bool match=false; for(int i=0;i<itemcount;++i){ if(st[i].stnumber==id) match=true;}

return match; }

void writedata(){ //save to studentrecords.data file st[0].numberOfitem=itemcount; fstream file("studentrecords.dat",ios::out); file.write((char *)(&st),sizeof(st)); file.close();

} void ViewAll(){ int i=0; displayheading();

while(i<=itemcount){ if(st[i].stnumber!=0){ cout<<left<<setw(5)<<st[i].stnumber<<setw(20)<<st[i].stname<<setw(5)<<st[i].sex; cout<<setw(5)<<st[i].quizz1<<setw(5)<<st[i].quizz2<<setw(5)<<st[i].assigment <<setw(5)<<st[i].midterm<<setw(5)<<st[i]. final<<setw(5) <<st[i].total;

cout<<"\n";} i=i+1;

void Delete(){ int id; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i){ if((st[i].stnumber==id)&&(itemcount!=0)){

st[i].stnumber=0;

strcpy(st[i].stname,"0"); st[i].sex='0'; st[i].quizz1=0; st[i].quizz2=0; st[i].assigment=0; st[i].midterm=0; st[i].final=0; st[i].total=0;

} }

void Update(){ int id; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i){ if((st[i].stnumber==id)&&(itemcount!=0)){

cout<<"Enter student's Name:";cin>>st[i].stname; cout<<"Enter student's Sex:";cin>>st[i].sex;

cout<<"Enter quizz1 score:";cin>>st[i].quizz1; cout<<"Enter quizz2 score:";cin>>st[i].quizz2; cout<<"Enter assigment score:";cin>>st[i].assigment; cout<<"Enter mid term score:";cin>>st[i].midterm; cout<<"Enter final score:";cin>>st[i].final; st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final; }

void Sortbyid(){ int i, j; for (i = 0; i < itemcount; i++) for (j = itemcount - 1; j > i; j--) if (st[j].stnumber < st[j - 1].stnumber ) { student temp = st[j]; st[j] = st[j - 1]; st[j - 1] = temp; }

void Sortbytotal(){ int i, j; for (i = 0; i < itemcount; i++)

for (j = itemcount - 1; j > i; j--) if (st[j].total< st[j - 1].total ) { student temp = st[j]; st[j] = st[j - 1]; st[j - 1] = temp; } } void Displayaverage(){ int id; float avg; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i){ if(st[i].stnumber==id){

//st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final; avg=st[i].total/5; } }

cout<<"\nThe average score is:"<<avg; cout<<"\n"; }

void DisplayHL(){

Showmax(); Showmin();

void Showmax(){ float max=st[0].total; for(int i=0;i<itemcount;++i){ for(int j=0;j<itemcount;++j) if(max<st[j].total) max=st[j].total;

} cout<<"The highest score is:"<<max<<"\n"; } void Showmin(){ float min=st[0].total; for(int i=0;i<itemcount;++i){ for(int j=0;j<itemcount;++j) if(st[j].stnumber!=0) if(min>st[j].total) min=st[j].total;

} cout<<"The highest score is:"<<min<<"\n";

} void Searchbyid(){ int id; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i) if(st[i].stnumber==id){ displayheading(); cout<<left<<setw(5)<<st[i].stnumber<<setw(20)<<st[i].stname<<setw(5)<<st[i].sex; cout<<setw(5)<<st[i].quizz1<<setw(5)<<st[i].quizz2<<setw(5)<<st[i].assigment <<setw(5)<<st[i].midterm<<setw(5)<<st[i]. final<<setw(5) <<st[i].total;

cout<<"\n"; } }

void displayheading(){ cout<<left<<setw(5)<<"ID"<<setw(20)<<"NAME"<<setw(5)<<"SEX"<<setw(5)<<"Q1" <<setw(5)<<"Q2"<<setw(5)<<"As"<<setw(5)<<"Mi"<<setw(5)<<"Fi" <<setw(5)<<"TOTAL"<<"\n"; cout<<"================================================\n";

//main method int main(int argc, char *argv[]) {

//read from studentrecords.dat file fstream file("studentrecords.dat",ios::in); file.read((char *)(&st),sizeof(st)); file.close();

itemcount=st[0].numberOfitem; //cout<<"No of Students:"<<itemcount<<"\n";

//show menu displaymenu(); int yourchoice; char confirm; do { cout<<"Enter your choice(1-9):"; cin>>yourchoice;

switch(yourchoice){ case 1:Add();break; case 2:Delete();break; case 3:Update();break; case 4:ViewAll();break; case 5:Sortbyid();break; case 6:Sortbytotal();break; case 7:Displayaverage();break; case 8:DisplayHL();break; case 9:Searchbyid();break; default:cout<<"invalid!\n"; }

cout<<"Press y or Y to continue:"; cin>>confirm; }while(confirm=='y'||confirm=='Y'); cout<<"Save?y or n:"; char c; cin>>c; if(c=='y'||c=='Y') writedata();

system("PAUSE");

return EXIT_SUCCESS; }

Solving quadratic equation


This C++ example program is to calculate the root(s) of a quadratic equation: ax2+bx+c=0. The program firstly asks the user to input factors a, b, and c. The root(s) is calculated based on the following conditions: -If a and b are zero, then there is no root. -If a is zero, then there is one root, -c/b. -If delta is less than 0, then there is no root. -If all above conditions are not true, then there are two roots.

#include <cstdlib> #include <iostream> #include<cmath> using namespace std;

int main(int argc, char *argv[]) { float a, b,c; //declare local variables float x,x1,x2,delta;

cout<<"Enter a, b, c:"; cin>>a>>b>>c;

if((a==0) && (b==0)) cout<<"No root"<<endl;//no root else if(a==0) //one root { x=-c/b; cout<<"One root:"<<x<<endl;

else{ //calculate delta delta=b*b-4*a*c; if(delta<0) cout<<"No root"<<endl; //no root else{ //two roots x1=-b-sqrt(delta)/(2*a); x2=-b+sqrt(delta)/(2*a); cout<<"x1="<<x1<<"\tx2="<<x2<<endl; } }

system("PAUSE"); return EXIT_SUCCESS; }

Vous aimerez peut-être aussi