求几个简单的C语言小程序

1.输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数
2,输入两个正整数m和n,求其最大公约数和最小公倍数
3。求一个3乘3矩阵对角线元素之和
4,将两个字符串连接起来并输出,不可用strcat函数
5一篇文章,共三行文字,每行80个字符,编写程序并分别统计出其中英文大写字母,英文小写字母,数字,空格及其他字符的个数

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.第五个就是文件重定向的问题,和第一个差不多了,只要将输入定向到文件,将输出定向到标准输出即可
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-05-20
求最大公约数和最大公倍数的这个我感觉比较好,楼主看下:
#include<iostream>
#include<stdlib.h>
using namespace std;
int maxyue(int x,int y)
{if(x%y==0) return y;
else return maxyue(y,x%y);
}
int maxbei(int x,int y)
{return (x*y/maxyue(x,y));}
int main()
{
int a,b,maxy,maxb;
cout<<"Please input two numbers:";
cin>>a>>b;
maxy=maxyue(a,b);
maxb=maxbei(a,b);
cout<<a<<" "<<b<<"的最大公约数是:"<<maxy<<endl;
cout<<a<<" "<<b<<"的最大公倍数是:"<<maxb<<endl;
system("pause");
}
相似回答