正在加载图片...
Problem 2-Strings(20 points) When large numbers are written out on paper,it is traditional-at least in the United States-to use commas to separate the digits into groups of three.For example,the number one million is usually written in the following form: 1,000,000 To make it easier for programmers to display numbers in this fashion,implement a function string AddCommasToNumericString(string digits); that takes a string of decimal digits representing a number and returns the string formed by inserting commas at every third position,starting on the right.For example,if you were to execute the main program int main (int argc,char *argv[]) { ifstream inFile; ConsoleT console; string digits; bool done false; if (argc ==2)inFile.open (argv[1]); while (!done) if (argc =2)inFile >digits; else digits console.readString ("Enter a string of digits:") if (digits =="exit")done true; else console.printLine (AddCommasToNumericString(digits),endl); inFile.close () your implementation of the AddCommasToNumericString function should be able to produce the following sample run: Enter a string of digits: string AddCommasToNumericString(string &digits) 17 17 if (digits.length ()<4)return digits; Enter a string of digits: for (int i digits.length ()-3;i>0;i-=3) 1001 digits.insert(i,1,); 1,001 return digits; Enter a string of digits: 12345678 12,345,678 Enter a string of digits: 999999999 999,999.999 Enter a string of digits: exit Note that AddCommasToNumericString takes a string rather than an integer.Problem 2—Strings (20 points) When large numbers are written out on paper, it is traditional—at least in the United States—to use commas to separate the digits into groups of three. For example, the number one million is usually written in the following form: 1,000,000 To make it easier for programmers to display numbers in this fashion, implement a function string AddCommasToNumericString(string digits); that takes a string of decimal digits representing a number and returns the string formed by inserting commas at every third position, starting on the right. For example, if you were to execute the main program int main (int argc, char *argv[]) { ifstream inFile; ConsoleT console; string digits; bool done = false; if (argc == 2) inFile.open (argv[1]); while (!done) { if (argc == 2) inFile >> digits; else digits = console.readString ("Enter a string of digits: "); if (digits == "exit") done = true; else console.printLine (AddCommasToNumericString(digits), endl); } inFile.close (); } your implementation of the AddCommasToNumericString function should be able to produce the following sample run: Enter a string of digits: 17 17 Enter a string of digits: 1001 1,001 Enter a string of digits: 12345678 12,345,678 Enter a string of digits: 999999999 999,999,999 Enter a string of digits: exit Note that AddCommasToNumericString takes a string rather than an integer
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有