实验一 C简单程序设计1. 以下程序用于输入长方形的长、宽,计算长方形的面积、周长和对角线长。
#include <stdio.h>#include <math.h>main(){float x,y,s,l,r;printf("input x,y=");scanf("%f,%f",&x,&y);s=x*y;l=2*(x+y);r=sqrt(x*x+y*y);printf("s=%6.2f l=%6.2f r=%6.2f ",s,l,r);}请调试运行程序,要求:(1) 编辑、编译、链接并运行程序。
(2) 将输出语句改为 printf("s=%6.3f l=%6.3f r=%6.3f ",s,l,r); ,再运行程序,结果是否有变化?(3) 将程序第4行float x,y,s,l,r;与第6行 scanf("%f,%f",&x,&y); 交换位置,再编译,会如何?(4) 混淆程序中的大小写字母。
试分别将printf写为Printf,或将main写为Main或MAIN,或将程序中语句s=x*y;写为S=x*y; 再编译、链接、运行,结果如何?(5) 遗漏语句未尾分号。
如将printf("input x,y=");的分号去掉,再编译、链接、运行,情况如何?(6) 若遗漏scanf()语句中的“&”符号。
例如:若将 scanf("%f,%f",&x,&y); 改为 scanf("%f,%f",x,y); ,或改为 scanf("%f,%f",&x,y);,再重新编译、链接、运行,情况如何?(7) 将scanf()语句中“%f,%f”改为“%f%f”,输入数据的格式是否要变?(8) 参考教材P360页,C.3 高级调试方法,在程序中设置断点,并单步调试程序。
2. 编程:要求程序运行后在屏幕上输出如下信息:My Program Is Running!Good,and I'm very glad now!#include <stdio.h>#include <Windows.h>main(){printf(" My Program Is Running!\n Good,and I'm very glad now!\n");system("pause");}3.编程:输入圆的半径,计算圆的面积并输出。
#include <stdio.h>#include <Windows.h>#define PI 3.14main(){float s,r;printf("请输入r的值:\n");scanf("%f",&r);s=PI*r*r;printf("s=%g",s);system("pause");}4.编程:输出一个形如“▲”的三角图形(要求:第1行输出一个“*”,第2行输出三个“*”,......,最后一行输出9个“*”)。
(此题为选做)。
#include<stdio.h>#include<windows.h>main(){int i,j;for(i=1;i<=5;i++){for(j=1;j<=5-i;j++)printf(" ");for(j=1;j<2*i;j++)printf("*");printf("\n");}system("pause");}5.编程:输出一个形如“◣”的三角图形(要求:共5行,第1行一个“*”,第2行二个“*”,......,最后一行5个“*”)。
(此题为选做)。
#include<stdio.h>#include<windows.h>main(){int i,j;for(i=1;i<=5;i++){for(j=1;j<=i;j++)printf("*");printf("\n");}system("pause");}实验二 C基本数据类型及运算1.编程:根据需要定义相关变量,键盘输入10.0、20.6、5.0三个实数分别赋给相关三个变量,输出该3个变量的和s、乘积p和平均值a。
#include <stdio.h>#include <Windows.h>main(){float x,y,z,s,p,a;x=10.0;y=20.6;z=5.0;s=x+y+z;p=x*y*z;a=s/3;printf("s=%g\ny=%g\nz=%g\n",s,p,a);system("pause");}2.编程:输入球的半径,计算球体表面积和球体积。
#include <stdio.h>#include <Windows.h>main()#define L 3.14{float r,s,v;printf("请输入r的值:\n");scanf("%f",&r);s=4*L*r*r;v=4.0/3.0*L*r*r*r;printf("s=%f,v=%f",s,v);system("pause");}3.编程:定义5个变量,通过键盘将“10,-10, 8.123678309, a, 3.14”这五个值分别赋给相应变量,然后在屏幕上输出变量的值(每行输出一个)。
#include <stdio.h>#include <Windows.h>main(){int a,b;double c;char d;float e;a=10,b=-10,c=8.123678309,d='a',e=3.14;printf("%d\n%d\n%.9f\n%c\n%.2f\n",a,b,c,d,e);system("pause");}4.编程:键盘输入三个整数分别赋给整型变量a、b、c,用三目运算方法求出它们中的最小值,并输出。
#include <stdio.h>#include <Windows.h>main(){int a,b,c,min;printf("请输入a,b,c:\n");scanf("%d%d%d",&a,&b,&c);min=(a<b)?a:b;min=(min<c)?min:c;printf("min=%d\n",min);system("pause");}5.编程:调用标准库函数sin(x)计算135°的正弦值(注意:x为弧度)。
(此题为选做)#include <math.h>#include <stdio.h>#include <Windows.h>#define P 3.1415926main (){float a,b;b=135*P/180.0;a=sin(b);printf ("a=%g\n",a);system("pause");}6. 编程:键盘输入一个实数赋给变量x,计算 y=x3+x 的值,分别输出y值、y值的整数部分和小数部分(此题为选做)。
#include <stdio.h>#include <Windows.h>main(){float x,y,b;int a;printf("input x:");scanf("%f",&x);y=x*x*x+x;a=(int)y;b=y-a;printf("%g\n%d\n%g\n",y,a,b);system("pause");}实验三 C分支结构程序设计1.编程:从键盘输入一个字符,如是大写字母,则输出相应小写字母;如是小写字母,则原样输出;其它字符输出“Not letter!”。
#include <stdio.h>#include<Windows.h>void main(){char x;printf("请输入一个字符:\n");scanf("%c",&x);if(x>='a'&&x<='z')printf("%c\n",x);else if(x>='A'&&x<='Z')printf("%c\n",x+32);elseprintf("Not letter!\n");system("pause");}2.编程:判断输入的正整数是否既是3又是5的整数倍。
若是,输出Yes,否则输出No。
#include <stdio.h>#include <Windows.h>void main(){int a;printf("输入正整数a:\n");scanf("%d",&a);if (a>0){ if(a%3==0,a%5==0)printf("Yes\n");elseprintf("No\n");}elseprintf("Error\n");system("pause");}3.编程:从键盘输入三个整数,分别赋给变量a,b,c,请按从大到小的顺序依次输出。
#include <stdio.h>#include <Windows.h>int main(){int a,b,c,d,max,min;printf("请输入a,b,c:\n");scanf("%d%d%d",&a,&b,&c);max=a>b?a:b;min=a<b?a:b;max=max>c?max:c;min=min<c?min:c;d=a+b+c-max-min;printf("%d>%d>%d\n",max,d,min);system("pause");}4. 设函数f(x)如下,求f(x)的值。