2784: Code formatting (c++)

Title description

The younger brother gave the senior a copy of the code and asked the senior to help debug, but this code is like this:


#include<stdio.h>
int main(){int a,b;int c=a+b;printf(" Hello world!\n");return 0;}


This code can be understood by the machine, but not by the seniors.
There are two problems with this code: there is no newline indentation, and there is no space near the binary operator.
Obviously this is a piece of unformatted code, format it into a format that meets the requirements. The curly braces do not include nesting.
Format requirements:
1. Line break is required
. 2. Four spaces are used as an indentation unit
. 3. The curly braces {do not break lines, followed by the previous statement, but separated by a space.
4. There is no space around the binary operator, because the code of the student only contains the


formatted code of the six binary operators =, +, -, *, /,%   :


#include<stdio.h>
int main() {     int a,b;     int c = a + b;     printf("Hello world!\n");     return 0; }





 

enter

Two lines of code that can be compiled but the format does not meet the requirements, one line is the header file (make sure there is only one header file), and one line is the main function code. The problem with the code is as mentioned above, the code length is less than 1000. The curly braces in the code only contain one pair in the main function. Ensure that there are statements in the main function of the code.
Note: Extra semicolons are not included in the code. Braces and binary operators will not appear in the printf function
 

Output

Code that meets the format requirements

Sample input Copy

#include<stdio.h>
int main(){int a, b;a=9900,b=99;int c=a+b;int d=1,e;int ans=a+b+c/d%a*a-89+e;return 0;}

Sample output Copy

#include<stdio.h>
int main() {
    int a, b;
    a = 9900,b = 99;
    int c = a + b;
    int d = 1,e;
    int ans = a + b + c / d % a * a - 89 + e;
    return 0;
}

Source/Classification

 Zhengzhou University of Light Industry 2020 Freshman Competition  

 

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int i,j;
	char s[1000];
	char ss[1010];
	gets(ss);
	gets(s);
	puts(ss);
	//int l1=strlen(s);//这里可以优化一下 
	for(i=0;i<strlen(s);i++)
	{
		if(s[i]=='+'||s[i]=='='||s[i]=='-'||s[i]=='/'||s[i]=='*'||s[i]=='%')
		printf(" %c ",s[i]);
		else if(s[i]==';')
		{
			if(s[i+1]=='}')
			{
				printf("%c\n",s[i]);
			}
			else
			printf("%c\n    ",s[i]);
		}
		
		else if(s[i]=='{')
		printf(" %c\n    ",s[i]);
		else
		printf("%c",s[i]);
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/with_wine/article/details/114829892