CSC 313 Code, Fall 2007
Notre Dame University–Louaize · Data structures using C++ · 15 pages
Showing the first 10 of 15 pages. Download the PDF to see all of it.
More CSC 313 files
- Code · Fall 2007FileView
- Exam 1 with Solution · Spring 2008Exam · with solutionView
- Final · Spring 2008Final examView
- Final with Solution · Spring 2008Final exam · with solutionView
- Midterm · Spring 2008ExamView
- Homework 1 · Fall 2007HomeworkView
- Homework 2 · Fall 2007HomeworkView
- Exam 2 with Solution · Fall 2005Exam · with solutionView
Show the text of this file
////////////////////////////////////////////////Libraries
Definition//////////////////////////////////////////////////////
#include<fstream.h>
#include<stdlib.h>
#include<iomanip.h>
#include<string.h>
#include<io.h>
#include<ctype.h>
#define fin '$'
////////////////////////////////////////////////VARIABLES
Definiton//////////////////////////////////////////////////////
char xyz[20]="questions",c,b;
int choice;
char username[20],password[20];
////////////////////////////////////////////////Functions
Prototypes/////////////////////////////////////////////////////
bool create(char *);
void Display(char *);
bool check(char *);
void NewTest(char *,char *);
void SecondTest(char *,char *);
////////////////////////////////////////////////Main
Function////////////////////////////////////////////////////////////
int main()
{
int choice;
do
{
cout<<"\n\t Choose One of the following choices by inserting its
number: \n";
cout<<"\t #0 Exit." << endl;
cout<<"\t #1 Edit Questions. " << endl;
cout<<"\t #2 Enter Previously created Questions File Name ." <<
endl;
cout<<"\t #3 Students test. " << endl;
cout<<"\t #4 Display Questions File." << endl;
cin>>choice;
switch(choice)
{
case 0:
cout<<"\n\t////////// You have Selected to Exit the
Program ///////////" << endl;
cout<<"\n\t////////// Thanks For Using My
Software ///////////" << endl;
break;
///////////////////////////////////////end of case
0 /////////////////////////////////////////////////////
case 1:
cout<<"\tUserName:";
cin>>username;
cout<<"\tPassword:";
cin>>password;
cout<<"\n\tInsert the File Name:";
cin>>xyz;
if(!create(xyz))
{
cout<<"\tThe File Can't Be Created\n";
} // end of "if"
else
cout<<"\tFile With Name << "<<xyz<<" >> Is Created\n";
break;
/////////////////////////////////////////end of case
1//////////////////////////////////////////////////////
case 2:
cout<<"\tUserName:";
cin>>username;
cout<<"\tPassword:";
cin>>password;
cout<<"\n\tInsert the File Name:";
cin>>xyz;
break;
/////////////////////////////////////////end of case
2//////////////////////////////////////////////////////
case 3:
cout<<"\tUserName:";
cin>>username;
if(check(username))
{
SecondTest(xyz,username);
} // end of "if"
else
{
NewTest(xyz,username);
} // end of "else"
break;
//////////////////////////////////////////end of case
3/////////////////////////////////////////////////////
case 4:
cout<<"\tUserName:";
cin>>username;
cout<<"\tPassword:";
cin>>password;
Display(xyz);
break;
//////////////////////////////////////////end of case
4/////////////////////////////////////////////////////
default:
cout<<"\tEnter #0 For Exit\n"<<endl;
break;
} // end of "switch"
cout<<"\n\n\n\tContinue(y/n)";
cin>>b;
if(isupper(b))
b=tolower(b);
while(b=='y');
cout<<"\n\t////////// Thanks For Using My Software /////////// "<<
endl;
return 0;
} // end of main function
////////////////////////////////////////////////end of
main//////////////////////////////////////////////////////////////
////////////////////////////////////////////////Functions
Declarations///////////////////////////////////////////////////
bool create(char *xyz)
{
ofstream Question(xyz,ios::out);
int QuestionNumbers;
// Declartion of variables
char DataArray[100],answers;
// Declartion of variables
if(!Question){
// Setting Condition
return false;
}
// end of "if"
statement
else
{
cout<<"\tEnter Questions Number:";
cin>>QuestionNumbers;
Question<<QuestionNumbers<<"\n"<<"Questions\n";
for(int i=1;i<=QuestionNumbers;i++)
{
Question<<i<<"\t";
cout<<"\n";
cout<<"\tEnter Question "<<i<<" Difficulty Level:";
cin.getline(DataArray,75);
Question<<"\t"<<DataArray<<"\t";
cout<<"\n\tEnter Question "<<i<<" :";
cin.getline(DataArray,75);
Question<<DataArray<<"\n";
}
// end of "for" Loop
Question <<"Answers\n";
for(int i=1;i<=QuestionNumbers;i++)
{
cout<<"\n\tEnter Question "<<i<<" Answer:";
cin>>answers;
if(islower(answers))
answers=toupper(answers);
while(answers!='T' && answers!='F')
{
cout<<"\tYou have to insert 'T' or 'F'\n";
cout<<"\n\tEnter Question "<<i<<" Answer:";
cin>>answers;
if(islower(answers))
answers=toupper(answers);
}
// end of "While" Loop
Question<<answers<<"\n";
}
// end of "for" Loop
}
// end of "else"
return true;
}
////////////////////////////////////////////////////end of function
"create" ////////////////////////////////////////////
void Display(char *xyz)
{
ifstream f1;
f1.open(xyz);
if(!f1)
// Error Checking
{
cerr<<"Error.\n";
exit(1);
}
cout<<"Contnet Of file << "<<xyz<<" >> is:\n";
while(f1)
{
f1.get(c);
cout<<c;
}
cout<<endl;
f1.close();
}
/////////////////////////////////////////////////////end of function
"Display"////////////////////////////////////////////
bool check(char *username){
ifstream f2(username,ios::in);
int j=0;
while(f2)
{
f2.get(c);
j++;
}
if(j==0 ||j==1)
return false;
return true;
}
//////////////////////////////////////////////////////end of function
"check"/////////////////////////////////////////////
void NewTest(char *xyz,char *username)
{
int z=0,count1=0,count2=0,p=0,q=0;
int QuestionNumber=0;
char AnswerTable[100],Question[100][100],YourAnswer[100];
ifstream f1;
f1.open(xyz);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(c=='\n')
QuestionNumber++;
} // end of "while" Loop
QuestionNumber=(QuestionNumber-3)/2;
f1.close();
cout<<QuestionNumber<<"\n";
f1.open(xyz);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(c=='\n')
count1++;
if(count1>=(QuestionNumber+3))
{
if(count2<QuestionNumber && c!='\n')
{
AnswerTable[count2]=c;
count2++;
} // end of "if"
} // end of "if"
} // end of "while"
f1.close();
count1=0;
count2=0;
f1.open(xyz);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(c=='\n')
count1++;
if(count1>=2 && count1<(QuestionNumber+2))
{
if(count2>2 && c!='\n')
{
Question[p][q]=c;
q++;
} //end of "if"
if(c=='\n')
{
Question[p][q]='\n';
count2=0;
p++;
q=0;
} // end of "if"
if(c=='\t')
count2++;
} // end of "if"
} // end of "while" Loop
Question[p][q]='\n';
f1.close();
cout<<"\tAnswer Only By 'T' or 'F' \n";
for(int i=1;i<=QuestionNumber;i++)
{
cout<<"\t Question "<<i<<" is :\n\t\t\t";
for(int j=0;j<100;j++)
{
if(Question[i][j]=='\n')
{
j=100;
}
// end of "if"
else
{
cout<<Question[i][j];
} // end of "else"
}
// end of "for" Loop
if(i!=0)
{
cout<<"\t Your Answer Is:";
cin>>YourAnswer[i];
}
// end of "if"
int CorrectCount=0;
ofstream UserF(username,ios::out);
for(i=0;i<QuestionNumber;i++)
{
if(toupper(AnswerTable[i])==toupper(YourAnswer[i+1]))
{
CorrectCount++;
UserF<<i+1<<"\t"<<"1"<<"\n";
} // end of "if"
else
UserF<<i+1<<"\t"<<"0"<<"\n";
} // end of "for" Loop
cout<<"\n\tYou have answered "<<CorrectCount<<" correct answers and
"<<QuestionNumber-CorrectCount<<" wrong anwers.";
}
/////////////////////////////////////////////////////end of Function
"NewTest"///////////////////////////////////////////
void SecondTest(char *src,char *username)
{
int z=0,count1=0,count2=0,p=0,q=0;
int QuestionNumber=0;
char AnswerTable[100],Question[100][100],YourAnswer[100];
ifstream f1;
f.open(xyz);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(c=='\n')
QuestionNumber++;
} // end of "while" Loop
QuestionNumber=(QuestionNumber-3)/2;
f1.close();
cout<<QuestionNumber<<"\n";
f1.open(xyz);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(c=='\n')
count1++;
if(count1>=(QuestionNumber+3))
{
if(count2<QuestionNumber && c!='\n')
{
AnswerTable[count2]=c;
count2++;
} //end of "if"
} // end of "if"
} // end of "while" Loop
f1.close();
count1=0;
count2=0;
f1.open(xyz);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(c=='\n')
count1++;
if(count1>=2 && count1<(QuestionNumber+2))
{
if(count2>2 && c!='\n')
{
Question[p][q]=c;
q++;
} // end of "if"
if(c=='\n'){
Question[p][q]='\n';
count2=0;
p++;
q=0;
} // end of "if"
if(c=='\t')
count2++;
} // end of "if"
} // end of "while" Loop
Question[p][q]='\n';
f1.close();
int History1=0,History2=1;
char HistoryAnswer[100];
f1.open(username);
if(!f1)
{
cerr<<"Error.\n";
exit(1);
} // end of "if"
while(f1)
{
f1.get(c);
if(History1==1)
{
HistoryAnswer[History2]=c;
History2++;
History1=0;
} // end of "if"
if(c=='\t')
History1++;
} // end of "while" Loop
f1.close();
int y=0;
for(int i=0;i<QuestionNumber;i++)
{
if(HistoryAnswer[i]=='0')
y=1;
} // end of "for" Loop
if(y!=0)
{
cout<<"\tAnswer Only By 'T' or 'F' \n";
int r=0;
for(int i=1;i<=QuestionNumber;i++)
{
if(HistoryAnswer[i]=='0')
{
cout<<"\t Question "<<i<<" is :\n\t\t\t";
for(int j=0;j<100;j++)
{
if(Question[i][j]=='\n')
{
cout<<'\n';
j=100;
} //end of "if"
else
{
cout<<Question[i][j];
} // end of "else"
} // end of "for" Loop
cout<<"\t Your Answer Is:";
cin>>YourAnswer[i];
r++;
} // end of "if"
} // end of "for" Loop
int CorrectCount=0;
for(i=0;i<r;i++)
{
if(toupper(AnswerTable[i])==toupper(YourAnswer[i+1]))
{
CorrectCount++;
} // end of "if"
} // end of "for" Loop
cout<<"\n\tYou have answered "<<CorrectCount<<" correct answers and "<<r-
CorrectCount<<" wrong answers.";
ofstream UserF("temp",ios::out);
for(i=1;i<=QuestionNumber;i++)
{
if(HistoryAnswer[i]=='1')
UserF<<i<<"\t"<<"1"<<"\n";
else
{
if(toupper(AnswerTable[i])==toupper(YourAnswer[i+1]))
{
UserF<<i<<"\t"<<"1"<<"\n";
} // end of "if"
else
{
UserF<<i<<"\t"<<"0"<<"\n";
} // end of "else"
} // end of "else"
UserF.close();
remove(username);
rename("temp",username);
} // end of "for" Loop
else
{
cout<<"\n\tAll your answers are right\n";
} // end of "else"
}
//////////////////////////////////////////////////end of Function
"SecondTest" ///////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct question
{
int linenbr;
int level;
char *ptr;
char answer;
};
struct studans
{
int questionnbr;
int answer;
};
int main()
{
int noofq=0;
char *ques=new char;
studans std[50];
int nbans=-1;
//------------ asking the student for the ID ----------------------
char filename[20];
for(int y=0;y<20;y++)
{
filename[y]='\0';
}
cout<<"Please enter ID"<<endl;
cin>>filename;
int t=0;
while(t<20)
{
if(filename[t]!='\0')
{
t++;
}
else
{
filename[t]='.';
t++;
filename[t]='d';
t++;
filename[t]='a';
t++;
filename[t]='t';
break;
}
}
//-----------------------Searching and getting input from students record
file------------
ifstream input1;
input1.open(filename);
if(!input1)
{
ofstream outdata;
outdata.open(filename);
}
else
{
while(!input1.eof())
{
nbans+=1;
input1>>std[nbans].questionnbr>>std[nbans].answer;
}
nbans-=1;
}
//-----------------------Getting input from the question file
-----------------------------
question q[100];
ifstream input;
input.open("questions.dat");
input>>noofq;
input.getline(ques,10,'\n');
input.getline(ques,10,'\n');
for(int i=0;i<noofq;i++)
{
input>>q[i].linenbr;
input>>q[i].level;
q[i].ptr=new char[100];
input.getline(q[i].ptr,100,'\n');
}
input.getline(ques,10,'\n');
for(int k=0;k<noofq;k++)
{
input>>q[k].answer;
}
//------------------------Student solving the exam
-------------------------------
int *anschk;
anschk=new int[noofq];
for(int k2=0;k2<noofq;k2++)
{
anschk[k2]=0;
}
char ans;
int random=0;
int chek=0;
for(int k1=0;k1<noofq;)
{
random=rand()%noofq;
if(anschk[random]==0)
{
chek=1;
anschk[random]=1;
k1++;
}
for(int k3=0;k3<=nbans;k3++)
{
if(std[k3].questionnbr==random+1 && std[k3].answer==1)
{
chek=2;
}
}
if(chek==1)
{
cout<<q[random].ptr<<endl;
cout<<"Please Answer T for true and F for false"<<endl;
cin>>ans;
if((ans=='T'||ans=='t') && q[random].answer=='T')
{
nbans+=1;
std[nbans].questionnbr=random+1;
std[nbans].answer=1;
}
if((ans=='F'||ans=='f') && q[random].answer=='F')
{
nbans+=1;
std[nbans].questionnbr=random+1;
std[nbans].answer=1;
}
if((ans=='T'||ans=='t') && q[random].answer=='F')
{
nbans+=1;
std[nbans].questionnbr=random+1;
std[nbans].answer=0;
}
if((ans=='F'||ans=='f') && q[random].answer=='T')
{
nbans+=1;
std[nbans].questionnbr=random+1;
std[nbans].answer=0;
}
chek=2;
}
}
ofstream write1;
write1.open(filename);
for(int k4=0;k4<=nbans;k4++)
{
write1<<std[k4].questionnbr<<' '<<std[k4].answer<<endl;
}
cout<<"End of Exam GOOD LUCK"<<endl;
//----------------------------------------------------------------------
//-------------------------------deleting
pointers-------------------------
for (int j=0;j<noofq;j++)
{
delete q[j].ptr;
q[j].ptr=NULL;
}
//-----------------------------------------------------------------------
return 0;
}
10
QUESTIONS
1 2 Amman is the capital of Jordan.
2 1 Zahle is the capital of Lebanon.
3 1 Paris is the capital of France.
4 2 The Nile river crosses Sudan from South to North.
5 3 Lebanon has 3 different countrys on his borders.
6 3 The first book was invited in Lebanon.
7 2 China is the fourth biggest country.
8 4 22 of November is the independance day for Lebanon.
9 2 London is the capital of Britain.
10 3 Ferrara is in Italy.
ANSWERS
T
F
T
T
F
T
F
T
T
T