| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2683 人关注过本帖
标题:All about C++
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
记得以前写过最小公倍数和最大公约数的程序,其中最大公约数曾经贴在这里过,现在找不着了,如果朋友们有兴趣,可以写写。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-11-08 06:46
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

以下是这两个函数

#include <iostream> #include <cstdlib> using namespace std;

long zui_da_gong_yue_shu(long a, long b) { for( ;(a = a%b) && (b = b%a); ) ; return (a|b); } long zui_xiao_gong_bei_shu(long a, long b) { return a*b/zui_da_gong_yue_shu(a, b); }

int main() { int a = 15; // 作为测试取了两个值,你也可以取别的数值 int b = 25; int result = zui_da_gong_yue_shu(a, b); // 只是传值运算, a, b 本身并未得到改变 cout<<result<<endl; // 输出运算结果来测试正确与否 cout<<a<<" "<<b<<endl; // 输出 a, b 确定其未得到改变 //////////////////////////////////////// 以下同样,只是测试最小公倍数的运行 result = zui_xiao_gong_bei_shu(a, b); cout<<result<<endl; cout<<a<<" "<<b<<endl; system("pause"); return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-11-08 18:40
polyrandom
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2004-12-10
收藏
得分:0 

kai,好久不见了,还以为找不到你了呢,呵呵

第一题用count更方便点吧?

函数的原型都有问题


http://www./ http://www./
2004-12-10 09:12
BlueDreame
Rank: 1
等 级:新手上路
帖 子:545
专家分:2
注 册:2004-12-16
收藏
得分:0 
getline()是不是要用两个回车结束输入?

2004-12-19 23:28
littletiger2005
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2004-12-17
收藏
得分:0 

gao


2004-12-22 13:47
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
BlueDreame, 我想你用的一定是VC6.0。这应该是VC6.0 的一个Bug , 据说VC7.0 已经作了改进。不过我没有VC7.0 无法替你验证。不过从语言角度来讲,敲两次回车实现输入,这是不对的。如果你改用Dev 来测试你的程序,你会发现敲一次回车就可以了。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-01-07 22:28
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
Namespaces  (Part 1)



下面来看一个及其简单的例程



#include &lt;iostream&gt;

#include &lt;cstdlib&gt; // when you use Dev, you can find it under

                  
// C:\Dev-Cpp\include\c++\3.3.1

                  
// you can see it using WordPad

using namespace std;



int main()

{

    cout&lt;&lt;"Hello World!\n";   

    system("pause");        // to see the output in the screen

    return 0;

}



这样的头文件形式,我们已经看到很多了,有些朋友是在不知觉的应用这样的书写形式。

其实上面的书写形式是与下面的书写形式等价的:

#include &lt;iostream&gt;

#include &lt;cstdlib&gt; // when you use Dev, you can find it under

                  
// C:\Dev-Cpp\include\c++\3.3.1

                  
// you can see it using WordPad

//using namespace std;



int main()

{

    std::cout&lt;&lt;"Hello World!\n";   

    std::system("pause");        // to see the output in the screen

    return 0;

}



大家可以发现 using namespace std; 被隐去了,取而代之是在程序中相关的语句前添加std::

这里我们就用到了 namespace ,中文或许可以翻译为 名字空间。 初次接触C++ 的朋友一定会对namespace
这个概念觉得比较别扭,理解起来不爽。 那么我们先从另外一个概念谈起,这对于学习C, C++的朋友都不会陌生。这个概念便是变量的作用域问题。

当你申明或定义一个变量后,这个变量便有其有效的作用域,比如当你申明一个全局变量,那么这个全局变量将在其申明的文件中有效。如果你申明一个变量于某个
函数中,那么其作用空间只能在那个函数快中。如果你在那个函数中,再申明一个相同名字的变量,编译器会给你一个错误报告信息,我想大家对这一点都应该非常
清楚。比如来看下面这段代码:

#include &lt;iostream&gt;

#include &lt;cstdlib&gt;



int main()

{

    using namespace std;

    int a = 3;

    char a = 't';    // error, we can not have 2 variable with the same name.

    cout&lt;&lt;a&lt;&lt;endl;   // which a will be showed???

    system("pause");        

    return 0;

}



大家看到,上面这段代码是错误的,错误之处显而易见的,程序试图定义两个相同名字的变量,这显然不合理。

但是当两个变量处于不同的作用域时,其可以采用相同的名字,哪怕类型相同也可以,下面来看一段例程。

#include &lt;iostream&gt;

#include &lt;cstdlib&gt;



int main()

{

    using namespace std;

    int a = 8;  // 他的作用区域为:从这里到 return 0: 执行完毕

    {

        int a = 3;    // 仅仅在这个花括号内有效

        cout&lt;&lt;a&lt;&lt;endl;  // 所以输出 3 ,没有任何名字冲突

    }

    {

        int a = 5;

        cout&lt;&lt;a&lt;&lt;endl;

    }

    {

        char a = 't';

        cout&lt;&lt;a&lt;&lt;endl;

    }            

    cout&lt;&lt;a&lt;&lt;endl;

    system("pause");        

    return 0;

}

那么现在问题来了,如果上面那个程序中我一定要在花括号内调用第一个integer a, 该怎么做呢?

我们可以通过作用域操作符来实现在局部函数中调用全局变量。下面是相关代码:

#include &lt;iostream&gt;

#include &lt;cstdlib&gt;



int a = 8;

int main()

{

    using namespace std;

   

    int a = 3;

    cout&lt;&lt;a&lt;&lt;endl;

    cout&lt;&lt;::a&lt;&lt;endl;

   

    system("pause");        

    return 0;

}



通过上面的简单介绍,我们可以看到,变量名可能在程序中有冲突。对于上面这么短小的程序大家一看便知,但是如果程序大了呢?
我们知道,我们写程序往往不会只有一个 cpp
程序,我们往往会有一些头文件。如果头文件源于不同的供应商,而恰恰在其库函数中对某个函数取了相同的函数名,那么当你在包含了这两个头文件时,如果你调
用这个具有相同函数名的函数时,编译器将无法明确到底调用哪个函数,这样就产生了名字冲突。为解决名字冲突问题,C++ 采用了namespace
这个技术术语,使得名字冲突得以避免。



下面来看一下 namespace 的具体使用。

// Part2 to be continued





 

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-01-08 17:04
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
谈到 namespace 的具体使用,分两步来谈。
1)声明或定义一个 namespace
2)  对某个namespace 下的变量或函数具体调用。

首先来谈第一点:
  下面具体来看两个 namespace 的声明(代码摘录于 C++ PrimerPlus, Fifth  Edition,  Page  426)
  namespace Jack
{
    double pail;            // variable declaration
    void fetch();           // function prototype
    intpal;               // variable declaration
    struct Well{ ... };     // structure declaration
}   
namespace Jill
{
    double bucket(double n){ ... } // function definition
    doublefetch;                 // variable declaration
    intpal;                      // variable declaration
    structHill{...};             // structure declaration
}   

Namespaces 可以像设置全局变量一样设置,namespaces  可以嵌套与其他  namespaces中,但是namespaces 不可以设置于某个某个块中,即不可以局部设置于某个函数块中或独立的设置于 两个花括号之中。也就是说,一个位于namespace  中的变量或函数在默认情况下具有外部连接能力(除非他被指定为  const)
此外,namespace 是开放的,也就是说,你可以在 namespace 的声明或定义之外,添加代码,即添加变量以及函数。
比如
namespace Jill
{
    char * goose(const char * );  // adds the name goose to the exiting list of names in Jill
}

通常的做法是我们将 namespace 的声明写入一个或多个头文件,将定义写入一个 cpp 或多个 cpp 文件

第二点, 对某个namespace 下的变量或函数具体调用:
最简单的做法是采用作用域操作符 ::
比如  Jack::pail = 12.34;   // use a variable
    Jill::Hill mole;      // create a type Hill structure
    Jack::fetch();        // use a function

此外必须提到的是  using Declarations and using Directives
由于使用作用域操作符的比较麻烦,因为它增加了你打字的工作,我想没几个人喜欢在每句cout 前增加 std:: 的。但是在某种情况下采用作用域的方法不可避免,这也是  姬老师 那个 抗议贴中提到的担心。 下面我也将提到。

  a) using Declarations - 对namespace 下的具体变量以及函数声明调用。如:using Jill::fetch;  // a using declaration
这样声明后,以后在程序中如出现 对变量 fetch 的操作将被认为是 Jill 那个namespace 下的 fetch, 如果在程序中Jill::fetch的作用域内再声明或定义同样名字的变量将被认为出错。下面的代码说明这个问题:
namespace Jill
{
    double bucket(double n){ ... } // function definition
    doublefetch;                 // variable declaration
    intpal;                      // variable declaration
    structHill{...};             // structure declaration
}   

char fetch;   // ok
int main()
{
    using Jill::fetch;    // put fetch into local namespace
    double fetch;         // Error! Already have a local fetch
   cin&gt;&gt;fetch;          // read a value into Jill::fetch
    cin&gt;&gt;::fetch;         // read a value into global fetch
   
    ......
    return 0;
}

与此相比, using directive 使得整个namespace 下的变量以及函数成为有效调用。

比如如果在程序中这样写:
using namespace Jack;
cin&gt;&gt;pail;
fetch();
那么这里的pail, fetch()  将被认为是 Jack  名字空间下的变量和函数。

现在来谈  姬老师  所担心的那个歧义性:
在使用了 using directives 以及  使用了 using declarations  之后,随之也带来一个问题,那就是歧义性问题。
比如如下代码:
using Jack::pal;
    using Jill::pal;
    pal = 4;          // which one? now have a conflict

但如果我们明确使用作用域符合,那么这样的歧义性就没有了。 如 Jill::pal= 4;  //  no conflict

下面摘入书上程序 listing 9.10,  listing 9.11 以及 listing 9.12, 让大家感受一下 namespace 在程序中的使用。

// namesp.h
// create the pers and debts namespaces
namespace pers
{
    const int LEN = 40;
    struct Person
    {
        char fname[LEN];
        char lname[LEN];
    };
    void getPerson(Person &amp;);
    void showPerson(const Person &amp;);
}

namespace debts
{
    using namespace pers;
    struct Debt
    {
        Person name;
        double amount;
    };
    void getDebt(Debt &amp;);
    void showDebt(const Debt &amp;);
    double sumDebts(const Debt ar[], int n);
}

// namesp.cpp -- namespaces
#include &lt;iostream&gt;
#include "namesp.h"

namespace pers
{
    using std::cout;
    using std::cin;
    void getPerson(Person &amp; rp)
    {
        cout&lt;&lt;"Enter first name: ";
        cin&gt;&gt;rp.fname;
        cout&lt;&lt;"Enter last name: ";
        cin&gt;&gt;rp.lname;
    }

    void showPerson(const Person &amp; rp)
    {
        std::cout&lt;&lt;rp.lname&lt;&lt;", "&lt;&lt;rp.fname;
    }
}

namespace debts
{
    void getDebt(Debt &amp; rd)
    {
        using namespace pers;
        getPerson(rd.name);      // hier ist merkwuerdig
        std::cout&lt;&lt;"Enter debt: ";
        std::cin&gt;&gt;rd.amount;
    }

    void showDebt(const Debt &amp; rd)
    {
        pers::showPerson(rd.name);     // hier ist merkwuerdig
        std::cout&lt;&lt;": $"&lt;&lt;rd.amount&lt;&lt;std::endl;
    }

    double sumDebts(const Debt ar[], int n)
    {
        double total = 0;
        for(int i = 0; i&lt;n; i++)
            total += ar[i].amount;
        return total;
    }
}

// usenmsp.cpp -- using namespaces
#include &lt;iostream&gt;
#include "namesp.h"

void other(void);
void another(void);
int main(void)
{
    using debts::Debt;
    using debts::showDebt;
    Debt golf = {{"Benny", "Goatsniff"}, 120.0};
    showDebt(golf);
    other();
    another();

    return 0;
}

void other(void)
{
    using std::cout;
    using std::endl;
    using namespace debts;
    Person dg = {"Doodles", "Glister"};
    showPerson(dg);
    cout&lt;&lt;endl;
    Debt zippy[3];
    int i;

    for(i = 0; i&lt;3; i++)
        getDebt(zippy[i]);
    for(i = 0; i&lt;3; i++)
        showDebt(zippy[i]);
    cout&lt;&lt;"Total debt: $"&lt;&lt;sumDebts(zippy, 3)&lt;&lt;endl;
    return ;  // Dieser return ist auch ueberfluessig
}

void another(void)
{
    using pers::Person;;

    Person collector = {"Milo", "Rightshift"};
    pers::showPerson(collector);
    std::cout&lt;&lt;std::endl;
}
  

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-01-08 18:36
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
//  number to string:

我们可以通过以下3种方法将一个数值转换为字符串.

---------

1. std::ostringstream
C/C++ Code:
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;
int main()
{
    double d = 1.23456;
    ostringstream Str;
    Str &lt;&lt; d;
    string numToString(Str.str());
    cout &lt;&lt; numToString &lt;&lt; endl;
   
    system("pause");
    return EXIT_SUCCESS;
}

2. std::ostrstream
C/C++ Code:
#include &lt;strstream&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

int main()
{
    double d = 1.23456;
    char buffer[10];
    ostrstream Str(buffer, 10);
    Str &lt;&lt; d &lt;&lt; ends;
    string numToString(Str.str());
    cout &lt;&lt; numToString &lt;&lt; endl;
   
    system("pause");
    return EXIT_SUCCESS;
}
3. sprintf
C/C++ Code:
#include &lt;stdio.h&gt;
#include &lt;cstdlib&gt;

int main()
{
    double d = 1.2345;
    char buffer[10];
    sprintf(buffer, "%g", d);
    printf("%s\n", buffer);
   
    std::system("pause");
    return EXIT_SUCCESS;
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-01-29 21:58
猫妓
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2005-2-11
收藏
得分:0 
第二题的答案是错的
比如
输入 99990000
和 99

他99出现了 3次,但应是2次

2005-02-11 04:59
快速回复:All about C++
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013636 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved