2009年5月21日

struct 与class 的区别

以前一直没有明白struct结构体与class类 的区别:
(1)
     名字不同一个是struct,一个是class
(2)
  默认的访问属性不同 struct是public,class 是private

posted @ 2009-05-21 22:19 弹杯一笑 阅读(202) | 评论 (0)编辑 收藏

2009年5月12日

C/C++: 控制台输入密码, 用*号掩藏

//此代码有一个网友所写

#include 
<iostream>
#include 
<conio.h>

/**
 * 秘密在于conio.h中的getch()从键盘中读取字符时,并不会在屏幕上输出已经输入的字符,
 * 而用一个putch('*')来哄骗,代表已经输入一个字符
 * 怪不得这个头文件要叫conio.h, con的意思就有哄骗,看来就是由此而来.
 
*/


using namespace std;

int main() {
        
char* password;
        
char* passwordConfirm;

        
int length = 4;
        password 
= new char[length + 1];
        passwordConfirm 
= new char[length + 1];

        
char* p = NULL;
        
int count = 0;

        cout 
<< "Input password : ";
        p 
= password;
        count 
= 0;
        
//fflush(stdin);
        while (((*= getch()) != 13&& count < length) {
                
// 这里不是'\n'(10), new line
                
// 而是'\r'(13), reback. 即是按下回车键,好像这个东西是linux的.
                
// 主要是与getch这个函数有关.
                putch('*');
                fflush(stdin);

                p
++;
                count
++;
        }

        password[count] 
= '\0';

        cout 
<< endl << "Confirm the password : ";
        p 
= passwordConfirm;
        count 
= 0;
        
//fflush(stdin);
        while (((*= getch()) != 13&& count < length) {
                putch(
'*');
                fflush(stdin);

                p
++;
                count
++;
        }

        passwordConfirm[count] 
= '\0';

        cout 
<< endl;
        
if (strcmp(password, passwordConfirm) == 0{
                cout 
<< "The password is right." << endl;
                cout 
<< password << endl;
        }
 else {
                cout 
<< "Confirm password fail." << endl;
                cout 
<< password << endl << passwordConfirm << endl;
        }


        
return 0;
}


posted @ 2009-05-12 10:44 弹杯一笑 阅读(776) | 评论 (0)编辑 收藏

2009年4月1日

N皇后问题求解

     摘要: //N皇后问题求解(此处为8皇后)   1#include <iostream>  2#include <cstdio>  3#include <ctime>  4#include <cmath>   ...  阅读全文

posted @ 2009-04-01 23:17 弹杯一笑 阅读(393) | 评论 (0)编辑 收藏

2009年3月30日

如何设计一个范型算法

// essential  c++
向一个容器(比如 vector)中添加数字后,然后随便输入待比较的数字( int a ;),求找出在vector中比a大的数字,比a小的数字,和a相等的数字,或者是a的3倍,5倍等之类的数字的集合.

//开始看下面的代码之前,你是怎么思考的呢?你是不是觉得直接在里面写个函数比较就是了?或者是其他的呢?

 1#include<iostream>
 2#include <vector>
 3#include <string>
 4using namespace std;
 5/*
 6* 单独写出相关的函数  如 比较,倍数之类的,后面调用 。
 7* 此处仅写了小于和大于函数   其他的依次类推。
 8* 注意参数 与指针函数的匹配
 9*/

10bool less_than(int v1,int v2)
11{
12     return v1<v2?true:false;
13}

14bool greater_than(int v1,int v2)
15{
16     return v1 > v2 ? true:false;
17}

18
19vector<int> filter_ver1(const vector<int> &vec,int filter_value,bool(*pred)(int,int))         
20
21//个人觉得这个函数设置得很好,用到函数指针传递相关的函数。注意相关形参的匹配
22{           
23            vector<int> nvec;
24            for(size_t ix = 0;ix != vec.size(); ++ix)
25                    if(pred(vec [ix],filter_value))            //  调用相关函数进行操作
26                    nvec.push_back(vec[ix]);           //满足结果就保存
27            return nvec;
28}

29int main()
30{   
31    int value;
32    vector<int> ivec; 
33    cout <<"请输入数字:  "<<endl;
34    while(cin >> value)
35    ivec.push_back(value);
36    cin.clear();             //使输入流有效
37    int ival;
38    cout<<"请输入你要比较数字: "<<endl;
39    cin >>ival;
40    vector<int> vec=filter_ver1(ivec,ival ,greater_than);  // 函数的调用  传递函数名即可
41    vector<int>::iterator it=vec.begin();
42    while(it!= vec.end())
43               cout<<*it++<<"  ";
44               cout<<endl;
45
46    system("pause");
47    return 0;
48}

posted @ 2009-03-30 23:54 弹杯一笑 阅读(256) | 评论 (0)编辑 收藏

仅列出标题  
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(2)

随笔分类(7)

随笔档案(4)

搜索

最新评论

阅读排行榜

评论排行榜