#include <windows.h> #include <valarray> #include <iostream> #include <iomanip> #include <math.h> #define STARTING 100
using namespace std;
//win32中输出控制台左上角的坐标为(0,0),向右和向下均为正方向,所以现在100为起始位置
void gotoxy(double x,double y) //将光标定位到x,y的位置
{ CONSOLE_SCREEN_BUFFER_INFO csbiInfo; HANDLE hConsoleOut; hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
csbiInfo.dwCursorPosition.X = x; csbiInfo.dwCursorPosition.Y = y; SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); }
void print_sine() //打印正弦波,
{ double i,j=0.0;
for (i = 1;i <= 80;i++) { //根据横坐标求纵坐标 ,使用公式 y=Asin(ωx+φ) + k,这里使用的A = 10,ω=0.3,φ= 0,k=0,可以根据需要自己设置这几个参数
j = STARTING - sin(double(i)*0.3)*10; gotoxy(i,j); //光标跳转到横坐标i,纵坐标j所指向的位置
cout<<"*"; //打印出*
} getchar(); }
void print_square() //打印方波
{ int amplitude = 5;//默认的振幅为5
int interval = 10;//默认的方波的间距为10,即一个周期有20个像素点
cout <<"输入方波的振幅(1~10,默认为5):"; cin >> amplitude; cout << "输入方波的间距(1~20,默认为10):"; cin>> interval; if (amplitude < 1 || amplitude > 10 || interval < 1 || interval > 20) { cout << "输入有误!"; return; } int i,j; for (i = 1 ;i <= 80;i++) { j = STARTING - amplitude; gotoxy(i-1,j); cout<<"*"; if ( i % interval == 0) amplitude = 0 - amplitude; } getchar(); }
void print_triangle() //打印三角波
{ int i,j;
int interval = 10;//默认的方波的间距为10,即一个周期有20个像素点
cout <<"输入三角波的间距(1~20,默认为10):"; cin >> interval; if (interval < 1 || interval > 20) { cout << "输入有误!"; return; }
int r = 0; for (i = 1;i <= 80;i++) { if (i / interval % 2 == 0) //表示曲线上升
{ j = STARTING - (i % interval); } else j = STARTING - (interval - i % interval); //曲线下降时求纵坐标
gotoxy(i,j); cout << "*"; } getchar(); }
int main() { //下面三行用来设置字体颜色
//HANDLE hConsoleOut;
//hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
//SetConsoleTextAttribute(hConsoleOut, 100);//改动后面第二个参数,可以设置不同颜色
int choice; cout << "打印正弦波选择1,打印方波选择2,打印三角波选择3"<<endl; cout << "选择: ";
cin >> choice; switch (choice) { case 1: print_sine();break; case 2: print_square();break; case 3: print_triangle();break; default: cout << "输入有误!"; }
getchar(); return 0; }
|