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...
-
Make a control volume around the turbine in the steam power plant in Fig. 1.1 and list the flows of mass and energy that are there.
-
Suggest reasonable explanations for each of the following observations: (a) The CO stretching frequency of a, -unsaturated ketones (about 1675 cm-1) is less than that of typical dialkyl ketones...
-
Maribel Baltazar was hired by clothing retail merchandiser Forever 21 in 2007. During the hiring process, Baltazar was given an 11-page document to sign, two pages of which contained an arbitration...
-
Garfield Company purchased, as a held-to-maturity investment, $80,000 of the 9%, 5-year bonds of Chester Corporation for $74,086, which provides an 11% return. Prepare Garfields journal entries for...
-
There are four types of relational models: communal sharing, authority ranking, equality matching, and market pricing. Which relational models do you use in your interpersonal interactions? Is one...
-
One way to see whether this procedure will be successful is to split the original data set into two subsets: one subset for estimation and one subset for validation. A regression equation is...
-
-. (10pt) (a.) Give a context free grammar for the following language {ak-18k-1 | k 1}. (b.) State the pumping lemma for context free languages. (c.) Prove that {02k13k 25k | k 1}. is not context...
-
oday's exchange rate between British pounds and US dollars is 1:1.29, meaning 1 pound is worth 1.29 dollars. For simplicity, we say the exchange rate between pound and dollar is 1.29. Consider a...
-
Question 1 (1 point) Listen A long-term care resident is to take 15 mL of a medication 3 times a day. How many teaspoons (tsp) will this patient need for a 3 day supply of this medication based on...
-
Just by having money doesn't make a country rich. It's whatthe country can produce that makes a country rich. Inflation/deflation is all around us. Have you notice any price changes in the everyday...
-
L.A. is a bigger market than Chicago. Los Angeles fans will spend money on baseball if the Dodgers are a good team. The Chicago fans will spend money on the cubs no matter how good they are. Draw a...
-
A study of fast-food nutrition compared the caloric content ofFrench Fries. Thirty-eight randomly selected servings of BurgerKing medium fries had a mean of 360 calories and a standarddeviation of 50...
-
David, a calendar year married taxpayer, files a joint return for 2017. Information for the year includes the following: AGI (including wagering gains) $371,800 State income taxes 15,000 Charitable...
-
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
-
Classify the following topics as primarily macroeconomic or microeconomic: 1. The impact of a tax increase on aggregate output. 2. The relationship between two competing firms pricing behavior. 3. A...
-
Use the high and low volatility scenarios that we used for the call option to show that put options also are worth more when stock price volatility is higher.
-
In light of this discussion, explain why the put-call parity relationship is valid only for European options on non-dividend-paying stocks. If the stock pays no dividends, what inequality for...
Study smarter with the SolutionInn App