C++入门级简单的顺序结构

一、编写一个简单的C++程序——简单练习。

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

二、语法基础。

1、变量的定义

变量必须先定义才可以使用,且不能重名。

(1)变量的定义方式:

#include <iostream>
 
using namespace std;
 
int main()
{
    int a = 5;
    int b, c = a, d = 10 / 2;
 
    return 0;
}

附:常用变量及范围:

2、输入输出

(1)整数的输入输出:

#include <iostream>
 
using namespace std;
 
int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}

 (2)字符串的输入输出:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    string str;
    cin >> str;
    cout << str;
    return 0;
}

(3)输入输出多个不同类型的变量:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a, b;
    string str;
 
    cin >> a;
    cin >> b >> str;
 
    cout << str << " !!! " << a + b << endl;
 
    return 0;
}

3、表达式:

(1)整数的加减乘除四则运算:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a = 6 + 3 * 4 / 2 - 2;
 
    cout << a << endl;
 
    int b = a * 10 + 5 / 2;
 
    cout << b << endl;
 
    cout << 23 * 56 - 78 / 3 << endl;
 
    return 0;
}

(2)小数(浮点数)的运算:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    float x = 1.5, y = 3.2;
 
    cout << x * y << ' ' << x + y << endl;
 
    cout << x - y << ' ' << x / y << endl;
 
    return 0;
}

(3)整型变量的自增、自减:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a = 1;
    int b = a ++ ;
 
    cout << a << ' ' << b << endl;
 
    int c = ++ a;
 
    cout << a << ' ' << c << endl;
 
    return 0;
}

(4)变量的类型转换:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    float x = 123.12;
 
    int y = (int)x;
 
    cout << x << ' ' << y << endl;
 
    return 0;
}

4、顺序语句:

(1)输出第一个整数:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << a << endl;
    return 0;
}

(2)计算a*(b+c)的值:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a, b, c;
 
    cin >> a >> b >> c;
 
    cout << a*(b+c)<< endl;
 
    return 0;
}

(3)带余除法:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a, b;
 
    cin >> a >> b;
 
    int c = a / b, d = a % b;
 
    cout << c << ' ' << d << endl;
 
    return 0;
}

(4)求反三位数:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int n;
    cin >> n;
 
    int a = n % 10;
    n = n / 10;
    int b = n % 10;
    n = n / 10;
    int c = n;
 
    cout << a << b << c << endl;
 
    return 0;
}

(5)交换两个整数:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    int a = 3, b = 4;
 
    int t = a;
    a = b;
    b = t;
 
    cout << a << ' ' << b << endl;
 
    return 0;
}

(6)输出菱形:

#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    char c;
 
    cin >> c;
 
    cout << "  " << c << endl;
    cout << " " << c << c << c << endl;
    cout << c << c << c << c << c << endl;
    cout << " " << c << c << c << endl;
    cout << "  " << c << endl;
 
    return 0;
}

三、总结

        欢迎大家前来交流学习!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值