2014年9月

C (bu)科学的复数求解(两数加减乘除)

这次真的是记录一下思考过程了,反正是个很坎坷复杂的经历。
github地址:https://github.com/csvwolf/Sky-C-Study/tree/master/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84
老师给了我们一个结构体的框架,不得不说C的结构体和C++的还是有差别的:http://www.cppblog.com/percyph/archive/2009/03/06/75742.html

由此,我们这样定义一个结构体之后:

typedef struct
{
    float rpart;
    float ipart;
} complex;

- 阅读剩余部分 -

C++ string型初识与getline

string比C语言好用多了。只要引入一个#include <string>以及using std::string;就OK了,其他部分真是像极了其他的语言,一些用法可以看注释,getline就和gets差不多一个意思,但没有数组自然也没什么风险吧。大致是酱紫的。

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    string line;
    string s1 = "Hello", s2 = " World";
    string s3 = s1 + s2;
    cout << s3 << endl;
    while (getline(cin, line))
        /* 如果不为空 */
        //if (!line.empty())
        /* 输出大于10字符的行 */
        if (line.size() > 10)
            cout << line << endl;
        
    /*
    while (cin >> word)
        cout << word << endl;
    */
    return 0;
}

C++ 命名空间的using声明

C++中,老师上课说的一直有一句:using namespace std;,不过他是什么意思,其实也就是一个命名空间的概念,概念这东西不多说(Primer 74页)

我们为什么要使用命名空间,不使用会有什么麻烦。
这里我们看一段代码:

#include <iostream>

int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;

    return 0;
}

- 阅读剩余部分 -

C++ auto decltype 初始化变量类型

C++中的两种有趣的类型,让编译器自己去分析我们所需要的数据类型,这两者有有些区别:
auto 定义的变量必须具有初值

// 由val1和val2相加的结果可以推断出item的类型
auto item = val1 + val2;  // 初始化为val1和val2相加的结果

但是,注意以下两条:

auto i = 0, *p = &i;   // 正确:i是整数,p是整型指针
auto sz = 0, pi = 3.14;  // 错误,sz和pi的类型不一致

具体的,Primer61页之后有,稍微看下就好了,关于类型都是个麻烦的东西。

- 阅读剩余部分 -

git push本地代码到github出错

用谷歌反倒没找到能看懂的……转自:http://www.douban.com/note/332510501/

刚创建的github版本库,在push代码时出错:

$ git push -u origin master
To git@github.com:******/Demo.git
 ! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:******/Demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

网上搜索了下,是因为远程repository和我本地的repository冲突导致的,而我在创建版本库后,在github的版本库页面点击了创建README.md文件的按钮创建了说明文档,但是却没有pull到本地。这样就产生了版本冲突的问题。

- 阅读剩余部分 -

C 清空键盘缓冲区若干方法

写这次的数据结构的作业的时候对于缓冲区问题深表蛋疼,getchar()深表拙计,于是搜了一下……

以下文字来自:http://www.ludou.org/c-clear-buffer-area.html

清空键盘缓冲区很多种方法,如用fflush(stdin); rewind(stdin);等,但是在linux这些都不起作用,还得我今天试了半天都没成功,上网搜了一下发现setbuf(stdin, NULL);就能直接清空键盘缓冲区了。

以下几个实例:

Sample one

#include <stdio.h>

int main()
{
    char ch1;
    char ch2;

    ch1 = getchar();
    ch2 = getchar();
    printf("%d  %d", ch1, ch2);
    return 0;
}

- 阅读剩余部分 -