栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 学习笔记

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 学习笔记

Less-1:
#include 
using namespace std;
int main()
{
    cout << "Hello World";
    return 0;
}
Less-2 :
#include
#include

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 
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 
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 
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 
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 
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 
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 
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 << "+" <> r;
    cout << "l = " << 2*r*PI << endl;
    cout << "s = " << r*r*PI << endl;
    return 0;
}
Less-12:
#include 
#include 
#include 
#include 
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 
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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768261.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号