c++书第二章上的例子以及我在运行时发现的问题

来源:互联网 发布:深入浅出python 中文 编辑:程序博客网 时间:2024/05/22 05:03

问题1:求某一年份是不是瑞年?(了解瑞年的计算方法)

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int year;
    bool Leapyear;

    cout <<"Enter the year:" ;
    cin>>year;
    Leapyear=(year%4==0&&year%100==0)||(year%400==0);

    if(Leapyear) cout<<year<<"is a leap year.";
    else cout<<year<<"isn't a leap year.";

    return 0;

}

当我运行时,发现红色标记处有错误,然后我就百度了一下

int main(int argc,char *argv[])和int main() 的区别

我们一般常用的是int main(),当有时我们会用到int main(int argc,char *argv[]),括号中会带有字符串参数。这两个参数一个是字符串参数的个数,一个是字符串参数的值,可以在DOS命令下传参给main()。(注意:main 的两个形参和命令行中的参数在位置上不是一一对应的。因为,main的形参只有二个,而命令行中的参数个数原则上未加限制。他表示的是一个字符串指针数组)

因为main()函数是主函数,不能被其他函数调用,因此不能在程序内部取得实际的值。所以main函数的参数值不能直接赋予的,而是从操作系统命令上获得的。当我们要运行一个可执行文件时,在DOS提示符下键入文件名,再输入实际参数即可把这些实参传送到main的形参中去。

int main(int argc, char *argv[])改为 int main() ,结果就没什么问题。


问题2:比较两个数的大小?(运用if -else )

#include <iostream>

using namespace std;

int main()
{
    int x,y;

    cout<<"Enter x,y:";
    cin>>x>>y;

    if(x>=y) cout<<"the max number is"<<x<<endl;
    else cout<<"the max number is"<<y<<endl;

    return 0;
}


问题3:求0~9个数的总和(运用while)

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

int main()                                                                                                             int main()
{                                                                                                                            {
  int i,sum=0;                                                                                                        
int i,sum=0;

  cin>>i;                                                                                                                  cin>>i;

    while(i<10)                                                                                                         do{

    {                                                                                                                                    sum+=i;
        sum+=i;                                                                                                                    i++;
        i++;                                                                                                                  }while(i<10);
    }

    cout << "sum=" <<sum<< endl;                                                                       cout<<"sum="<<sum<<endl;

    return 0;                                                                                                              return 0;
}                                                                                                                                }

while和do-while的区别

当我们的判断条件为真时,他们的结果没有区别,而当判断条件为假时,他们的执行结果就不一样了。while是先判断,在执行时,他直接结束;do-while他会执行一次,然后在判断,所以他会有执行一次的结果。


问题4:将数字转换为星期输出(运用swich-case)

#include <iostream>

using namespace std;

int main()
{
    int day;

    cout<<"Please enter the number:";
    cin>>day;

    switch(day){
        case 1:cout<<"Monday"<<endl;
            break;
        case 2:cout<<"Tuseday"<<endl;
            break;
        case 3:cout<<"Wednesday"<<endl;
            break;
        case 4:cout<<"Thursday"<<endl;
            break;
        case 5:cout<<"Friday"<<endl;
            break;
        case 6:cout<<"Saturday"<<endl;
            break;
        case 7:cout<<"Sunday"<<endl;
            break;
        default:
            cout<<"the number is out of rangle ,please run it again ";
            break;
    }

    return 0;
}


注意:switch中的case选定后,如果没有break 跳出,则程序会一直向下进行选择。如果不是单项的选择,那就可以不用break 来跳出swtich。


问题5:输入整数,将数字反转后输出(运用do-while)

#include <iostream>

using namespace std;

int main()
{
    int raw_data,remainder=0;
    cout << "Please enter the number:";
    cin>>raw_data;

    do{
        remainder=raw_data%10;
        cout<<remainder;
        raw_data=raw_data/10;
    }while(raw_data!=0);

    return 0;
}

注意:cout<<"the number is:"<<a<<endl;

cout是输出的意思,输出显示在屏幕上,而一个个的<<分隔出输出的内容。带上“ ”是直接打印的效果,而不带双引号的是输出数值。

endl是代表换不换行,如果没有换行,那么输出的数就是连续的数。这里就运用了这个规则。

cin>>是从键盘输入的意思,没有>>endl


问题6:输入一个整数,求出他所有的因子(运用for循环)

#include <iostream>

using namespace std;

int main()
{
    int integer,factor=1;
    cout<<"Please enter an integer:";
    cin>>integer;

    cout<<"the factor of integer is";

    for(factor=1;factor<=integer;factor++)
    {
        if(integer%factor==0) cout<<factor<<" "<<endl;
    }

    return 0;
}







0 0
原创粉丝点击