1 /*2 returns the first n bits of the string and the first n bits of the returned integer3 */4 5#include <iostream>6 7UnsignedLongLeft (unsignedLongNumintn);8 Char* Left (Char* STR,intn);9 Ten using namespacestd; One A intMain () - { -UnsignedLongnum; the intN; -cout <<"Please enter an integer:"; -CIN >>num; -cout <<"Please enter the number of digits to intercept:"; +CIN >>N; -cout <<"before"<< N <<"bit is"<< left (num, n) <<Endl; + A Char* str =New Char( -); atcout <<"Please enter a string:"; -CIN >>str; -cout <<"Please enter the number of digits to intercept:"; -CIN >>N; -cout <<"before"<< N <<"bit is"<< left (str, n) <<Endl; - in //delete[] str; - toCin.Get(); +Cin.Get(); - } the * $UnsignedLongLeft (unsignedLongNumintN)Panax Notoginseng { -UnsignedLongLen =1;//indicates that there are several theUnsignedLongTemp_num =num; + while(Temp_num/=Ten) A { thelen++; + } -UnsignedLongCut_num_len = Len-(unsignedLong(n) >= Len? len:unsignedLong(n));//when n is less than the length of an integer, the original integer is retrieved directly $ while(cut_num_len--) $ { -Num/=Ten; - } the returnnum; - }Wuyi the Char* Left (Char* STR,intN) - { Wu intLen =strlen (str); -n = n >= len?len:n; About Char* Temp_char =New Char(n +1); $ - for(inti =0; I < n; i++) { -* (Temp_char + i) = * (str +i); - } ATemp_char[n] =' /'; + returnTemp_char; the}
Everybody look down, there's a problem when I cancel //delete[] str; Note, the program will error, not to encourage if it is a new self-made pointer is the best to delete it? Ask the right answer.
C + + Quiz code