**
snake.c
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include "LIST.h"
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
extern struct Snake *g_snake;
extern struct Snake g_food;
int g_flag = 0;
int g_direction = KEY_RIGHT;
int commitsuicide() {
struct Snake *p;
p = g_snake;
struct Snake *tmp;
tmp = g_snake->next;
while (tmp != NULL) {
if ((p->x == tmp->x) && (p->y == tmp->y)) {
return 0;
}
tmp = tmp->next;
}
return -1;
}
void initFood() {
int judge = 0;
do {
int x = rand() % 20 + 1;
int y = rand() % 20 + 1;
g_food.x = x;
g_food.y = y;
g_food.next = NULL;
if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
judge = 1;
}
} while (g_food.x == 0 || g_food.x > 18 || g_food.y == 0 || g_food.y > 18 ||
judge == 1);
}
int foodFind(int x, int y) {
if ((g_food.x == x) && (g_food.y == y)) {
return 0;
}
return -1;
}
int headTofood() {
if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
return 0;
}
return -1;
}
void initMap() {
int x;
int y;
for (x = 0; x < MAP_WIDTH; x++) {
for (y = 0; y < MAP_HEIGHT; y++) {
if (x == 0 || x == MAP_HEIGHT - 1) {
if (x == 0) {
printw("* ");
} else {
printw("* ");
}
} else {
if (y == 0 || y == MAP_WIDTH - 1) {
printw("* ");
} else {
if ((snakeFind(g_snake, x, y) == 0)) {
printw("[]");
} else if (foodFind(x, y) == 0) {
printw("++");
} else {
printw(" ");
}
}
}
}
printw("\n");
}
}
void handleKey() {
int key;
noecho();
keypad(stdscr, TRUE);
while (1) {
key = getch();
if (adjustDirection(key) == 0) {
continue;
}
}
}
int adjustDirection(int key) {
if (key == KEY_DOWN || key == KEY_UP || key == KEY_RIGHT ||
key == KEY_LEFT) {
if (((g_direction == KEY_RIGHT) && (key == KEY_LEFT)) ||
((g_direction == KEY_LEFT) && (key == KEY_RIGHT)) ||
((g_direction == KEY_UP) && (key == KEY_DOWN)) ||
((g_direction == KEY_DOWN) && (key == KEY_UP))) {
return 0;
}
}
g_direction = key;
return -1;
}
void moveSnakeauto() {
while (1) {
switch (g_direction) {
case KEY_DOWN:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
deleteSnake(&g_snake);
}
break;
case KEY_UP:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
deleteSnake(&g_snake);
}
break;
case KEY_LEFT:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
deleteSnake(&g_snake);
}
break;
case KEY_RIGHT:
if (headTofood() == 0) {
insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
g_flag = -1;
} else {
insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
deleteSnake(&g_snake);
}
break;
default:
break;
}
clear();
if (g_flag == -1) {
initFood();
g_flag = 0;
}
if (snakeDie() == 0 || commitsuicide() == 0) {
destorySnake();
createSnake();
}
initMap();
refresh();
usleep(100000);
}
}
int main() {
pthread_t t_movesnake;
pthread_t t_getinput;
initscr();
createSnake();
initFood();
initMap();
pthread_create(&t_movesnake, NULL, (void *)moveSnakeauto, NULL);
pthread_create(&t_getinput, NULL, (void *)handleKey, NULL);
while (1);
endwin();
return 0;
}
list.c
#include <curses.h>
#include <stdlib.h>
#include "LIST.h"
struct Snake *g_snake;
struct Snake g_food;
int snakeDie() // panduansiwang
{
if (g_snake->x == 0 || g_snake->x == 19 || g_snake->y == 0 ||
g_snake->y == 19) {
return 0;
}
return -1;
}
void destorySnake() // bianlishanchu
{
struct Snake *p;
while (g_snake != NULL) {
p = g_snake;
g_snake = g_snake->next;
free(p);
}
}
void insertSnake(struct Snake **head, int x, int y) // touchafa
{
struct Snake *node;
node = (struct Snake *)malloc(sizeof(struct Snake));
node->x = x;
node->y = y;
node->next = NULL;
node->next = *head;
*head = node;
}
void createSnake() {//chushihuashe
g_snake = (struct Snake *)malloc(sizeof(struct Snake));
g_snake->x = 3;
g_snake->y = 3;
g_snake->next = NULL;
insertSnake(&(g_snake), 3, 4);
insertSnake(&(g_snake), 3, 5);
insertSnake(&(g_snake), 3, 6);
}
int snakeFind(struct Snake *head, int x, int y) {//bianlipanduan
struct Snake *p;
p = head;
while (p != NULL) {
if ((p->x == x) && (p->y == y)) {
return 0;
}
p = p->next;
}
return -1;
}
void deleteSnake(struct Snake **head) {//shanchuweiba
struct Snake *p;
struct Snake *tmp;
tmp = NULL;
p = *head;
while ((p != NULL) && (p->next != NULL)) {
if ((p->next->next == NULL)) {
tmp = p->next;
p->next = NULL;
free(tmp);
break;
}
p = p->next;
}
}
list.h
#ifndef _LIST_H_
#define _LIST_H_
struct Snake
{
int x;
int y;
struct Snake *next;
};
void insertSnake(struct Snake **head,int x,int y);
void createSnake();
int snakeFind(struct Snake *head,int x,int y);
void deleteSnake(struct Snake **head);
int foodFind(int x,int y);
int snakeDie();
void destorySnake();
#endif
Makefile
snake:
gcc snake_final.c LIST.c -lncurses -lpthread -o snakepro
```c
#include <stdio.h>
int main() {
int n, m;
printf("请输入节点数n和边数m:");
scanf("%d %d", &n, &m);
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
int d[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
d[i][j] = a[i][j] - a[i][j-1];
}
}
printf("导纳矩阵为:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", d[i][j]);
}
printf("\n");
}
return 0;
}
```
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
首先,你需要读取图片并将其转化为离散的信号。你可以使用OpenCV库完成这个任务。然后,你需要使用数字信号处理来计算离散傅里叶变换(DFT)。DFT将为您提供频率响应,以便于计算导纳矩阵。最后,你可以使用公式来计算导纳矩阵。
这些步骤可以分成以下几个部分:
IplImage* image = cvLoadImage("image.jpg", CV_LOAD_IMAGE_GRAYSCALE); // 读取图像并转化为灰度图像
int width = image->width;
int height = image->height;
Mat_<float> signal(height, width);
Mat_<float> dft_signal(height, width);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
signal(y, x) = image->imageData[x + y * image->widthStep];
}
}
dft(signal, dft_signal, DFT_COMPLEX_OUTPUT);
Mat_<float> mag(height, width);
Mat_<float> phase(height, width);
Mat_<float> re(height, width);
Mat_<float> im(height, width);
Mat planes[] = {Mat_<float>(signal), Mat::zeros(signal.size(), CV_32F)};
Mat complexSignal;
merge(planes, 2, complexSignal);
dft(complexSignal, complexSignal);
split(complexSignal, planes);
magnitude(planes[0], planes[1], mag);
phase(planes[0], planes[1], phase);
re = planes[0];
im = planes[1];
double maxVal;
minMaxLoc(mag, NULL, &maxVal, NULL, NULL);
mag /= maxVal;
#define PI 3.14159265358979323846f
Mat_<float> j_re(height, width);
Mat_<float> j_im(height, width);
Mat_<float> adm(height, width);
complex<float> omega = complex<float>(2 * PI * 1000, 0);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
complex<float> z = complex<float>(re(y, x), im(y, x));
complex<float> j = complex<float>(0, 1);
complex<float> numerator(z * (1 - exp(-j * omega)));
complex<float> denominator(omega * z * exp(-j * omega));
adm(y, x) = abs(numerator / denominator);
}
}
这是一个简单的程序,可以实现对图像的导纳矩阵计算。当然,你也可以基于你自己研究的领域或者应用进行相应的修改和变化,进而拓展应用场景及更复杂的计算。
如果我的回答解决了您的问题,请采纳!
https://blog.csdn.net/belous_zxy/article/details/84332868 参考下
/*稀疏矩阵——采用十字链表*/
/*Author:Belous*/
/*Date:2018/11/22*/
/*Date:2018/11/26*/
#ifndef SparseMatrix_H
#define SparseMatrix_H
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INF -1
#define OVERFLOW -2
#define UNKNOWN -3
/*函数结果状态代码(预设)*/
//#define SMatrix_Help
/*开启提示*/
typedef int Status;
/*函数返回状态类型*/
typedef int ElemType;
#define ElemSp "%i"
/*节点数据类型*/
typedef struct OLNode
{
int x,y;
ElemType value;
struct OLNode *right,*down;
}OLNode;
typedef OLNode *OLink;
/*稀疏矩阵的元素体*/
typedef struct
{
OLink *rhead,*chead;
int row,column,unzero;
}CrossList;
/*稀疏矩阵结构体*/
typedef CrossList *CrossLink;
/*****矩阵指针类型 CrossLink *****/
Status CreateSMatrix(CrossLink *M);
/*创建,通过stdin读取数据*/
Status DestorySMatrix(CrossLink *M);
/*销毁*/
Status PrintSMatrix(CrossLink M);
/*打印,通过stdout输出数据,矩阵指针只读模式*/
Status CopySMatrix(CrossLink *M,CrossLink T);
/*复制,将稀疏矩阵T复制给M*/
Status ReferSMatrix(CrossLink *M,CrossLink N);
/*引用,将 M 矩阵指针指向 N 矩阵*/
Status AddMatrix(CrossLink M,CrossLink N);
Status AddSMatrix(CrossLink *M,CrossLink N,CrossLink *T);
/*矩阵加法*/
Status SubMatrix(CrossLink M,CrossLink N);
Status SubSMatrix(CrossLink *M,CrossLink N,CrossLink *T);
/*矩阵减法*/
Status MulSMatrix(CrossLink M,CrossLink N,CrossLink *T);
/*矩阵乘法*/
Status TranSMatrix(CrossLink M,CrossLink *T);
/*矩阵转置*/
Status InvSMatrix(CrossLink M,CrossLink *T);
/*矩阵求逆*/
Status ChangeMatrix(CrossLink M,int row,int column,ElemType value);
/*修改、添加矩阵元素*/
Status ReadMatrix(CrossLink M,int row,int column,ElemType *value);
/*读取矩阵元素,矩阵指针只读模式*/
Status DelMatrix(CrossLink M,int row,int column);
/*删除矩阵元素*/
Status DelRowMatrix(CrossLink M,int row);
/*删除矩阵行*/
Status DelColMatrix(CrossLink M,int column);
/*删除矩阵列*/
Status NewRowMatrix(CrossLink M,int row);
/*添加矩阵行*/
Status NewColMatrix(CrossLink M,int column);
/*添加矩阵列*/
#endif