摘要:
有没有同学经常碰到一些趣味作业?这篇文章就将对“用c或者c++语言编写出月亮的东升西落,阴晴圆缺”这个作业进行细致讲解。
代码:
首先直接先奉上代码
#include <bits/stdc++.h>
using namespace std;
int a[50], b[50];
int k = 1;
void moon()
{
double x, y;
for (x = 1; x > -1; x -= 0.05)
{
for (y = 1; y > -1; y -= 0.025)
{
if ((x * x + y * y) < 1)
a[k]++;
else
b[k]++;
}
k++;
}
return;
}
int main()
{
moon();
// ANSI escape code for yellow text
string yellow = "\033[33m";
// ANSI escape code to reset text color
string reset = "\033[0m";
int ciy = 400;
int cix = 1600;
for (int f = 1; f <= 8; f++)
{
for (int q = 0; q < ciy ; q+=50)
{
for (int y1 = 1; y1 <= ciy - q; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix - q*2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
for (int q = 0; q <= ciy; q += 50)
{
for (int y1 = ciy - q; y1 <= ciy; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix / 2 - q * 2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
}
for (int f = 1; f < 8; f++)
{
for (int q = 0; q < ciy ; q+=50)
{
for (int y1 = 1; y1 <= ciy - q; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix - q * 2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << " ";
for (int j = 1; j <= a[i] * (8 - f) / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
for (int q = 0; q <= ciy; q += 50)
{
for (int y1 = ciy - q; y1 <= ciy; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix / 2 - q * 2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << " ";
for (int j = 1; j <= a[i] * (8 - f) / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
}
}
做法:
1.编写字符圆形
首先呢,如果想要弄出月亮阴晴圆缺,就要先弄出一个很圆的圆,就要先利用以下代码:
double x, y;
for (x = 1; x > -1; x -= 0.05)
{
for (y = 1; y > -1; y -= 0.025)
{
if ((x * x + y * y) < 1)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
然后就可以弄出一个很圆的圆:
将整个圆弄出来存在数组中就可以十分方便的调用这个圆了。
2.弄出轨迹
轨迹可以直接弄成倒'V'形,这样代码也会比较简洁,最后敲出来的代码就是这样:
for (int f = 1; f <= 8; f++)
{
for (int q = 0; q < ciy ; q+=50)
{
for (int y1 = 1; y1 <= ciy - q; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix - q*2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
for (int q = 0; q <= ciy; q += 50)
{
for (int y1 = ciy - q; y1 <= ciy; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix / 2 - q * 2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
}
for (int f = 1; f < 8; f++)
{
for (int q = 0; q < ciy ; q+=50)
{
for (int y1 = 1; y1 <= ciy - q; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix - q * 2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << " ";
for (int j = 1; j <= a[i] * (8 - f) / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
for (int q = 0; q <= ciy; q += 50)
{
for (int y1 = ciy - q; y1 <= ciy; y1++)
cout << endl;
for (int i = 1; i <= k; i++)
{
for (int x1 = 1; x1 <= cix / 2 - q * 2; x1++)
cout << " ";
for (int j = 1; j <= b[i] / 2; j++)
cout << " ";
for (int j = 1; j <= a[i] * f / 8; j++)
cout << " ";
for (int j = 1; j <= a[i] * (8 - f) / 8; j++)
cout << yellow << "*" << reset;
cout << endl;
}
system("cls");
}
}
成品:
最后就可以跑出“阴晴圆缺”和”东升西落“的效果啦*
(不过跑程序之前需要先按住Ctrl然后滚动鼠标滚轮让分辨率降到最低,这样观感最好,这也会导致完整跑一遍这个程序会比较慢,然后月亮的出现不是很完美,观者如果有心,也可以选择做一个不是很圆的月亮来处理,这样运行效果会看起来更快。)
小结:
本节代码优点在于方便简洁,缺点还是分辨率和打印速度的限制问题,希望各位dalao们能够喜欢,欢迎在评论区批评指正。