基本格式:
#include <stdio.h>
#include <stdlib.h>
int main()
{
puts("C语言中文网");
// system("pause");
return 0;
}
输出字母C
#include "stdio.h"
int main()
{
printf("Hello C-world!\n");
printf(" ****\n");
printf(" *\n");
printf(" * \n");
printf(" ****\n");
}
x、y、z排序
#include "stdio.h"
int main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if (x>y) /*交换x,y的值*/
{
t=x;
x=y;
y=t;
}
if(x>z) /*交换x,z的值*/
{
t=x;
x=z;
z=t;
}
if(y>z) /*交换z,y的值*/
{
t=y;
y=z;
z=t;
}
printf("small to big: %d %d %d\n",x,y,z);
}
加减乘除
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[])
{
while (1){
int value1;
int value2;
char y='+';
//|输入第一个值
printf("请输入第一个数:");
scanf("%d", &value1);
//|输入运算符
printf("请输入运算符:");
scanf("%s",&y);
//|输入第二个值
printf("请输入第二个数:");
scanf("%d", &value2);
//|判断运算符
if (y=='+')
{
printf("运算结果:");
printf("%d",value1+value2);
}
else if (y=='-')
{
printf("运算结果:");
printf("%d ", value1 - value2);
}
else if (y == '*')
{
printf("运算结果:");
printf("%d ", value1 * value2);
}
else if (y == '/')
{
printf("运算结果:");
printf("%d ", value1 / value2);
}
else{
printf("您输入的运算符有误!");
}
printf("\n");
};
}
猜拳游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char gamer; // 玩家出拳
int computer; // 电脑出拳
int result; // 比赛结果
// 为了避免玩一次游戏就退出程序,可以将代码放在循环中
while (1){
printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n");
printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n");
scanf("%c%*c",&gamer);
switch (gamer){
case 65: //A
case 97: //a
gamer=4;
break;
case 66: //B
case 98: //b
gamer=7;
break;
case 67: //C
case 99: //c
gamer=10;
break;
case 68: //D
case 100: //d
return 0;
default:
printf("你的选择为 %c 选择错误,退出...\n",gamer);
getchar();
system("cls"); // 清屏
return 0;
break;
}
srand((unsigned)time(NULL)); // 随机数种子
computer=rand()%3; // 产生随机数并取余,得到电脑出拳
result=(int)gamer+computer; // gamer 为 char 类型,数学运算时要强制转换类型
printf("电脑出了");
switch (computer)
{
case 0:printf("剪刀\n");break; //4 1
case 1:printf("石头\n");break; //7 2
case 2:printf("布\n");break; //10 3
}
printf("你出了");
switch (gamer)
{
case 4:printf("剪刀\n");break;
case 7:printf("石头\n");break;
case 10:printf("布\n");break;
}
if (result==6||result==7||result==11) printf("你赢了!");
else if (result==5||result==9||result==10) printf("电脑赢了!");
else printf("平手");
system("pause>nul&&cls"); // 暂停并清屏
}
return 0;
}