【发布时间】:2015-06-23 15:28:25
【问题描述】:
我正在使用两个类创建一个基于类的库存系统:一个用于角色,一个用于物品。
Ryan Newell 编写的代码 - NEW14136796 Uclan 学生 (需要把它放进去,这样抄袭监视器就不会标记我抄袭了我自己的作品)
这是我到目前为止所得到的。
///Code by Ryan Newell - NEW14136796 Runshaw College - Uclan
#include<iostream> //Base C++ required #
#include<string> //Allows input of strings
#include<iomanip> //Base C++ required #
#include <conio.h> //Getchar error workaround
#include <cstdlib> // Provides EXIT_SUCCESS
#include <fstream> // Allows output of txt files
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <utility>
#include <Windows.h>//ConsoleSleep
using namespace std;
class itemspell{
string itemname; //Name ((( what if items are the same name ? make difficult for finding statements ? maybe more char with same name item ?
string itemtype; // Spell/Weapon/Armour/Magic Misc?
string itemact; // What type of action does this item do ?
string itemdesc; // Some random ingame description
itemspell* inext; //The next pointer location
bool isspell; //Is Spell Boolean
bool isweapon; // Weapon
bool isarmour; // So on so forth
bool ismisc; // Misc
bool offensiveitem; //Offensive item
bool defensiveitem; // defensive item
bool passiveitem; // Passive effect
int damage;
int armour;
int magicbonus;
int magicresistance;
int cost;
void item_spellsearch(itemspell* ihead, itemspell* &ipast, itemspell* &icurrent)
{
string search;
if (ihead==NULL)
{
cout<<"There are no items on the list to search"<<endl;
}
else
{
cout<<"Enter the type item you are searching for :"<<endl;
getline(cin,search);
icurrent=ihead; //current pointer goes to -> header
cout<<icurrent<<" "<<search<<" "<<icurrent->itemtype<<endl;
while(icurrent!=NULL && search>icurrent->itemname)
{
cout<<"Item Type:"<<icurrent->itemtype<<" "<<cout<<icurrent->itemname<<endl;
ipast=icurrent; //Past = new current pointer
icurrent=icurrent->inext; //current pointer = next pointer
}
}
}
public:
itemspell()//Building a new item // -- Cannot put byref / by values in here remember, needs to be default constructor for some reason ??
{
cout<<"Enter Item Type: "<<endl;
getline(cin,itemtype);
cout<<"Enter Item Name: "<<endl;
getline(cin,itemname);
cout<<"Enter Item Description: "<<endl;
getline(cin,itemdesc);
cout<<"Enter Item Action :"<<endl;
getline(cin,itemact);
}
~itemspell()//Delete record output
{
cout<<"Item being deleted"<<endl;
Sleep(500);
cout<<"Item deleted"<<endl;
getch();
}
void additemspell(itemspell* &ihead) // Add record code
{
itemspell *icurrent;
itemspell *ipast;
itemspell *newitem;
newitem = new itemspell; // New itemspell
if (ihead == NULL || newitem->itemname<ihead->itemname) //If no header or itemname is null;
{
newitem->inext = ihead;
ihead = newitem;
}
else
{
icurrent= ihead;
while(icurrent !=NULL&&icurrent->itemname<newitem->itemname) // While the current char Does not equal NULL/"Nothing and charcurrent itemname = newitemitemname"
{
ipast=icurrent;
icurrent=icurrent->inext;
}
newitem->inext=icurrent; // Sets the next char to current and the past char to next to add in new record.
ipast->inext=newitem;
}
}
void deleteitemspell(itemspell* &ihead) /// Getting the address of the itemspell not the itemspell itself
{
itemspell *ipast=NULL, *icurrent=NULL;
item_spellsearch(ihead, ipast, icurrent);
if(ihead!=NULL)
if(icurrent==NULL)
{
cout<<"ERROR: No itemspell Found"<<endl;
Sleep(500);
cout<<"returning to menu... press enter"<<endl;
getch();
}
else
{
if (icurrent == ihead)
{
ihead = ihead->inext;
}
else
{
ipast->inext=icurrent->inext; //Resets the pointer and header's back to previous positions....
delete icurrent;// Calls the deletion of the itemspell entry.
cout<<"itemspell found was deleted press enter to confirm"<<endl;
getch();
}
}
}
class character
{
string forename;
string surname;
string nickname;
string race;
string alignment;
string alignment_type;
string character_class;
int Strength; //Strength
int Intelligence; //Magick 2
int Willpower; //Magick 1
int Endurance; //Health
int Agility; //Agility
int StartingWealth; //StartingWealth
character* char_itemp; // Character - Item pointer - Adress location of the characters items
character* char_next; // Next Pointer Value- Points to the location of the next person in the Dynamic Linked List
我想要做的是,当我创建一个新项目时,我希望能够将该项目提供给一个角色使用。
我知道我需要在字符类中创建一个指针值以指向 itemspell 类,但我不确定该做什么并且可以在一些帮助下做。
本质上,当从项目类中的构造函数调用项目类时,我想让字符类指向项目类。
如果可以,请不要只提供代码。相反,请突出显示我应该更改的区域并提供建议示例。我的学位课程严禁抄袭。
伪代码
When newitemspell created
get input charactername ()
:
if no current itemspell created for that character
add item to
character->charactername
else
add item to next pointer character->charactername
【问题讨论】:
-
@bearsmahoney 如果你想看看这个?
-
一堵代码墙和一个模糊的问题陈述不会给你很多答案。你都尝试了些什么?造成了哪些问题?如果第一个问题的答案是“什么都没有”,那么你会过得很糟糕。
-
@user657267 我尽可能地缩短了它,你读过底部吗? - 除此之外,我尝试在字符类中创建一个数组,我尝试创建一个公共函数来获取字符类中的项目,但我不知道从哪里开始。
-
@user657267 - 这也不适合程序员。 OP 对基于伪代码的答案更感兴趣,这意味着它是一个实现问题,因此与 Progs 无关。根据我的阅读,您已经在之前的评论“给你的角色类一个
addItem方法”中提到了概念方面。 -
@user657267 - 您可能会发现这篇 Meta.Progs 帖子很有帮助:meta.programmers.stackexchange.com/questions/7182/…
标签: c++ class dynamic linked-list inventory