1.贪吃蛇c语言源代码
2.写C语言程序的语言实语言源代一般步骤是怎样的
3.Cè¯è¨å®ç°å¾ä¹¦ç®¡çç³»ç»
4.用c语言程序设计一个简单计算器,求其源代码
5.用C语言写的计算器源代码
贪吃蛇c语言源代码
下面是一个简单的贪吃蛇游戏的C语言实现框架,不包含完整的现源图形界面,但展示了游戏逻辑的码c码基本结构。此示例使用控制台字符来模拟蛇的程序移动和食物的生成。请注意,语言实语言源代这只是现源网站源码 java一个概念性的实现,实际应用中可能需要借助图形库(如SDL、码c码OpenGL或Windows API)来创建图形界面。程序
```c
#include
#include
#include // 注意:_kbhit() 和 _getch() 是语言实语言源代特定于某些编译环境的
// 假设的蛇身和地图大小
#define SIZE
int x, y, fruitX, fruitY, score;
int tailX[], tailY[];
int nTail;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;
void Setup() {
// 初始化代码
dir = STOP;
x = SIZE / 2;
y = SIZE / 2;
fruitX = rand() % SIZE;
fruitY = rand() % SIZE;
score = 0;
}
void Draw() {
// 绘制游戏界面,此处省略
// 使用循环打印蛇身和食物位置
}
void Input() {
// 处理用户输入
if (_kbhit()) {
switch (_getch()) {
case 'a': dir = LEFT; break;
case 'd': dir = RIGHT; break;
case 'w': dir = UP; break;
case 's': dir = DOWN; break;
}
}
}
void Logic() {
// 移动逻辑,现源碰撞检测等
// 此处省略
}
int main() {
Setup();
while (1) {
Draw();
Input();
Logic();
// 延时
Sleep();
}
return 0;
}
```
注意:`_kbhit()` 和 `_getch()` 是码c码特定于某些编译环境(如Microsoft Visual Studio)的函数,用于检测键盘输入。程序在其他环境中,语言实语言源代kada故事源码可能需要使用不同的现源方法来实现输入处理。此外,码c码由于篇幅限制,此代码省略了具体的绘制和逻辑实现细节。
写C语言程序的一般步骤是怎样的
1、编写源代码:首先,意愿公式源码使用C语言编写源代码,这是程序开发的第一步。源代码是程序员用高级语言编写的,人类可读的文本文件。
2、编译源代码:接下来,语言附带源码使用C语言编译器将源代码转换为可执行的二进制文件。编译过程包括词法分析、语法分析、语义检查、中间代码生成、代码优化和目标代码生成等阶段。济宁pc源码如果在编译过程中发现语法错误,编译器会提供错误提示,以便开发者修正。
3、链接目标文件:一旦源代码被编译成目标文件,还需要将这些目标文件与系统库链接起来,形成一个完整的可执行程序。这个过程称为链接,它将各个目标文件和系统库中的函数和变量合并,生成最终的可执行文件。
4、执行程序:最后,运行编译和链接后生成的可执行程序文件。这个文件包含了可以被计算机处理器执行的指令,执行程序将按照这些指令执行,完成预定的任务。
Cè¯è¨å®ç°å¾ä¹¦ç®¡çç³»ç»
å¾ä¹¦ç®¡çç³»ç»çcå®ç°ç¨äºå¾ä¹¦ä¿¡æ¯ç管çãå æ¬å¾ä¹¦ä¿¡æ¯çå建ãå¾ä¹¦ä¿¡æ¯çæå°ãå¾ä¹¦ä¿¡æ¯çæ¥è¯¢ãå¾ä¹¦ä¿¡æ¯çä¿®æ¹ãå¾ä¹¦ä¿¡æ¯çå é¤ãæ¹ä¾¿ç¨æ·æ´çå¾ä¹¦ï¼æ¥è¯¢å¾ä¹¦ã
è¿ä¸ªå¾ä¹¦ç®¡çç³»ç»æ¯ç±åé¾è¡¨è¿ä¸æ°æ®ç»æå®ç°çï¼æ¿åå æ¬å¾ä¹¦ä¿¡æ¯çå建ãæå°ãæ¥è¯¢ãä¿®æ¹ãå é¤ã以åå¾ä¹¦ä»·æ ¼çæåºçç»æã 代ç åé¢ä¹æ注éçï¼åºæ¬å¾å¥½ç解çã
ä¸é¢ä¸ºæºä»£ç ï¼
#include <stdio.h>#include <stdlib.h>#include <string.h>//3.æ°æ®ç设计//3.1ç¨åºçæ°æ®åå¨--->容å¨//3.2æ°æ®çç»æ --->å¾ä¹¦çä¿¡æ¯struct bookInfo{ char name[];//书åfloat price;//书ç±çä»·æ ¼int num;//书ç±çæ°é};//å®ä¹é¾è¡¨struct Node{ struct bookInfo data;struct Node* next;};struct Node* list = NULL;//å°é¾è¡¨å£°ææå ¨å±åé//å建表头ï¼è¡¨å¤´å°±æ¯ç»æä½åéstruct Node* createHead(){ //å¨æå åç³è¯·struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));//åéåå§åheadNode->next = NULL;return headNode;}//å建èç¹ï¼ä¸ºæå ¥ååå¤// æç¨æ·çæ°æ®åæç»æä½åéstruct Node* createNode(struct bookInfo data){ struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = data;newNode->next = NULL;return newNode;}//æ°æ®æå ¥ï¼å¤´ææ³ï¼void insertNodeByHead(struct Node* headNode, struct bookInfo data){ struct Node* newNode = createNode(data);newNode->next = headNode->next;headNode->next = newNode;}//å°¾ææ³/*struct insertNodeByTall(struct Node* headNode, int data){ struct Node* pMove = headNode;while (pMove != NULL){ pMove = pMove->next;}struct Node* newNode = createHead(data);pMove->next = newNode;}*///æå®å é¤ï¼å é¤é¾è¡¨ä¸å ç´ ï¼//posLeftNode->next=posNode->next;//free(posNode);void deleteNodeByName(struct Node* headNode, char* bookname){ struct Node* posLeftNode = headNode;struct Node* posNode = headNode->next;//书ç±ååæ¯å符串ï¼å符串æ¯è¾å½æ°while (posNode != NULL && strcmp(posNode->data.name, bookname)){ posLeftNode = posNode;posNode = posLeftNode->next;}//讨论æ¥æ¾çç»æif (posNode == NULL)return;else{ printf("å é¤æåï¼\n");posLeftNode->next = posNode->next;free(posNode);posNode = NULL;}}//æ¥æ¾struct Node* searchByName(struct Node* headNode, char* bookName){ struct Node* posNode = headNode->next;while (posNode != NULL && strcmp(posNode->data.name, bookName)){ posNode = posNode->next;}return posNode;}//æå°é¾è¡¨void printList(struct Node* headNode){ struct Node* pMove = headNode->next;printf("书å\tä»·æ ¼\tæ°é\n");while (pMove != NULL){ printf("%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num);pMove = pMove->next;}}//ç´æ¥æ件æä½//æ件åæä½void saveInfoToFile(const char* filename, struct Node* headNode){ FILE* fp = fopen(filename, "w");struct Node* pMove = headNode->next;while (pMove != NULL){ fprintf(fp, "%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num);pMove = pMove->next;}fclose(fp);}//æ件读æä½void readInfoFromFile(const char* fileName, struct Node* headNode){ FILE* fp = fopen(fileName, "r");if (fp == NULL){ //ä¸åå¨å°±å建åºæ¥è¿ä¸ªæ件fp = fopen(fileName, "w+");}struct bookInfo tempData;while (fscanf(fp, "%s\t%f\t%d\n", tempData.name, &tempData.price, &tempData.num) != EOF){ insertNodeByHead(list, tempData);}fclose(fp);}//å泡æåºï¼é¾è¡¨ï¼void bubbleSortList(struct Node* headNode){ for (struct Node* p = headNode->next; p != NULL; p = p->next){ for (struct Node* q = headNode->next; q->next != NULL; q = q->next){ if (q->data.price > q->next->data.price){ //交æ¢å¼struct bookInfo tempData = q->data;q->data = q->next->data;q->next->data = tempData;}}}printList(headNode);}//2.交äºvoid keyDown(){ int userkey = 0;struct bookInfo tempBook;//产çä¸ä¸ªä¸´æ¶çåéåå¨ä¹¦ç±ä¿¡æ¯struct Node* result = NULL;scanf("%d", &userkey);switch (userkey) { case 0:printf(" ã ç»è®° ã \n");printf("è¾å ¥ä¹¦ç±çä¿¡æ¯(name,price,num):");scanf("%s%f%d", tempBook.name, &tempBook.price, &tempBook.num);insertNodeByHead(list, tempBook);saveInfoToFile("bookinfo.txt", list);break;case 1:printf(" ã æµè§ ã \n");printList(list);break;case 2:printf(" ã åé ã \n"); printf("请è¾å ¥ä½ è¦åé ç书ç±ï¼");scanf("%s", tempBook.name);result = searchByName(list,tempBook.name);if (result == NULL)printf("没æç¸å ³ä¹¦ç±æ æ³åé ï¼\n");else{ if (result->data.num > 0){ result->data.num--;printf("åé æå\n");saveInfoToFile("bookinfo.txt", list);}else{ printf("å½å书ç±æ åºåï¼åé 失败ï¼\n");}}break;case 3:printf(" ã å½è¿ ã \n");printf("请è¾å ¥ä½ è¦å½è¿ç书ç±ï¼");scanf("%s", tempBook.name);result = searchByName(list, tempBook.name);if (result == NULL)printf("书ç±æ¥æºéæ³ï¼\n");else{ result->data.num++;printf("书ç±å½è¿æåï¼\n");saveInfoToFile("bookinfo.txt", list);}break;case 4:printf(" ã æ¥æ¾ ã \n");printf("ä½ è¦æ¥è¯¢ç书åï¼");scanf("%s", tempBook.name);result = searchByName(list, tempBook.name);if (result == NULL){ printf("æªæ¾å°ç¸å ³ç»æ!\n");}else{ printf("书å\tä»·æ ¼\tæ°é\n");printf("%s\t%.1f\t%d\n", result->data.name, result->data.price, result->data.num);}break;case 5:printf(" ã æåº ã \n");bubbleSortList(list);break;case 6:printf(" ã å é¤ ã \n");printf("è¾å ¥æ³è¦å é¤ç书å:");scanf("%s", tempBook.name);deleteNodeByName(list, tempBook.name);saveInfoToFile("bookinfo.txt", list);break;case 7:printf(" ã éåº ã \n");printf(" éåºæå \n");system("pause");exit(0); //å ³ææ´ä¸ªç¨åºbreak;default:printf(" ã error ã \n");break;}}//1.çé¢--->èå--->模åvoid makeMenu(){ printf("----------------------------------\n");printf("Eugeoå¾ä¹¦ç®¡çåé ç³»ç»\n");printf("t0.ç»è®°ä¹¦ç±\n");printf("t1.æµè§ä¹¦ç±\n");printf("t2.åé 书ç±\n");printf("t3.å½è¿ä¹¦ç±\n");printf("t4.æ¥æ¾ä¹¦ç±\n");printf("t5.æåºä¹¦ç±\n");printf("t6.å é¤ä¹¦ç±\n");printf("t7.éåºç³»ç»\n");printf("----------------------------------\n");printf("请è¾å ¥(0~7):");}int main(){ list = createHead();//é¾è¡¨åå§åreadInfoFromFile("bookinfo.txt", list);while (1){ makeMenu();keyDown();system("pause");system("cls");}}åæï¼/post/用c语言程序设计一个简单计算器,求其源代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* Define constants for the calculator */
#define UP 0x
#define DOWN 0x
#define LEFT 0x4B
#define RIGHT 0x4D
#define ENTER 0x0D
/* Global variables */
double num1 = 0, num2 = 0, result = 0;
char str1[] = ".+-*/知消扒Qc=^%";
char cnum[5], str2[] = "", c;
int x, y, x0, y0, i, j, v, m, n, act, flag = 1;
/* Function prototypes */
void drawboder(void);
void initialize(void);
void computer(void);
void changetextstyle(int font, int direction, int charsize);
void mwindow(char *header);
int specialkey(void);
int arrow();
/* Main function */
int main() {
initialize();
computer();
closegraph();
return 0;
}
/* Initialize the graphics system */
void initialize(void) {
int xasp, yasp;
GraphDriver = DETECT;
initgraph( &GraphDriver, &GraphMode, "" );
ErrorCode = graphresult();
if (ErrorCode != grOk) {
printf("Graphics System Error: %s\n", grapherrormsg(ErrorCode));
exit(1);
}
getpalette( &palette );
MaxColors = getmaxcolor() + 1;
MaxX = getmaxx();
MaxY = getmaxy();
getaspectratio( &xasp, &yasp );
AspectRatio = (double)xasp / (double)yasp;
}
/* Main calculator function */
void computer(void) {
struct viewporttype vp;
int color, height, width;
mwindow("Calculator");
color = 7;
getviewsettings( &vp );
width = (vp.right + 1) / ;
height = (vp.bottom - ) / ;
x = width / 2;
y = height / 2;
setfillstyle(SOLID_FILL, color + 3);
bar( x + width * 2, y, x + 7 * width, y + height );
setcolor( color + 3 );
rectangle( x + width * 2, y, x + 7 * width, y + height );
setcolor(RED);
outtextxy(x + 3 * width, y + height / 2, "0.");
x = 2 * width - width / 2;
y = 2 * height + height / 2;
for (j = 0; j < 4; ++j) {
for (i = 0; i < 5; ++i) {
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x + width, y + height );
rectangle( x, y, x + width, y + height );
sprintf(str2, "%c", str1[j * 5 + i]);
outtextxy( x + (width / 2), y + height / 2, str2);
x += width + (width / 2);
}
y += (height / 2) * 3;
x = 2 * width - width / 2;
}
x0 = 2 * width;
y0 = 3 * height;
x = x0;
y = y0;
gotoxy(x, y);
arrow();
m = 0;
n = 0;
strcpy(str2, "");
while ((v = specialkey()) != ) {
while ((v = specialkey()) != ENTER) {
putimage(x, y, rar, XOR_PUT);
if (v == RIGHT) {
if (x >= x0 + 6 * width)
x = x0;
else
x += width + width / 2;
m++;
}
if (v == LEFT) {
if (x <= x0)
x = x0 + 6 * width;
else
x -= width - width / 2;
m--;
}
if (v == UP) {
if (y <= y0)
y = y0 + 4 * height + height / 2;
else
y -= height - height / 2;
n--;
}
if (v == DOWN) {
if (y >= 7 * height)
y = y0;
else
y += height + height / 2;
n++;
}
putimage(x, y, rar, XOR_PUT);
}
c = str1[n * 5 + m];
if (isdigit(c) || c == '.') {
if (flag == -1) {
strcpy(str2, "-");
flag = 1;
}
sprintf(temp, "%c", c);
strcat(str2, temp);
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, * width / 2, 3 * height / 2);
outtextxy(5 * width, height, str2);
}
if (c == '+') {
num1 = atof(str2);
strcpy(str2, "");
act = 1;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
if (c == '-') {
if (strcmp(str2, "") == 0)
flag = -1;
else {
num1 = atof(str2);
strcpy(str2, "");
act = 2;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
}
if (c == '*') {
num1 = atof(str2);
strcpy(str2, "");
act = 3;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
if (c == '/') {
num1 = atof(str2);
strcpy(str2, "");
act = 4;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
if (c == '^') {
num1 = atof(str2);
strcpy(str2, "");
act = 5;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
用C语言写的计算器源代码
#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef float DataType;
typedef struct
{
DataType *data;
int max;
int top;
}Stack;
void SetStack(Stack *S,int n)
{
S->data=(DataType*)malloc(n*sizeof(DataType));
if(S->data==NULL)
{
printf("overflow");
exit(1);
}
S->max=n;
S->top=-1;
}
void FreeStack(Stack *S)
{
free(S->data);
}
int StackEmpty(Stack *S)
{
if(S->top==-1)
return(1);
return(0);
}
DataType Peek(Stack *S)
{
if(S->top==S->max-1)
{
printf("Stack is empty!\n");
exit(1);
}
return(S->data[S->top]);
}
void Push(Stack *S,DataType item)
{
if(S->top==S->max-1)
{
printf("Stack is full!\n");
exit(1);
}
S->top++;
S->data[S->top]=item;
}
DataType Pop(Stack *S)
{
if(S->top==-1)
{
printf("Pop an empty stack!\n");
exit(1);
}
S->top--;
return(S->data[S->top+1]);
}
typedef struct
{
char op;
int inputprecedence;
int stackprecedence;
}DataType1;
typedef struct
{
DataType1 *data;
int max;
int top;
}Stack1;
void SetStack1(Stack1 *S,int n)
{
S->data=(DataType1*)malloc(n*sizeof(DataType1));
if(S->data==NULL)
{
printf("overflow");
exit(1);
}
S->max=n;
S->top=-1;
}
void FreeStack1(Stack1 *S)
{
free(S->data);
}
int StackEmpty1(Stack1 *S)
{
if(S->top==-1)
return(1);
return(0);
}
DataType1 Peek1(Stack1 *S)
{
if(S->top==S->max-1)
{
printf("Stack1 is empty!\n");
exit(1);
}
return(S->data[S->top]);
}
void Push1(Stack1 *S,DataType1 item)
{
if(S->top==S->max-1)
{
printf("Stack is full!\n");
exit(1);
}
S->top++;
S->data[S->top]=item;
}
DataType1 Pop1(Stack1 *S)
{
if(S->top==-1)
{
printf("Pop an empty stack!\n");
exit(1);
}
S->top--;
return(S->data[S->top+1]);
}
DataType1 MathOptr(char ch)
{
DataType1 optr;
optr.op=ch;
switch(optr.op)
{
case'+':
case'-':
optr.inputprecedence=1;
optr.stackprecedence=1;
break;
case'*':
case'/':
optr.inputprecedence=2;
optr.stackprecedence=2;
break;
case'(':
optr.inputprecedence=3;
optr.stackprecedence=-1;
break;
case')':
optr.inputprecedence=0;
optr.stackprecedence=0;
break;
}
return(optr);
}
void Evaluate(Stack *OpndStack,DataType1 optr)
{
DataType opnd1,opnd2;
opnd1=Pop(OpndStack);
opnd2=Pop(OpndStack);
switch(optr.op)
{
case'+':
Push(OpndStack,opnd2+opnd1);
break;
case'-':
Push(OpndStack,opnd2-opnd1);
break;
case'*':
Push(OpndStack,opnd2*opnd1);
break;
case'/':
Push(OpndStack,opnd2/opnd1);
break;
}
}
int isoptr(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='(')
return(1);
return(0);
}
void Infix(char *str)
{
int i,k,n=strlen(str);
char ch,numstr[];
DataType opnd;
DataType1 optr;
Stack OpndStack;
Stack1 OptrStack;
SetStack(&OpndStack,n);
SetStack1(&OptrStack,n);
k=0;
ch=str[k];
while(ch!='=')
if(isdigit(ch)||ch=='.')
{
for(i=0;isdigit(ch)||ch=='.';i++)
{
numstr[i]=ch;
k++;
ch=str[k];
}
numstr[i]='\0';
opnd= atof(numstr);
Push(&OpndStack,opnd);
}
else
if(isoptr(ch))
{
optr=MathOptr(ch);
while(Peek1(&OptrStack).stackprecedence>=optr.inputprecedence)
Evaluate(&OpndStack,Pop1(&OptrStack));
Push1(&OptrStack,optr);
k++;
ch=str[k];
}
else if(ch==')')
{
optr=MathOptr(ch);
while(Peek1(&OptrStack).stackprecedence>=optr.inputprecedence)
Evaluate(&OpndStack,Pop1(&OptrStack));
Pop1(&OptrStack);
k++;
ch=str[k];
}
while(!StackEmpty1(&OptrStack))
Evaluate(&OpndStack,Pop1(&OptrStack));
opnd=Pop(&OpndStack);
cout<<"你输入表达式的计算结果为"<<endl;
printf("%-6.2f\n",opnd);
FreeStack(&OpndStack);
FreeStack1(&OptrStack);
}
void main()
{
cout<<"请输入你要计算的表达式,并以“=”号结束。"<<endl;
char str[];
gets(str);
Infix(str);
=================================================================
哈哈!给分吧!