《实验报告02》上
删除数组中的指定元素(无数组输入版)
该版本为初始版本:即数组已经固定。虽然功能不完善但也可参考解题的逻辑思路。(晚点更新由用户输入数组数据的版本以及思路)
#include<iostream>
using namespace std;
bool Delete(double Number[], long currentSize, double deleteElement)
{
int num = 1;
for (int i = 0; i < currentSize; i++)//遍历查到需要删除的元素
if (Number[i] == deleteElement)//删除对应的元素
{
for (int j = i; j < 6-num; j++)
Number[j] = Number[j + 1];
Number[6-num] = 0;num++;
}
//判断删除结果
if (num > 1)return true;//删除成功
else return false; //删除失败
}
int main()
{ system("color F0");
double Number[6]={88.8, -16.8, 99.8, -66.8, 88.8, 99.8},deleteElement;
cout << "数组中的元素为:" << endl;
for(int i = 0; i < 6; i++)cout<<Number[i] << " ";
cout << endl << "请输入需要删除的数据: ";
cin >> deleteElement;
if (Delete(Number, 6, deleteElement))
{
cout << "删除成功"<<endl<<"数组删除后的结果为:";
for(int i = 0;Number[i]; i++)cout<<Number[i] << " ";
}
else cout << "删除失败,在数组中找不到指定数据位置" << endl;
system("pause");
return 0;
}