C语言实验报告参考答案(原)

更新时间:2024-03-25 00:34:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C语言实验报告参考答案

实验一 熟悉C语言程序开发环境及数据描述

四、程序清单

1.编写程序实现在屏幕上显示以下结果: The dress is long The shoes are big

The trousers are black 答案:

#include

main() { printf(\ printf(\ printf(\}

2.编写程序:

(1) a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。 (2)a=160,b=46,c=18,d=170, 编写求(a+b)/(b-c)*(c-d)的程序。 答案: (1)

#include

main() { int a,b,c,x,y; a=150; b=20; c=45; x=a/b; y=a/c; printf(\的商=%d\\n\ printf(\的商=%d\\n\ x=a%b; y=a%c; printf(\的余数=%d\\n\ printf(\的余数=%d\\n\ }

(2)

#include

main() { int a,b,c,d; float x; a=160; b=46; c=18; d=170; x=(a+b)/(b-c)*(c-d); printf(\ }

3. 设变量a的值为0,b的值为-10,编写程序:当a>b时,将b赋给c;当a<=b时,将0赋给c。(提示:用条件运算符) 答案:

#include

main() { int a,b,c; a=0; b=-10; c= (a>b) ? b:a; printf(\ }

五、调试和测试结果

1.编译、连接无错,运行后屏幕上显示以下结果: The dress is long The shoes are big

The trousers are black

2、(1) 编译、连接无错,运行后屏幕上显示以下结果:

a/b的商=7 a/c的商=3

a/b的余数=10 a/c的余数=15

(2) 编译、连接无错,运行后屏幕上显示以下结果: (a+b)/(b-c)*(c-d)=-1064.0000

3. 编译、连接无错,运行后屏幕上显示以下结果: c =-10

实验二 顺序结构程序设计

四、程序清单

1.键盘输入与屏幕输出练习 问题1 D 。

问题2 改printf(\这条语句 改成:printf(\

问题3 改scanf(\这条语句 改为:scanf(\,%c,%d\ 问题4 改printf(\这条语句 改成:printf(\’%c\\’ \\’ %c\\’ %d\\n\

问题5 把scanf(\和printf(\

改成scanf(\

printf(\

2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。 #include int main() {

int a,b,c;

scanf(\ c = a + b;

printf(\ printf(\

return 0; }

2(2)编写程序:从键盘输入两个实数a和x,按公式计算并输出y的值: y?a?sin(ax)?ln(a?x)?e #include #include

int main() {

5ax

float a,x,y;

scanf(\

y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x); printf(\

return 0; }

五、调试和测试结果

2(1) 输入: 12 14 输出:26 1a 2(2) 输入:1 0

输出:2.000000

实验三 选择结构程序设计

四、设计流程(算法描述)

(请写出上机内容2(3)题的算法描述) 主要是两两比较,然后得出最大的数 五、程序清单

(1)输入一个整数,若大于等于0,输出提示信息“is positive”,否则输出“is negative”。 #include #include

main() {

int a;

scanf(\ if(a>=0)

printf(\ else

printf(\

return 0; }

(2)输入两个整数a和b,若a>=b时,求其积c并显示;若a main() {

int a,b,c;

scanf(\ if(a>=b)

printf(\ else

printf(\

return 0; }

(3)输入a、b、c三个整数,输出最大数。 #include main() {

int a,b,c,x;

scanf(\

if(a>=b) x=a; else x=b; if (x

printf(\

return 0;

}

六、调试和测试结果

2(1) 输入: 2

输出:the number is positve 输入: 0

输出:the number is positve

输入: -2

输出:the number is negetive 2(2) 输入: 3 2 输出:c=6

输入: 2 3 输出:c=0

2(3) 输入:3 2 1 输出:the max number is:3 输入:2 3 1 输出:the max number is:3

输入:1 2 3 输出:the max number is:3

实验四 循环结构程序设计

四、设计流程(算法描述)

(请写出上机内容2的算法描述)

i=0; j=n-1;

while(i

k=a[i]; a[i]=a[j]; a[j]=k; i +=1; j -=1; }

}

2.已知某数列的前两项为2和3,其后每一项为其前两项之积。编程实现:从键盘输入一个整数x,判断并输出x最接近数列的第几项? #include #include

void Mad(int a[],int n) {

int i;

a[0]=2; a[1]=3;

for(i=2;i

a[i] = a[i-1] * a[i-2]; } }

int main(void) {

int a[100],x,k1,k2; int i;

Mad(a,100);//产生序列 printf(\ scanf(\ i=0;

for(;x>a[i];i++); k1 = abs(x-a[i-1]); k2 = abs(x-a[i]); if(k1>k2)

printf(\

else

printf(\

return 0; }

3.编程实现:输入10个学生5门课的成绩并完成如下功能 (1)求每个学生的平均分; (2)求每门课程的平均分。 #include #include #define num 10

typedef struct student {

char name[20]; float math; float englis; float computer; float Chinese; float history; }STUDENT;

int main(void) {

STUDENT stu[num]; int i;

float score,sum,average; char s[10];

float scoreMath,scoreEng,scoreCom,scoreChi,scoreHis;

for(i=0;i

printf(\ gets(stu[i].name);

printf(\ scanf(\ stu[i].math = score;

printf(\ scanf(\ stu[i].englis = score;

printf(\ scanf(\

stu[i].computer = score;

printf(\ scanf(\ stu[i].Chinese = score;

printf(\ scanf(\ stu[i].history = score;

gets(s);//功能是接受最后一个回车符,然后下一次gets(stu[i].name);才能起到作用 }

//求每个学生的平均分数 for(i=0;i

sum=0;

sum +=stu[i].math; sum +=stu[i].englis; sum +=stu[i].computer; sum +=stu[i].Chinese; sum +=stu[i].history; average = sum/5;

printf(\ }

//求每门课的平均成绩 scoreMath=0; scoreEng=0; scoreCom=0; scoreChi=0; scoreHis=0;

for(i=0;i

scoreMath += stu[i].math; scoreEng += stu[i].englis; scoreCom += stu[i].computer;

scoreChi += stu[i].Chinese; scoreHis += stu[i].history; }

printf(\ printf(\ printf(\ printf(\ printf(\

return 0; }

实验七 数组和函数

四、程序清单

(请写出上机内容2中函数的源代码)

void fun(int tt[M][N],int pp[N]) { int i,j,max;

for(j=0; j

for(i=1;imax)max=tt[i][j]; pp[j]=max; } }

五、调试和测试结果

(写出上机内容1中填空的内容)

(1) (1) sum=0 (2) t[i][i] (3) 1 (2) (1) 1 (2) i (3) a[p+i]

实验八 指针(1)

四、程序清单

(请写出上机内容2中的函数)

求出每个位上的数字,然后放在千位上的数字乘以1000,放在百位上的数字乘以100,放在10位上的数字乘以10,然后相加。

void fun(int a,int b,long *c) {

int a10,a1,b10,b1; a10=a/10;

}

a1=a; b10=b/10; b1=b;

*c = a10 * 1000 + b1 * 100 + a1 *10 + b10;

五、调试和测试结果(请写出上机内容1的输出结果)

1(1) 输出结果为:8,7,7,8 (2) 6

(3) (1)x=10 y=20

(2)x=20 y=10

(4) 【1】 int *p 【2】 &a[i] 【3】 p[i] 输入:1 2 3 4 5 6 输出: 1 2 3 4 5 6

实验九 指针(2)

设计流程(算法描述)

(请写出上机内容2中的算法描述)

五、程序清单

1.已知一个整型数组a[5],其各元素值为4,6,8,10,12。使用指针编程求数组元素之积。 #include

int main(void) {

int a[]={4,6,8,10,12},sum; int *p;

i=0 当 *(x+i)!=’\\0’ *(x+i)= =y T F return 1 i=i+1 return 0

} }

menu_select() {

char *menu[]={\\\

\\

\\\\

\\\\\\char s[3]; int c,i;

gotoxy(1,25);

printf(\getch(); clrscr(); gotoxy(1,1);

textcolor(YELLOW);

textbackground(BLACK); gotoxy(10,2); putch(0xc9); for(i=1;i<44;i++) putch(0xcd); putch(0xbb); for(i=3;i<20;i++) {

gotoxy(10,i);putch(0xba); gotoxy(54,i);putch(0xba); }

gotoxy(10,20);putch(0xc8); for(i=1;i<44;i++) putch(0xcd); putch(0xbc);

window(11,3,53,19);

clrscr();

for(i=0;i<16;i++) {

gotoxy(10,i+1);

cprintf(\}

textbackground(BLACK); window(1,1,80,25); gotoxy(10,21); do{

printf(\scanf(\c=atoi(s);

}while(c<0||c>14); return c; }

STUDENT *init() {

return NULL; }

STUDENT *create() {

int i; int s;

STUDENT *h=NULL,*info; for(;;) {

info=(STUDENT *)malloc(sizeof(STUDENT)); if(!info) {

printf(\return NULL; }

inputs(\if(info->no[0]=='0') break; /*when the first number is 0,break*/ inputs(\printf(\s=0; /*s is sum,begins with 0*/ for(i=0;i

printf(\if(i==1)

printf(\

scanf(\/* socre[0] stores maths scores,socore[1] stores program scores*/

if(info->score[i]>100||info->score[i]<0) printf(\

}while(info->score[i]>100||info->score[i]<0); s=s+info->score[i]; }

info->sum=s; info->order=0; info->next=h; h=info; }

return(h); }

inputs(char *prompt, char *s, int count) {

char p[255]; do{

printf(prompt); scanf(\

if(strlen(p)>count)printf(\}while(strlen(p)>count); strcpy(s,p); }

/*Print infor*/

void print(STUDENT *h) {

int i=0;

STUDENT *p; clrscr(); p=h;

printf(\printf(\printf(\while(p!=NULL) { i++;

printf(\|\\n\p=p->next; }

printf(\}

=

STUDENT *delete(STUDENT *h) {

STUDENT *p,*q; char s[11]; clrscr();

printf(\scanf(\q=p=h;

while(strcmp(p->no,s)&&p!=NULL) { q=p;

p=p->next; }

if(p==NULL)

printf(\else {

printf(\printf(\printf(\printf(\

|\\n\

printf(\getch(); if(p==h) h=p->next; else

q->next=p->next; free(p);

printf(\}

return(h); }

STUDENT *searchno(STUDENT *h) {

STUDENT *p,*q; char s[11]; clrscr();

printf(\scanf(\q=p=h;

=

while(strcmp(p->no,s)&&p!=NULL) { q=p;

p=p->next; }

if(p==NULL)

printf(\else {

printf(\

printf(\printf(\printf(\printf(\

|\\n\

printf(\getch(); }

return(h); }

void search(STUDENT *h) {

STUDENT *p; char s[15]; clrscr();

printf(\scanf(\p=h;

while(strcmp(p->name,s)&&p!=NULL) p=p->next; if(p==NULL)

printf(\else {

printf(\

printf(\printf(\printf(\printf(\

|\\n\

printf(\}

=

=

本文来源:https://www.bwwdw.com/article/cp68.html

Top