我想创建重载方法additem()的方法,该方法可以在列表前面添加项目(在数组[0]位置) [英] I want to create method that overloads method additem() that can add item in front of the list(at array [0] location)

查看:95
本文介绍了我想创建重载方法additem()的方法,该方法可以在列表前面添加项目(在数组[0]位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是否正确?因为我后面的输出数字为零......实际上它必须在列表前...希望你能帮助我



我有什么试过:



//List.h



  #ifndef LIST_H 
#define LIST_H

const int maxSize = 100 ;

class 列出
{
public
List();
~List();

int numberOfItem();
void addItem( int item, int position);
void printItem();

private
int size;

int array [maxSize];

};
#endif





//List.cpp

  #include    <   iostream  >  
using namespace std;
#include List.h

List :: List()
{
size = 0 ;
}

List :: ~List()
{}

int 列表:: numberOfItem()
{
return size;
}

void List :: addItem( int 项, int position)
{
if (size == maxSize)
{
cout<< **没有剩下的地方*** \ n;
return ;
}

if (position< 0 || position> size)
{
cout< < invalid position\\\
;
return ;
}

for int i = size - 1 ; i> = position - 1 ; i--)
array [i + 1] = array [i];

array [position- 1 ] = item;
size ++;
}

void List :: printItem()
{
for int i = 0 ; i< size; i ++)
cout<< array [i]<< ;
cout<< \ n;

}





//ListMain.cpp



  #include   <   iostream  >  
使用 命名空间标准;
#include List.h

void main()
{
int 项目,位置;

List ItemList;

if (ItemList.numberOfItem()== 0

cout<< 此列表中没有元素<< ENDL;


for int i = 0 ; i< 10 ; i ++)
ItemList.addItem(i * 2 ,I);

cout<< 加法运算后列表的形成:<< endl;
ItemList.printItem();
cout<< 输入要添加到列表中的数字:;
cin>>项目;
cout<< 输入数字的位置:;
cin>>位置;
ItemList.addItem(item,position);
cout<< 新列表为:;
ItemList.printItem();这是 addItem 功能。看看wgat algotiyhm你使用并与你的代码进行比较。



当你不明白你的代码在做什么或为什么它做它做的时候,答案是< b>调试器。

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Is my code correct? because i got output number zero at the back...actually it must in front of the list...hope u can help me

What I have tried:

//List.h

#ifndef LIST_H
#define LIST_H

const int maxSize = 100;

class List
{
public:
	List();
	~List();

	int numberOfItem();
	void addItem(int item,int position);
	void printItem();

private:
	int size;

	int array[maxSize];

};
#endif  



//List.cpp

#include <iostream>
using namespace std;
#include "List.h"

List::List()
{
	size = 0;
}

List::~List()
{}

int List::numberOfItem()
{
	return size;
}

void List::addItem(int item, int position)
{
	if (size == maxSize)
	{
		cout << "**There is no more place left***\n";
		return;
	}

	if (position<0 || position>size)
	{
		cout << "invalid position\n";
		return;
	}

	for (int i = size - 1; i >= position - 1; i--)
		 array[i+1]=array[i];

	array[position-1] = item;
	size++;
}

void List::printItem()
{
	for (int i = 0; i < size; i++)
	cout << array[i] << " ";
	cout << "\n";

}



//ListMain.cpp

#include <iostream>
using namespace std;
#include "List.h"

void main()
{
	int item, position;

	List ItemList;

	if (ItemList.numberOfItem() == 0)

		cout << "there are no elements in this list" << endl;


	for (int i = 0; i < 10; i++)
		ItemList.addItem(i*2,i);

	cout << "the forming of list after addition operation: "<<endl;
	ItemList.printItem();
	cout << "enter number to be added in the list: ";
	cin >> item;
	cout << "enter position of the number: ";
	cin >> position;
	ItemList.addItem(item, position);
	cout << "the new list are: ";
	ItemList.printItem();

}

解决方案

Take a sheet of paper and simulate what should do the addItem function. see wgat algotiyhm you use and compare with your code.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我想创建重载方法additem()的方法,该方法可以在列表前面添加项目(在数组[0]位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆