[英]How can I convert my Windows C program to run on a Linux system?
我想將以下程序轉換為在 linux 系統上運行以完成我的學校作業。 我正在努力奮斗,因為 linux 計算機實驗室因新冠疫情而關閉。 因此,我在 windows 計算機上編寫了作業。 我今天可以訪問,但由於多個錯誤而無法運行。 如何轉換我的代碼以使其與 linux 兼容?
#include <stdio.h>
#include <string.h>
char ch;
#define NUMROWS 6
#define NUMCOLS 6
int i, j;
char str1[42] = {'S', '#', '#', '#', '#', '#', '\n', '.', '.', '.', '.', '.', '#', '\n', '#', '.', '#', '#', '#', '#', '\n', '#', '.', '#', '#', '#', '#', '\n', '.', '.', '.', '#', '.', 'G', '\n', '#', '#', '.','.','.','#','\n'};
main()
{
char response[10];
char response2[10];
char yes;
char no;
char ready;
char notready;
printf("Do you want to play the maze game? yes|no : \n");
scanf("%s", &response);
if (!strcmp(response, "yes"))
{
printf("\n");
printf("Game will begin!");
printf("\n");
printf("\n");
printf("Maze Board Example: \n");
printf("S##### \n");
printf(".....# \n");
printf("#.#### \n");
printf("#.#### \n");
printf("...#.G \n");
printf("##...# \n");
printf("\n");
printf("Intstructions:");
printf("Are you ready to play? ready|no \n");
scanf("%s", &response);
if (!strcmp(response, "ready"))
{
printf("playing...");
system("cls");
while(1)
{
mazeGo();
}
}
}
if (!strcmp(response, "no"))
{
printf("\n");
printf("Game will exit");
}
return 0;
}
void mazeGo()
{
str1[0]= '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[7] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[8] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[9] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[10] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[11] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[15] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[22] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[28] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[29] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[30] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[37] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[38] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[39] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[32] = '+';
printf("%s", str1);
sleep(1);
system("cls");
str1[33] = '+';
printf("%s", str1);
sleep(1);
system("cls");
printf("\n You win!");
exit(0);
}
我無法想象您在 Windows 上沒有收到任何警告...
...
printf("Do you want to play the maze game? yes|no : \n");
scanf("%s", &response);
...
=> 格式“%s”需要“char ”類型的參數,但參數 2 的類型為“char ( )[10]
應該是scanf("%s", response);
甚至更好: scanf("%9s", response);
這足以調用未定義的行為並讓程序順利運行,隨時崩潰或提供不一致的 output ......並且程序中重復錯誤......
system("cls");
=> 警告:function 'system' 的隱式聲明
當然不是錯誤,但最佳實踐建議在使用函數之前聲明函數。 您應該使用#include <stdlib>
void mazeGo()
=> 警告:'mazeGo' 的類型沖突
該標識符首先在mazeGo();
並被隱含地聲明為int mazeGo()
。 你應該有一個void mazeGo();
main 之前的聲明
和一個數字或未使用的變量。 這又不是錯誤,但在代碼中留下未使用的變量可能會隱藏拼寫錯誤並使未來的維護更加困難。
除此之外, system("cls")
只會在 Windows 上清除屏幕。 在 Linux 上, system
啟動的 shell 將抱怨未知命令,但不應中止程序。
TL/DR:
首先,您應該將 void 的返回類型添加到您的主 function。
void main()...
然后您嘗試執行“cls”命令,該命令在 linux 中將不起作用,因為它是 windows 特定命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.