请问在C语言中,怎么样对调两个文件里读取到变量,第一个文件有2个,而第二个文件有1个,对调以后变量的个数不能改变!
利用临时变量做交换,具体的实现思路已经回过你的帖子了:
int temp = a;
a = b;
b = c;
c = temp;
在C语言中,对调两个文件里读取到的变量,可以使用指针来实现。假设第一个文件中有两个变量a和b,第二个文件中有一个变量c,要在不改变变量个数的情况下将它们对调。
可以先在第二个文件中定义两个指针变量p1和p2,分别指向第一个文件中的变量a和b,然后将变量c的值赋给a,a的值赋给b,最后将p1所指向的变量b的值赋给变量c即可。代码示例如下:
// file1.c
int a = 1;
int b = 2;
// file2.c
#include <stdio.h>
extern int a, b;
int c = 3;
int main() {
int *p1 = &a;
int *p2 = &b;
printf("before swap: %d, %d, %d\n", a, b, c);
a = c;
c = *p2;
*p2 = *p1;
*p1 = c;
printf("after swap: %d, %d, %d\n", a, b, c);
return 0;
}
在此示例代码中,通过extern关键字来引用第一个文件中定义的变量a和b。然后在文件2中定义两个指针变量p1和p2,分别指向变量a和b。在进行变量对调时,首先将变量c的值赋给变量a,然后将指针p2所指向的变量b的值赋给变量c,最后通过指针p1和指针p2实现变量对调。
不知道你这个问题是否已经解决, 如果还没有解决的话:**
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
根据您提供的问题描述,我理解“对调”是指交换两个文件中变量的值。并且在交换后,两个文件中的变量个数仍然是原来的数量。
如果您需要交换两个文件中变量的值,您需要完成以下操作:
读取每个文件并获取变量的值 您需要使用适当的方法,例如打开文件和读取文件,获取每个文件中变量的值。在读取文件时,您可以按照逗号或其他分隔符划分变量。您需要将这些变量存储在向量或数组中,这样您才能够对它们进行操作。
确保文件中变量的数量是正确的 根据您提供的描述,两个文件中变量的数量不应该改变。在读取文件时,请确保您读取了正确数量的变量。如果文件中的变量数量不正确,您需要返回错误或重新打开文件并读取正确数量的变量。
交换变量的值 一旦您已经获取了每个文件中变量的值并将它们存储在向量或数组中,您可以交换它们的值。这可以通过使用一个临时变量来完成。
将新的变量值写入文件中 您需要使用适当的方法,例如打开文件和写入文件,将交换后的变量值写入文件中。在写入文件时,您可以使用逗号或其他分隔符将变量分开。
这是一个伪代码示例,显示如何交换两个文件中变量的值:
// 打开和读取第一个文件,并存储变量。
file1.open("file1.txt");
vector<string> file1Variables;
while (读取文件中的变量) {
file1Variables.push_back(变量);
}
file1.close();
// 打开和读取第二个文件,并存储变量。
file2.open("file2.txt");
vector<string> file2Variables;
while (读取文件中的变量) {
file2Variables.push_back(变量);
}
file2.close();
// 确保文件中变量的数量是正确的。
if (file1Variables.size() != 2 || file2Variables.size() != 1) {
返回错误;
}
// 交换变量的值。
string temp = file1Variables[0];
file1Variables[0] = file2Variables[0];
file2Variables[0] = temp;
// 将新的变量值写入文件中。
file1.open("file1.txt");
for (每个变量 in file1Variables) {
写入变量到文件中;
}
file1.close();
file2.open("file2.txt");
for (每个变量 in file2Variables) {
写入变量到文件中;
}
file2.close();
请注意,这只是一个伪代码示例,并且实际操作可能因特定的问题而有所不同。如果您需要更多帮助,请提供更多信息,以便我更好地理解您的问题。