栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言练习内存管理和排序和读写文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言练习内存管理和排序和读写文件

申请的内存管理是可以被返回的

// ConsoleApplication9.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include 
#include 
#include  
#include 
using namespace std;

struct Student
{
	int number; //xuehao
	char name[1024];
	double yuwen;
	double shuxue;
	double yingyu;
};

class MyClass
{
public:
	MyClass();
	~MyClass();
	
	
private:
	
};

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}
int *description;

int *cinmyArray(int array[])
{
	cout << "请输入10个数字" << endl;
	description = (int *)malloc(10 * sizeof(int));
	if (description == NULL)
	{
		fprintf(stderr, "Error - unable to allocate required memoryn");
	}
	else
	{
		for (int  i = 0; i < 10; i++)
		{
			cin >> description[i];
		}
	}
	cout << "输入完毕" << endl;
	return description;
}

int *sortMyArray(int *description)
{
	for (int i = 0; i<10; i++) {
		for (int j = i + 1; j<10; j++) {
			if (description[i]>description[j]) {//若前一个元素比后一个元素大,则借助临时变量temp交换(此排序为升序排序,若为降序排序则为arr[i]
				int temp = description[i];
				description[i] = description[j];
				description[j] = temp;
			}
		}
	}
	return description;
}

void adjust(int a[], int n)
{
	for (int i = 0; i
		//找最大值
		if (i % 2 == 0)
		{
			int j = i + 1;
			//记录最大值的位置
			int loc = i;
			for (; j
				if (a[j]>a[loc])loc = j;
			}
			//发生交换
			if (loc != i)
			{
				int temp = a[loc];
				a[loc] = a[i];
				a[i] = temp;
			}
		}
		//找最小值
		else
		{
			int j = i + 1;
			//记录最小值的位置
			int loc = i;
			for (; j
				if (a[j]
				int temp = a[loc];
				a[loc] = a[i];
				a[i] = temp;
			}
		}
	}
}

int *coutMyArray(int *description)
{
	for (int i = 0; i < 10; i++)
	{
		cout << description[i] << endl;
	}
	return description;
}

int _tmain(int argc, _TCHAR* argv[])
{ 
	

	
	int myArray[10] = { 0 };
	FILE*fp;
	cinmyArray(myArray);
	sortMyArray(description);
	coutMyArray(description);
	cout << "*************" << endl;
	adjust(description, 10);
	coutMyArray(description);


	if ((fp = fopen("stud", "w")) == NULL)
	{
		printf("error :cannot open file!n");
		exit(0);
	}
	for (int i = 0; i<10; i++)
		fprintf(fp, "%dn", description[i]);

	fclose(fp);


	if (description != nullptr) {
		free(description);
		cout << "内存清理完毕" << endl;
	}
	return 0;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938441.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号