最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

C語言程序設(shè)計(jì)實(shí)驗(yàn)答案

2022-03-21 09:39 作者:答案鬼  | 我要投稿

實(shí)驗(yàn)一 熟悉Turbo C的編程環(huán)境

一:目的要求

1.熟悉Turbo C的編程環(huán)境;

2.知道程序編輯、鏈接、執(zhí)行的基本步驟;

3.學(xué)習(xí)如何查錯(cuò)并修改程序;

4.上機(jī)前預(yù)習(xí)編寫好程序。

二:實(shí)驗(yàn)內(nèi)容與步驟

調(diào)試以下兩個(gè)程序

/* The ?first ?C ?Program*/

#include <stdio.h>

void main()

{

? ? ? ?printf(“Hello,World!\n”);

}

#include <stdio.h>

void main()

{

int a, b, sum;

? ? ? ?a = 123;

? ? ? ?b = 456;

? ? ? ?sum = a + b;

? ? ? ?printf(“sum is %d\n”, sum);

}

調(diào)試通過后,自己修改程序,使程序出錯(cuò),并讀懂錯(cuò)誤提示,進(jìn)行修改。

實(shí)驗(yàn)二 數(shù)據(jù)輸入輸出格式的程序設(shè)計(jì)

一.目的要求

1.進(jìn)一步熟悉調(diào)試程序的方法;

? ?2.熟練掌握格式輸入與格式輸出函數(shù)的使用。

? ?3.掌握字符與ASCⅡ值之間的轉(zhuǎn)換方法。


編輯

? ?4.上機(jī)前預(yù)習(xí)編寫好程序;

編輯

編輯

四、利用接口庫函數(shù)dos.h的調(diào)用

1.類型定義結(jié)構(gòu)類型struct ?date.d;

2.getdate(&d);

3.printf(“%d/%d/%d”,d.da_year, d.da_mon, d.da_day)。

五、程序提示:

#include<conio.h>

#include<dos.h>

main()

{

char c1;

struct date d;

clrscr();

getdate(&d);

···.

? ?···

? ? ···

getch(); ? ?暫停等待輸入任意字符(或按鍵)

}

解答:

#include<stdio.h>

#include<conio.h>

#include<dos.h>

main()

{

char c1;

struct date d;

clrscr();

getdate(&d);

printf("\t\t%d/%d/%d",d.da_year, d.da_mon, d.da_day);

printf("\n* * * * * * * * * * * * *\n");

printf("* ? ? ? Menu ? ? ? ? ? ?*\n");

printf("* ? ? 1.Input ? ? ? ? ? *\n");

printf("* ? ? 2.Output ? ? ? ? ?*\n");

printf("* ? ? 3.End ? ? ? ? ? ? *\n");

printf("* * * * * * * * * * * * *\n");

printf(" ? ? Enter Number=> \n");

printf(" ? ? Ch=----------> ");

gotoxy(20,8);

c1=getch();

gotoxy(20,8);

putchar(c1);

gotoxy(9,9);

putchar(c1);

gotoxy(20,9);

printf("%d",c1);

}

實(shí)驗(yàn)三 選擇結(jié)構(gòu)程序設(shè)計(jì)

一:目的要求

1.熟悉用關(guān)系運(yùn)行與邏輯運(yùn)行符的應(yīng)用;

2.掌握if語句與switch語句的使用;

3.上機(jī)前按實(shí)驗(yàn)要求預(yù)習(xí)編寫出完整的程序,才允許上機(jī)。

二:實(shí)驗(yàn)內(nèi)容與步驟

1. 在書店買書 ,以100本為限。如果買 1 本不打折扣 ;買2本打折10%; 買3本折扣為 15 %,買3本以上折扣為 20 %。設(shè)書本數(shù)為 x,單價(jià)為 20.00元。

請(qǐng)使用else if多分支結(jié)構(gòu)或if嵌套結(jié)構(gòu)設(shè)計(jì)實(shí)現(xiàn)該算法的C程序。

2. 從鍵盤上輸入一個(gè)百分制成績(jī)score,按下列原則輸出其等級(jí):score≥90,等級(jí)為A;80≤score<90,等級(jí)為B;70≤score<80,等級(jí)為C;60≤score<70,等級(jí)為D;score<60,等級(jí)為E。

請(qǐng)使用switch 語句實(shí)現(xiàn)上述功能。

(提示:將成績(jī)整除10,把score轉(zhuǎn)變?yōu)樘幱?~10之間的整數(shù),從而轉(zhuǎn)化成switch語句中的case標(biāo)號(hào))

三:選做實(shí)驗(yàn)

實(shí)驗(yàn)指導(dǎo)書56頁第二章選擇結(jié)構(gòu)的任意實(shí)驗(yàn)

解答:

一、

#include<conio.h>

#include<stdio.h>

main()

{

int n;

float x,y;

clrscr();

printf("Please Enter The Number:");

gotoxy(25,1);

scanf("%d",&n);

x=20.00;

y=0;

if (n<0)

?printf("Sorry,you put the wrong number!");

else if (n==0)

?printf("Please buy at least 1 book!");

else if (n==1)

{ ?y=x;

printf("------You should pay:%.2f",y*n);

}

else if (n==2)

{ ?y=x*0.9;

printf("------You should pay:%.2f",y*n);

}

else if (n==3)

{ ?y=x*0.85;

printf("------You should pay:%.2f",y*n);

}

else if (n<=100)

{ ?y=x*0.8;

printf("------You should pay:%.2f",y*n);

}

else if (n>100)

?printf("Sorry,you can buy less then 100 books.");

}

二、

#include<stdio.h>

#include<conio.h>

main()

{

int t;

float n;

clrscr();

printf("Please Enter The Score:");

gotoxy(24,1);

scanf("%f",&n);

t=n/10;

if (n<0)

printf("Please Write The Right Score!");

else if (n>100)

printf("Is He A God?");

else

?switch(t)

?{

?case 10:printf("A+");break;

?case 9:printf("A");break;

?case 8:printf("B");break;

?case 7:printf("C");break;

?case 6:printf("D");break;

?default:printf("E");break;

?}

}

實(shí)驗(yàn)四 循環(huán)控制

一:目的要求

1.熟悉用while語句,do-while 語句和for語句實(shí)現(xiàn)循環(huán)的方法;

2.掌握在程序設(shè)計(jì)中用循環(huán)方法實(shí)現(xiàn)各種算法;

3.掌握計(jì)算程序運(yùn)行所占機(jī)時(shí)的計(jì)算方法;

4.上機(jī)前按實(shí)驗(yàn)要求預(yù)習(xí)編寫出完整的程序,才允許上機(jī)。

二:實(shí)驗(yàn)內(nèi)容與步驟

目標(biāo):100匹馬馱100擔(dān)貨;

假設(shè):大馬一匹馱3擔(dān),中馬一匹馱2擔(dān),小馬兩匹馱一擔(dān);

組合方法1:大馬、中馬、小馬每種不能少于一匹;

組合方法2:對(duì)馬匹種類無限制,可以缺少一種或缺二種

問題: 1. 采用組合方法1,用while求解,輸出所有組合法,輸出組合的總數(shù)?

2.采用2種組合,用do-while求解,輸出所有組合法,輸出組合的總數(shù)?

3.采用2種組合,用三重或二重for循環(huán)求解,輸出所有組合法,輸出組合的總數(shù)?

4.除打印結(jié)果和多少種組合法外,還要分別打印三種算法所費(fèi)機(jī)時(shí)。

提示:計(jì)算一種算法所占機(jī)時(shí)的程序提示:

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t start,end; ? ? ? ? ? ? ?/* time_t ?start,end;*/

int i,big,middle,small,ncount;

clrscr();

start=clock(); ? ? ? ? ? ? ? ?/* start = time();*/

big=1; middle=1; small=2;

ncount=0;

printf("This a while program\n");

while (big<=33)

{

? ? ? ? ? ?.

}

end=clock(); ? ? ? ? ? ? ? /* end = time();*/

printf("The num ?of method1 is: %d\n",ncount);

printf("and the time is: %5.1f time\n",difftime(end,start));

/*printf f(“”The difference is :%5.1f second\n”, difftime(end,start)/18.2);*/

……}

解答:

一、

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t ?start,end; ? ? ? ? ? ? ?/* time_t ?start,end;*/

int ?i,big,middle,small,ncount,n;

clrscr();

start=clock(); ? ? ? ? ? ? ? ?/* start = time();*/

big=1; middle=1; small=2,i=0,n=0;

ncount=0;

printf("This a while program\n");

while (big<=33)

{

for(small=2;small<=100;small=small+2)

for(middle=1;middle<=100;middle++)

{

n=big*3+middle*2+small/2;

i=big+middle+small;

if (n==100 && i==100)

{

printf("Big=%d,Middle=%d,Small=%d,n=%d,i=%d\n",big,middle,small,n,i);

ncount++;

}

}

big++;

/*

printf("n=%d ",n);

printf("big=%d small=%d middle=%d ",big,small,middle);

printf("\n");*/

}

end=clock();

printf("The ?num ?of ?method1 ?is: %d\n",ncount);

printf("and the time ?is: %5.1f time\n",difftime(end,start));

}

二、

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t ?start,end; ? ? ? ? ? ? ?/* time_t ?start,end;*/

int ?i,big,middle,small,ncount,n;

clrscr();

start=clock(); ? ? ? ? ? ? ? ?/* start = time();*/

big=0; middle=0; small=0,i=0,n=0;

ncount=0;

printf("This a while program\n");

do

{

for(small=0;small<=100;small=small+2)

for(middle=0;middle<=100;middle++)

{

n=big*3+middle*2+small/2;

i=big+middle+small;

if (n==100 && i==100)

{

printf("Big=%d,Middle=%d,Small=%d,n=%d,i=%d\n",big,middle,small,n,i);

ncount++;

}

}

big++;

/*

printf("n=%d ",n);

printf("big=%d small=%d middle=%d ",big,small,middle);

printf("\n");*/

}

while(big<=34);

end=clock();

printf("The ?num ?of ?method1 ?is: %d\n",ncount);

printf("and the time ?is: %5.1f time\n",difftime(end,start));

}

三、

#include <time.h>

#include <conio.h>

#include <dos.h>

main()

{

clock_t ?start,end; ? ? ? ? ? ? ?/* time_t ?start,end;*/

int ?i,big,middle,small,ncount,n;

clrscr();

start=clock(); ? ? ? ? ? ? ? ?/* start = time();*/

big=0; middle=0; small=0,i=0,n=0;

ncount=0;

printf("This a while program\n");

for(big=0;big<=34;big++)

{

for(small=0;small<=100;small=small+2)

for(middle=0;middle<=100;middle++)

{

n=big*3+middle*2+small/2;

i=big+middle+small;

if (n==100 && i==100)

{

printf("Big=%d,Middle=%d,Small=%d,n=%d,i=%d\n",big,middle,small,n,i);

ncount++;

}

}

/*

printf("n=%d ",n);

printf("big=%d small=%d middle=%d ",big,small,middle);

printf("\n");*/

}

end=clock();

printf("The ?num ?of ?method1 ?is: %d\n",ncount);

printf("and the time ?is: %5.1f time\n",difftime(end,start));

}

實(shí)驗(yàn)五 函數(shù)

一、 目的要求

1. 掌握函數(shù)的定義和調(diào)用方法;

2. 掌握函數(shù)實(shí)參與行參的對(duì)應(yīng)關(guān)系的,以及“值傳遞”的方式;

3. 掌握求最大公約數(shù)和最小公倍數(shù)的方法;

4. 按實(shí)驗(yàn)內(nèi)容要求完成全程程序設(shè)計(jì)后才允許上機(jī)。

二、 實(shí)驗(yàn)內(nèi)容與步驟

1. 設(shè)計(jì)一個(gè)函數(shù)f,求二個(gè)數(shù)的最大公約數(shù)int f(int x,int y);

2. 設(shè)計(jì)一個(gè)函數(shù)g,求二個(gè)數(shù)的最小公倍數(shù)int g(int x,int y);

3. 從鍵盤輸入三個(gè)浮點(diǎn)數(shù),求三個(gè)數(shù)中最大數(shù)和最小數(shù)的差值。

三、 函數(shù)的定義要求

在main( )中實(shí)現(xiàn)下列操作

1. a=inNumber( );

2. b= inNumber( );

3. c=f(a,b);

4. d=g(a,b);

5. 輸出a,b,c,d。

解答:

一、

#include<stdio.h>

#include<math.h>

void main()

{

int a,b,c,d;

clrscr();

a=inNumber();

b=inNumber();

c=f(a,b);

d=g(a,b);

printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);

}

int f(int x,int y)

{

int i,t,a;

float p1,p2;

if (x>=y)

t=y;

else

t=x;

?for (i=1;i<=t;i++)

?{

? p1=x%i;

? p2=y%i;

? if (p1==0 && p2==0)

? {

? ?a=i;

? }

? }

return a;

}

int g(int x,int y)

{

int i,t,p1,p2,a;

if (x>=y)

t=x;

else

t=y;

?for (i=t;i<=(x*y);i++)

?{

? p1=i%x;

? p2=i%y;

? if (p1==0 && p2==0)

? {

? a=i;break;

? }

?}

return a;

}

int inNumber(int x)

{

int a;

a=0;

while (a==0)

{

scanf("%d",&x);

if (x>0)

a=1;

else

{

printf("Wrong!\n");

a=0;

}

};

return x;

}

二、

#include<stdio.h>

void main()

{

float a[3],x,y;

int i,j;

clrscr();

for (i=0;i<3;i++)

scanf("%f",&a[i]);

for (i=0;i<3;i++)

?for (j=0;j<2;j++)

? if (a[j]>a[j+1])

? {

? ?x=a[j];

? ?a[j]=a[j+1];

? ?a[j+1]=x;

? }

y=a[2]-a[0];

printf("\ny=Max{a[i]}-Min{a[i]}:\n\t%.3f\n\t(%f)",y,y);

}

實(shí)驗(yàn)六 數(shù)組

一、 目的要求

1. 掌握數(shù)組的定義、賦值和輸入輸出的方法;

2. 掌握清屏函數(shù)clrscr()的調(diào)用方法;

3. 掌握產(chǎn)生隨機(jī)數(shù)函數(shù)randomize()的初始化及調(diào)用方法;

4. 上機(jī)前按實(shí)驗(yàn)要求預(yù)習(xí),完成全部程序設(shè)計(jì)后才允許上機(jī)。

二、 實(shí)驗(yàn)內(nèi)容與步驟

已知二維數(shù)組a[5][5],完成下列要求

(1) 輸入數(shù)據(jù)

a[i][j]=random(100); ? /*產(chǎn)生100以內(nèi)隨機(jī)數(shù)*/

(2) 顯示數(shù)組各元素,要求整齊排列;

(3) 將第1與第5行對(duì)調(diào)后,再顯示之;

(4) 求出每行元素的最大值,并指出其行號(hào)和列號(hào)。

三、 輸入隨機(jī)數(shù)的要求

#include <stdlib.h>

#define RMAX 5

#define cMAX 5

#define nMAX 100

main()

{ . ? ? ? ? ?/*變量初始化說明*/

.

clrscr(); ? ? ? ?/*調(diào)清屏函數(shù)清屏*/

randomize(); ? ?/*在初始化后調(diào)用產(chǎn)生隨機(jī)數(shù)函數(shù)*/

.

.

a[i][j]=random(nMAX);

.

.

.

}

解答:

#include <stdlib.h>

#define RMAX 5

#define cMAX 5

#define nMAX 100

main()

{

int a[5][5],i,j,temp[1][5],t,max,x,y;/*定義變量為整型(我的程序里面有些定義的變量沒用,懶得刪除了)*/

clrscr();/*清屏*/

randomize();/*在初始化后調(diào)用產(chǎn)生隨機(jī)數(shù)函數(shù)*/

for (i=0;i<5;i++)

for(j=0;j<5;j++)

a[i][j]=random(nMAX);/*給數(shù)組a[i][j]賦隨機(jī)值*/

for (i=0;i<5;i++)

{ for(j=0;j<5;j++)

?printf("%d\t",a[i][j]);/*輸出數(shù)組a[i][j]*/

?printf("\n");/*在輸出完一行后自動(dòng)換行(不理解的話可以暫時(shí)刪掉這句話,然后運(yùn)行程序看結(jié)果)*/

}

for(j=0;j<5;j++)/*這個(gè)循環(huán)是用于換行的*/

{

temp[0][j]=a[0][j];/*將數(shù)組a的第一行的每個(gè)值都賦予temp(這里的temp是一個(gè)1*5的數(shù)組)*/

a[0][j]=a[4][j];/*將數(shù)組a的第一行的每個(gè)值替換成第五行的值*/

a[4][j]=temp[0][j];/*將數(shù)組a的第五行的每個(gè)值替換成temp的值*/

}

? printf("\n");/*輸出一個(gè)回車*/

for (i=0;i<5;i++)

{ for(j=0;j<5;j++)

?printf("%d\t",a[i][j]);/*將換行后的新數(shù)組a輸出出來*/

?printf("\n");/*在輸出完每一行后自動(dòng)換行*/

}

for (i=0;i<5;i++)

{

max=a[i][0];/*定義一個(gè)最大值變量max,令其暫時(shí)等于第i行的第一個(gè)數(shù)*/

for (j=0;j<5;j++)/*讓max和該行的所以數(shù)比較,如果有某個(gè)數(shù)大于max,max變?yōu)樵摂?shù)的值*/

?if (a[i][j]>max)

?{

? max=a[i][j];

?}

for (j=0;j<5;j++)/*令max和第i行的每個(gè)數(shù)進(jìn)行比較,輸出所有等于max的數(shù)及該數(shù)的坐標(biāo)*/

if(max==a[i][j])

printf("\na[%d][%d]=%d",i,j,max);

}

}

實(shí)驗(yàn)七 指針

一、 目的要求

1. 掌握指針的定義和使用指針變量;

2. 學(xué)會(huì)使用字符串的指針和指向數(shù)組的指針變量;

3. 學(xué)會(huì)使用指向函數(shù)的指針變量;

4. 按實(shí)驗(yàn)內(nèi)容要求完成全程程序設(shè)計(jì)后才允許上機(jī)。

二、 實(shí)驗(yàn)內(nèi)容與步驟

設(shè)計(jì)一個(gè)函數(shù),它有三個(gè)形參

(1) 被查找的字符串str;

(2) 待查找的字符xCh;

(3) 在字符串str中xCh出現(xiàn)的位置i=0,1,…

它的返回值是在str中xCh ?出現(xiàn)的次數(shù)(若str中無xCh,則返回值=0)

三、 上機(jī)要求

1、 鍵入待查的字符xCh;

2、 鍵入被查的字符串str;

3、 調(diào)用該函數(shù);

4、 打印它的返回值和出現(xiàn)的位置;

5、 允許重復(fù)執(zhí)行,每次以清屏開始(用循環(huán)語句控制重復(fù)執(zhí)行)。

四、 提示

xCh在str出現(xiàn)位置應(yīng)設(shè)計(jì)為一整型指針,以便記下0~N個(gè)位置(整數(shù))。

#include<stdio.h>

void main()

{

char temp;

int run(),j;

int (*prun)();

temp='Y';

while(temp!='N'||temp!='n')

{

?if(temp=='Y'||temp=='y')

?{

?prun=run;

?j=(*prun)();

? ?if (j==0)

? ?{

? ?printf("Can Not Find The xCh! j=%d",j);

? ?}

? ?else

? ?{

? ?printf("\nj=%d",j);

? ?}

?printf("\nParden>Y/N:");

?fflush(stdin);

?temp=getch();

?}

?if(temp=='N'||temp=='n')

?break;

?if(temp!='Y'&&temp!='y')

?{

?printf("Wrong!You can only put Y(N) or y(n)\nPlease put again(Y/N):");

?fflush(stdin);

?temp=getch();

?}

}

}

int run (char xCh,char str[100],int i)

{

int j;

char *p;

clrscr();

?printf("xCh=");

?xCh=getch();

?printf("%c\nstr=",xCh);

?gets(str);

?p=&str[0];

?i=0;

?j=0;

? while(*p)

? {

? if (*p==xCh)

? ?{

? ?j++;

? ?printf("xCh :%d\t",i);

? ?}

? p=p+1;

? i++;

? }

? return j;

}

Mian()版:

#include<stdio.h>

void main()

{

int i,j;

char xCh,str[100],*p,temp;

temp='Y';/*給temp賦初值Y,防止第一個(gè)while循環(huán)無法運(yùn)行*/

while(temp!='N'||temp!='n')/*如果temp不等于n或N時(shí),進(jìn)行循環(huán)*/

{

?if(temp=='Y'||temp=='y')/*當(dāng)temp為y或Y時(shí),進(jìn)行下列循環(huán),用于進(jìn)行題目要求的操作*/

?{

?clrscr();/*清屏*/

?printf("xCh=");/*在屏幕輸出提示xCh=*/

?xCh=getch();/*從屏幕讀取一個(gè)字符賦給xCh(getch()和getchar()的區(qū)別:前者只要輸入一個(gè)字符就結(jié)束輸入過程,后者需要按回車或空格后才結(jié)束輸入過程)*/

?printf("%c\nstr=",xCh);/*在屏幕xCh=后面輸出剛才輸入的xCh的值,并提示用戶輸入str(因?yàn)槲覀冇胓etch(),輸入完字符后會(huì)自動(dòng)結(jié)束xCh的輸入進(jìn)入下一指令的執(zhí)行(在本題中,下一指令是:printf("%c\nstr=",xCh);),而不在屏幕輸出剛才輸入的字符)【不理解的話把該句改成{printf("\nstr=");}看看輸出結(jié)果就知道了】*/

?gets(str);/*輸入str*/

?p=&str[0];/*將指針地址指向str這個(gè)字符串的首字符位置*/

?i=0;

?j=0;

? while(*p)/*當(dāng)p所指向的字符不為空字符時(shí),進(jìn)行判斷循環(huán)*/

? {

? if (*p==xCh)/*當(dāng)p所指向的字符為所需尋找的xCh時(shí),進(jìn)行以下操作*/

? ?{

? ?i++;/*i自加1,用于累計(jì)str中xCh的數(shù)目*/

? ?printf("xCh :%d\t",j);/*輸出xCh在str中出現(xiàn)的位置*/

? ?}

? p=p+1;/*指針地址移向str的下個(gè)字符*/

? j++;/*j用于記錄此時(shí)p的位置,在str中第一個(gè)字符時(shí)=0,第二個(gè)時(shí)=1,以此類推*/

? }

? ?if (i==0)/*當(dāng)str中沒有xCh這個(gè)字符時(shí),i=0*/

? ?{

? ?printf("Can Not Find The xCh! i=%d",i);/*在屏幕中提示無法找到str中的xCh,并輸出i=0*/

? ?}

? ?else

? ?{

? ?printf("\ni=%d",i);/*\n為換行*/

? ?}

?printf("\nParden>Y/N:");

fflush(stdin);/*清空計(jì)算機(jī)緩存*/

?temp=getch();/*從屏幕中讀取一個(gè)字符賦給temp*/

?}

?if(temp=='N'||temp=='n')/*當(dāng)temp為N或n時(shí)*/

?break;/*跳出循環(huán)*/

?if(temp!='Y'&&temp!='y')/*當(dāng)輸入的temp不為Y、y、N、n時(shí)*/

?{

?printf("Wrong!You can only put Y(N) or y(n)\nPlease put again(Y/N):");

fflush(stdin);

?temp=getch();

?}

}

}

/*fflush(stdin)*/

實(shí)驗(yàn)八 結(jié)構(gòu)體與共用體

一、 目的要求

1、 掌握結(jié)構(gòu)體類型變量與數(shù)組的定義和使用;

2、 學(xué)會(huì)使用指針變量和結(jié)構(gòu)體指針數(shù)組;

3、 按實(shí)驗(yàn)內(nèi)容要求完成全程程序設(shè)計(jì)后才允許上機(jī)。

二、 實(shí)驗(yàn)內(nèi)容與步驟

1. 設(shè)計(jì)一個(gè)結(jié)構(gòu)

struct student {

long no; ? ? /*學(xué)號(hào)*/

char name[10]; /*姓名*/

char sex; ? /*性別*/

int age; ? /*年齡*/

float score; ?/*平均成績(jī)*/

}

2. 完成下列任務(wù):

(1) 輸入實(shí)際學(xué)生人數(shù)n (2<n<4);

(2) 輸入每個(gè)學(xué)生的信息,組成結(jié)構(gòu)數(shù)組,并輸出;

(3) 統(tǒng)計(jì)男、女生人數(shù)并輸出;

(4) 計(jì)算全班平均成績(jī)并輸出;

(5) 將低于全班平均成績(jī)的學(xué)生信息按行輸出

三、 上機(jī)要求

1. 可劃分為若干個(gè)函數(shù),或?qū)懗梢粋€(gè)main( );

2. 要求輸出格式有提示及相應(yīng)數(shù)據(jù)。

#include<stdio.h>

struct student

?{

?long no; ? ? /*學(xué)號(hào)*/

?char name[10]; /*姓名*/

?char sex; ? /*性別(gender)*/

?int age; ? /*年齡*/

?float score; ?/*平均成績(jī)*/

?}s[3];

void main()

{

int i,n,t,m,na;

float av,sum;

float temp;

clrscr();

m=0;

sum=0;

printf("The number of the studens:");

scanf("%d",&n);

printf("\n");

? for(i=0;i<n;i++)

? {

? printf("\nNo.");

? scanf("%ld",&s[i].no);

? printf("\nName:");

? ?na=0;

? ?while(na==0)

? ?{

? ?scanf("%s",&s[i].name);

? ?if(strlen(s[i].name)>10)

? ?printf("Wrong!You can only put 10 characters!\nName:");

? ?else

? ?na=1;

? ?}

? printf("\nGender:(W/M)");

? ?t=0;

? ?while(t==0)

? ?{

? ?scanf("%s",&s[i].sex);

? ?if(s[i].sex!='W'&&s[i].sex!='w'&&s[i].sex!='m'&&s[i].sex!='M')

? ?printf("Wrong!\nGender:");

? ?else

? ?t=1;

? ?}

? ? if(s[i].sex=='m'||s[i].sex=='M')

? ? m++;

? printf("\nAge:");

? scanf("%d",&s[i].age);

? printf("\nScore:");

? scanf("%f",&temp);

? s[i].score=temp;

? }

? ? for(i=0;i<n;i++)

? ? {

? ? sum=sum+s[i].score;

? ? printf("\nNo.%ld\nName:%s\nGender:%c\nAge:%d\nScore:%.2f",s[i].no,s[i].name,s[i].sex,s[i].age,s[i].score);

? ? }

? ? av=sum/n;

? ? ?printf("\nThe number of girl(s):%d\nThe number of boy(s):%d\n",n-m,m);

? ? ?printf("\nThe average of the score is:%.2f",av);

? ? ? for(i=0;i<n;i++)

? ? ? {

? ? ? if (s[i].score<=av)

? ? ? printf("\nNo.%ld; Name:%s; Gender:%c; Age:%d; Score:%.2f",s[i].no,s[i].name,s[i].sex,s[i].age,s[i].score);

? ? ? }

}


C語言程序設(shè)計(jì)實(shí)驗(yàn)答案的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
海口市| 庆安县| 甘洛县| 克什克腾旗| 墨脱县| 喜德县| 嵊州市| 八宿县| 连山| 佳木斯市| 新营市| 万宁市| 泸州市| 万山特区| 福泉市| 华容县| 白玉县| 密山市| 荥经县| 安泽县| 丹江口市| 满洲里市| 庆城县| 桃源县| 澄城县| 定州市| 桓仁| 安平县| 潼南县| 北流市| 洪雅县| 岗巴县| 安乡县| 富平县| 界首市| 山丹县| 大庆市| 云安县| 朝阳县| 莆田市| 名山县|