论文
政研论文下载论文大学下载论文大学下载关于长拳的论文浙大论文封面下载
—C程序的运行环境和运行一个C程序的方法
实验一 C程序的运行环境和运行一个C程序的方法
1(编写一程序,在屏幕上输出如下内容:
#include
#include "e:\myc\zylib\zylib.h" int main()
{
printf(" X | X | X \n");
printf("---+---+---\n");
printf(" | | \n");
printf("---+---+---\n");
printf(" O | O | O \n");
return 0;
}
2(编写一程序,接受用户输入的两个整数,并计算它们的和、差、积、商,程序运行
结果应如下所示。注意,尖括号部分表示用户输入数据或要被替换的输出内容,尖括号本身
并不需要输入或输出。多使用几组整数尝试一下,你发现了什么, The program gets two integers,and computes their sum,difference,product and quotient.
The first number:<第一个整数在此输入>
The second number:<第二个整数在此输入>
Results as follows:
<第一个整数>+<第二个整数>=<和>
<第一个整数>-<第二个整数>=<差>
<第一个整数>*<第二个整数>=<积>
<第一个整数>/<第二个整数>=<商>
#include
#include "e:\myc\zylib\zylib.h" int main()
{
int x,y,sum,difference,product,quotient;
printf("The program gets two integers,and computes their sum,difference,product and
quotient.\n");
printf("The first number:");
x=GetIntegerFromKeyboard();
printf("The second number:");
y=GetIntegerFromKeyboard();
sum=x+y;
difference=x-y;
product=x*y;
quotient=x/y;
printf("Results as follows:\n");
printf("%d+%d=%d\n",x,y,sum);
printf("%d-%d=%d\n",x,y,difference);
printf("%d*%d=%d\n",x,y,product);
printf("%d/%d=%d\n",x,y,quotient);
return 0;
}
实验二 顺序结构程序
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
1(如何实现下述输出要求,
A(输出整数1234,场宽8位,数据左对齐。
B(输出整数1234,场宽10位,数据右对齐。
C(输出十六进制整数0xFFDE3C02,场宽8位,数据左对齐。 D(输出十六进制整数0xFFDE3C,场宽8位,数据右对齐,前补0。 E(输出浮点数10.36,场宽6位,数据右对齐。
F(输出浮点数123.4567890,场宽12位,精度6位,数据右对齐。 G(输出浮点数123.4567890,精度3位,数据左对齐。
H(输出字符串“abcdefghijklmnopqrstuvwxyz”,数据左对齐。 I(输出字符串“abcdefghijklmnopqrstuvwxyz”,场宽10位,数据右对齐。 J(输出字符串“abcdefghijklmnopqrstuvwxyz”,场宽10位,数据左对齐,多余字符
截断。
#include
int main()
{
printf("%-8d\n",1234);
printf("%10d\n",1234);
printf("%-8X\n",0xffde3c02);
printf("%08x\n",0xffde3c);
printf("%6f\n",10.36);
printf("%12.6f\n",123.4567890);
printf("%.3f\n",123.4567890);
printf("%s\n","abcdefghijklmnopqrstuvwxyz");
printf("%10s\n","abcdefghijklmnopqrstuvwxyz");
printf("%-10.10s\n","abcdefghijklmnopqrstuvwxyz");
return 0;
}
2(编写一程序,声明某个整数,并将其值设为INT,MAX,将其递增1,输出结果。INT
,MAX为表示最大整数的宏,其定义位于头文件“limits.h”中,编程时记住包含此头文件。 #include
#include
int main()
{
int a=INT_MAX;
a=a+1;
printf("%d\n",a);
return 0;
}
3(编制程序,接受用户输入的数值,输出以该值为半径的圆面积,以该值为半径的球
体表面积与体积,pi取值3.1415926536,结果保留10位有效数字。 #include
int main()
{
double r,s,area,v;
double const pi=3.1415926536;
printf("请输入半径:");
scanf("%lf",&r);
s=pi*r*r;
area=4*pi*r*r;
v=4/3.0*pi*r*r*r;
printf("半径=%.9e\n圆面积=%.9e\n球表面积=%.9e\n球体积
=%.9e\n",r,s,area,v);
return 0;
}
4(编制程序,输出下述数据。说明:(1)表中数据来自总参谋部测绘局编制的,世界
地图集,,数据可能已不准确;(2)面积单位为万平方公里,人口单位为万人,GDP单位为
十亿美元。(3)表中所有数据都必须以变量的形式保存;(4)如果不知道每字段宽度到底为
多少,请仔细数数作为分隔标记的短横数目。
--------------------------------------------------------------------
COUNTRY AREA(10K km2) POP(10K) GDP(Billion$)
--------------------------------------------------------------------
China 960.00 129500.00 1080.00
Iceland 10.30 27.57 8.20
India 297.47 97000.00 264.80
Madagascar 62.70 1635.00 3.60
Maldive 0.0298 27.80 0.23
--------------------------------------------------------------------
#include
#include "e:\myc\zylib\zylib.h"
int main()
{
STRING country1, country2, country3, country4, country5;
double area1, area2, area3, area4, area5; double pop1, pop2, pop3, pop4, pop5;
double gdp1, gdp2, gdp3, gdp4, gdp5;
country1 = "China"; area1 = 960; pop1 = 129500; gdp1=1080;
country2 = "Iceland"; area2 = 10.3; pop2 = 27.57; gdp2=8.2;
country3 = "India"; area3 = 297.47; pop3 = 97000; gdp3=264.8;
country4 = "Madagascar"; area4 = 62.70; pop4 = 1635; gdp4=3.6;
country5 = "Maldive"; area5 = 0.0298; pop5 = 27.8; gdp5=0.23;
printf("----------------------------------------------------------\n");
printf("COUNTRY AREA(10K km2) POP(10K) GDP(BILLION$)\n");
printf("----------------------------------------------------------\n");
printf("%-13.13s %11.2lf %10.2lf %9.2lf\n", country1, area1, pop1,gdp1);
printf("%-13.13s %11.2lf %10.2lf %9.2lf\n", country2, area2, pop2,gdp2);
printf("%-13.13s %11.2lf %10.2lf %9.2lf\n", country3, area3, pop3,gdp3);
printf("%-13.13s %11.2lf %10.2lf %9.2lf\n", country4, area4, pop4,gdp4);
printf("%-13.13s %11.4lf %10.2lf %9.2lf\n", country5, area5, pop5,gdp5);
printf("-----------------------------------------------------------\n");
return 0;
}
实验三 程序流程控制
1(给出一个百分制成绩,要求输出成绩等级‘A’,‘B’,‘C’,‘D’,‘E’。
90分以上为‘A’,80,89分为‘B’,70,79分为‘C’,60,69分为‘D’,60分以
下为‘E’。分别用if和switch语句实现。
用if实现:
#include
#include
#include "e:\myc\zylib\zylib.h"
int main()
{
int score;
printf("请输入百分制成绩:");
score=GetIntegerFromKeyboard();
if(score<0||score>100)
{printf("非法的成绩\n");exit(0);}
printf("成绩等级为");
if(score>=90)printf("A\n");
else if(score>=80)printf("B\n");
else if(score>=70)printf("C\n");
else if(score>=60)printf("D\n");
else printf("E\n");
return 0;
}
用switch实现:
#include
#include
#include "e:\myc\zylib\zylib.h" int main()
{
int score;
printf("请输入百分制成绩:");
score=GetIntegerFromKeyboard();
if(score<0||score>100)
{printf("非法的成绩\n");exit(0);}
printf("成绩等级为");
switch(score/10)
{
case 10:case 9:printf("A\n");break;
case 8:printf("B\n");break;
case 7:printf("C\n");break;
case 6:printf("D\n");break;
default: printf("E\n");
}
return 0;
}
2(打印所有100至999之间的水仙花数。所谓水仙花数是指满足其各位数字立方和为
该数字本身的整数。
#include
int main()
{
int i,s,a,b,c;
for(i=100;i<1000;i++)
{
a=i%10;
b=i/10%10;
c=i/100;
s=a*a*a+b*b*b+c*c*c;
if(i==s) printf("%5d\n",i);
}
return 0;
}
3(使用循环结构打印下述图形,打印行数n由用户输入。图中每行事实上包括两部分,
中间间隔空格字符数m也由用户输入。
* *********
*** *******
***** *****
******* ***
********* * #include
int main()
{
int i,j,k,m,n;
printf("请输入行数和中间间隔的空格数:");
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i+1;j++)printf(" ");
for(k=1;k<=2*i-1;k++)printf("*");
for(j=1;j<=m;j++)printf(" ");
for(k=1;k<=2*n-2*i+1;k++)printf("*");
printf("\n");
}
return 0;
}
4(编制程序,按照下述格式打印九九乘法表。
Nine – by – nine Multiplication Table ----------------------------------------------------------------------
1 2 3 4 5 6 7 8 9
----------------------------------------------------------------------
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64 9 9 18 27 36 45 54 63 72 81 ----------------------------------------------------------------------
#include
int main()
{
int i,j;
printf("Nine - by - nine Multiplication Table\n");
printf("---------------------------------------------\n ");
for(i=1;i<=9;i++)
printf("%4d",i);
printf("\n");
for(i=1;i<=9;i++)
{
printf("%d",i);
for(j=1;j<=i;j++)
printf("%4d",i*j);
printf("\n");
}
printf("---------------------------------------------\n");
return 0;
}
5(按照下述格式打印2006年12月日历:
Calander 2006-12
------------------------------------------------------
Su Mo Tu We Th Fr Sa ------------------------------------------------------
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 26 27 28 29 30 31
------------------------------------------------------
#include
#include"e:\myc\zylib\zylib.h"
typedef enum {SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,
SATURDAY} WEEKDAY;
int main()
{
int date;
const WEEKDAY original_date = FRIDAY;
WEEKDAY weekday;
printf("Calendar 2006 - 12\n");
printf(" ---------------------------\n");
printf(" Su Mo Tu We Th Fr Sa\n");
printf(" ---------------------------\n");
for(date=1;date<=31;++date)
{
weekday=(WEEKDAY)((date+(int)original_date-1)%7);
if(date==1)printf("%24d",date);
else if(weekday == SATURDAY) printf("%4d\n",date);
else printf("%4d",date);}
printf("\n");
printf("-----------------------------\n");
return 0;
}
1111,,,...,1!2!n!6(接受用户输入的正整数n,计算f(n)=的值。
#include
int main()
{
int i,n;
double t=1,s=1;
printf("请输入一个正整数:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t=t*i;
s=s+1/t;
}
printf("总和为:%lf\n",s);
return 0;
}
实验四 函数
1(编制函数GetReal与GetString,要求能够:(1)输出主调函数调用时传递的提示用
户输入数据的字符串信息,(2)将用户输入结果返回给主调函数。
#include
#include "e:\myc\zylib\zylib.h"
STRING GetString(STRING prompt);
double GetReal(STRING prompt);
int main()
{
double bookprice;
STRING bookname;
bookname=GetString("请输入字符串:");
bookprice=GetReal("请输入实数:");
printf("字符串为:%s\n",bookname);
printf("实数为:%.2f\n",bookprice); }
STRING GetString(STRING prompt)
{
STRING name;
printf("%s",prompt);
name=GetStringFromKeyboard();
return name;
}
double GetReal(STRING prompt) {
double price;
printf("%s",prompt);
price=GetRealFromKeyboard();
return price;
}
2(编写一函数IsPrime,判断某个大于2的正整数是否为素数。 #include
#include "e:\myc\zylib\zylib.h"
BOOL IsPrime(int n); int main()
{
int n;
printf("请输入一个整数:");
scanf("%d",&n);
if(n>2)
if(IsPrime(n))printf("%d是素数\n",n);
else printf("%d不是素数\n",n);
else printf("数据非法\n");
return 0;
}
BOOL IsPrime(int n)
{
int i;
for(i=2;i
BOOL gcd(int x,int y); int main()
{
int m,n,max;
printf("请输入两个正整数:");
scanf("%d %d",&m,&n);
max=gcd(m,n);
printf("最大公约数为:%d\n",max);
return 0;
}
int gcd(int x,int y)
{
int r;
r=x%y;
while(r!=0)
{
x=y;
y=r;
r=x%y;
}
return y;
}
4(已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)
某年某月的日历,2007年以前的拒绝打印。
Calendar 2007-01
------------------------------------------------------
Su Mo Tu We Th Fr Sa ------------------------------------------------------
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 26 27 28 29 30 31
------------------------------------------------------
#include
#include "e:\myc\zylib\zylib.h" typedef enum{sun,mon,tue,thi,wen,fri,sat}WEEKDAY;//定义枚举类型 int GetInteger(STRING prompt);//输入一下整数
int Count(int year,int month);//计算某年某月之前到2007年1月1日的天数 BOOL IsLeapYear(int n);//判断某年是否是闰年
int month_day(int year,int month);//计算某个月的天数
void print(int year,int month,int total);//打印某年某月的日历
void print1(WEEKDAY weekday);//打印某月的第1天
int main()
{
int year,month,total;
year=GetInteger("please input year:");
if(year<2007)
PrintErrorMessage(FALSE,"年份小于2007,错误\n");
month=GetInteger("please input month:");
total=Count(year,month);
print(year,month,total);
}
int GetInteger(STRING prompt) {
int t;
printf("%s",prompt);
t=GetIntegerFromKeyboard();
return t;
}
int Count(int year,int month) {
int s,i;
s=0;
for(i=2007;i
#include
#include "e:\myc\zylib\zylib.h"
BOOL IsPrime(int n);
int main()
{
int i,n,c;
printf("请输入一个整数:");
scanf("%d",&n);
for(i=2;i<=n;i++)
if(IsPrime(i))
{
printf("%5d",i);
c++;
if(c%5==0)printf("\n");
}
return 0;
}
BOOL IsPrime(int n)
{
int i,t;
if(n<=1)
PrintErrorMessage(FALSE,"Failed in testing\n");
if(n==2)return TRUE;
if(n%2==0) return FALSE;
i=3;
t=(int)sqrt(n)+1;
while(i<=t)
{
if(n%i==0) return FALSE;
i+=2;
}
return TRUE;
}
2(假设有一对兔子,一个月后成长为大兔子,从第二个月开始,每对大兔子生一对小
兔子。不考虑兔子的死亡,设计算法求第n个月的兔子总数。 #include
int main()
{
int i,n,f1,f2,f3;
printf("请输入月份:");
scanf("%d",&n);
f1=f2=1;
for(i=3;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
}
printf("第%d个月的兔子数为%d\n",n,f2 );
return 0;
}
1k,0,nkkkk,1,C,CC,Cnnn,1n,10,k,n ,使用递归方法求解。 3(已知
#include
int func(int n,int k); int main()
{
int n,k,c;
printf("请输入n和k:");
scanf("%d %d",&n,&k);
c=func(n,k);
printf("c (%d,%d)=%d\n",n,k,c);
return 0;
}
int func(int n,int k) {
int s;
if(k= =0||k= =n) s=1;
else s=func(n-1,k)+func(n-1,k-1);
return s;
}
4(存在自然数,其所有小于自身的因子之和等于该数,这样的数称为完数。设计算法,
判断某个给定的自然数n是否为完数,要求具有容错处理功能。 #include
#include "e:\myc\zylib\zylib.h" BOOL isws(int n);
int main( )
{
int i;
printf("请输入一个自然数:");
scanf("%d",&i);
if(isws(i)) printf("%d是完数\n",i);
else printf("%d 不是完数\n",i);
return 0;
}
BOOL isws(int n)
{
int i,s=0;
if(n<1)
PrintErrorMessage(FALSE,"本题目要求是自然数! %d不是自然数\n",n);
for(i=1;i
#include "e:\myc\zylib\zyrandom.h"
void play();
int main()
{
STRING s;
printf("欢迎大家玩掷骰子游戏\n");
Randomize();
while(TRUE)
{
play();
printf("再玩一次吗?(y/n)");
s=GetStringFromKeyboard();
if(CompareString(s,"y")!=0)break;
}
printf("再见!\n");
return 0;
}
void play()
{
STRING s;
int a,b;
a=GenerateRandomNumber(1,6);
printf("计算机生成的随机数是:%d\n",a);
printf("输入g命令开始游戏:");
s=GetStringFromKeyboard();
while(CompareString(s,"g")!=0)
{
printf("输入有误,你必须输入g命令才能开始:");
s=GetStringFromKeyboard();
}
b=GenerateRandomNumber(1,6);
printf("用户掷的骰子数是:%d\n",b);
if(b
#include "e:\myc\zylib\zyrandom.h" void play();
int GetRandom();
int main()
{
int a,b,c;
a=GetRandom();
b=(a-1)/13;
c=(a-1)%13;
printf("产生的随机数是:%d\n映射的扑克牌是:",a);
switch(b)
{
case 0:printf("梅花");break;
case 1:printf("方块");break;
case 2:printf("红桃");break;
case 3:printf("黑桃");break;
}
if(c>=0&&c<8)printf("%c\n",'0'+c+2);
else switch(c)
{
case 8:printf("%s\n","10");break;
case 9:printf("%s\n","J");break;
case 10:printf("%s\n","Q");break;
case 11:printf("%s\n","K");break;
case 12:printf("%s\n","A");break;
}
return 0;
}
int GetRandom( )
{
int a;
Randomize( );
a=GenerateRandomNumber(1,52);
return a;
}
实验七 复合数据类型
1(给定字符串s,其内容为英语长句,其中包含
英语单词
七年级上册英语单词表高考英语单词3500记忆高中3500个英语单词表七年级下册英语单词表小学六年级英语单词表
、标点符号、空格等内容,
每个英语单词使用标点符号、一个或多个空格分隔。将英语长句分隔成英语单词序列输出,
并输出其单词数目。
#include
#include "e:\myc\zylib\zylib.h" int ispace(char c);
int fun(STRING s,STRING str[]); void print(STRING str[],int n); int main()
{
STRING s,str[100];
int n;
printf("请输入字符串:");
s=GetStringFromKeyboard();
n=fun(s,str);
print(str,n);
return 0;
}
int fun(STRING s,STRING str[]) {
int i,n,word=0,count=0,j=0,start=0;
STRING sub;
char c;
s=DuplicateString(ConcatenateString(s," "));
n=GetStringLength(s);
for(i=0;i
#include "e:\myc\zylib\zylib.h" typedef struct
{
int no;
double score[3];
double aver;
}STU;
void input(STU s[],int n); void average(STU s[],int n); void output(STU s[],int n); int main()
{
STU s[3];
input(s,3);
average(s,3);
output(s,3);
return 0;
}
void input(STU s[],int n) {
int i,j;
for(i=0;i
#include "e:\myc\zylib\zyrandom.h"
void print(int b[],int n); void getrand(int b[],int n); void sort(int b[],int n); int DeleteRepeatedIntegers(int b[],int n);
int main()
{
int a[30],n;
getrand(a,30);
print(a,30);
sort(a,30);
n=DeleteRepeatedIntegers(a,30);
print(a,n);
return 0;
}
void print(int b[],int n) {
int i;
for(i=0;ib[j])
{
t=b[i];b[i]=b[j];b[j]=t;
}
}
int DeleteRepeatedIntegers(int a[],int n) {
int i,j=1;
for(i=1;i
int half(int b[],int x,int n); int main()
{
int a[15]={11,12,13,14,15,16,17,18,19,20,21,22,23,24,25},f,x;
printf("请输入要找的数:");
scanf("%d",&x);
f=half(a,x,15);
if(f==-1)printf("没找到!\n");
else printf("该数是第%d个数\n",f+1);
return 0;
}
int half(int b[],int x,int n) {
int i,j,k;
i=0;j=n-1;
while(i<=j)
{k=(i+j)/2;
if(x==b[k])return k;
else if(x>b[k])i=k+1;
else j=k-1;}
return -1;
}
实验八 指针
1(编写函数,求包含n个元素的整数数组中元素的平均值。要求在函数内部使用指针
操纵数组元素。
2(编写函数,完成n X n矩阵的转置操作,矩阵各元素值随机生成。 3(独立实现
标准
excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载
字符串库的strcmp函数。
4(接受用户输入的整数n,随机生成n个0~100间的整数,使用动态数组存储所有元
素,分别统计0~59,60~84,85~100之间的元素个数。
实验九 文件与数据存储
1(编写程序,从文本文件中读取全部内容,并复制到另一文件中。要求将文本文件中
的所有英文字母都转换成大写后输出。
2(编写程序,读取一个文本文件,统计其中的英文字母与数字字符的个数,全部字符
个数与单词个数。
实验十 学生成绩管理
编写一个C程序,对一个班的学生成绩进行管理,其中:每个学生的数据包括学号、
姓名、三门课的成绩(英语、数学、计算机)和平均成绩。主菜单如下:1、输入学生数据 2、
输出学生数据 3、数据排序(按平均分)4、数据查找5、数据删除 6、追加学生数据 7、
统计数据(统计各分数段平均分的人数。分五档:60分以下,60,69,70,79,80,89,90,100)
8、读取数据文件9、写入数据文件10、退出系统。