C++博客-我的编程乐园-文章分类-编程体会和收获http://www.cppblog.com/deercoder/category/12424.html思考至上zh-cnFri, 27 Nov 2009 14:07:16 GMTFri, 27 Nov 2009 14:07:16 GMT60++弄错了……http://www.cppblog.com/deercoder/articles/102114.html刘畅刘畅Fri, 27 Nov 2009 13:43:00 GMThttp://www.cppblog.com/deercoder/articles/102114.htmlhttp://www.cppblog.com/deercoder/comments/102114.htmlhttp://www.cppblog.com/deercoder/articles/102114.html#Feedback0http://www.cppblog.com/deercoder/comments/commentRss/102114.htmlhttp://www.cppblog.com/deercoder/services/trackbacks/102114.htmlusing namespace std;

int main()
{
    int a=0, b = 1, c = 2, d = 3, e = 4;
    a = (b++, c++, d++, e++);
    cout << a << endl;
    (a = b++), c++, d++, e++;
    cout << a << endl;
}
猜测结果是:
4
2
为什么,因为++是在该语句执行后才完成的,所以赋值操作执行前,++是不会执行的,呵呵,知道吧!!

刘畅 2009-11-27 21:43 发表评论
]]>
C++关键字弄错了……http://www.cppblog.com/deercoder/articles/102111.html刘畅刘畅Fri, 27 Nov 2009 13:39:00 GMThttp://www.cppblog.com/deercoder/articles/102111.htmlhttp://www.cppblog.com/deercoder/comments/102111.htmlhttp://www.cppblog.com/deercoder/articles/102111.html#Feedback0http://www.cppblog.com/deercoder/comments/commentRss/102111.htmlhttp://www.cppblog.com/deercoder/services/trackbacks/102111.html
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    vector<string> v;
    string line;
    ifstream in ("1.cpp");
    while (getline(in,line))
    {
        v.push_back(line);
    }
    string test;
    for (int i = 0; i != v.size(); i++)
    {
        test = test + v[i];
    }
    cout << test;
    return 0;
}


刘畅 2009-11-27 21:39 发表评论
]]>
数据读入的问题(巨大的BUG)http://www.cppblog.com/deercoder/articles/102102.html刘畅刘畅Fri, 27 Nov 2009 13:22:00 GMThttp://www.cppblog.com/deercoder/articles/102102.htmlhttp://www.cppblog.com/deercoder/comments/102102.htmlhttp://www.cppblog.com/deercoder/articles/102102.html#Feedback0http://www.cppblog.com/deercoder/comments/commentRss/102102.htmlhttp://www.cppblog.com/deercoder/services/trackbacks/102102.html#define MAX 4
void main()
{
    int n,num=0,per=0;
    int i,j;
    char a[MAX][MAX];
    scanf("%d%*c",&n); //开始这里没有虚读,所以回车键被后面的字符读入了。因为我没有处理,后面   
    while(n!=0)             //就直接读入了。一定要注意这个问题。
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%c",&a[i][j]);
            }
            getchar(); //读入回车符,每次结束的时候
        }
        for(i=0;i<n;i++)  //求出每行的个数并估计最多
        {
            for(j=0;j<n;j++)
                if((a[i][j]=='x')||(a[i][j]=='X'))
                    ++num; //这里直接用它,原来还分每次都用一个per来求个数。
        }
        printf("%d\n",num);
    }
}

相比较而言,下面的代码更完善:
#include<stdio.h>
#define MAX 4
void main()
{
    int n,num=0,per=0;
    int i,j;
    char a[MAX][MAX];
    while(scanf("%d%*c",&n)!=0)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%c",&a[i][j]);
            }
            getchar(); //读入回车符,每次结束的时候
        }
        for(i=0;i<n;i++)  //求出每行的个数并估计最多
        {
            for(j=0;j<n;j++)
                if((a[i][j]=='x')||(a[i][j]=='X'))
                    ++num;
        }
        printf("%d\n",num);
        num=0;
    }
}
首先,将读入送入while中,每次都要循环判断多少。其次,每次while完成后,记得要清零,否则的话会继续的叠加num,这样最终得到的就不是每次输入矩阵的x的个数了。


刘畅 2009-11-27 21:22 发表评论
]]>