C++入门基础第一篇:入门及简单的顺序结构

第一章 入门及简单的顺序结构

c++标准的I/O库

iostream:支持对标准的输入输出设备(键盘显示器的读写);
fstream:支持对文件的读写
stringstream:支持对srting对象更灵活的处理;
algorithm:C++ 标准库中的 <algorithm> 头文件提供了一组用于操作容器(如数组、向量、列表等)的算法。这些算法包括排序、搜索、复制、比较等,它们是编写高效、可重用代码的重要工具。
CSDN文章

基础及知识


输入输出
#include<iostream>
using namespace std;
int main()
{
	int a = -1;
	cout << "please input a" << endl;
	cin >> a;
	if (a == -1)
		printf("输入失败");
	else printf("a=%d", a);
}

#include<iostream>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b; //输入两个数,以空格或者回车隔开,分别输入ab
	cout << a + b << endl;
	return 0;
}
  1. enl是换行的作用
  2. cout<<“输出内容”;cin<<“输入内容”;
  3. printf()不采用&a了,直接a

变量定义
#include<iostream>
using namespace std;
int main()
{
	int a = 5;
	int b=a ,c = a, d = 10 / 2;
	printf("%d,%d,%d,%d", a, b, c, d);
	return 0;
}
  1. int a,b=c这样的不可以了,a会显示未赋予初值的变量
变量内存
变量字节输出格式
int4%d
char1%c
int_81%d
int_162%d
int_324%d
int_648%d
short2
long4
long long8
std::string32
float4%f
double8%lf

字符串的输入输出
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	cin >> str;
	cout << str;
	return 0;
}

string库字符串的一些操作

输入多个不同数据类型
#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;
}
  1. 在输入不同的数据时,“ ”和回车是可以隔开的
  2. using namespace std的作用:在 C++ 中,using namespace std; 是一个常用的语句,它的作用是引入 std 命名空间中的所有成员到当前的命名空间中,这样在当前的作用域中就可以直接使用 std 命名空间中的所有成员,而不需要在每个标识符前加上 std:: 的前缀。

表达式

整数的加减乘除混合运算

#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;
}

![[逻辑运算符.png]]
浮点数的运算:

#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;
}

整型变量的自增、自减:

#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;

}

变量的类型转换:

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

}

顺序语句

  • 输出第二个整数
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << b;
	return 0;
}
  • 计算 (a + b) * c的值
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << (a+b)*c;
	return 0;
}
  • 带余除法
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	cout << "商:" << a / b <<endl<< "余数:" << a % b;
	return 0;
}
  • 求反三位数(将每一位分解出来):
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n=0;
	cout << "please input n" << endl;
	cin >> n;
	int a, b, c;
	a = n / 100;
	n = n % 100;
	b = n / 10;
	n = n % 10;
	c = n;
	cout << "百位:" << a << " " << "十位:" << b << " " << "个位:" << c;
	return 0;
}
  • 在不消耗额外内存的条件下交换两个整数
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a = 10, b = 20;
	a = a + b;
	b = a - b;
	a = a - b;
	cout << "a:" << a << endl;
	cout << "b:" << b << endl;
	return 0;
}
  • 输出菱形
#include <iostream>
#include <string>
using namespace std;

int main() {
    char c;
    int n;
    cout << "输入字符:";
    cin >> c;
    cout << "输入打印的大小(对角线长):";
    cin >> n;

    int num = 0;
    int i, j;

    if (n % 2 == 0) {
        for (i = 1; i <= n; i++) {
            int limit = (i <= n / 2) ? i : (n - i + 1);
            for (j = 1; j <= n; j++) {
                if (j >= (n / 2 + 1 - limit + 1) && j <= (n / 2 + limit)) {
                    cout << c;
                    num++;
                }
                else {
                    cout << " ";
                }
            }
            cout << endl;
        }
    }
    else {
        for (i = 1; i <= n; i++) {
            int limit = (i <= n / 2 + 1) ? i : (n - i + 1);
            for (j = 1; j <= n; j++) {
                if (j >= (n / 2 + 1 - limit + 1) && j <= (n / 2 + limit)) {
                    cout << c;
                }
                else {
                    cout << " ";
                }
            }
            cout << endl;
        }
    }

    return 0;
}

我的代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	char c; int n;
	cout << "输入符号:";
	cin >> c;
	cout << "输入打印的大小(对角线长):";
	cin >> n;
	int num = 0;
	int i = 1, j = 1;
	if (n % 2 == 0)
	{
		for (i = 1; i <= n; i++)
		{
			int limit = (i > n / 2) ? (n - i + 1) : i;
			for (j = 1; j <= n; j++)
			{ 
				if (n / 2 + 1 - limit <= j && j <= n / 2 + limit)
				{
					cout << c;
				}
				else cout << " ";
			
			}
			cout << endl;
		}
	}
	else
	{
		for (i = 1; i <= n; i++)
		{ 
			int limit = (i > n / 2+1) ? (n +2- i) : i;
			for (j = 1; j <= n; j++)
			{
				if (n / 2 + 1 - limit < j && j < n / 2 + 1 + limit)
					cout << c;
				else cout << " ";
			}
			cout << endl;
		}
	}
	return 0;

}
  1. 反思:用于控制循环的变量怎么能重新复制啊歪
  2. 反思:先把远离搞清楚再去搞算法,是不是更省时间!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值