C++语言程序设计基础笔记(实例1-10)

实例1:输出
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	cout << "Hello!" << endl;
	cout << "Welcome to C++!" << endl;
    return 0;
}

结果:

Hello!
Welcome to C++!
实例2:读取并显示整数
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int radius;
	cout << "Please enter the radius!\n";
	cin >> radius;
	cout << "The radius is:" << radius << "\n";
	cout << "Pi is:" << 3.14 << endl;
	cout << "Please enter a different radius!\n";
	cin >> radius;
	cout << "Now the radius is changed to:"<< radius << endl;
    return 0;
}

结果:

Please enter the radius!
10
The radius is:10
Pi is:3.14
Please enter a different radius!
2
Now the radius is changed to:2
补充实例:常量定义
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	const double pi(3.14159);
	int radius(0);
	cout << "Please enter the radius!\n";
	cin >> radius;
	cout << "The radius is:" << radius << "\n";
	cout << "Pi is:" << pi << endl;
	cout << "Please enter a different radius!\n";
	cin >> radius;
	cout << "Now the radius is changed to:"<< radius << endl;
	cout << "Pi is still:" << pi << endl;
    return 0;
}

结果:

Please enter the radius!
5
The radius is:5
Pi is:3.14159
Please enter a different radius!
7
Now the radius is changed to:7
Pi is still:3.14159
实例3:查看数据类型所占字节数及注释的使用
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	/*
	常用数据类型:int、short int、long int、char、float、double
	sizeof() 函数来获取各种数据类型的所占字节数大小。
	*/
	// 所占字节数
	cout << "The size of an int is:\t\t" << sizeof(int) << "bytes.\n";
	cout << "The size of an short int is:\t" << sizeof(short) << "bytes.\n";
	cout << "The size of an long int is:\t" << sizeof(long) << "bytes.\n";
	cout << "The size of an char is:\t\t" << sizeof(char) << "bytes.\n";
	cout << "The size of an float is:\t" << sizeof(float) << "bytes.\n";
	cout << "The size of an double is:\t" << sizeof(double) << "bytes.\n";
    return 0;
}

结果:

The size of an int is:          4bytes.
The size of an short int is:    2bytes.
The size of an long int is:     4bytes.
The size of an char is:         1bytes.
The size of an float is:        4bytes.
The size of an double is:       8bytes.
实例4:减法操作
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	signed int x;
	unsigned int y = 100;
	unsigned int z = 50;
	x = y - z;
	cout << "Difference is:" << x;
	x = z - y;
	cout << "\nNow difference is:" << x << endl;
    return 0;
}

结果:

Difference is:50
Now difference is:-50

注意:如果将x声明为unsigned int x,则会输出错误的结果。但程序不报错

实例5:求两数之差的绝对值
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int a, b, x;
	cout << "input value of b:" << endl;
	cin >> a;

	cout << "input value of a:" << endl;
	cin >> b;

	x = (a-b) > 0 ? (a-b) : (b-a);
	cout << "The difference of a and b is:" << x;

    return 0;
}

结果:

input value of b:
10
input value of a:
5
The difference of a and b is: 5
实例6:进制转换与浮点数小数点位数保留
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	/*
	#include <iomanip> :io 流控制头文件, 
	主要是一些操纵用法
	setbase(int n) : 将数字转换为 n 进制.
	setw(int):设置域宽度
	setprecision(int):设置浮点数的小数位数(包括小数点)
	*/
	//域宽度5,保留两位小数
	cout << setw(5) << setprecision(3) << 3.14159 <<endl;
	//转换为16进制,域宽度5
	cout << setbase(16) << setw(5) <<17<<endl;
    return 0;
}

结果:

 3.14
   11
实例7:输入一个年份,判断是否为闰年
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	//输入一个年份,判断是否为闰年
	int year;
	bool isLeapYear;
	cout << "Enter the year:\n";
	cin >> year;
	isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
	if (isLeapYear)
		cout << year << "是闰年" << endl;
	else
		cout << year << "不是闰年" << endl;

    return 0;
}

结果:

Enter the year:
2012
2012是闰年
实例8:输入两个整数,比较两个数的大小
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int x, y;
	cout << "Enter x and y:\n";
	cin >> x >> y;
	if (x != y)
		if (x > y)
			cout << "x>y" << endl;
		else
			cout << "x<y" << endl;
	else
		cout << "x=y" << endl;

    return 0;
}

结果:

Enter x and y:
4 3
x>y
实例9:输入一个0~6的整数,转换成星期输出。
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int day;
	cout << "输入一个0-6的整数" << endl;
	cin >> day;
	switch (day) {
	case 0:cout << "Sunday" << endl; break;
	case 1:cout << "Monday" << endl; break;
	case 2:cout << "Tuesday" << 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;
	default:
		cout << "Day out of range Sunday..Saturday" << endl; break;
	}
    return 0;
}

结果:

输入一个0-6的整数
5
Friday
实例10:求自然数1~10之和
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int i = 1, sum = 0;
	while (i <= 10) {
		sum += i;//sum = sum + i;
		i++;
	}
	cout << "sum = " << sum << endl;
	return 0;
}

结果:

sum = 55
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陨星落云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值