发表于2006-12-15 8:32:00
实验二 数据类型、运算符和表达式 1.要将“China”译成密码,密码规律是:用原来的字母后面第5个字母代替原来的字母。例如,字母“A”后面第5个是“F”,用“F”代替“A”。因此,“China”应译为“Hmnsf”。请编一程序,用赋初值的方法使c1、c2、c3、c4、c5五个变量的值分别为’C’、’h’、’i’、’n’、’a’,经过运算,使其分别变为’H’、’m’、’n’、’s’、’f’,并输出。
#include <stdio.h>
#include <conio.h> void main() { char c1,c2,c3,c4,c5; c1='C'; c2='h'; c3='i'; c4='n'; c5='a'; printf("The old string is:%c%c%c%c%c\n",c1,c2,c3,c4,c5); printf("The new string is:%c%c%c%c%c\n",c1+5,c2+5,c3+5,c4+5,c5+5); getch(); } 实验三 最简单的C程序设计
1.编写程序,并上机运行。题目为:已知圆半径r=1.5,圆柱高h=3, 求圆周长,圆面积,圆球表面
积,圆球体积,圆柱体积。要求用scanf()输入圆半径和圆柱高,输出计算结果,输出时要求有文字提示,取小数点后2位数字,pi可取3.1415926。
#include <stdio.h>
void main() { float h,r,l,s,sq,vq,vz; float pi=3.1415926; printf("\nPlease input h,r:"); scanf("%f,%f",&h,&r); l=2*pi*r; s=r*r*pi; sq=4*pi*r*r; vq=3.0/4.0*pi*r*r*r; vz=pi*r*r*h; printf("l=%6.2f\n",l); printf("s=%6.2f\n",s); printf("sq=%6.2f\n",sq); printf("vq=%6.2f\n",vq); printf("vz=%6.2f\n",vz); getch();
} 实验四 选择结构程序设计
1、编写并调试程序:输入3个整数,按由小到大的顺序输出;
#include <stdio.h>
void main() { int a,b,c,t; printf("Please input three numbers:");
scanf("%d%d%d",&a,&b,&c); if (a>b)
{ t=a;a=b;b=t;} if (a>c)
{ t=a;a=c;c=t; } if (b>c)
{ t=b;b=c;c=b;} printf("The Ordered numbers list is :\n");
printf("%d %d %d \n",a,b,c); getch(); } 实验五 循环结构程序设计
1、编写程序。输入两个正整数m和n,求他们的最大公约数和最小公倍数(本题是教材第6章习题6.1),要求对输入的数字进行格式检验(是否为正整数等),多组数据进行调试。
#include <stdio.h>
void main()
{
int n=0,m=0,r,p,temp;
printf("Please input two integer:");
while ((m<=0) ||( n<=0)) scanf("%d%d",&n,&m); //非正数就重新输入
if (n<m) //把大的数放在n中,小数放在m中
{temp=m;n=m;m=temp}
p=n*m;
while (m!=0) //辗转相除求最大公约数
{
r=n%m;
n=m;
m=r;
}
printf("The result is the best yueshu=%d,gongbeishu=%d",n,p/n);
}
实验六 数组
1、编写程序并上机调试。用scanf函数输入10个整数,存入一数组,然后按照倒序存放,最后输出数组元素。
#include <stdio.h>
void main() { int i,num[10]; printf("\nPlease input 10 integers:\n");
for (i=9;i>=0;i--) scanf("%d",&num[i]); //直接倒序存放 printf("\nThe memory result is:\n");
for(i=0;i<10;i++) printf("%d ",num[i]); } 2、编写一个程序,筛选法求100之内的素数(逐一排除是否为素数),也可以自己选择另外的方法。
#include <stdio.h>
#include <math.h> void main() //注意本程序中代码的精简部分 { int i,j,n=0,a[100]; a[0]=0;//把第1个数,数值置为0
a[1]=2; for (i=3;i<=100;i++) { a[i-1]=i; //给a[i-1]赋初值 if (i%2==0) //非2的偶数肯定不是素数 a[i-1]=0; else for (j=2;j<sqrt(i);j++) if (i%j==0) {a[i-1]=0;break;} //只要可以确定是非素数即可终止 } for (i=2;i<=100;i++) //每行输出10个素数 { if (a[i-1]!=0) { printf("%5d",a[i-1]); n++; } if (n==10) { printf("\n"); n=0; } } } 4.编写一个程序实现两个字符串的连接,要求使用数组,不能使用C语言中自带的字符串连接函数strcat().
#include <stdio.h>
void main()
{
void linkstr(char string1[],char string2[],char string[]);
char s1[100],s2[100],s[100];
printf("\nPlease input string1:");
scannf("%s",s1);
printf("\nPlease input string2:");
scannf("%s",s2);
linkstr(s1,s2,s);
printf("The new string is %s\n",s);
}
void linkstr(char string1[],char string2[],char string[])
{
int i,j;
for(i=0;string1[i]!='\0';i++)
string[i]=string1[i];
for (j=0;string2[j]!='\0';j++)
string[i+j]=string2[j];
string[i+j]='\0';/*不可缺少,添加结束符*/
}
实验七 函数
1、用一个函数来实现将一行字符串中最长的字符串输出。此行字符串从主函数传递给该函数。(最长字符串指的是最长的连续字符串,遇空格则终止)
#include <stdio.h>
#include <string.h> void longest(char str[]); void main() { char tempstr[100]; gets(tempstr);
longest(tempstr); }
void longest(char str[])
{ int i=0,len=0,start=0,str_length=0,str_start=0; for (i=0;i<strlen(str);i++)
{ if (str[i]!=' ') len++; else { if (len>str_length) {str_start=start;str_length=len;} len=0; start=i; } } if (len>str_length) {str_start=start;str_length=len;} printf("\nThe largest str is: \n ");
for (i=str_start;i<=str_start+str_length;i++) printf("%c",str[i]); printf("\n"); } 2、从键盘输入10个整数,输出其中的最大数和最小数,并将这10个数从小到大排序输出。要求分别编写函数imaxnum、avenum、ordernum来求最大数、平均值和排序,然后在主函数中调用这些函数输出结果。(源程序省略)
3、求两个整数的最大公约数和最小公倍数,分别用一个函数来实现(程序省略)。
4、编写一个计算表达式m! /(n! *(m-n)!)值的程序(m>=n>=0),要求:
(1) 用键盘输入m和n的值,编写函数:int fact(int x)求x!的值。
(2) 程序根据输入自动判断m和n值的大小赋值,注意0!=1需要另外定义。 #include <stdio.h>
float fact(int x);
void main()
{
int m,n,temp;
float result;
printf("\nPlease input two disequal integers:");
scanf("%d%d",&m,&n);
if (m<n)
{
temp=m;
m=n;
n=temp;
}
result=fact(m)/(fact(n)*fact(m-n));
printf("\n The result is %f",result);
}
float fact (int x)
{
int i=1;
float result=1.0;
if (x<2) returen 1;
while (i<=x) result*=i++;
return result;
}
|
|
楼主 |