#include <graphics.h> #include<conio.h> #include<cmath> #include<iostream> using namespace std; class SineGraph { protected: double A, w, b; int m, n;//m,n表示函数显示范围 int color, step; public: SineGraph(double i, double j, double k, int x, int y, int getcolor = WHITE, int getstep = 1) { i = A; j = w; k = b; m = x; n = y; getcolor = color; getstep = step; } void draw() { for (int x = m; m < n; x += step) { cleardevice(); line(x, A * sin((w * x + b) + 200), x + 1, A * sin(w * (x + 1) + b) + 200); Sleep(30); } } void update() { b += 0.1; if (b > 100) b = -100; } }; int main() { initgraph(640, 480,1); // 设置窗口大小为 640x480 SineGraph sg(100,0.05,10,200,500,RED); for (int i = 0; i < 1000; i++) { sg.update(); sg.draw(); } sg.draw(); // 绘制正弦图形 _getch(); // 等待按键退出 closegraph(); // 关闭图形窗口 return 0; }找出代码错误
时间: 2024-04-10 15:28:24 浏览: 17
代码中有几处错误:
1. 在 SineGraph 构造函数中,应该将传入的参数赋值给类的成员变量,而不是将类的成员变量赋值给传入的参数。正确的写法是:
```
SineGraph(double i, double j, double k, int x, int y, int getcolor = WHITE, int getstep = 1) {
A = i;
w = j;
b = k;
m = x;
n = y;
color = getcolor;
step = getstep;
}
```
2. 在 draw() 函数中,for 循环的条件应该是 x < n 而不是 m < n。正确的写法是:
```
for (int x = m; x < n; x += step) {
```
3. 在 draw() 函数中,line() 函数的参数不正确。应该将 A * sin((w * x + b) + 200) 改为 A * sin(w * x + b) + 200。正确的写法是:
```
line(x, A * sin(w * x + b) + 200, x + 1, A * sin(w * (x + 1) + b) + 200);
```
请注意以上错误并进行修正。
相关问题
#include <stdlib.h> #include <conio.h>
#include <stdlib.h> 是C语言中的一个头文件,它包含了一些常用的函数和宏定义,用于进行内存分配、类型转换、随机数生成等操作。其中一些常用的函数包括malloc()、free()、atoi()等。
#include <conio.h> 是C语言中的另一个头文件,它主要用于实现控制台输入输出相关的功能。其中一些常用的函数包括getch()、putch()等,可以用于获取键盘输入、输出字符到控制台等操作。
这两个头文件通常在C语言的程序中使用,可以根据需要选择性地包含它们来使用相应的函数和宏定义。
#include <conio.h> 告诉我用法
`<conio.h>` 是 C 语言中的一个头文件,它提供了一组常见的用于控制台 I/O 的函数,例如 `getch()` 和 `clrscr()` 等。
`getch()` 函数用于从控制台读取一个字符,而不需要用户按下回车键。它的用法如下:
```c
#include <conio.h>
int getch(void);
```
`getch()` 函数返回读取到的字符的 ASCII 码值。
`clrscr()` 函数用于清空控制台的屏幕。它的用法如下:
```c
#include <conio.h>
void clrscr(void);
```
该函数没有返回值,它只会清空控制台的屏幕。
需要注意的是,`<conio.h>` 是一个非标准的头文件,它只在一些特定的编译器中可用。在一些操作系统或编译器中,这些函数可能不存在或者有不同的实现。因此,如果你想编写跨平台的程序,最好避免使用 `<conio.h>`。