gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
#include<iostream> #include<fstream> #include<vector> #include<sstream>…
- User
#include #include #include #include #include
using namespace std;
int loc(vector v, string word) {
for (int i = 0; i<v.size();i++) { if (v[i]==word) { return i; } } return -1;}
int main(int count, char** args) {
if (!strcmp("A",args[1])) { ifstream myfile; myfile.open(args[3]); string line; float gate_delay[5]; while(getline(myfile,line)){ stringstream ss(line); string word; ss>>word; if (word == "AND2") { ss>>word; gate_delay[0] = stof(word); } else if (word == "NAND2") { ss>>word; gate_delay[1] = stof(word); } else if (word == "OR2") { ss>>word; gate_delay[2] = stof(word); } else if (word == "NOR2") { ss>>word; gate_delay[3] = stof(word); } else if (word == "INV") { ss>>word; gate_delay[4] = stof(word); } }; myfile.close(); vector<string> signame; vector<float> sigtime; vector<int> sigtype; myfile.open(args[2]); while(getline(myfile,line)) { stringstream ss(line); string word; ss>>word; if (word=="PRIMARY_INPUTS") { while(ss>>word) { signame.push_back(word); sigtype.push_back(0); sigtime.push_back(0); } } else if (word=="PRIMARY_OUTPUTS") { while(ss>>word) { signame.push_back(word); sigtype.push_back(-2); sigtime.push_back(0); } } else if (word=="INTERNAL_SIGNALS") { while(ss>>word) { signame.push_back(word); sigtype.push_back(-1); sigtime.push_back(0); } } } myfile.close(); bool done = false; while(!done) { myfile.open(args[2]); while(getline(myfile,line)) { stringstream ss(line); string word; string s1,s2,s3; int n1,n2,n3; ss>>word; if (word=="AND2") { ss>>s1; ss>>s2; ss>>s3; n1 = loc(signame,s1); n2 = loc(signame,s2); n3 = loc(signame,s3); if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) { sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[0]; sigtype[n3] = -sigtype[n3]; } } if (word=="NAND2") { ss>>s1; ss>>s2; ss>>s3; n1 = loc(signame,s1); n2 = loc(signame,s2); n3 = loc(signame,s3); if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) { sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[1]; sigtype[n3] = -sigtype[n3]; } } if (word=="OR2") { ss>>s1; ss>>s2; ss>>s3; n1 = loc(signame,s1); n2 = loc(signame,s2); n3 = loc(signame,s3); if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) { sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[2]; sigtype[n3] = -sigtype[n3]; } } if (word=="NOR2") { ss>>s1; ss>>s2; ss>>s3; n1 = loc(signame,s1); n2 = loc(signame,s2); n3 = loc(signame,s3); if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) { sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[3]; sigtype[n3] = -sigtype[n3]; } } if (word=="INV") { ss>>s1; ss>>s2; n1 = loc(signame,s1); n2 = loc(signame,s2); if (sigtype[n1] >= 0 && sigtype[n2] < 0) { sigtime[n2] = (sigtime[n1]) + gate_delay[4]; sigtype[n2] = -sigtype[n2]; } } } myfile.close(); done = true; for (int i = 0; i < signame.size(); i++) { if (sigtype[i]<0) { done = false; break; } } } ofstream outputter; outputter.open("output_delays.txt",ios::trunc); for (int i = 0; i < signame.size(); i++) { if (sigtype[i]==2) { outputter<<signame[i]<<" "<<sigtime[i]<<endl; } } outputter.close(); } else if (!strcmp("B",args[1])) { vector<string> gates = {"AND2","NAND2","OR2","NOR2","INV"}; ifstream myfile; myfile.open(args[3]); string line; float gate_delay[5]; while(getline(myfile,line)){ stringstream ss(line); string word; ss>>word; if (word == "AND2") { ss>>word; gate_delay[0] = stof(word); } else if (word == "NAND2") { ss>>word; gate_delay[1] = stof(word); } else if (word == "OR2") { ss>>word; gate_delay[2] = stof(word); } else if (word == "NOR2") { ss>>word; gate_delay[3] = stof(word); } else if (word == "INV") { ss>>word; gate_delay[4] = stof(word); } }; myfile.close(); vector<string> signame; vector<string> inputs; vector<float> sigtime; vector<int> sigtype; myfile.open(args[2]); while(getline(myfile,line)) { stringstream ss(line); string word; ss>>word; if (word=="PRIMARY_INPUTS") { while(ss>>word) { signame.push_back(word); inputs.push_back(word); sigtype.push_back(1); sigtime.push_back(0); } } else if (word=="PRIMARY_OUTPUTS") { while(ss>>word) { signame.push_back(word); sigtype.push_back(0); sigtime.push_back(0); } } else if (word=="INTERNAL_SIGNALS") { while(ss>>word) { signame.push_back(word); sigtype.push_back(1); sigtime.push_back(0); } } } myfile.close(); myfile.open(args[4]); float m; while(getline(myfile,line)) { stringstream ss(line); string word; string s1; while(ss>>word) { int n1 = loc(signame,word); if (n1>=0) { ss>>s1; sigtime[n1] = stof(s1); m = max(m,stof(s1)); } } } for(int i = 0; i < sigtime.size(); i++) { if (sigtime[i] == 0) { sigtime[i] = m; } } myfile.close(); bool done = false; while(!done) { myfile.open(args[2]); while(getline(myfile,line)) { stringstream ss(line); string word; string s1,s2,s3; int n1,n2,n3; ss>>word; if (word=="AND2" || word=="NAND2" || word == "OR2" || word == "NOR2") { ss>>s1; ss>>s2; ss>>s3; n1 = loc(signame,s1); n2 = loc(signame,s2); n3 = loc(signame,s3); sigtime[n1] = min(sigtime[n1],sigtime[n3]-gate_delay[loc(gates,word)]); sigtime[n2] = min(sigtime[n2],sigtime[n3]-gate_delay[loc(gates,word)]); sigtype[n1]+=sigtype[n3]; sigtype[n2]+=sigtype[n3]; } else if (word == "INV") { ss>>s1; ss>>s2; n1 = loc(signame,s1); n2 = loc(signame,s2); sigtime[n1] = min(sigtime[n1],sigtime[n2]-gate_delay[loc(gates,word)]); sigtype[n1]+=sigtype[n2]; } } for(int i = 0; i < signame.size(); i++) { if (sigtype[i]==1) { sigtype[i] = 0; } if (sigtype[i] > 1) { sigtype[i] = 1; sigtime[i] = m; } } myfile.close(); done = true; for (int i = 0; i < signame.size(); i++) { if (sigtype[i]!=0) { done = false; break; } } } ofstream outputter; outputter.open("input_delays.txt", ios::trunc); for (string word : inputs) { outputter<<word<<" "<<sigtime[loc(signame,word)]<<endl; } outputter.close(); }} change this code in python.
- Assistant
Here is the equivalent code in Python:
import sys def loc(v, word): for i in range(len(v)): if v[i] == word: return i return -1 if sys.argv[1] == "A": file1 = open(sys.argv[3], 'r') gate_delay = [0] * 5 for line in file1: words = line.split() if words[0] == "AND2": gate_delay[0] = float(words[1]) elif words[0] == "NAND2": gate_delay[1] = float(words[1]) elif words[0] == "OR2": gate_delay[2] = float(words[1]) elif words[0] == "NOR2": gate_delay[3] = float(words[1]) elif words[0] == "INV": gate_delay[4] = float(words[1]) file1.close() signame = [] sigtime = [] sigtype = [] file2 = open(sys.argv[2], 'r') for line in file2: words = line.split() if words[0] == "PRIMARY_INPUTS": for word in words[1:]: signame.append(word) sigtype.append(0) sigtime.append(0) elif words[0] == "PRIMARY_OUTPUTS": for word in words[1:]: signame.append(word) sigtype.append(-2) sigtime.append(0) elif words[0] == "INTERNAL_SIGNALS": for word in words[1:]: signame.append(word) sigtype.append(-1) sigtime.append(0) file2.close() done = False while not done: file2 = open(sys.argv[2], 'r') for line in file2: words = line.split() if words[0] == "AND2": s1 = words[1] s2 = words[2] s3 = words[3] n1 = loc(signame, s1) n2 = loc(signame, s2) n3 = loc(signame, s3) if sigtype[n1] >= 0 and sigtype[n2] >= 0 and sigtype[n3] < 0: sigtime[n3] = max(sigtime[n1], sigtime[n2]) + gate_delay[0] sigtype[n3] = -sigtype[n3] if words[0] == "NAND2": s1 = words[1] s2 = words[2] s3 = words[3] n1 = loc(signame, s1) n2 = loc(signame, s2) n3 = loc(signame, s3) if sigtype[n1] >= 0 and sigtype[n2] >= 0 and sigtype[n3] < 0: sigtime[n3] = max(sigtime[n1], sigtime[n2]) + gate_delay[1] sigtype[n3] = -sigtype[n3] if words[0] == "OR2": s1 = words[1] s2 = words[2] s3 = words[3] n1 = loc(signame, s1) n2 = loc(signame, s2) n3 = loc(signame, s3) if sigtype[n1] >= 0 and sigtype[n2] >= 0 and sigtype[n3] < 0: sigtime[n3] = max(sigtime[n1], sigtime[n2]) + gate_delay[2] sigtype[n3] = -sigtype[n3] if words[0] == "NOR2": s1 = words[1] s2 = words[2] s3 = words[3] n1 = loc(signame, s1) n2 = loc(signame, s2) n3 = loc(signame, s3) if sigtype[n1] >= 0 and sigtype[n2] >= 0 and sigtype[n3] < 0: sigtime[n3] = max(sigtime[n1], sigtime[n2]) + gate_delay[3] sigtype[n3] = -sigtype[n3] if words[0] == "INV": s1 = words[1] s2 = words[2] n1 = loc(signame, s1) n2 = loc(signame, s2) if sigtype[n1] >= 0 and sigtype[n2] < 0: sigtime[n2] = sigtime[n1] + gate_delay[4] sigtype[n2] = -sigtype[n2] file2.close() done = all(sig < 0 for sig in sigtype) outputter = open("output_delays.txt", 'w') for i in range(len(signame)): if sigtype[i] == 2: outputter.write(signame[i] + " " + str(sigtime[i]) + "\n") outputter.close() elif sys.argv[1] == "B": gates = ["AND2", "NAND2", "OR2", "NOR2", "INV"] file1 = open(sys.argv[3], 'r') gate_delay = [0] * 5 for line in file1: words = line.split() if words[0] == "AND2": gate_delay[0] = float(words[1]) elif words[0] == "NAND2": gate_delay[1] = float(words[1]) elif words[0] == "OR2": gate_delay[2] = float(words[1]) elif words[0] == "NOR2": gate_delay[3] = float(words[1]) elif words[0] == "INV": gate_delay[4] = float(words[1]) file1.close() signame = [] inputs = [] sigtime = [] sigtype = [] file2 = open(sys.argv[2], 'r') for line in file2: words = line.split() if words[0] == "PRIMARY_INPUTS": for word in words[1:]: signame.append(word) inputs.append(word) sigtype.append(1) sigtime.append(0) elif words[0] == "PRIMARY_OUTPUTS": for word in words[1:]: signame.append(word) sigtype.append(0) sigtime.append(0) elif words[0] == "INTERNAL_SIGNALS": for word in words[1:]: signame.append(word) sigtype.append(1) sigtime.append(0) file2.close() file3 = open(sys.argv[4], 'r') m = 0 for line in file3: words = line.split() for word in words: n1 = loc(signame, word) if n1 >= 0: sigtime[n1] = float(words[1]) m = max(m, float(words[1])) for i in range(len(sigtime)): if sigtime[i] == 0: sigtime[i] = m file3.close() done = False while not done: file2 = open(sys.argv[2], 'r') for line in file2: words = line.split() if words[0] == "AND2" or words[0] == "NAND2" or words[0] == "OR2" or words[0] == "NOR2": s1 = words[1] s2 = words[2] s3 = words[3] n1 = loc(signame, s1) n2 = loc(signame, s2