天天看點

2013-7-18學習作業[有常考面試題]

作業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];

        val[i] = i+1;

    //将資料存儲到檔案中

    ofstream onfile1,onfile2;//定義輸出檔案類(将資料寫入到檔案中)

    onfile1.open("file1.txt");//打開一個輸入檔案file1用來儲存26個大寫的英文字母

    onfile2.open("file2.txt");//打開一個輸入檔案file2用來儲存1-26個數字

        onfile1<<num[i]<<' ';

        onfile2<<val[i]<<' ';

    onfile1.close();

    onfile2.close();

    char f1[10];

    char f='Y';

        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類進行重寫(重要,面試常考)

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;

            if (this->m_data) {

                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;

                this->m_data = new char(strlen(other.m_data)+1);

            //this->m_data = other.m_data;

    //顯示函數

    char *show()

        return this->m_data;

};

    String1 s1("dingxiaowei");

    String1 s2;

    s2 = s1;

    char *p = s2.show();

    cout<<p<<'\n'<<endl;

下一篇: vector