#endif #include< iostream> 使用std :: cin; 使用std: :cout; 使用std :: endl; #include< string> 使用std :: string; #include" List.h" //测试列表的功能 模板< typename T> void testList(List< T& listObject,const string& typeName) { cout<< 测试列表 << typeName<< " values \ n" ;; instructions(); //显示说明 int choice; //商店用户选择 T值; //存储输入值 执行//执行用户选择的操作 { cout<< " ;? " ;; cin>选择; 开关(选择) { case 1://开头插入 cout<< 输入 << typeName<< ":"; cin> value; listObject.insertAtFront(value); listObject.print(); 休息; 案例2://在结尾插入 cout<< 输入 << typeName<< " ;:" ;; cin> value; listObject.insertAtBack(value); listObject.print(); 休息; 案例3://从开头删除 if(listObject.removeFromFront(value)) cout <<值<< "从列表中删除\ n" ;; listObject.print(); break; case 4://删除从结束 if(listObject.removeFromBack(value)) cout<<值<< "从列表中删除\ n" ;; listObject.print(); break; } //结束开关 } while(选择!= 5); //结束做...而 cout<< 结束列表test\\\\ n;; } //结束函数testList 无效指令() { cout<< 输入以下内容之一:\ n" << " 1在列表的开头插入\ n" << " 2在列表末尾插入\ n" << " 3从列表开头删除\ n" << " 4从列表末尾删除\ n" << " 5到结束列表处理\ n" ;; } //结束函数指令 int main() { //测试int值列表 列表< int intList; testList(intList," integer"); // test字符串值列表 List< string stringList; testList(stringList," string"); 返回0; } //结束主 我希望能够包含我的电话号码类,这样就可以接受 电话号码作为输入。这是电话课 #ifndef PHONENUMBER_H #define PHONENUMBER_H #include< iostream> 使用std :: ostream; 使用std :: istream; #include< string> 使用std :: string; #include< iomanip> 使用std :: setw; class PhoneNumber { 朋友ostream& operator<<(ostream&,const PhoneNumber&); 朋友istream& operator>>(istream&,PhoneNumber&); private: string areaCode; // 3位数区号 字符串交换; // 3位数交换 string line; // 4位数线 }; //结束类PhoneNumber ostream& operator<<(ostream& output,const PhoneNumber& number) { 输出<< "("<<< number.areaCode<<")" << number.exchange<< " - " << number.line; 返回输出; //启用cout<< a<< b<< c; } //结束函数运算符<< istream& operator>>(istream& input,PhoneNumber& number) { input.ignore(); //跳过( 输入> setw(3)> number.areaCode; //输入区域代码 input.ignore(2); // skip)和空间 输入> setw(3)> number.exchange; //输入交换 input.ignore(); // skip dash( - ) input> setw(4)> number.line; //输入行 返回输入; //启用cin> a> b> c; } //结束函数运算符>> #endif 解决方案 * B. Williams: 我需要帮助,包括课程以便 它将允许输入格式正确的电话号码。 尝试更具体一点。你想让别人为你写代码 吗? - 答:因为它弄乱了订单人们通常会阅读文字。 问:为什么这么糟糕? A:热门帖子。 问:什么是usenet和电子邮件中最烦人的事情是? " Alf P. Steinbach" < al *** @ start.nowrote in message news:4t ************* @ mid.individual.net ... > * B. Williams: >我需要帮助,包括一个类,以便它允许输入一个格式正确的电话号码。 尝试更具体一点。你想让别人为你写代码 吗? - 答:因为它弄乱了订单人们通常会阅读文字。 问:为什么这么糟糕? A:热门帖子。 问:什么是usenet和电子邮件中最烦人的事情是什么? 我为不具体而道歉。我知道这两项工作都是独立的,但我需要帮助将电话号码等级加入到程序中。我无法找到更好的方法来获得该计划 接受电话号码。我尝试输入电话号码作为字符串,但是空白空格结束字符串 。我想我在写代码时要求帮助,但我只是需要一个启动。这是测试 电话号码类的代码。 #include< iostream> 使用std :: cout ; 使用std :: cin; 使用std :: endl; #include" PhoneNumber.h" int main() { PhoneNumber手机; cout< < 在表格(123)456-7890中输入电话号码: <<结束; cin>电话; cout<< 输入的电话号码是:" ;; cout<<电话<< endl; 返回0; } //结束主。 B. Williams< wi ******* @ hotmail.comwrote: 我尝试输入电话号码作为字符串,但是 空格结束字符串。 尝试使用std :: getline()代替。 - Marcus Kwok 将''invalid''替换为''net''以回复 I have written some code to accept input and place this input at thebeginning or end of a list, but I need assistance including a class so thatit will allow input of a phone number that is properly formatted. I''ll postmy code below. Thanks in advance. This is the code for the linked list. It tests using int''s and string''s. #ifndef LIST_H#define LIST_H #include <iostream>using std::cout; #include "Listnode.h" template< typename NODETYPE >class List{public:List();~List();void insertAtFront( const NODETYPE & );void insertAtBack( const NODETYPE & );bool removeFromFront( NODETYPE & );bool removeFromBack( NODETYPE & );bool isEmpty() const;void print() const;private:ListNode< NODETYPE *firstPtr;ListNode< NODETYPE *lastPtr; // utility function to allocate new nodeListNode< NODETYPE *getNewNode( const NODETYPE & );}; // end class List // default constructortemplate< typename NODETYPE >List< NODETYPE >::List(): firstPtr( 0 ), lastPtr( 0 ){} // destructortemplate< typename NODETYPE >List< NODETYPE >::~List(){if ( !isEmpty() ) // List is not empty{cout << "Destroying nodes ...\n"; ListNode< NODETYPE *currentPtr = firstPtr;ListNode< NODETYPE *tempPtr; while ( currentPtr != 0 ) // delete remaining nodes{tempPtr = currentPtr;cout << tempPtr->data << ''\n'';currentPtr = currentPtr->nextPtr;delete tempPtr;} // end while} // end if cout << "All nodes destroyed\n\n";} // end List destructor // insert node at front of listtemplate< typename NODETYPE >void List< NODETYPE >::insertAtFront( const NODETYPE &value ){ListNode< NODETYPE *newPtr = getNewNode( value ); // new node if ( isEmpty() ) // List is emptyfirstPtr = lastPtr = newPtr; // new list has only one nodeelse // List is not empty{newPtr->nextPtr = firstPtr; // point new node to previous 1st nodefirstPtr = newPtr; // aim firstPtr at new node} // end else} // end function insertAtFront // insert node at back of listtemplate< typename NODETYPE >void List< NODETYPE >::insertAtBack( const NODETYPE &value ){ListNode< NODETYPE *newPtr = getNewNode( value ); // new node if ( isEmpty() ) // List is emptyfirstPtr = lastPtr = newPtr; // new list has only one nodeelse // List is not empty{lastPtr->nextPtr = newPtr; // update previous last nodelastPtr = newPtr; // new last node} // end else} // end function insertAtBack // delete node from front of listtemplate< typename NODETYPE >bool List< NODETYPE >::removeFromFront( NODETYPE &value ){if ( isEmpty() ) // List is emptyreturn false; // delete unsuccessfulelse{ListNode< NODETYPE *tempPtr = firstPtr; // hold tempPtr to delete if ( firstPtr == lastPtr )firstPtr = lastPtr = 0; // no nodes remain after removalelsefirstPtr = firstPtr->nextPtr; // point to previous 2nd node value = tempPtr->data; // return data being removeddelete tempPtr; // reclaim previous front nodereturn true; // delete successful} // end else} // end function removeFromFront // delete node from back of listtemplate< typename NODETYPE >bool List< NODETYPE >::removeFromBack( NODETYPE &value ){if ( isEmpty() ) // List is emptyreturn false; // delete unsuccessfulelse{ListNode< NODETYPE *tempPtr = lastPtr; // hold tempPtr to delete if ( firstPtr == lastPtr ) // List has one elementfirstPtr = lastPtr = 0; // no nodes remain after removalelse{ListNode< NODETYPE *currentPtr = firstPtr; // locate second-to-last elementwhile ( currentPtr->nextPtr != lastPtr )currentPtr = currentPtr->nextPtr; // move to next node lastPtr = currentPtr; // remove last nodecurrentPtr->nextPtr = 0; // this is now the last node} // end else value = tempPtr->data; // return value from old last nodedelete tempPtr; // reclaim former last nodereturn true; // delete successful} // end else} // end function removeFromBack // is List empty?template< typename NODETYPE >bool List< NODETYPE >::isEmpty() const{return firstPtr == 0;} // end function isEmpty // return pointer to newly allocated nodetemplate< typename NODETYPE >ListNode< NODETYPE *List< NODETYPE >::getNewNode(const NODETYPE &value ){return new ListNode< NODETYPE >( value );} // end function getNewNode // display contents of Listtemplate< typename NODETYPE >void List< NODETYPE >::print() const{if ( isEmpty() ) // List is empty{cout << "The list is empty\n\n";return;} // end if ListNode< NODETYPE *currentPtr = firstPtr; cout << "The list is: "; while ( currentPtr != 0 ) // get element data{cout << currentPtr->data << '' '';currentPtr = currentPtr->nextPtr;} // end while cout << "\n\n";} // end function print #endif #ifndef LISTNODE_H#define LISTNODE_Htemplate< typename NODETYPE class List; template< typename NODETYPE >class ListNode{friend class List< NODETYPE >; public:ListNode( const NODETYPE & );NODETYPE getData() const;private:NODETYPE data;ListNode< NODETYPE *nextPtr; // next node in list}; // end class ListNode // constructortemplate< typename NODETYPE >ListNode< NODETYPE >::ListNode( const NODETYPE &info ): data( info ), nextPtr( 0 ){} // end ListNode constructor // return copy of data in nodetemplate< typename NODETYPE >NODETYPE ListNode< NODETYPE >::getData() const{return data;} // end function getData #endif #include <iostream>using std::cin;using std::cout;using std::endl; #include <string>using std::string; #include "List.h" // function to test a Listtemplate< typename T >void testList( List< T &listObject, const string &typeName ){cout << "Testing a List of " << typeName << " values\n";instructions(); // display instructions int choice; // store user choiceT value; // store input value do // perform user-selected actions{cout << "? ";cin >choice; switch ( choice ){case 1: // insert at beginningcout << "Enter " << typeName << ": ";cin >value;listObject.insertAtFront( value );listObject.print();break;case 2: // insert at endcout << "Enter " << typeName << ": ";cin >value;listObject.insertAtBack( value );listObject.print();break;case 3: // remove from beginningif ( listObject.removeFromFront( value ) )cout << value << " removed from list\n"; listObject.print();break;case 4: // remove from endif ( listObject.removeFromBack( value ) )cout << value << " removed from list\n"; listObject.print();break;} // end switch} while ( choice != 5 ); // end do...while cout << "End list test\n\n";} // end function testListvoid instructions(){cout << "Enter one of the following:\n"<< " 1 to insert at beginning of list\n"<< " 2 to insert at end of list\n"<< " 3 to delete from beginning of list\n"<< " 4 to delete from end of list\n"<< " 5 to end list processing\n";} // end function instructions int main(){// test List of int valuesList< int intList;testList( intList, "integer" ); // test List of string valuesList< string stringList;testList( stringList, "string" ); return 0;} // end main I want to be able to include my phone number class so that this will acceptphone numbers as input. Here is the phone class #ifndef PHONENUMBER_H#define PHONENUMBER_H #include <iostream>using std::ostream;using std::istream; #include <string>using std::string; #include <iomanip>using std::setw; class PhoneNumber{friend ostream &operator<<( ostream &, const PhoneNumber & );friend istream &operator>>( istream &, PhoneNumber & );private:string areaCode; // 3-digit area codestring exchange; // 3-digit exchangestring line; // 4-digit line}; // end class PhoneNumber ostream &operator<<( ostream &output, const PhoneNumber &number ){output << "(" << number.areaCode << ") "<< number.exchange << "-" << number.line;return output; // enables cout << a << b << c;} // end function operator<< istream &operator>>( istream &input, PhoneNumber &number ){input.ignore(); // skip (input >setw( 3 ) >number.areaCode; // input area codeinput.ignore( 2 ); // skip ) and spaceinput >setw( 3 ) >number.exchange; // input exchangeinput.ignore(); // skip dash (-)input >setw( 4 ) >number.line; // input linereturn input; // enables cin >a >b >c;} // end function operator>> #endif 解决方案 * B. Williams:I need assistance including a class so thatit will allow input of a phone number that is properly formatted.Try to be a little more specific. Do you want someone to write the codefor you? --A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail?"Alf P. Steinbach" <al***@start.nowrote in messagenews:4t*************@mid.individual.net...>* B. Williams:>I need assistance including a class so that it will allow input of aphone number that is properly formatted. Try to be a little more specific. Do you want someone to write the codefor you?--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail?I apologize for not being more specific. I know both of these workindependently, but I need help with incoporating the phone number class intothe program. I couldn''t figure out any better way to get the program toaccept phone numbers. I tried inputting the phone number as a string, butthe blank space ends the string. I guess I am asking for some assistance onwriting the code, but I just need a kick start. This is the code to test thephone number class. #include <iostream>using std::cout;using std::cin;using std::endl; #include "PhoneNumber.h" int main(){PhoneNumber phone; cout << "Enter phone number in the form (123) 456-7890:" << endl; cin >phone; cout << "The phone number entered was: "; cout << phone << endl;return 0;} // end main.B. Williams <wi*******@hotmail.comwrote: I tried inputting the phone number as a string, butthe blank space ends the string.Try using std::getline() instead. --Marcus KwokReplace ''invalid'' with ''net'' to reply 这篇关于协助链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 07:25