写一个把a变成A的c语言代码
时间: 2023-03-30 14:03:05 浏览: 112
#include <stdio.h>
int main() {
char a = 'a';
char A = a - 32;
printf("%c", A);
return ;
}
相关问题
如何写一个dfa判断a和b俩个字母词法的代码用C语言
以下是一个简单的使用C语言实现判断字母'a'和'b'的DFA代码:
```
#include <stdio.h>
int main() {
char input;
int state = 0;
printf("请输入一个由'a'和'b'构成的字符串:");
while ((input = getchar()) != '\n') {
if (input == 'a' && state == 0) {
state = 1;
} else if (input == 'b' && state == 1) {
state = 2;
} else {
state = -1;
break;
}
}
if (state == 2) {
printf("输入的字符串符合要求。\n");
} else {
printf("输入的字符串不符合要求。\n");
}
return 0;
}
```
这个代码实现了一个简单的DFA,接收由'a'和'b'构成的字符串,并且只接受以'a'开头,以'b'结尾的字符串。在程序中,变量state表示当前的状态,初值为0,如果读取到'a',则状态变为1,如果读取到'b',则状态变为2,否则即为非法输入,将状态设为-1并跳出循环。如果最终状态为2,则输出“输入的字符串符合要求”,否则输出“输入的字符串不符合要求”。
写个贪吃蛇代码用C语言
好的,以下是一个简单的贪吃蛇的C语言代码:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define map_width 30 // 地图的宽度
#define map_height 20 // 地图的高度
int score = 0; // 得分
int dir = 0; // 移动的方向
int map[map_width][map_height]; // 地图
// 蛇的结构体
struct Snake
{
int x;
int y;
struct Snake *next;
} *head, *tail, *node;
// 在指定坐标处输出字符
void draw(int x, int y, char c)
{
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
putchar(c);
}
// 初始化蛇和地图
void init()
{
// 初始化地图
for (int i = 0; i < map_width; i++) {
for (int j = 0; j < map_height; j++) {
if (i == 0 || i == map_width - 1 || j == 0 || j == map_height - 1) {
map[i][j] = 1;
}
else {
map[i][j] = 0;
}
}
}
// 初始化蛇
head = tail = (struct Snake*)malloc(sizeof(struct Snake));
head->x = map_width / 2;
head->y = map_height / 2;
head->next = NULL;
map[head->x][head->y] = 2;
node = (struct Snake*)malloc(sizeof(struct Snake));
node->x = head->x + 1;
node->y = head->y;
node->next = NULL;
tail = node;
map[node->x][node->y] = 2;
// 随机放置食物
int x, y;
do {
x = rand() % (map_width - 2) + 1;
y = rand() % (map_height - 2) + 1;
} while (map[x][y] != 0);
draw(x, y, '@');
map[x][y] = 3;
// 输出地图边框
for (int i = 0; i < map_width; i++) {
for (int j = 0; j < map_height; j++) {
if (map[i][j] == 1) {
draw(i, j, '#');
}
}
}
}
// 判断按键
void check_key()
{
char c = getch();
switch (c) {
case 'w':
if (dir != 2) {
dir = 0;
}
break;
case 'd':
if (dir != 3) {
dir = 1;
}
break;
case 's':
if (dir != 0) {
dir = 2;
}
break;
case 'a':
if (dir != 1) {
dir = 3;
}
break;
}
}
// 移动蛇
void move()
{
int x = head->x, y = head->y;
switch (dir) {
case 0: // 上
y--;
break;
case 1: // 右
x++;
break;
case 2: // 下
y++;
break;
case 3: // 左
x--;
break;
}
if (map[x][y] == 1 || map[x][y] == 2) { // 碰到边界或自己
draw(map_width / 2 - 5, map_height / 2, 'G');
Sleep(2000);
exit(0);
}
else if (map[x][y] == 3) { // 吃到食物
node = (struct Snake*)malloc(sizeof(struct Snake));
node->x = x;
node->y = y;
node->next = NULL;
tail->next = node;
tail = node;
map[x][y] = 2;
// 随机放置食物
int i, j;
do {
i = rand() % (map_width - 2) + 1;
j = rand() % (map_height - 2) + 1;
} while (map[i][j] != 0);
draw(i, j, '@');
map[i][j] = 3;
score += 10; // 每吃一个果子得10分
}
else { // 移动
node = head;
head = head->next;
map[head->x][head->y] = 2;
map[node->x][node->y] = 0; // 头移动后,原来的蛇身所在位置变为空地
node->x = x;
node->y = y;
map[node->x][node->y] = 2;
}
draw(head->x, head->y, '*');
}
int main()
{
system("title 贪吃蛇小游戏");
system("mode con cols=50 lines=25");
srand((unsigned int)time(NULL));
init();
while (1) {
check_key();
move();
draw(map_width + 3, 1, 'S');
draw(map_width + 4, 1, 'C');
draw(map_width + 5, 1, 'O');
draw(map_width + 6, 1, 'R');
draw(map_width + 7, 1, 'E');
draw(map_width + 8, 1, ':');
printf("%d", score);
Sleep(100);
}
return 0;
}