好吧,这就是我最终要做的。
“翻译”
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <unistd.h>
std::vector<std::string> sortTerms(int n, char* argv[]) {
std::vector<std::string> sortedTerms (n);
for (int i = 0; i < n; i++) {
sortedTerms[i] = argv[i+1]; // first term argv is program name
}
std::sort(sortedTerms.begin(),sortedTerms.end());
return sortedTerms;
}
std::vector<std::string> splitString(int n,std::string str) {
std::vector<std::string> stringVector (n);
std::istringstream iss(str);
for (int i = 0; i < n; i++)
std::getline(iss, stringVector[i], ' ');
return stringVector;
}
int main(int argc, char** argv) {
const int NUM_TERMS = (argc - 1); // number of words to translate
std::string output[NUM_TERMS][2]; // used to store a translated word alongside the English equivalent
std::string stringBuffer; // used to start dictionary with arguments
std::vector<std::string> stringVector (NUM_TERMS); // used as a buffer
std::ofstream outputFile("translated.txt"); // file to write translations to
const bool VERBOSE = true;
stringBuffer.clear();
stringBuffer.append("./dictionary ");
// Sort English words and load them into output
stringVector = sortTerms(NUM_TERMS, argv);
for (int i = 0; i < NUM_TERMS; i++) {
output[i][0] = stringVector[i];
stringBuffer = stringBuffer.append(stringVector[i]);
stringBuffer = stringBuffer.append(" ");
}
int pipeStatus;
int pipeOutput[2]; // file descriptor
pipeStatus = pipe(pipeOutput); // create output read/write pipe ends
if (pipeStatus < 0) {
std::cerr << "ERROR CREATING PIPE" << std::endl;
exit(1);
}
int pid = 0;
pid = fork();
if (pid == 0) { // dictionary
// Connect the pipes
dup2 (pipeOutput[1],1);
// Execute the program
system(stringBuffer.c_str());
// Close pipes
close(pipeOutput[0]);
close(pipeOutput[1]);
exit(0);
}
else if (pid > 0) { // Original process
char* buffer = new char[1024]; // input buffer
// Receive string from dictionary
read(pipeOutput[0],buffer,1024); // read in from output of dictionary
stringBuffer = buffer; // I'd rather work with a std::string
stringVector = splitString(NUM_TERMS, stringBuffer);
for (int i = 0; i < NUM_TERMS; i++)
output[i][1] = stringVector[i];
// Close pipes
close(pipeOutput[0]);
close(pipeOutput[1]);
if (VERBOSE) {
for (int i = 0; i < NUM_TERMS; i++)
std::cout << output[i][0] << " -> " << output[i][1] << std::endl;
}
// write translationString to file
for (int i = 0; i < NUM_TERMS; i++) {
outputFile.write(output[i][0].c_str(),output[i][0].length());
outputFile.write(" -> ",4);
outputFile.write(output[i][1].c_str(),output[i][1].length());
outputFile.write("\n",1);
}
outputFile.close();
exit(0);
}
else if (pid == -1) {
std::cerr << "ERROR FORKING PROCESS" << std::endl;
exit(1);
}
return 0;
}
“字典”
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
std::vector<std::string> splitString(std::string str)
{
std::vector<std::string> stringVector (2);
std::istringstream iss(str);
std::getline(iss, stringVector[0], ' ');
std::getline(iss, stringVector[1], ' ');
return stringVector;
}
int main(int argc, char* argv[]) {
const int NUM_TERMS = (argc - 1);
std::string stringBuffer;
std::string returnString[NUM_TERMS];
std::vector<std::string> stringVector;
std::ifstream dictionaryFile ("./dictionary.txt");
// There must be at least one arguement
if (argc <= 1)
std::cout << "Nothing to translate..." << std::endl;
for (int i = 0; i < NUM_TERMS; i++) {
while (dictionaryFile) {
getline(dictionaryFile,stringBuffer);
stringVector = splitString(stringBuffer);
if (stringVector[0] == argv[i+1]) { // wut
returnString[i] = stringVector[1];
break;
}
}
}
// clear string buffer
stringBuffer.clear();
// Form translated words string
for (int i = 0; i < NUM_TERMS; i++) {
stringBuffer.append(returnString[i]);
if (i < (NUM_TERMS - 1))
stringBuffer.append(" "); // append a space after each but the last term
}
// print translated words
std::cout << stringBuffer << std::endl;
dictionaryFile.close();
return 0;
}
“字典.txt”
Apple Apfel
Banana Banane
Blackberry Brombeere
Blueberry Heidelbeere
Cherry Kirsche
Fruit Obst
Grape Traube
Lemon Zitrone
Lime Limone
Orange Orange
Peach Pfirsich
Pear Birne
Plum Zwetschge
Raspberry Himbeere
Strawberry Erdbeere
意味着运行$ ./dictionary Apple Orange Strawberry
产生“translated.txt”
Apple -> Apfel
Orange -> Orange
Strawberry -> Erdbeere
在上交之前,我还有一些润色工作要做,但这就是要点。多谢你们!