/*
用#打印三角形
2023'9.29'8:24
目的:写一个短小的程序来打印出三角形
设计程序:利用3个循环,先打印空格再打印#
*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
for( int i = 1; i <= 7; i += 2 )
{
for( int j = 1; j <= (7 - i) / 2; ++j )
{
printf( " " );
}
for( int j = 1; j <= i; ++j )
{
printf( "#" );
}
printf( "\n" );
}
system( "pause" );
return 0;
}
/*
输出结果:
————————————————
#
###
#####
#######
请按任意键继续. . .
————————————————
*/
/*
用*打印图形
2023'9.29'8:14
目的:用*打印
*****
*****
*****
*****
*****
这个图形
设计程序:利用循环可以实现,但是鉴于这里每一行的*数都相等,因此可以使用字符数组
*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char stars[6] = { '*', '*', '*', '*', '*' };
for( int i = 1; i <= 5; ++i )
{
for( int j = 1; j < i; ++j )
{
printf( " " );
}
printf( "%s\n", stars );
}
system( "pause" );
return 0;
}
/*
输出结果:
————————————————
*****
*****
*****
*****
*****
请按任意键继续. . .
————————————————
*/
/*
绘制余弦图像
目的:实现用*绘制出余弦图像
设计程序:math.h里定义了一个函数acos(),通过给予一个1到-1间的值,返回横坐标的值
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main( void )
{
double m;
int x, i;
for( double m = 1; m >= -1.0; m -= 0.1 )
{
x = acos( m ) * 10;
for( i = 1; i < x; ++i )
{
printf( " " );
}
printf( "*" );
for( ; i < 62 - x; ++i )
{
printf( " " );
}
printf( "*\n" );
}
system( "pause" );
return 0;
}
/*
输出结果:
————————————————
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
请按任意键继续. . .
————————————————
*/
/*
打印乘法口诀表
目的:打印99乘法表
设计程序:使用两个for循环
*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
for( int i = 1; i <= 9; ++i )
{
for( int j = 1; j <= i; ++j )
{
printf( "%d x %d = %-2d ", i, j ,i*j ); //2位左对齐可以让输出结果更美观
}
printf( "\n" );
}
//以下为参考答案,答案中没有使用%2d,但是输出结果却是%2d的形式,于是在这里做出改善
// int i, j;
// for( i = 1; i <= 9; i++ )
// {
// for( j = 1; j <= i; j++ )
// {
// printf( "%d*%d=%2d ", i, j, i*j ); //2位右对齐
// }
// printf( "\n" );
// }
system( "pause" );
return 0;
}
/*
输出结果:
————————————————
1 x 1 = 1
2 x 1 = 2 2 x 2 = 4
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25
6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
请按任意键继续. . .
————————————————
*/