简单的c语言程序代码
1. c语言跪求 最简单的 求和程序 代码
#include<stdio.h>void main(){ int a,b; printf("please input tow number:"); scanf("%d%d",&a,&b); printf("a+b=%d",a+b);}我调试过了觉得正确无误!
2. 求简单C语言程序代码!
小游戏2048源码:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int jsk( ); //计算空格数
void rsgm( ); //重置游戏
void inkey( ); //按键输入
void left( ); //向左移动
void right( ); //向右移动
void up( ); //向上移动
void down( ); //向下移动
void show( ); //输出界面
void adnum( ); //添加随机数
void yes( ); //游戏是否结束(1是0否)
void gtxy(int x, int y); //控制光标位置的函数
int a[4][4]; //存储16个格子中的数字
int score = 0; //每局得分
int best = 0; //最高得分
int ifnum; //是否需要添加数字(1是0否)
int over; //游戏结束标志(1是0否)
int i,j,k;
int main( )
{ rsgm( ); //重置游戏
inkey( ); //按键输入
return 0;
}
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
} //用于控制字符颜色的函数
void rsgm( ) //重置游戏
{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //启动随机数发生器
int n = rand( ) % 16; //随机函数产生0-15的数字
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }
else { a[i][j] = 4; } n--; }
else { a[i][j] = 0; n--; }
}
}
adnum( );
system("cls");
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
setColor(14, 0); //设置字体淡红色,背景为黑色
printf(" 2048小游戏"); setColor(7, 0); //恢复白字黑底
printf(" ┌──────┬──────┬──────┬──────┐");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" └──────┴──────┴──────┴──────┘");
show( );
}
void show( ) //输出界面
{ for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是光标到指定位置输出数字
if(a[i][j]==0){printf(" "); setColor(7, 0); printf("│");}
else if(a[i][j]<10){ if (a[i][j] == 2) {setColor(14, 0); }
else if (a[i][j] == 4) {setColor(13, 0); }
else if (a[i][j] == 8) {setColor(12, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 100){if (a[i][j] == 16) {setColor(12, 0); }
else if (a[i][j] == 32) {setColor(10, 0); }
else if (a[i][j] == 64) {setColor(2, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 1000) {if (a[i][j] == 128) {setColor(9, 0); }
else if (a[i][j] == 256) {setColor(1, 0); }
else if (a[i][j] == 512) {setColor(13, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 10000) {if (a[i][j] == 1024) {setColor(5, 0); }
else {setColor(15, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
}
if (jsk( ) == 0)
{ yes( ); if (over) { gtxy(9,12); setColor(10, 0);
printf(" 游戏结束!是否继续? [ Y/N ]:"); }
}
}
void inkey( ) //按键输入
{ int key;
while (1)
{ key = getch( );
if (over) { if (key == 89|| key == 121) {rsgm( ); continue; }
else if (key == 78|| key == 110) { return; }
else continue; }
ifnum = 0;
if(key==224)key=getch( );
switch (key)
{ case 75: left( ); break;
case 77: right( ); break;
case 72: up( ); break;
case 80: down( );break;
}
if (score > best) { best = score; }
if (ifnum) { adnum( ); show( ); }
}
}
int jsk( ) //计算空格数
{ int n = 0;
for (i = 0; i < 4; i++)
{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }
return n;
}
void left( ) //向左移动
{ for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{ if (a[i][j] > 0)
{ if ( a[i][k] == a[i][j])
{ a[i][k] *= 2; k++;
score = score + 2 * a[i][j];
a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }
k++; }
}
}
}
}
void right( ) //向右移动
{for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[i][j] > 0)
{ if (a[i][k] == a[i][j])
{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }
}
}
}
}
void up( ) //向上移动
{for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{if (a[j][i] > 0)
{if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];
a[j][i] = 0; ifnum = 1; }
else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }
k++; }
}
}
}
}
void down( ) //向下移动
{ for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[j][i] > 0)
{if (a[k][i] == a[j][i])
{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }
else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else {a[k - 1][i] = a[j][i];
if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }
}
}
}
}
void adnum( ) //添加随机数
{ srand(time(0)); //启动随机数发生器
int n = rand( ) % jsk( );
for (int i = 0; i < 4; i++)
{for (int j = 0; j < 4; j++)
{ if (a[i][j] == 0) { if (n != 0) { n--; }
else {int k = rand() % 3;
if (k == 0 || k == 1) {a[i][j] = 2; return; }
else {a[i][j] = 4; return; } }
}
}
}
}
void yes( ) //游戏是否结束
{ for (int i = 0; i < 4; i++)
{for (int j = 0; j < 3; j++)
{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}
}
over = 1;
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
3. c语言编程代码
两种方法我写在一起,可以独立拆开。
#include <stdio.h>
void finda1(char a[3][10]);
void finda2(char a[3][10]);
void show(char (*p)[10]);
int main()
{
char a[3][10]={{"gehajl"},{"788a987a7"},{"ccabbbabbb"}};
printf("原数组内容: ");
show(a);
printf(" 1、用数组指针的方法(函数finda1): ");
finda1(a);
printf("执行后: ");
show(a);
printf(" --------------------- ");
char b[3][10]={{"gehajl"},{"788a987a7"},{"ccabbbabbb"}};
printf("原数组内容: ");
show(a);
printf(" 2、用指针数组的方法(函数finda2): ");
finda2(b);
printf("执行后: ");
show(b);
return 0;
}
void finda1(char a[3][10])
{
int i,j;
char (*p)[10]=a;
for(i=0;i<3;i++)
for(j=0;j<10;j++)
if(p[i][j]=='a')
printf("发现:第%d行第%d个元素是‘a’,已替换 ",i+1,j+1),p[i][j]='1';
}
void finda2(char a[3][10])
{
int i,j;
char *p[3]={&a[0][0],&a[1][0],&a[2][0]};
for(i=0;i<3;i++)
for(j=0;j<10;j++)
if(p[i][j]=='a')
printf("发现:第%d行第%d个元素是‘a’,已替换 ",i+1,j+1),p[i][j]='1';
}
void show(char (*p)[10])
{
int i,j;
for(i=0;i<3;i++,printf(" "))
for(j=0;j<10;j++)
printf("%c ",p[i][j]);
}
4. 求几个简单的C语言小程序
1.代码如下
#include <stdio.h>
int main()
{
char c;
int letter=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:letter=%d space=%d digit=%d others=%d\n",letter,space,digit,others);
getch();
}
2.代码如下
//求a和b最大公约数程序1:
int yue(int a,int b)
{
int k=1;
int t=a>b?b:a;//a大取b,否则取a
for(int i=1;i<=t;i++)
{
if((a%i==0)&&(b%i==0)) k=i;
else continue;
}
return k;//返回最大公约数
}
//求a和b的最小公倍数,参数c传递的是a和b的最大公约数
int bei(int a,int b,int c)
{
return (a*b)/c;
}
void main()
{
int a,b;
cout<<"请按从大到小的顺序输入2个要求值的数"<<endl;
cin>>a>>b;
cout<<"两个数的最大公约数是"<<yue(a,b)<<endl;
cout<<"两个数的最小公倍数是"<<bei(a,b,yue(a,b))<<endl;
}
//求最大公约数程序2
#include <stdio.h>
int main()
{
int p,r,n,m,temp;
printf("please enter two positive integer numbers n,m:");
scanf("%d%d",&n,&m);
if(n<m) //大数放在n中,小数放在m中;
{
temp=n;
n=m;
m=temp;
}
p=n*m; //先将n和m的乘积保存在P中,以便求最小公倍数用
while(m!=0)
{
r=n%m; //求n和m的最大公约数
n=m;
m=r;
}
printf("最小公倍数为:%d\n",n);
printf("最大公约数为:%d\n",p/n);
return 0;
}
3.代码如下
#include <iostream>
using namespace std;
int main()
{
int i,j;
int a[3][3];
for(i=0;i<3;i++)
{
printf("input the %d line' element:",i);
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%2d",a[i][j]);
}
cout<<endl;
}
int sum=a[0][0]+a[1][1]+a[2][2]+a[0][2]+a[1][1]+a[2][0];
printf("该矩阵对角线元素之和为:%d\n",sum);
return 0;
}
4.代码如下
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string s1,s2;
cin>>s1>>s2;
cout<<s1<<s2<<endl;
return 0;
}
5.第五个就是文件重定向的问题,和第一个差不多了,只要将输入定向到文件,将输出定向到标准输出即可