//***************************************
// author: Guanshan Yu
// hw6 problem 1 varification file
//
// This verification algorithm assumes that indexing starts at 1.
// That is, the first item on the list is item 1, the second is
// item 2, etc. Notice that this may not correspond to the index
// of the item in an array, so be careful!
//
// The total amount of 100,000,000 is hard-coded into this algorithm,
// but the input list and list of items to take are read in from
// the files as listed below.
//
//
// required files: Prob1.dat, Prob1.out
//***************************************
#include <stdlib.h>
#include <fstream>
#include <iostream>
using namespace std;
int main(){
char stat[256];
int file_size, result_size, in_num;
FILE *source = fopen("Prob1.dat","r");
FILE *result = fopen("Prob1.out","r");
int *itemlist;
int totalcost = 0;
int lot = 100000000;
fscanf(source,"%s",stat);
in_num = atoi(stat);
file_size = in_num;
itemlist = new int[file_size+1];
fscanf(result,"%s",stat);
in_num = atoi(stat);
result_size = in_num;
//store sourcelist
for(int i=1; i<=file_size; i++){
fscanf(source,"%s",stat);
in_num = atoi(stat);
itemlist[i] = in_num;
}
//compute totalcost
for(int i=1; i<=result_size; i++){
fscanf(result,"%s",stat);
in_num = atoi(stat);
totalcost = totalcost + itemlist[in_num];
}
fscanf(result,"%s",stat);
in_num = atoi(stat);
if(lot - totalcost != in_num){
cout<<"incorrect!"<<endl;
cout<<"The cost of all your items are "<<totalcost<<"."<<endl;
cout<<"And you have "<<lot - totalcost<<" left."<<endl;
}else{
cout<<"correct!"<<endl;
cout<<"The cost of all your items are "<<totalcost<<"."<<endl;
cout<<"And you have "<<lot - totalcost<<" left."<<endl;
}
return 0;
}