0
点赞
收藏
分享

微信扫一扫

C++学习记录一

静守幸福 2022-03-12 阅读 74
c++学习

目录

 第一题代码:

问题记录: 

2.第二问代码: 

问题记录: 

4.错误


 

碎碎念:上来就留这种作业我都麻了,基础都没学... 

  1.  第一题代码:

#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printBubbleArr(int arr[],int k){
	for(int i=0;i<k;i++){
		cout<<arr[i]<<endl;
	}
}
void bubbleSortPrint(){
	int *const bubbleArr = new int[10];
	//cout<<"数组大小"<<endl; 
	int k =10;
	//cout<<k<<endl; 
	for (int i=0;i<k;i++){
		//cout<<"请输入第"+i+"个内容"<<endl; 
		cin>>bubbleArr[i];
	}
	//print out array:
	cout<<"数组初始内容:"<<endl;	
	printBubbleArr(bubbleArr,k);
	// bubble sort it :	
	int temp;
	bool flag=true;
	for(int i=1;i<k&&flag;i++){
		flag=false;
		for(int j=0;j<k;j++){
			if(bubbleArr[j]>bubbleArr[j+1]){//desc and exchange
				temp=bubbleArr[j];
				bubbleArr[j]=bubbleArr[j+1];
				bubbleArr[j+1]=temp;
				flag=true;
			}
		}
	}
	cout<<"排序后结果"<<endl; 
	printBubbleArr(bubbleArr,k); 
	//release space:
	delete []bubbleArr;
}

int main(int argc, char** argv) {
	bubbleSortPrint();
}

问题记录: 

 1.新建使用数组堆空间(动态空间):

http://c.biancheng.net/view/2199.html

要使用指针,因为指针不断移动位置,所以使用常指针能最后的时候确保为首地址

https://blog.csdn.net/m0_37884601/article/details/81174621

2.sizeof的用法以及C++语言数组如何确定长度:

https://www.cnblogs.com/huolong-blog/p/7587711.html

确定数组长度:

https://www.runoob.com/note/29489

https://blog.csdn.net/qing101hua/article/details/52180445

3.代码已修改仍执行原未修改代码:

保存已修改.cpp,删除.exe重新生成

4.自定义函数写在main之前,函数套函数,内层放在前面写

5.布尔类型是bool

6.仍存在问题:

  • 函数原型复习
  • 如何函数返回数组

2.第二问代码: 

#include <iostream>
#include <math.h>
using namespace std; 
class C3Point{
	float x,y,z;
	public:
		C3Point(float c_x,float c_y,float c_z) {
			x=c_x;
			y=c_y;
			z=c_z;
		}
		float Calculation(){
			return sqrt(pow(x,2)+pow(y,2)+pow(z,2));
		}	
	
};
int main(int argc, char** argv) {
	C3Point point(1,3,5);
	cout<<point.Calculation()<<endl;

问题记录: 

1.构造函数不用this,形参避免和成员变量同名。调用方法:

https://zhuanlan.zhihu.com/p/168787937

2.pow、sqrt函数在math.h头文件内

#include "math.h"

3.记得接受函数结果 

4.错误

 C语言--[Error] ld returned 1 exit status--解决方法。_字母的小草的博客-CSDN博客_ldreturned1exitstatus怎么解决

练习时写了多个main,要注掉,把其他不需要运行的改成main01,main02......

5.仍存在问题:

  • 如何在main文件引用其他.cpp定义类
举报

相关推荐

0 条评论