可直接运行
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node;
typedef struct Node *PNode;
struct Node
{
int zhishu;
int xishu;
PNode next;
};
typedef struct Node *LinkList;
LinkList createNullList_link(void)//创建空链表
{
LinkList llist = (LinkList)malloc(sizeof(struct Node));
if(llist != NULL) llist -> next = NULL;
else
{
printf("out of space!\n");
}
return llist;
}
PNode locate_link(LinkList llist,int zhishu)//求某元素的存储位置
{
PNode p;
if(llist == NULL) return NULL;
p = llist->next;
while(p != NULL && p->zhishu!=zhishu)p=p->next;
return p;
}
int insertPost_link(LinkList llist,PNode p,int zhishu,int xishu)//在p节点后添加新节点
{
PNode q = (PNode)malloc(sizeof(struct Node));
if(q==NULL) {
printf("out of space.");
return 0;
}
else
{
q->zhishu=zhishu;
q->xishu=xishu;
q->next=p->next;
p->next=q;
return 1;
}
}
int main()
{
LinkList a1 = createNullList_link();
insertPost_link(a1,a1,1,4);
PNode p1 = a1->next;
insertPost_link(a1, p1, 0, 5);
LinkList a2 = createNullList_link();
insertPost_link(a2, a2, 1, 6);
PNode p2 = a2->next;
insertPost_link(a2, p2, 0, 3);
//以上设置好了两个一元多项式
PNode p11 = locate_link(a1,1);
PNode p21 = locate_link(a2, 1);
PNode p10 = locate_link(a1, 0);
PNode p20 = locate_link(a2, 0);
int xishu1 = p11->xishu + p21->xishu;
int xishu0 = p10->xishu+p20->xishu;
printf("the sum is %d x + %d",xishu1,xishu0);
return 0;
}
思路: 1. 使用传感器来检测循迹小车所在位置,根据传感器的输出确定小车的行进方向。 2. 可以使用循环来实现循迹小车的移动,根据传感器的输出来确定小车应该怎么移动(向左、向右、向前)。 3. 使用一个数据结构(如栈)来保存小车走过的路径,每次移动后将当前位置保存在栈中。 4. 当小车需要原路返回时,从栈中取出上一个位置,并将小车移动到该位置。 5. 循迹小车的循迹部分可以根据具体的硬件设备和传感器来进行编写,需要了解传感器的工作原理和输出信号的处理方法。 6. 确定小车移动的规则,根据传感器的输出状态来决定小车的行进方向。 7. 使用C语言编写代码,包括传感器的初始化和数据处理部分。
以下是一个简单的示例代码:
#include <stdio.h>
// 传感器输入的宏定义
#define SENSOR_LEFT 1
#define SENSOR_MIDDLE 2
#define SENSOR_RIGHT 3
// 模拟传感器输出的函数,根据实际情况进行修改
int getSensorOutput() {
// 返回传感器的输出状态,如左边有黑线返回SENSOR_LEFT
// 中间有黑线返回SENSOR_MIDDLE,右边有黑线返回SENSOR_RIGHT
return SENSOR_MIDDLE;
}
// 小车移动的函数,根据实际情况进行修改
void moveCar(int direction) {
// 根据传感器输出的方向来移动小车
// 可以调用具体的控制函数来控制小车的移动
if (direction == SENSOR_LEFT) {
printf("Move car to left\n");
} else if (direction == SENSOR_RIGHT) {
printf("Move car to right\n");
} else {
printf("Move car forward\n");
}
}
// 栈结构的定义和操作函数
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
int isEmpty() {
return top == -1;
}
int isFull() {
return top == MAX_SIZE - 1;
}
void push(int data) {
if (isFull()) {
printf("Stack is full\n");
return;
}
stack[++top] = data;
}
int pop() {
if (isEmpty()) {
printf("Stack is empty\n");
return -1;
}
return stack[top--];
}
int main() {
int sensorOutput;
int currentPosition = 0;
int previousPosition;
// 循环检测传感器输出并移动小车
while (1) {
sensorOutput = getSensorOutput();
// 假设传感器输出为SENSOR_LEFT、SENSOR_MIDDLE、SENSOR_RIGHT分别代表左中右三种位置
if (sensorOutput == SENSOR_LEFT) {
moveCar(1); // 向左移动小车
currentPosition -= 1;
} else if (sensorOutput == SENSOR_RIGHT) {
moveCar(2); // 向右移动小车
currentPosition += 1;
} else {
moveCar(0); // 向前移动小车
currentPosition += 0;
}
// 保存路径
push(currentPosition);
// 假设当小车到达终点时需要原路返回
if (currentPosition == 10) {
// 从栈中取出上一个位置
previousPosition = pop();
// 小车移动到上一个位置
while (currentPosition > previousPosition) {
moveCar(1); // 向左移动小车
currentPosition -= 1;
}
while (currentPosition < previousPosition) {
moveCar(2); // 向右移动小车
currentPosition += 1;
}
}
}
return 0;
}
这个示例代码中,使用了一个模拟的传感器和一个栈来实现循迹小车的移动和保存路径。小车根据传感器输出的方向来移动,根据具体的需求和硬件设备进行调整。在需要原路返回时,从栈中取出上一个位置,并移动小车到该位置。