To count open blocks, we need to count how many curly braces get opened and closed...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
To count open blocks, we need to count how many curly braces get opened and closed on each line, so we need to count { and } Write a function int countChar(string line, char c); that scans the line and returns the number of occurrences of the character c. Write a new program indent.cpp that enhances the program from the previous task. As it reads the input line by line, it should also count the number of open and closed { } in it, and keep track of how many blocks is currently open at the beginning of each line. In the listing below, the number of open blocks is shown on the left: int main(){ 1 // Hi, I'm a program! int x - 1; 1 for (int i = e; i < 10; i+) { cout << i; 2 cout << endl3; } 1 Then instead of printing the number of open blocks, add that number of tabs '\t' at the beginning of each line, and you will get: int main(){ // Hi, I'm a program! int x = 1; for (int i = 0; i < 10; i++) { cout << i; cout << endl; } // <-- closing for loop } // <-- closing main Notice that the closing curly braces are indented one level further than what they should be. To fix that, when indenting the line, check its very first character. If it is a closing curly brace }, its indentation level should be reduced by one: int main(){ // Hi, I'm a program! int x = 1; for (int i = 0; i < 10; i++) { cout << i; cout << end%; } Shortcomings of our program: • No support for loops and if statements that don't have curly braces. For example, if (c -- 'A') S = S + C; will be incorrectly indented as if (c 'A') == S = s + C; No support for // and * */. Commented out curly braces should not affect indentation Symbols {, } inside string and char literals are misinterpreted as blocks if (true) { s = "{{"; t = "ABC"; } will be incorrectly indented as if (true) { s = "{{"; t = "ABC"; } To count open blocks, we need to count how many curly braces get opened and closed on each line, so we need to count { and } Write a function int countChar(string line, char c); that scans the line and returns the number of occurrences of the character c. Write a new program indent.cpp that enhances the program from the previous task. As it reads the input line by line, it should also count the number of open and closed { } in it, and keep track of how many blocks is currently open at the beginning of each line. In the listing below, the number of open blocks is shown on the left: int main(){ 1 // Hi, I'm a program! int x - 1; 1 for (int i = e; i < 10; i+) { cout << i; 2 cout << endl3; } 1 Then instead of printing the number of open blocks, add that number of tabs '\t' at the beginning of each line, and you will get: int main(){ // Hi, I'm a program! int x = 1; for (int i = 0; i < 10; i++) { cout << i; cout << endl; } // <-- closing for loop } // <-- closing main Notice that the closing curly braces are indented one level further than what they should be. To fix that, when indenting the line, check its very first character. If it is a closing curly brace }, its indentation level should be reduced by one: int main(){ // Hi, I'm a program! int x = 1; for (int i = 0; i < 10; i++) { cout << i; cout << end%; } Shortcomings of our program: • No support for loops and if statements that don't have curly braces. For example, if (c -- 'A') S = S + C; will be incorrectly indented as if (c 'A') == S = s + C; No support for // and * */. Commented out curly braces should not affect indentation Symbols {, } inside string and char literals are misinterpreted as blocks if (true) { s = "{{"; t = "ABC"; } will be incorrectly indented as if (true) { s = "{{"; t = "ABC"; }
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Write a function called hms_to_secs() that takes three int values for hours, minutes, and secondsas arguments, and returns the equivalent time in seconds (type long). Function prototypes is given...
-
Write a program that displays a single character in all possible combinations of foreground and background colors (16 x 16 = 256). The colors are numbered 0 to 15, so you can use a nested loop to...
-
How do open feedwater heaters differ from closed feedwater heaters?
-
A semicircular plate of radius r, oriented as in the figure, is submerged in fluid of density 68 lb/ft 3 so that its diameter is located at a depth of m feet. Calculate the force on one side of the...
-
The repeated-measures ANOVA can be viewed as a two-stage process. What is the purpose of the second stage?
-
What is the value of understanding the central Limit theorem?
-
The balance sheet of Morrisey Management Consulting, Inc., at December 31, 2007, reported the following stockholders' equity: During 2008, Morrisey completed the following selected transactions:...
-
The following information concerns production in the Forging Department for June.The Forging Department uses the average cost method. a. Determine the cost per equivalent unit.b. Determine the cost...
-
Harms Inc. reported in its 2006 annual report the following information: Plan Status: December 31, 2006 Accumulated Benefit Obligation (ABO) $ 90 Million Projected Benefit Obligation (PBO) $ 95...
-
Kia Lopez (SSN 412-34-5670) resides at 101 Poker Street, Apt. 12A, Hickory, FL 34714. Her W-2 shows the following: Wages $ 56,500 Federal withholding 6,100 Social security wages 56,500 Social...
-
Use the dataset given at the end of the homework to answer the following questions. a) Construct a stem-and-leaf plot for that sample. (5 pts) b) Find the sample median, Q1, Q3, and IQR. Show work...
-
What competitive strategies is Crayola pursuing? How does digital technology support those strategies?
-
If the predecessor in a start-to-start dependency ends very late, what problem arises? Select an answer: The predecessor's late finish doesn't cause a problem. The successor finish date is delayed...
-
A coil of mass M = 3m is wound by a weightless rope and is at rest on a horizontal rough surface. The moment of inertia with respect to the axis passing through its center of mass, where R is the...
-
9. Three point-like charges are placed at the following points on the x-y system coordinates (q1 is fixed at x = -1.00 cm, 92 is fixed at y = +2.00 cm, and q3 is fixed at x = +3.00 cm. Find the...
-
At the beginning of the year, Golden Gopher Company reports a balance in Supplies of $430. During the year, Golden Gop purchases an additional $2,600 of supplies for cash. By the end of the year,...
-
Economists are saying that currently, Bangladesh's GDP is lower than it is supposed to in the long run, hence prices are falling. Given the above situation, in which state is the economy in? In which...
-
Orange juice producers are dismayed and puzzled. An economist told them that the reason the demand for orange juice fell is that a new technology allow tomato producers to pick ripe tomatoes more...
-
The truck has a mass of 4 Mg and mass center at G 1 , and the trailer has a mass of 1 Mg and mass center at G 2 , Determine the absolute maximum live moment developed in the bridge. G2 l1,5 ml1.5 ml...
-
Determine the maximum moment in the suspended rail at point B if the rail supports the load of 2.5 k on the trolley. 8 ft- 8 ft -6 ft -6 ft B, A. 1 ft|2 ft 2.5 k
-
Determine the horizontal displacement of joint D. Assume the members are pin connected at their end points. AE is constant. Use the method of virtual work. 8 m 2k D. 6 ft 3k 6 ft
-
An automobile's suspension system determines the vehicle's response to inputs from uneven road surfaces. Figure 3.56 shows a drawing of a suspension system that includes a coil spring, a shock...
-
For a mass-spring-damper system under harmonic loading per unit mass \(F(t) / m=\cos \omega t \mathrm{ft} / \mathrm{s}^{2}\), solve for the response amplitude for the case where \(k=\) \(20...
-
For the system in Figure 3.57, the forcing per unit mass is \(F(t) / m=10 \sin 15 t \mathrm{ft} / \mathrm{s}^{2}\). Solve for the transient response, the steady-state response, and the constants of...
Study smarter with the SolutionInn App