#include <iostream>
using namespace std;
#include <list>
#include <map>
#include <vector>
#include <fstream>
void getValue(vector<char> &vecc,vector<int> &veci)
{
// int j;
// char c[2];
char cTemp;
int iTemp;
ifstream infile1,infile2;//定義檔案輸出類
infile1.open("file1.txt");
infile2.open("file2.txt");
for (int i=0; i<26; i++) {
infile1>>cTemp;
vecc.push_back(cTemp);
//c[0] = cTemp;
infile2>>iTemp;
veci.push_back(iTemp);
//j = iTemp;
}
infile1.close();
infile2.close();
}
int serch(char c)
//int j;//記錄下标
vector<char> vecc;
vector<int> veci;
getValue(vecc, veci);
map<char, int>maplive;
for (int i=0;i<vecc.size() ; i++) {
maplive.insert(pair<char, int>(vecc.at(i),veci.at(i)));
} //鍵值對存在map中
//補充一個空格的map
maplive.insert(pair<char, int>(' ',0));
map<char,int>::iterator l_it;
l_it = maplive.find(c);
return l_it->second;
// return ;
int main(int argc, const char * argv[])
char num[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int val[26];
val[i] = i+1;
//将資料存儲到檔案中
ofstream onfile1,onfile2;//定義輸出檔案(将資料寫入到檔案中)
onfile1.open("file1.txt");//打開一個輸入檔案file1用來儲存26個英文字母
onfile2.open("file2.txt");//儲存1-26數字
onfile1<<num[i]<<' ';
onfile2<<val[i]<<' ';
onfile1.close();
onfile2.close();
char f1[10];
do {
int sum=0;
char s[50];
string ss;
cout<<"請輸入要計算的字元串"<<endl;
cin.getline(s, 40);
char *p = s;
int j=strlen(p);
for (int i=0; i<j; i++){
sum+=serch(s[i]);
}
cout<<s<<":"<<sum<<"%"<<endl;
cout<<"您還要繼續計算嗎?(Y/N)";
//cin>>f>>f;
cin.getline(f1,10);
}while('Y' == f1[0]);
cout<<"歡迎使用!謝謝!"<<endl;
return 0;
本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366568,如需轉載請自行聯系原作者