C++ 学习笔记

Less-1:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World";
    return 0;
}

Less-2 :

#include<iostream>
#include<limits>

using namespace std;

extern int a, b;
extern int c;
extern float f;

int main(int argc, char **argv){
    int a, b, c;
    float f;

    a = 10;
    b = 20;
    c = a + b ;
    cout << c << endl;

    f = 70.0/3.0;

    cout << f << endl;

    return 0;
}

Less-3:

#include <iostream>
using namespace std;

int main(int argc, char **argv){
    unsigned int a = 60; // 60 = 0011 1100
    unsigned int b = 13; // 13 = 0000 1101
    int c = 0;

    c = a << 2;
    cout << "Line 1 - c 的值是" << c << endl;
    c = a >> 2;
    cout << "Line 2 - c 的值是" << c << endl;
    cout << sizeof(c) << endl;
    return 0;
}

Less-4:

#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
     int a, b, c;
     char ch;
     cin >> a >> ch >> b >> c;
     cout << a << endl
          << ch << endl
          << b << endl
          << c;
     return 0;
}

Less-5:

#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
    int arr[4] = {1, 2, 3, 4}, i;
    int *a = arr;
    int *&p = a;
    // *a = 1000;
    // cout << *a << "\t" << *p << endl;
    p++;
    *p = 100;
    cout << *a << "\t" << *p << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << arr[i] << "\t";
    }
    cout << endl;
    return 0;
}

Less-6:

#include <iostream>
using namespace std;
int i = 0;
int main()
{
    int i = 5;
    {
        int i = 7;
        cout << "::i=" << ::i << endl;
        cout << "i=" << i << endl;
        ::i = 1;
        cout << "::i=" << ::i << endl;
    }
    cout << "i=" << i << endl;
    cout << "please input x,y: ::i= " << ::i << endl;
    i += ::i;
    ::i = 100;
    cout << "i=" << i << endl;
    cout << "::i=" << ::i << endl;
    return 0;
}

Less-7:

#include <iostream>
using namespace std;
void f(double x = 50.6, int y = 10, char z = 'A');
int main()
{
    double a = 216.34;
    int b = 2;
    char c = 'E';
    f();
    f(a);
    f(a, b);
    f(a, b, c);
    return 0;
}
void f(double x, int y, char z)
{
    cout << "x=" << x << '\t' << "y=" << y << '\t';
    cout << "z=" << z << endl;
}

Less-8:

#include <iostream>
using namespace std;
int &s(const int &a, int &b)
{
    b += a;
    return b;
}
int main()
{
    int x = 500, y = 1000, z = 0;
    cout << x << '\t' << y << '\t' << z << '\t' << endl;
    s(x, y);
    cout << x << '\t' << y << '\t' << z << '\t' << endl;
    z = s(x, y);
    cout << x << '\t' << y << '\t' << z << '\t' << endl;
    s(x, y) = 200;
    cout << x << '\t' << y << '\t' << z << '\t' << endl;
    return 0;
}

Less-9:

#include "iostream"
using namespace std;
void fun(int x, int y)
{
    x += y;
    y += x;
}
int main()
{
    int x = 5, y = 10;
    fun(x, y);
    fun(y, x);
    cout << "x=" << x << ",y=" << y << endl;
    return 0;
}

Less-10:

#include <iostream>
using namespace std;
int add(int a, int b);
int main(int argc, char **argv)
{
    int x, y, sum;
    cout << "Please input x and y :" << endl;
    cin >> x >> y;
    sum = add(x, y);
    cout << "Please input x and y : " << x << "+" <<y << " = " << sum << endl;
    return 0;
}
int add(int a, int b){
    return a+b ;
}

Less-11:

#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
    int r;
    const double PI = 3.141592653589793;
    cout << "Please input r:" << endl;
    cin >> r;
    cout << "l = " << 2*r*PI << endl;
    cout << "s = " << r*r*PI << endl;
    return 0;
}

Less-12:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace std;
const int N = 10;
int main(int argc, char **argv)
{
    int *r, *sum, i;
    sum = new int(0);
    r = new int[10];
    if (r == NULL)
    {
        cout << "allocation failure.\n";
        return 0;
    }
    srand(time(NULL));
    for (int i = 0; i < N; i++)
    {
        r[i] = rand() % 10000 - 3000;
        if (r[i] < 0)
            (*sum)++;
    }
    for (int i = 0; i < N; i++)
    {
        cout << setw(4) << r[i] << endl;
    }
    cout << "the number of negative number: " << *sum << endl;
    cout << "the number of positive number: " << N - *sum << endl;
    delete[] r;
    delete sum;
    return 0;
}

Less-13:

#include <iostream>
using namespace std;
class Student
{
public:
    void setName(string na)
    {
        name = na;
    }
    void setAge(int ag)
    {
        age = ag;
    }
    void setScore(double sc)
    {
        score = sc;
    }
    void checkAgeScore(void)
    {
        try
        {
            cout << "Before dividing." << endl;
            if (age < 16 || age > 25)
            {
                throw age;
            }
            else if (score < 0 || score > 5)
            {
                throw score;
            }
        }
        catch (double d)
        {
            cout << "Error: score " << d << endl;
        }
        catch (int e)
        {
            cout << "Error: age " << e << endl;
        }
    }
    void printStudent(void)
    {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "score:" << score << endl;
    }

private:
    string name;
    int age;
    double score;
};

int main(int argc, char **argv)
{
    Student Student1;
    Student Student2;
    Student Student3;
    Student1.setName("Joker");
    Student1.setAge(16);
    Student1.setScore(3.0);
    Student1.checkAgeScore();
    Student1.printStudent();
    Student2.setName("Helen");
    Student2.setAge(33);
    Student2.setScore(-1.0);
    Student2.checkAgeScore();
    Student2.printStudent();
    Student3.setName("Jack");
    Student3.setAge(17);
    Student3.setScore(-4.0);
    Student3.checkAgeScore();
    Student3.printStudent();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值