-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (91 loc) · 2.88 KB
/
main.cpp
File metadata and controls
110 lines (91 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// main.cpp
// ds_hw2_array_gcd
//
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <string>
#include "GCD.h"
using namespace std;
GCD Gcd;
bool LineParser(ifstream &fin, long *input);
int main(int argc, char** argv){
clock_t t_cpu_start, t_use_start;
double t_use=0;
t_cpu_start=clock();
//input file
ifstream fin;
fin.open("example", ios::in);
if(!fin){
cout << "Where is the pattern file given by TA ?????" << endl;
exit(-1);
}//input file
bool check;
int total_num=0, correct_num=0, i;
long *out;
long *in1 = new long [ARRAY_MAX];
long *in2 = new long [ARRAY_MAX];
long *ans = new long [ARRAY_MAX];
while(LineParser(fin, in1) && LineParser(fin, in2) && LineParser(fin, ans)){
total_num++;
t_use_start = clock();
//your code
out = Gcd.FindGCD(in1, in2);
//end of your code
t_use += clock()-t_use_start;
//check
check=1;
for(i=0;i<ARRAY_MAX;i++){ if(out[i]!=ans[i]){ check=0; break; } }
if( check ){ correct_num++; }
if( out!=in1 && out!=in2 ){ delete [] out; }//delete out if you allocate new memory to out
}
delete [] in1, in2, ans;
fin.close();
cout<<"Total CPU time : "<<double(clock()-t_cpu_start)/CLOCKS_PER_SEC<<" seconds"<<endl;
cout<<"Your performance : "<<double (t_use/CLOCKS_PER_SEC)<<" seconds"<<endl;
cout<<"Total pass : "<<double (correct_num*100)/total_num<<" percentages of cases"<<endl;
return 0;
}
bool LineParser(ifstream &fin, long *input){
int a, b, i, op_pre=0, x_post, temp, len, size_str;
for(i=0;i<ARRAY_MAX;i++){ input[i] = 0; }//reset
string in_str;
if( !getline(fin, in_str) ){ return false; }
size_str = in_str.size();
while(1){
//find variable x
x_post = in_str.find("x", op_pre);
if( x_post==std::string::npos ){//no more "x" -> only last constant
a=atoi( (in_str.substr(op_pre, size_str-op_pre)).c_str() );
input[0]=a;
break;
}
//coefficient
len = x_post-op_pre;
if( len==0 ){ a=1; }
else if( len==1 && !in_str.compare(op_pre,1,"-") ){ a=-1; }
else { a=atoi( (in_str.substr(op_pre, len)).c_str() ); }
//find next operator
op_pre = in_str.find("+", x_post);
temp = in_str.find("-", x_post);
if( temp<op_pre && temp!=std::string::npos && op_pre!=std::string::npos ){ op_pre=temp; }//"-" before "+"
if( op_pre==std::string::npos && temp!=std::string::npos ){ op_pre=temp; } //only "-"
if( op_pre==std::string::npos ){//no more operator -> only last "x"
len = size_str-x_post;
if( len==1 ){ b=1; }
else { b=atoi( (in_str.substr(x_post+2, len-2)).c_str() ); }
input[b]=a;
break;
}
//power
len = op_pre-x_post;
if( len==1 ){ b=1; }
else { b=atoi( (in_str.substr(x_post+2, len-2)).c_str() ); }
if( !in_str.compare(op_pre,1,"+") ){ op_pre++; }//pass through "+"
input[b]=a;
}
return true;
}