※ 引述《TKirby (叫我垃圾其餘免談)》之銘言:
: ※ 引述《Psycap (嗚啊哇啊!期中考...)》之銘言:
: : 修正-: for(;i<2*pi;){i=4*pi-i;i=abs(i)}
: 我覺得你的寫法很好
: 因為有一般性
: 我的式子還限定0<=x<=4
: 嗚嗚 Psycap好厲害
這是我在高雄寫的,po出來看能不能有幫助
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.1415926
#define TEXTATTR 0x0d00
inline double RtoD(double Rad) { return Rad*PI/180; }
void CharOut(int,int,int);
double TWave(double);
double (*Fn)(double);
int main(void)
{
double StartAng,Interval,Amplitude;
int i,ch;
//------------------user input the data
do {
clrscr();
printf("Please enter the start the angle in degree(0-360):");
scanf("%lf",&StartAng);
} while( StartAng > 360.0 || StartAng < 0.0);
do {
clrscr();
printf("and the amplitude (no larger than 39 please...) :");
scanf("%lf",&Amplitude);
} while(Amplitude > 39.0 || Amplitude < 0.0);
do {
clrscr();
printf("and the intergval of the angle in degree :");
scanf("%lf",&Interval);
} while(Interval < 0.0);
printf("Which function you want ? (S=Sin T=Triangle Wave)");
do
ch=getche();
while (ch != 's' && ch !='S' && ch != 't' && ch !='T');
//----------------show the status
clrscr();
printf("\nStatus:\n\n");
printf("Draw from: %lg(in degree)\nAmplitude: %lg\n",
StartAng,Amplitude);
printf("Interval: %lg\n",Interval);
if(ch == 's' || ch == 'S') printf("----Using sin for current function----\n");
else printf("----Using triangle wave for current function----\n");
_setcursortype(_NOCURSOR);
printf("\nPress any key to see the result");
getch();
//-----------------draw
clrscr();
if( ch =='s' || ch=='S') Fn=&sin;
else Fn=&TWave;
for(i=0;i<25;i++)
CharOut( int( (*Fn)( RtoD(StartAng+i*Interval))*Amplitude)+40,i,'*');
getch();
_setcursortype(_NORMALCURSOR);
clrscr();
return 0;
}
void CharOut(int x,int y,int ch)
{
int far *Addr;
int i;
Addr=(int far *)0xb8000000;
if(x >= 0 && y >= 0) *(Addr+y*80+x) = TEXTATTR | ch;
}
double TWave(double Radius)
{
while(Radius > PI*2) Radius -= PI*2; //2 PI for a circle
if( Radius == 0.0 || Radius == PI || Radius == PI * 2) return 0.0;
else if( Radius == PI / 2) return 1.0;
else if( Radius == PI / 2 * 3) return -1.0;
else if( Radius > PI / 2 * 3) return (Radius - PI / 2 * 3) / PI *2 - 1.0;
else if( Radius > PI / 2) return 1 - (Radius - PI / 2) / PI * 2;
else return Radius / PI * 2;
}
--
Filled with love..
by you...
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: ntucsa.csie.ntu.