《C++语言程序设计》(2)

本文介绍了C++程序设计的基础概念,包括数据结构、算法设计、面向对象编程等核心内容,并通过多个实例展示了如何使用C++解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、程序设计工作主要包括数据结构和算法的设计。算法要由一系列控制结构组成

2、c语言已经不能满足运用面向对象方法开发软件的需要。c++比c更安全,c++的编译系统能检查出更多的类型错误

3、对象是程序的基本单位,对象的静态属性往往需要用某种类型的数据来表示,对象的动态属性要由成员属性来实现,而函数的实现归根到底还是算法的设计

4、2_1.cpp

#include<iostream>
using namespace std;
int main(){
	cout<<"Hello"<<endl;
	cout<<"Welcome to C++!"<<endl;
	return 0;
}
2_2.cpp

#include<iostream>
using namespace std;
int main(){
	int year;
	bool isLeapYear;
	
	cout<<"Enter the year:";
	cin>>year;
	isLeapYear=((year%4==0 && year%100!=0) || (year%4==0));
	
	if(isLeapYear)
		cout<<year<<"is a leap year"<<endl;
	else
		cout<<year<<"is not a leap year"<<endl;
	
	return 0;
}

2_3.cpp

#include<iostream>
using namespace std;
int main(){
	int x,y;
	cout<<"Enter x and y:";
	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;
}

5、函数是c++程序中最小的功能单位

6、我们编写计算机程序,目的就是为了解决客观世界中的现实问题

7、

//2_4.cpp
#include<iostream>
using namespace std;

int main() {
	int day;
	
	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;
}

//2_5.cpp
#include<iostream>
using namespace std;

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

//2_6.cpp
#include<iostream>
using namespace std;

int main() {
	int n,right_digit,newnum=0;
	cout<<"Enter the number:";
	cin>>n;
	
	cout<<"The number in reverse order is:";
	do{
		right_digit=n%10;
		cout<<right_digit;
		n/=10;
	}while(n!=0);
	cout<<endl;
	
	return 0;
	
}

//2_7.cpp
#include<iostream>
using namespace std;

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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值