我正在嘗試撰寫一個程式,允許用戶通過使用名稱、原子序數或符號進行搜索來搜索元素周期表上的任何元素。目前名稱和原子序數作業正常,但是當我嘗試復制和粘貼符號的代碼時,它突然不起作用。甚至在 cout 中進行硬編碼 << "Hello World!"; 不會顯示。有小費嗎?
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
std::vector<std::string> readAtomicNumber(std::string file_name,std::string search_term);
std::vector<std::string> readName(std::string file_name,std::string search_term);
std::vector<std::string> readSymbol(std::string file_name,std::string search_term);
int main()
{
int searchchoice;
int AtomicNumber;
string Name;
string Symbol;
string term;
cout << "How will you be searching?\nYou may search using;\n1 = Element Name\n2 = Element Symbol\n3 = Atomic Number\n4 = Show me the entire Periodic Table.\n\n";
cin >> searchchoice;
if(searchchoice == 4)
{
//something here to cout the entire periodic table
}
else if(searchchoice == 3)
{
cout << "\n\nWhat is the Atomic Number of the Element you are searching for?\n";
cin >> term;
}
else if(searchchoice == 2)
{
cout << "\n\nWhat is the Symbol of the Element you are searching for?\n"; // going to need something to return
cin >> term; // "that is not a name/atomic number etc"
} // incase of a false input
else if(searchchoice == 1)
{
cout << "\n\nWhat is the Name of the Element you are searching for?\n";
cin >> term;
}
else
{
cout << "\n\nError. Please re-run the program, and input 1, 2, or 3.\n";
return 0;
}
if(searchchoice == 3)
{
std::vector<std::string> data = readAtomicNumber("PeriodicTableupdated",term); //atomic number
}
else if(searchchoice == 2)
{
std::vector<std::string> data = readSymbol("PeriodicTableupdated",term); //symbol
}
else if(searchchoice == 1)
{
std::vector<std::string> data = readName("PeriodicTableupdated",term); //name
}
return 0;
}
std::vector<std::string> readAtomicNumber(std::string file_name,std::string search_term) //READ ATOMIC NUMBER
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while(getline(file,field_one,',') && !found_record)
{
getline(file,field_two,',');
getline(file,field_three,',');
getline(file,field_four,'\n');
if(field_one == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[1] << "\nAtomic Number:\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[0] << "\t\t" << record[1] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
std::vector<std::string> readName(std::string file_name,std::string search_term) // READ NAME
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while(getline(file,field_two,',') && !found_record)
{
getline(file,field_one,',');
getline(file,field_three,',');
getline(file,field_four,'\n');
if(field_one == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[0] << "\nAtomic Number:\t\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[1] << "\t\t\t" << record[0] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
std::vector<std::string> readSymbol(std::string file_name,std::string search_term) // READ SYMBOL
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while(getline(file,field_three,',') && !found_record)
{
getline(file,field_one,',');
getline(file,field_two,',');
getline(file,field_four,'\n');
if(field_three == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[2] << "\nAtomic Number:\t\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[0] << "\t\t\t" << record[1] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
我使用的周期表 csv 有 118 個元素,因此我將為此目的只包含前 10 個元素。
AtomicNumber,Element,Symbol,AtomicMass
1,Hydrogen,H,1.007
2,Helium,He,4.002
3,Lithium,Li,6.941
4,Beryllium,Be,9.012
5,Boron,B,10.811
6,Carbon,C,12.011
7,Nitrogen,N,14.007
8,Oxygen,O,15.999
9,Fluorine,F,18.998
10,Neon,Ne,20.18
uj5u.com熱心網友回復:
我強烈建議不要使用并行陣列,而是使用對資料(記錄)建模的結構向量。讓我們將記錄定義為一個文本行上的資料,減去分隔符。
struct Element
{
unsigned int atomic_number;
std::string name;
std::string symbol;
double atomic_mass;
friend std::istream& operator>>(std::istream& input, Element& e);
};
std::istream& operator>>(std::istream& input, Element& e)
{
std::string text_record;
std::getline(input, text_record);
std::istringstream text_stream(text_record);
text_stream >> e.atomic_number;
char comma;
text_stream >> comma;
std::getline(text_stream, e.name, ',');
std::getline(text_stream, e.symbol, ',');
text_stream >> e.atomic_mass;
return input;
};
上面的代碼對 Element 進行建模并多載operator>>以讀取Element.
您的資料輸入可能如下所示:
std::vector<Element> database;
Element e;
//... open file.
while (file >> e)
{
database.push_back(e); // Append to database;
}
然后,您可以通過蠻力搜索資料庫。
編輯 1:索引表
索引表是按關鍵成員排序并包含到資料庫的鏈接或偏移量的表。這將允許您按關鍵字進行搜索,而不必求助于資料庫。
我們將需要一個索引表,按名稱進行比較:
std::map<string, unsigned int> index_by_name;
這是對輸入的修改:
unsigned int db_index = 0;
while (file >> e)
{
database.push_back(e);
index_by_name[e.name] = db_index;
db_index;
}
因此,要在資料庫中搜索鈉,您可以執行以下操作:
unsigned int sodium_index = index_by_name.at("Sodium");
Element sodium_element = database.at(sodium_index);
如果沒有索引表,您將不得不進行線性搜索,或者對資料庫進行排序然后進行二分搜索(或其他基于排序內容的搜索)。
uj5u.com熱心網友回復:
std::vector<std::string> readSymbol(std::string file_name, std::string search_term) // READ SYMBOL
{
std::vector<std::string> record;
std::ifstream file;
file.open(file_name);
bool found_record = false;
std::string field_one; //atomic number
std::string field_two; // name
std::string field_three; // symbol
std::string field_four;
while (getline(file, field_three, ',') && !found_record)
{
getline(file, field_one, ',');
getline(file, field_two, ',');
getline(file, field_four, '\n');
if (field_three == search_term)
{
found_record = true;
record.push_back(field_one);
record.push_back(field_two);
record.push_back(field_three);
record.push_back(field_four);
}
}
std::cout << "\nThat Element is: " << record[2] << "\nAtomic Number:\t\tName:\t\tSymbol:\t\tAtomic Mass:\n" << record[0] << "\t\t\t" << record[1] << "\t" << record[2] << "\t\t" << record[3];
return record;
}
在這部分中,您實際上將第一個字串分配給了,to field_three。
但第一個字串實際上是行號,因為您的檔案結構是:
lineNumber , Name, Symbol, Mass
所以比較(field_three == search_term)實際上lineNumber and Symbol是不合邏輯的比較。
當您除錯時,您會更好地看到變數。
我會在下面放一個截圖。

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382279.html
上一篇:匹配列并洗掉Shell中的重復項
