1、太原理工大学13级c语言试验指导书参考答案
试验一 次序
1-3.温度*/编写程序,输入华氏温度(F),按下列公式计算并输出对应摄氏温度(C)。
C = 5/9(F – 32)
#include
2、 其中: #include "stdio.h" #include "math.h" void main() { float a,b,c,s,area; scanf("%f,%f,%f",&a,&b,&c); s=1.0/2.0*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("area=%f\n",area); } /*1-5两数合并*/编写程序,将两个两位数正整数a、b合并形成一个整数放在c中。 合并方法是:将a数十位和个位数依次放在c数十位和千位上, b数十位和个位数依次放在c数个位和百位上
3、
比如,当a=45,b=12,运行结果为:c=5241。
#include
4、include
5、10)
3x – 11 (x≥10)
用 scanf函数输入x值(分别取x<1、1~10、≥10三种情况),求y值。
#include
6、再输入目前日期(年:y1、月:m1、日:d1)数据,计算并输出该学生实足年纪。
#include
7、1;
printf("年纪 = %d\n", nl);
}
2.4编写程序,定义四个字符变量c1,c2,c3和c4,分别用getchar()函数任意输入四个字母,分别输出其中最大值和最小值。
#include
8、f(c3>max)max=c3;
if(c4>max)max=c4;
min=c2
9、 else if(a < 40) m = 2;
else if(a < 50) m = 3;
else if(a < 60) m = 4;
else m = 6;
printf(”m = %d\n”, m);
}
#include
10、ak;
case 3:m=2;break;
case 4:m=3;break;
case 5:m=4;break;
default:m=6;}
printf("m=%d\n",m);
}
else
printf("a为非正数\n");
}
试验三 循环
必做题
3.1编写程序,计算并输出正整数n全部因子(1和n除外)之和。
#include
11、ntf("Input n:");
scanf("%d",&n);
for(i=2;i 12、x0),求出一个新x1;
(4)若x0-x1绝对值小于0.000001,实施步骤(5),不然实施步骤(2);
(5)所求x1就是方程cos(x)-x=0一个实根。
#include 13、统计出其中大写字母、小写字母、空格和数字个数。
提醒:用以下循环格式处理输入一行字符。
while((c = getchar())!=’\n’) /* 若输入c不是回车符时继续 */
循环体
或:
c = getchar();
while(c !=’\n’) /* 若c不是回车符时继续 */
{
循环体
c = getchar();
}
#include 14、nt n1,n2,n3,n4;
n1=n2=n3=n4=0;
printf("请输入一行字符,以换行结束:");
while((c=getchar())!='\n')
if(isupper(c))n1++;
else if(islower(c))n2++;
else if(isdigit(c))n3++;
else if( c==' ')n4++;
printf("\n大写=:%d,小写: %d,数字: %d,空格: %d,\n", n1,n2,n3,n4);
}
3.4编写程序, 15、在屏幕上输出杨辉三角形。
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 1
… … … … …
#include "stdio.h"
#define N 20
void main()
{int i,j,array[N][N]={0},n;
printf("请输入n行:\n");
scanf("%d",&n);
for(i=0;i 16、1;j
#include 17、0;
int s=1;
printf("请输入x:");
scanf("%f",&x);
t=x;
while(fabs(t)>1e-6)
{sum+=t;
s=-s;
i++;
m*=i;
t=s*1.0*pow(x,i)/m;
}
printf("\nx=%f,f(x)=%f\n", x,sum);
}
3.2
#include 18、 double s;
printf("请输入正整数m,n(m 19、 int i,m=2,j,n;
double s=0.0,k=1.0,t;
printf("请输入正整数n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{t=m/k;
s+=t;
j=m;
m=m+k;
k=j;
}
printf("\n和为:%lf\n",s);
}
3.4
#include "stdio.h"
void main()
{int i;
double h=100.0,h1,s=100.0;
for(i=2;i 20、<=10;i++)
{h1=h/2;
h=h1;
s+=2*h;
}
printf("s=%lf,h=%lf\n",s,h);
}
试验四 数组
必做题
4.1编写程序,将3~n之间全部非素数存入数组array中,然后输出该数组元素。
比如:若n=20,则输出 4,6,8,9,10,12,14,15,16,18,20。
#include 21、i=3;i<=N;i++)
{m=sqrt(i);
for(j=2;j<=m;j++)
if(i%j==0){array[k++]=i;break;}
}
for(i=0;i<=k-1;i++)
printf("%d ",array[i]);
printf("\n");
}
4.2 编写程序,将字符数组str中字符串逆置后输出。
比如:若str字符串为“This is a C programe.”,则输出为“.emargorp C a si sihT”。
#include 22、
#include 23、array[N],m,t;
printf("请输入10个整数:");
for(i=0;i 24、[i]);
printf("\n");
}
4.4计算5×5矩阵主对角线元素和此对角线元素之和。若有下列矩阵,则输出值应该是130。
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
#include 25、输入5×5个整数:");
for(i=0;i 26、"\n");}
printf("s=%d\n",s);
}
选做题
4.1
#include "stdio.h"
#include "string.h"
#define M 5
void main()
{int i,j,a[M],k,x;
char str[M][80],t[M];
printf("请输入5个字符串:");
for(i=0;i 27、a[j]>a[k])k=j;
if(k!=i){strcpy(t,str[i]);strcpy(str[i],str[k]);strcpy(str[k],t);
x=a[k];a[k]=a[i];a[i]=x;}
}
for(i=0;i 28、w[M];
printf("请输入1个字符串:\n");
gets(s);a=strlen(s);
for(i=0;i='0'&&s[i]<='9'))t[j++]=s[i];
else w[k++]=s[i];
t[j]='\0';w[k]='\0';
strcat(t,w);strcpy(s,t);
puts(s);
printf("\n");
}
4.3
#include "stdio.h"
#define N 6
void main()
{int i,array[N],t,k;
pr 29、intf("请输入N个整数:\n");
for(i=0;i 30、t;
printf("请输入4×4个整数:\n");
for(i=0;i 31、0){t=array[i][j];array[i][j]=array[i][2-j];array[i][2-j]=t;}
else {t=array[i][j];array[i][j]=array[i][4-j];array[i][4-j]=t;}
printf("移动后矩阵:\n");
for(i=0;i 32、素数函数 int Isprime(int m)。在主函数中输入一个整数n,然后调用Isprime,假如n是素数,则输出:n is prime! 不然输出:n isn’t prime!
#include 33、prime(int m)
{int j;
for(j=2;j<=m/2;j++)
if(m%j==0)break;
if(j>m/2)return 1;
else return 0;}
5.2编写两个函数,分别求两个整数最小公倍数和最大条约数并返回给主函数,两个整数由主函数输入。
#include 34、
void main()
{
int m,n,t,p,k;
printf("请输入两个正整数m,n:\n");
scanf("%d,%d",&m,&n);
if(m 35、t n,int h)
{ return m*n/h;}
int Greatest_common_divisor(int m,int n)
{int r;
while((r=m%n)!=0)
{m=n;n=r;}
return n;
}
5.3编一个函数fun(char s[]),其功效是把字符串中小写字母转换成大写字母,其它字符不变。
#include 36、
printf("请输入一个字符串:\n");
gets(s1);puts(s1);
k=strlen(s1);
fun(s1);
printf("\n%s\n",s1);
}
void fun(char s[])
{int i;
for(i=0;s[i]!='\0';i++)
if(islower(s[i]))s[i]=s[i]-32;
}
5.4编一函数 void convert(int a[][3]),求3×3矩阵转置矩阵。
比如,原矩阵是:
1 2 3
4 5 6
7 8 9
37、
则转置后矩阵是:
1 4 7
2 5 8
3 6 9
#include 38、or(j=0;j<3;j++)
printf("%4d ",array[i][j]);
printf("\n");
}
convert(array);
printf("转置后:\n");
for(i=0;i<3;i++)
{ for(j=0;j<3j++)
printf("%4d ",array[i][j]);
printf("\n");
}
printf("\n");
}
void convert(int a[][3])
{int i,j,t;
for(i=0;i<2;i++)
for( 39、j=i+1;j<3;j++)
{t=a[i][j];a[i][j]=a[j][i];a[j][i]=t;}
}
选做题
5.1
#include 40、int fun(int t)
{int j;
for(j=0;j 41、],av;
printf("请输入10个数组元素:\n");
for(i=0;i 42、 av;
}
5.3
#include "stdio.h"
#define M 4
#define N 4
void fun(int xx[][N],int pp[]);
void main()
{int i,j,xx[M][N],pp[M*N];
printf("请输入4×4个整数:\n");
for(i=0;i 43、rintf("%4d ",xx[i][j]);
printf("\n");}
fun(xx,pp);
printf("转换后一维数组:\n");
for(i=0;i 44、 "string.h"
#define N 80
void deletechar(char str[],char ch);
void main()
{
char str[N],ch;
printf("请输入一个字符串:\n");
gets(str);
printf("请输入要删除一个字符:\n");
ch=getchar();
deletechar(str,ch);
printf("\n%s\n",str);
}
void deletechar(char str[],char ch)
{int i,j=0;
for(i=0;str[i]!='\ 45、0';i++)
if(str[i]!=ch)str[j++]=str[i];
str[j]='\0';
}
试验六 指针
必做题
6.1
#include 46、pa>*pc){t=*pa;*pa=*pc;*pc=t;}
if(*pb>*pc){t=*pb;*pb=*pc;*pc=t;}
printf("\n从小到大排序:%d,%d,%d\n",a,b,c);
}
6.2
#include "stdio.h"
#include "string.h"
#define N 80
void fun(char *s);
void main(void)
{
char s1[N];
printf("\n请输入一个字符串:\n");
gets(s1);
fun(s1);
printf("\n逆置后:% 47、s\n",s1);
}
void fun(char *s)
{int i,k,t;
k=strlen(s);
for(i=0;i 48、);
printf("请输入第二个字符串:\n");
gets(s2);
scat(s1,s2);
printf("输出连接后字符串:\n");
puts(s1);
printf("\n");
}
void scat(char *s1,char *s2)
{int i,k,m;
k=strlen(s1);m=strlen(s2);
for(i=0;s2[i]!='\0';i++)
s1[k+i]=s2[i];
s1[k+m]='\0';
}
6.5
#define N 10
#include 49、 averf(int x[],int n,int *max,int *min);
main()
{
int i,x[N],max,min;
float av;
printf("请输入10个数组元素:\n");
for(i=0;i 50、 j;
float sum=x[0],av;
*max=*min=x[0];
for(j=1;j






