字符模拟正弦图形问题

Dreamz 2008-12-13 11:39:46
有个题目要求是用字符模拟输出正弦图形,正弦函数我知道如何用,但是我不知道如何定位每一个字符的输出位置,搜了一下也没搜到解决方法,还请大家给个思路,谢谢先^^
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
idaretobe 2009-07-11
  • 打赏
  • 举报
回复
#include<iostream>
#include<fstream>
#include<cmath> // for sin()
#include<algorithm> // for stable_sort()
#include<vector>
#include<string>
#include<math.h>


using namespace std;
//-------------------------------------
struct Point{ int x,y; } zero={0,0};
bool operator<(const Point& a, const Point& b){ return a.y>b.y; }
void display(const vector<Point>& p, char ch){
for(int k=0,i=p[0].y; i>=-p[0].y; --i){
for(int n=0; k<p.size() && p[k].y==i; n=p[k++].x+1)
cout<<string(p[k].x-n,' ')<<ch;
cout<<"\n";
}
}//------------------------------------
int main(){
ifstream cin("sin.in");
char ch;
for(int period,amplitude; cin>>period>>amplitude>>ch; ){
vector<Point> p(4*period+1,zero);
for(int i=1; i<=period*2; ++i){
p[i].x = i;
p[i].y = amplitude*sin(i*asin(-1)/(2*period))+0.5;
p[period*2+i].x = period*2+i;
p[period*2+i].y = -p[i].y;
}
stable_sort(p.begin(), p.end());
display(p, ch);
}
}//====================================
在BCB中运行通过
星羽 2008-12-14
  • 打赏
  • 举报
回复

#include "iostream"
#include "cmath"
using namespace std;

const float Pi = 3.14159265358979323846f;
const int Width = 40;
const int Step = 5;

int main()
{
for (int i = Step; i >= -Step; i -= 1)
{
int a = 0;
int b = 0;

if (i >= 0)
{
a = (int)(asinf(i / (float)Step) / (2.f * Pi) * Width);
b = Width / 2 - a;
}
else
{
a = -(int)(asinf(i / (float)Step) / (2.f * Pi) * Width) + Width / 2;
b = Width - (a - Width / 2);
}

for (int j = 0; j < Width; ++j)
{
if (j == a || j == b)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}

return 0;
}

-------------

*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*



zhyinty 2008-12-14
  • 打赏
  • 举报
回复

#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;

#define PI 3.1415926f

int line = 0;
char a[20][20] = {' '};
int main()
{
for(float x = 0.0; x < 2 * PI; x += PI / 10.0f)
{
a[line][int(sin(x) * 10) + 10] = '*';
line ++ ;
}
for (int i = 0; i< 20;i++)
{
for (int j = 0; j< 20;j++)
{
cout << a[j][i];
}
cout << endl;
}
return 0;
}
jia_xiaoxin 2008-12-14
  • 打赏
  • 举报
回复
#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;

#define PI 3.1415926f

int main()
{
for(float x = 0.0; x < 2 * PI; x += PI / 10.0f)
{
cout << std::setw(int(sin(x) * 10) + 10) << '*' << endl;
}
return 0;
}
Dreamz 2008-12-13
  • 打赏
  • 举报
回复
wu637朋友的代码是纵向输出的,我要的是横向输出的,不过也要谢谢^^
wuyu637 2008-12-13
  • 打赏
  • 举报
回复
int main()
{
for(int i=0;i<10000;i++)
{
int temp = 20*sin((double)i/(2*3.1415))+20;
while(temp>0)
{
cout << " ";
temp --;

}
cout << "a" << endl;
}
}
wuyu637 2008-12-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
int main()
{
for(int i=0;i<100;i++)
{
int temp = 20*sin((double)i/(2*3.1415))+20;
while(temp>0)
{
cout << " ";
temp --;

}
cout << "a" << endl;
}
}
Dreamz 2008-12-13
  • 打赏
  • 举报
回复
呃...这出来的效果和我发帖时编辑的不一样啊...大家知道意思就行了,嘿嘿
Dreamz 2008-12-13
  • 打赏
  • 举报
回复
就是像下面这样:)
###
# #
# #
# #
# #

# # #

# #
# #
# #
# #
###
zy1691 2008-12-13
  • 打赏
  • 举报
回复
挺变态的玩法
张赐 2008-12-13
  • 打赏
  • 举报
回复
什么叫用字符模拟输出??

什么意思??

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧