Answers to questions reviewed by C/C ++ skills of a Company X

Source: Internet
Author: User

Non-standard answer: only one test case is passed, and then improvements are made.

# Include "stdafx. H "# include <stdio. h> # include <string. h> # include <vector> # include <list> # include <iostream> # include <stack> # include <queue> # include <algorithm> using namespace STD; typedef basic_string <char >:: size_type S_T; int imylistcount = 0; struct mylist {int index; string strdominname; STD: Stack <string> listsondomin;}; STD :: list <mylist> m_list; int add_sondomin (mylist & mylist); bool mysort (const myli St & list1, const mylist & list2); // sorting method int add_host_name (const char * host_name); int get_host_name (INT serial_number, int host_name_max_length, char * host_name ); void clear (void); bool mysort (const mylist & list1, const mylist & list2) // sorting method {STD: Stack <string> templist1; STD :: stack <string> templist2; const char * chlist1; const char * chlist2; templist1 = list1.listsondomin; templist2 = list2.listsondomin; While ((! Templist1.empty ())&&(! Templist2.empty () {chlist1 = templist1.top (). c_str (); chlist2 = templist2.top (). c_str (); While (* chlist1 = * chlist2) & (* chlist1! = '\ 0' & * chlist2! = '\ 0') {chlist1 ++; chlist2 ++;} If (* chlist1) <(* chlist2) {return true; // before templist1} If (* chlist1)> (* chlist2) {return false; // After templist2} templist1.pop (); templist2.pop ();} if (templist1.empty () {return true; // top of templist1} If (templist2.empty () {return false ;}} int add_sondomin (mylist & mylist) {S_T ulstartindex = 0; S_T ulfindindex = 0; S_T Len = 0; bool BGO = true; do {ulfindindex = mylist. strdominname. find ('. ', ulstartindex); If (ulfindindex = string: NPOs) {BGO = false;} Len = ulfindindex-ulstartindex; If (LEN <0) {cout <"ulfindindex is error, please check out \ n" <Endl; Return-1;} mylist. listsondomin. push (mylist. strdominname. substr (ulstartindex, Len); ulstartindex = ulfindindex + 1;} while (BGO); Return 0 ;} /*************************************** * ************************************ description: add the host name input Param: host_name host name string, non-empty string output Param: no return value: 0 is returned successfully, -1 ************************************ **************************************** */INT add_host_name (const char * host_name) {/* implement the function here */mylist templist; templist. index = imylistcount; templist. strdominname = host_name; add_sondomin (templist); m_list.push_back (templist); // cout <templist. strdominname. c_str () <Endl; // templist. strdominname. fin // string: NPOs; return 0 ;} /*************************************** * ************************************ description: obtain the serial number after the host name input Param: serial_number is sorted. The maximum length of host_name_max_length host_name starts from 1, including '\ 0' output Param: host_name host name string, which must include' \ 0 ', memory is allocated and released by the caller. Return Value: 0 is returned for success, and-1 is returned for failure (for example, serial_number is out of range and the maximum length is not enough) **************************************** * ***********************************/INT get_host_name (INT serial_number, int host_name_max_length, char * host_name) {/* implement the function here */m_list.sort (mysort); If (m_list.size () <serial_number) {cout <"input serial_number is wrong" <Endl; Return-1 ;}list <mylist >:: iterator my_iter = m_list.begin (); List <mylist> :: iterator iter_end = m_list.end (); int I = 0; while (I <(serial_number-1) {my_iter ++; I ++;} Char * temp_host_name = (char *) malloc (host_name_max_length); If (null = temp_host_name) {cout <"malloc temp_host_name failed \ n" <Endl; Return-1;} // strcpy (temp_host_name, (* my_iter ). strdominname. c_str (); memcpy (host_name, (const char *) (* my_iter ). strdominname. c_str (), host_name_max_length); // * (temp_host_name + host_name_max_length) = '\ 0'; // strcpy (host_name, temp_host_name); // host_name = temp_host_name; return 0 ;} /*************************************** * ************************************ description: clear all host names input Param: No output Param: no return value: no *************************************** * ***********************************/void clear (void) {m_list.clear ();/* implement the function here */}
// The main function is a test case int main () {If (0 = add_host_name ("mail.w.wei.com") {cout <"add mail.w.wei.com successfully" <Endl ;} if (0 = add_host_name ("Your wei.com") {cout <"add your wei.com successfully" <Endl;} If (0 = add_host_name ("teltalk.org ")) {cout <"add teltalk.org successfully" <Endl;} If (0 = add_host_name ("google.com.hk") {cout <"add google.com.hk successfully" <Endl ;} if (0 = add_host_name ("imail.w.wei.com") {cout <"add imail.w.wei.com successfully" <Endl;} Char out_str [20]; if (0 = get_host_name (4, sizeof (out_str), out_str )) {cout <"Get successfully \ n" <out_str <"\ n" <Endl;} Char A; scanf (& A); return 1 ;}

I am in a hurry and have less comments. Please try again later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.