c语言生成矩形图形
时间: 2023-07-13 22:15:18 浏览: 67
在C语言中,可以通过使用嵌套循环来生成矩形图形。以下是一个简单的示例代码:
```c
#include <stdio.h>
int main()
{
int rows, columns;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= columns; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
```
在上面的代码中,我们首先通过 `scanf` 函数获取用户输入的行数和列数。然后使用两个嵌套的 `for` 循环来生成矩形图形,外层循环控制行数,内层循环控制列数。每次内层循环输出一个 `*` 符号,表示矩形的一个小方块。内层循环结束后,使用 `printf` 函数输出一个换行符,表示该行已经输出完毕。
你可以根据需要修改上面的代码,来生成不同形状的矩形。
相关问题
c语言生成怎么生成两个图形窗口
在 C 语言中,可以使用不同的图形库来生成窗口。其中比较常用的是 Windows API 和 OpenGL。
使用 Windows API,可以使用 CreateWindow 函数创建一个新的窗口,例如:
```c
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Register the window class.
const char CLASS_NAME[] = "My Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
"Window 1", // Window title
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
// Show the window
ShowWindow(hwnd, nCmdShow);
// Create a second window.
HWND hwnd2 = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
"Window 2", // Window title
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
// Show the second window
ShowWindow(hwnd2, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
```
这个程序会创建两个窗口,分别显示 "Window 1" 和 "Window 2"。
使用 OpenGL,可以使用 glut 库,例如:
```c
#include <GL/glut.h>
void display1()
{
// Set the clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT);
// Draw a red triangle
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.0f, 0.5f);
glEnd();
// Swap the buffers
glutSwapBuffers();
}
void display2()
{
// Set the clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT);
// Draw a green rectangle
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
// Swap the buffers
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
// Create the first window
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Window 1");
// Set the display function for the first window
glutDisplayFunc(display1);
// Create the second window
glutInitWindowSize(400, 300);
glutInitWindowPosition(500, 100);
glutCreateWindow("Window 2");
// Set the display function for the second window
glutDisplayFunc(display2);
// Start the main loop
glutMainLoop();
return 0;
}
```
这个程序会创建两个窗口,分别显示一个红色的三角形和一个绿色的矩形。
C语言高级复杂代码
以下是一个较为复杂的 C 语言代码示例,用于实现一个简单的图形界面程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ROWS 10
#define MAX_COLS 10
#define MAX_NAME_LEN 20
typedef struct {
int x;
int y;
} Point;
typedef struct {
char name[MAX_NAME_LEN];
Point pos;
} Widget;
void draw_widget(Widget w) {
printf("Widget %s at (%d,%d)\n", w.name, w.pos.x, w.pos.y);
}
void draw_grid(int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("+");
if (i == 0) {
printf("---");
} else {
printf(" ");
}
}
printf("+\n");
for (int j = 0; j < cols; j++) {
printf("| ");
}
printf("|\n");
}
for (int j = 0; j < cols; j++) {
printf("+---");
}
printf("+\n");
}
int main() {
srand(time(NULL));
Widget widgets[MAX_ROWS * MAX_COLS];
int num_widgets = 0;
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
Widget w;
sprintf(w.name, "Widget%d", num_widgets++);
w.pos.x = j * 3;
w.pos.y = i * 2;
widgets[i * MAX_COLS + j] = w;
}
}
draw_grid(MAX_ROWS, MAX_COLS);
for (int i = 0; i < num_widgets; i++) {
draw_widget(widgets[i]);
}
return 0;
}
```
这个程序实现了一个简单的图形界面,通过在控制台上输出一系列的字符来模拟。其中,程序定义了两个结构体 `Point` 和 `Widget`,分别表示坐标点和界面上的控件。程序还定义了一些常量,包括最大行数、最大列数和最大名称长度等。在 `main` 函数中,程序使用两个嵌套的循环遍历所有的行和列,并为每个控件生成一个随机名称和坐标。最后,程序调用 `draw_grid` 函数绘制矩形网格,并调用 `draw_widget` 函数绘制所有的控件。
虽然这个程序相对复杂,但是它展示了如何使用 C 语言实现一个简单的图形界面。同时,这个程序也展示了如何使用结构体和循环等基本语言特性来组织代码。