作业1.C++文件流的应用
如果令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
//
// main.cpp
// 2013-7-18作业1
//
// Created by 丁小未 on 13-7-18.
// Copyright (c) 2013年 dingxiaowei. All rights reserved.
//
//如果令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
#include <iostream>
#include <fstream>
using namespace std;
//输入一个字母,然后返回他对应的值
void getValue(char num[26],int n[26])
{
int j;
char c[2];
ifstream infile1,infile2;//定义文件输出类
infile1.open("file1.txt");
infile2.open("file2.txt");
for (int i=0; i<26; i++) {
infile1>>num[i];
infile2>>n[i];
}
infile1.close();
infile2.close();
}
int serch(char c)
{
int j; //记录下标
char num[26];
int n[26];
getValue(num, n);
int i;
for (i=0; i<26; i++) {
if (c == num[i]) {
j=i;
break;
}
}
if(26 == i)
{
return 0;
}
return n[j];
// infile1.close();
// infile2.close();
}
int overridestrlen(const char *p)
{
int i=0;
do {
p++;
i++;
} while (*p!='\0');
return i;
}
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];
for (int i=0; i<26; i++) {
val[i] = i+1;
}
//将数据存储到文件中
ofstream onfile1,onfile2;//定义输出文件类(将数据写入到文件中)
onfile1.open("file1.txt");//打开一个输入文件file1用来保存26个大写的英文字母
onfile2.open("file2.txt");//打开一个输入文件file2用来保存1-26个数字
for (int i=0; i<26; i++) {
onfile1<<num[i]<<' ';
onfile2<<val[i]<<' ';
}
onfile1.close();
onfile2.close();
char f1[10];
char f='Y';
do {
int sum=0;
char s[50];
string ss;
cout<<"请输入要计算的字符串"<<endl;
//cin>>s;
cin.getline(s, 80);
char *p=s;
int j = overridestrlen(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;
}
结果:
请输入要计算的字符串
LOVE
LOVE:54%
作业2.对string类进行重写(重要,面试常考)
#include <iostream>
using namespace std;
class String1
{
char *m_data;
public:
String1(const char *str = NULL) //普通构造函数
{
if (str==NULL) {
m_data = new char[1];
m_data[0] = '\0';
}
else
{
if (m_data) {
delete []m_data;
}
else
{
m_data = new char[strlen(str)];
strcpy(m_data, str);
}
}
}
String1(const String1&other) //拷贝构造函数
{
if (this == &other) {
cout<<"不能复制本身!"<<endl;
}
else if (this->m_data == other.m_data)
{
cout<<"不能复制相同的值"<<endl;
}
else
{
if (this->m_data) {
delete []m_data;
}
else
{
this->m_data = new char[strlen(other.m_data)+1];
strcpy(this->m_data, other.m_data);
}
}
}
~String1()//析构函数
{
if (this->m_data) {
delete []m_data;
}
}
String1 &operator = (const String1&other) //赋值函数(通过重载=运算符)
{
if (this == &other)
{
return *this;
}
else
{
if (m_data) {
delete []m_data;
}
this->m_data = new char(strlen(other.m_data)+1);
strcpy(this->m_data, other.m_data);
//this->m_data = other.m_data;
}
}
//显示函数
char *show()
{
return this->m_data;
}
};
int main(int argc, const char * argv[])
{
String1 s1("dingxiaowei");
String1 s2;
s2 = s1;
char *p = s2.show();
cout<<p<<'\n'<<endl;
return 0;
}
本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366451,如需转载请自行联系原作者