C++中文件的操作
#include <iostream> #include <fstream> using namespace std; int main() { char s[27],m[27]; int a[27]; int i,j,sum; ifstream infile1,infile2; //定义输入文件 ofstream outfile; //定义输出文件 infile1.open("file1.txt"); infile2.open("file2.txt"); outfile.open("file3.txt"); for (i=0; i<26; i++) { infile1>>s[i]; } for (i=0; i<26; i++) { infile2>>a[i]; } cout<<"输入"<<endl; for (j=0; j<18; j++) { cin>>m[j]; for (i=0; i<26; i++) { if (s[i]==m[j]) { sum+=a[i]; } } } cout<<sum<<endl; outfile<<sum; infile1.close(); infile2.close(); outfile.close(); }
如果令A,B,C,D,……,X,Y,Z这26个英文字母,分别等于百分之1,2,……,24,25,26个数值,那么我们就能得出如下有趣的结论:
HARD WORD 8+1+18+4+23+15+18+11=98%
KNOWLEDGE 96%
LOVE 54% LUCK 47%
计算一下MONEY STUDY ATTITUDE