This post is used to summarize the most recent graphic printing problems encountered in C language learning, but also hope that Daniel
1. isosceles Triangle Printing
Enter a number to print the inverted isosceles triangle.
As input: 5
Print:
*****
***
*
Analysis: For such topics should first analyze, find the law
Line number empty well number
1 0 5
2 1 3
3 2 1
Number of rows: n = (input number + 1)/2;
The relationship between the space and the number of lines: number of spaces = number of lines-1;
Asterisk: Number of lines * 2-1; (number of rows from large to small control output)
The code is as follows:
1 voidexam4 ()2 {3 intN, Num, I, J;4printf"Please enter the isosceles triangle bottom Length:");5scanf"%d", &n);6 //number of rows calculated7num = (n +1) /2;8 //Number of control lines (reverse line number)9 for(i = num; i >0; i--) {Ten //Control Spaces One for(j =0; J < Num-i; J + +) { Aprintf" "); - } - //control asterisk the for(j =2I1; J >0; j--) { -printf"*"); - } -printf"\ n"); + } -}
2. Pass in a letter, print graphics (10 points)
Incoming: ' D ', printed like:
A
ABA
ABCBA
Abcdcba
Dcbabcd
Cbabc
BAB
A
Analysis: It is easy to see the graphics into the upper and lower parts of the graph
The top half of the graph is all a, the inner layer is B, and then C, you can consider printing from the outermost layer to the inner layers first.
Look at the lower half to find out: the lower half of the graph is the "DCBABCD" string in order to print less characters
The code is as follows:
1 voidPrintgraph (Charc)2 {3 intsizeup, Sizedown, size, I, M, N;4 //gets the size of the constructed string5Size = ((c +1) -'A') *2;6 //calculate the number of rows in the upper half7Sizeup = Sizedown = Size/2;8 //Constructing Arrays9 CharArray[size][size];Ten Charb ='A'; Onem = n = sizeup-1; A //set all the positions in the array to spaces before you need to assign a value - for(i =0; i < Sizeup; i++) { - for(intj =0; J < size; J + +) { theARRAY[I][J] =' '; - } - } - //think about assigning a value first, and then to the specified letter . + for(intK =0; K < Sizeup; k++) { - for(i = k; i < sizeup; i++) { + for(intj =0; J < size; J + +) { A if(i + j = = N | | j-i = =m) { atARRAY[I][J] =b; - } - } - } -b++; -m--; inn++; - } to //output Upper Half + for(inti =0; i < Sizeup; i++) { - for(intj =0; J < size; J + +) { theprintf"%c", Array[i][j]); * } $printf"\ n");Panax Notoginseng } - the //construct the lower half array + CharFuck[size]; A intp =0, q = Size-2; the while(P <=q) { +FUCK[P] = Fuck[q] =C; -c--; $p++; $q--; - } -Fuck[size] =' /'; the CharArray2[sizedown][size]; - for(i =0; i < Sizedown; i++) {Wuyi strcpy (array2[i], fuck); the } - //Control Printing Wu for(i =0; i < Sizedown; i++) { - for(intK =0; K < I; k++) { Aboutprintf" "); $ } - for(intj = i; J < Size-i-1; J + +) { -printf"%c", Array2[i][j]); - } Aprintf"\ n"); +}
Third, implement the function, according to the input content printing graphics
Input: "1234567"
Print:
1234567
2 6
3 5
4
3 5
2 6
1234567
Observation graphs can be found, you can consider traversing the input string, and set two tag value K, M, respectively, to the second and second-to-last number of the array, traversing the array table and tag values are equal, then print out
The corresponding character in the array, while moving the tag value, k++,m--;
1 voidprint ()2 {3 Chara[ -];4 gets (a);5 intSize = (int) strlen (a);6 inti =0, j =0, k =1, m = Size-2;7 puts (a);8 for(i =0; I < size-2; i++) {9 for(j =0; J < size; J + +) {Ten if(J! = k && j!=m) { Oneprintf" "); A -}Else{ -printf"%c", A[j]); the } - } -k++; -m--; +printf"\ n"); - } + puts (a); A}
A summary of C-language graphic printing problems