-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
168 lines (132 loc) · 3.39 KB
/
main.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <climits>
#include <unistd.h>
#include <getopt.h>
using namespace std;
int main(int argc, char* argv[]){
const char *optstring = "vi:o:h";
int c;
static const struct option opts[] = {
{"version", 0, NULL, 'v'},
{"input", 1, NULL, 'i'},
{"help", 0, NULL, 'h'},
{"output", 1, NULL, 'o'}
};
char* file_path = NULL;
char* output_path = NULL;
string command(argv[1]);
argc--;
argv++;
while((c = getopt_long(argc, argv, optstring, opts, NULL)) != -1) {
switch(c) {
case 'i':
printf("Input: %s\n", optarg);
file_path = optarg;
break;
case 'v':
printf("version: 0.0.1\n");
break;
case 'h':
printf("help:\n");
printf(" -h: help\n");
printf(" -v: version\n");
printf(" -i: input file path\n");
printf(" -o: ioutput file path\n");
break;
case 'o':
printf("output: %s\n", optarg);
output_path = optarg;
break;
case '?':
printf("unknown option\n");
return 1;
break;
case 0 :
printf("the return val is 0\n");
break;
default:
printf("------\n");
}
}
if(argc <= 1)
{
cout << "please input Data." << endl;
return 1;
}
/*read file from input path*/
ifstream file;
file.open(file_path);
if(file.fail())
{
cout << "File failed to open." << endl;
return 1;
}
cerr << "read file form " << file_path << endl;
stringstream buffer;
buffer << file.rdbuf();
string file_contents;
file_contents = buffer.str();
file.close();
if(command=="dna"){
cout << argc << argv[1] << endl;
/* compute DNA string ... */
int adenine = 0, thymine = 0, guanine = 0, cytosine = 0;
char genome;
for(int i = 0; i < file_contents.size(); i++)
{
genome = file_contents[i];
switch (genome) {
case 'A':
adenine++;
break;
case 'T':
thymine++;
break;
case 'G':
guanine++;
break;
case 'C':
cytosine++;
break;
default:
break;
}
}
cout << "A = " << adenine << endl;
cout << "C = " << cytosine << endl;
cout << "G = " << guanine << endl;
cout << "T = " << thymine << endl;
/*write output*/
ofstream fileOut;
if(output_path == NULL){
char output[] = "output.txt";
output_path = output;
}
fileOut.open(output_path);
fileOut << adenine << " " << cytosine << " " << guanine << " " << thymine << endl;
fileOut.close();
cerr << "write Result to " << output_path << endl;
} else if(command=="rna") {
/* Transcribing DNA into RNA ... */
for(int i = 0; i < file_contents.size(); i++){
if(file_contents[i] == 'T')
file_contents[i] = 'U';
}
cout << file_contents << endl;
/*write output*/
ofstream fileOut;
if(output_path == NULL){
char output[] = "output.txt";
output_path = output;
}
fileOut.open(output_path);
fileOut << file_contents;
fileOut.close();
cerr << "write Result to " << output_path << endl;
}
return 0;
}