Tuesday, December 2, 2014

Grade Program in C++



Grade1
 
#include 
#include
#include 
typedef int bool;
const bool true = 1;
const bool false = 0;
char printablebool (bool i) 
{
  if (i==false) 
    return 'f';
  else return 't';
}
int main () 
{
  ifstream f1, f2, f3check;
  ofstream f3;
  f1.open("exam");
  if (!f1) 
  {
    cout << "Cannot open file exam" << endl;
    return 1;
  }
  f2.open("key");
  if (!f2) 
   {
    cout << "Cannot open file key" << endl;
    return 1;
  }
  f3check.open("result");
  if (f3check) 
  {
    cout << "File result already exists" << endl;
    return 1;
  }
  f3.open("result");
  if (!f3) 
  {
    cout << "Cannot open file result" << endl;
    return 1;
  }
  int runningSum;
  int ans, key, score;
  runningSum = 0;
  f1 >> ans;
  if (!f1)
  {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
  {
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key)
 {
    runningSum = runningSum + 25;
    score = true;
  }
  else score = false;
  f3 << printablebool(score) << endl;
  if (!f3) 
  {
    cout << "Error writing to file result" << endl;
    return 1;
  }
  f1 >> ans;
  if (!f1) 
  {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
  {
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key) 
  {
    runningSum = runningSum + 25;
    score = true;
  }
  else score = false;
  f3 << printablebool(score) << endl;
  if (!f3) 
  {
    cout << "Error writing to file result" << endl;
    return 1;
  }
  f1 >> ans;
  if (!f1) 
  {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
  {
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key)
   {
    runningSum = runningSum + 25;
    score = true;
  }
  else score = false;
  f3 << printablebool(score) << endl;
  if (!f3) 
 {
    cout << "Error writing to file result" << endl;
    return 1;
  }
  f1 >> ans;
  if (!f1) 
 {
    cout << "Error reading from file exam" << endl;
    return 1;
  }
  f2 >> key; 
  if (!f1) 
 {
    cout << "Error reading from file key" << endl;
    return 1;
  }
  if (ans == key) 
  {
    runningSum = runningSum + 25;
    score = true;
  }
  else score = false;
  f3 << printablebool(score) << endl;
  if (!f3) {
    cout << "Error writing to file result" << endl;
    return 1;
  }
  f3 << endl << runningSum << endl;  
 }
 
 Grade2
 
#include 
#include 
typedef int bool;
const bool true = 1;
const bool false = 0;
char printablebool (bool i) 
{
  if (i==false) 
  {
    return 'F';
  }
  else 
  {
    return 'T';
  }
}
int main () 
 {
           ifstream f1, f2; 
  ofstream f3;
  f1.open("exam");
  f2.open("key");
  f3.open("result");
  int runningSum;
  int ans, key, score;
  runningSum = 0;
  f1 >> ans;
  f2 >> key;
  while (f1) 
 {
    if (ans == key) 
    {
      runningSum = runningSum + 25;
      score = true;
    }
    else 
   {
      score = false;
    }
    f3 << printablebool (score) << endl;
    f1 >> ans;
    f2 >> key;
  }
  f3 << endl << runningSum << endl;  
}

No comments:

Post a Comment