加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

26个英文字母对应数字的奇妙意义(C/C++)

(2017-12-07 21:40:39)
分类: ChangeTheWorldByProgram
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <string>
#include <map>

#define IS_UPPER(x) (x>='A' && x<='Z')//判断是否为大写。
#define IS_LOWER(x) (x>='a' && x<='z')//判断是否为小写。

using namespace std;



inline int Get_words(charwords)
{
    cout << "please enter words(english)!" << endl;
    fgets(words, 100stdin);
    //stdin表示从键盘输入而不是从文件
    //printf("%s", a);//这里%s后没有\n,但输出是有回车的
    return strlen(words);
}


void Case_num(intsum, const charwords, const map<intchar>& num_upper, const map<intchar>& num_lower)
{

    for (int 0strlen(words); ++j)
    {
        //cout << words[j] << endl;
        //Nonum_lower.find()
        if (IS_UPPER(words[j]))
        {
            for (map<intchar>::const_iterator it_upper num_upper.begin(); it_upper != num_upper.end(); it_upper++)
            {
                if (it_upper->second == words[j])
                    sum += it_upper->first;
            }

        }
        if (IS_LOWER(words[j]))
        {
            for (map<intchar>::const_iterator it_lower num_lower.begin(); it_lower != num_lower.end(); it_lower++)
            {
                if (it_lower->second == words[j])
                {
                    sum += it_lower->first;
                }
            }
        }

    }

}
//C面向过程的实现
int main01()
{

    //1 保存数字和字母对应的值
    map<intcharNonum_upper;
    map<intcharNonum_lower;
    char words[100];
    int sum 0;


    intnew int[26];
    for (int 026i++)
    {
        a[i] 1;
        //printf("%d\t",a[i]);
    }


    string str "abcdefghijklmnopqrstuvwxyz";
    const char lowerLetter[] "abcdefghijklmnopqrstuvwxyz";
    const char upperLetter[] "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int 026i++)
    {
        Nonum_upper[i 1upperLetter[i];
        Nonum_lower[i 1lowerLetter[i];
    }


    //2 从屏幕获取要计算的单词
    
        bool out false;
        while (true)
        {
            Get_words(words);
            string words;
            if (s == "break\n"out true;
            if (out) break;
            //获取大小写字母对应的数字并相加
            Case_num(sum, words, Nonum_upper, Nonum_lower);
            cout << words " << words <<     " << "the number is " << sum << "%" << endl;
            sum 0;
        }
    
        cout << Thank you using the system!\n" << "Have nice day!\n" << ::endl;

    system("pause");



    return 0;
}

class vocab
{
private:
    //数字和大小写字母关联
    map<intcharnum_upper;
    map<intcharnum_lower;
    //存放从外设中得到的单词
    char vocabulary[100];

    int sum;//最后的计算结果
    const int num;//26个字母
    const charlowerLetter;
    const charupperLetter;


public:
    //从键盘得到词汇
    int Case_letter(const charwords);
    inline int Get_vocab(charwords);
    friend int Get_sum(vocab& vc);
    inline friend void Clear_sum( vocab* vc);
    int Get_Sum();
    void map_letter();
    vocab();
    ~vocab();



};

vocab::vocab() :sum(0), num(26)
{
    lowerLetter "abcdefghijklmnopqrstuvwxyz";
    upperLetter "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int 0this->num; i++)
    {
        num_upper[i 1upperLetter[i];
        num_lower[i 1lowerLetter[i];
    }
}

vocab::~vocab()
{
    lowerLetter NULL;
    upperLetter NULL;

}

int vocab::Case_letter(const charwords)
{
    for (int 0strlen(words); ++j)
    {
        //cout << words[j] << endl;
        //Nonum_lower.find()
        if (IS_UPPER(words[j]))
        {
            for (map<intchar>::const_iterator it_upper num_upper.begin(); it_upper != num_upper.end(); it_upper++)
            {
                if (it_upper->second == words[j])
                    sum += it_upper->first;
            }
        }
        if (IS_LOWER(words[j]))
        {
            for (map<intchar>::const_iterator it_lower num_lower.begin(); it_lower != num_lower.end(); it_lower++)
            {
                if (it_lower->second == words[j])
                {
                    sum += it_lower->first;
                }
            }
        }

    }

    return sum;

}
inline int vocab::Get_vocab(charwords)
{
    cout << "please enter words(english)!" << endl;
    fgets(words, 100stdin);//stdin表示从键盘输入而不是从文件
    //printf("%s", words);//这里%s后没有\n,但输出是有回车的
    return strlen(words);
}



int Get_sum(vocab& vc)
{
    return vc.sum;
}

int vocab::Get_Sum()
{
    return sum;
}
inline void Clear_sum(vocab* vc)
{
    vc->sum 0;
}

//C++面向对象的实现
int main()
{
    char voc[100];
    vocab vo;
    bool out false;
    while (true)
          
        vo.Get_vocab(voc);
        string voc;
        if (s == "break\n"out true;
        if (out) break;

        cout << voc << is " << vo.Case_letter(voc) << "% " << endl;
        Clear_sum(&vo);
    }
    //printf("%s is %d%\n", voc, Get_sum(vo));
    //printf("%s is %d%\n", voc, vo.Get_Sum());
    cout << Thank you using the system!\n" << "Have nice day!\n" << ::endl;
    return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有