急求c语言编程答案

急求c语言编程答案,第1张

// test.cpp : 定义控制台应用程序的入口点。

//

// test1.cpp : Defines the entry point for the console application.

//

// test.cpp : Defines the entry point for the console application.

# include "stdafx.h"

# include "stdio.h"

# include "stdlib.h"

# include <iostream>

# include <fstream>

# include <iomanip> /*需引用的头文件*/

using namespace std

struct node /*定义结点结构体保存结点信息*/

{

struct student /*定义结构体变量student保存学生的信息*/

{

char studentnumber[10]/*学生学号*/

char studentname[5] /*学生姓名*/

struct course /*定义结构体变量Course保存课程信息*/

{

float course1 //*********************************

float course2

float course3 //各门课程的分数和平均分

float averagemark //*****************************

}struct course allcourse

}struct student stu

struct node *next

}

node *top

struct node *createlist(char info[2][9],float marksinfo[]) //**************************

{

static struct node *new_node,*current_node

new_node=(struct node *)new(node) /*建立新的结点*/

strcpy(new_node->stu.studentnumber,info[0])

strcpy(new_node->stu.studentname,info[1])

new_node->stu.allcourse.course1=marksinfo[0]

new_node->stu.allcourse.course2=marksinfo[1]

new_node->stu.allcourse.course3=marksinfo[2]//对结点的元素付值

new_node->stu.allcourse.averagemark=(marksinfo[0]+

marksinfo[1]+marksinfo[2])/3

new_node->next=NULL

if (top==NULL)

{

top=new_node

current_node=top

}

current_node->next=new_node/*指向新的结点*/

current_node=new_node /*总指向新的结点*/

return top //*****************************

}

void studentaverage(node *top) /*显示平均分函数*/

{

int i=0,maxcount=0 /*结点的数目maxcount*/

char response='y'

struct node *t=top

fstream readcount("countsave.txt",ios::in) /*定义文件流*/

readcount>>maxcount/*将文件信息读入变量Maxcount*/

system("cls") /*清屏*/

cout.flags(ios::left) /*输出格式为左对齐*/

cout<<setw(14)<<"学号"<<setw(14)<<"姓名"<<setw(14)<<"课程1"<<setw(14)

<<"课程2"<<setw(14)<<"课程3"<<"平均分"<<endl

for (i=0i<maxcounti++)

{

t=top

top=top->next

cout.flags(ios::left)

cout<<setw(14)<<t->stu.studentnumber<<setw(14)<<

t->stu.studentname<<setw(14)<<t->stu.allcourse.course1<<setw(14)<<t->stu.allcourse.course2

<<setw(14)<<t->stu.allcourse.course3<<setw(14)<<t->stu.allcourse.averagemark<<endl

}

system("pause")

}

void courseaverage(node *top)/*显示每门课程的平均分*/

{

int maxcount

node *t=top

float courseaverage_1=0.00//********************************

float courseaverage_2=0.00// 保存各门平均分的变量

float courseaverage_3=0.00//********************************

fstream readcount("countsave.txt",ios::in)

readcount>>maxcount

system("cls")

for (int i=0i<maxcounti++) //********************************************************************

{

t=top

top=top->next//遍历结点累加各门课程分数求平均值

float(courseaverage_1)+=float(t->stu.allcourse.course1)

courseaverage_2+=t->stu.allcourse.course2

courseaverage_3+=t->stu.allcourse.course3

} //********************************************************************

cout.flags(ios::left)

cout<<setw(25)<<"课程1"<<setw(25)<<"课程2"<<setw(25)<<"课程3"<<endl

cout<<setw(25)<<float(courseaverage_1/(maxcount+1))<<setw(25)<<

courseaverage_2/float(maxcount+1)<<setw(25)<<courseaverage_3/float(maxcount+1)<<endl

system("pause")

}

void orderWithAverageMark(node *top)/*按平均分排序*/

{

struct node *t=top

static struct node *t0=top/*保存结点的头*/

float averagemark_1=0.00

float averagemark_2=0.00

static char temp[10]/*保存字符串的中间变量*/

float temp0 /*整型变量的中间变量*/

int maxcount

int i

bool flag/*判断冒泡算法是否结束*/

fstream readcount("countsave.txt",ios::in||ios::out)

readcount>>maxcount

//*****************************************************************************

//冒泡排序

//if (count

for(int j=0j<maxcountj++)

{

top=t0//结点指针指向表头,执行下一次排序

for (i=0i<(maxcount-1)i++)

{

flag=false

t=top

averagemark_1=t->stu.allcourse.averagemark

averagemark_2=top->next->stu.allcourse.averagemark

if (averagemark_1<=averagemark_2)

{ //****************************************************************

strcpy(temp,t->stu.studentnumber)

strcpy(t->stu.studentnumber,top->next->stu.studentnumber)

strcpy(top->next->stu.studentnumber,temp)

strcpy(temp,t->stu.studentname)

strcpy(t->stu.studentname,top->next->stu.studentname)

strcpy(top->next->stu.studentname,temp)

temp0=t->stu.allcourse.course1

t->stu.allcourse.course1=top->next->stu.allcourse.course1//交换两结点的各数值

top->next->stu.allcourse.course1=temp0

temp0=t->stu.allcourse.course2

t->stu.allcourse.course2=top->next->stu.allcourse.course2

top->next->stu.allcourse.course2=temp0

temp0=t->stu.allcourse.course3

t->stu.allcourse.course3=top->next->stu.allcourse.course3

top->next->stu.allcourse.course3=temp0

temp0=t->stu.allcourse.averagemark

t->stu.allcourse.averagemark=top->next->stu.allcourse.averagemark

top->next->stu.allcourse.averagemark=temp0 //*************************************************************

flag=true

}

top=top->next

}

}

//********************************************************************************************

}

int menu()//主菜单

{

int choice

cout<<"*******************************************************************************"<<endl

cout<<"* *"<<endl

cout<<"* 1. 输入学生信息 *"<<endl

cout<<"* *"<<endl

cout<<"* 2. 显示学生平均分 *"<<endl

cout<<"* *"<<endl

cout<<"* 3. 显示每门课的平均分 *"<<endl

cout<<"* *"<<endl

cout<<"* 4. 显示总分在某平均分以上的学生 *"<<endl

cout<<"* *"<<endl

cout<<"* 5. 保存数据 *"<<endl

cout<<"* *"<<endl

cout<<"* 0. 退出 *"<<endl

cout<<"* *"<<endl

cout<<"*******************************************************************************"<<endl

cout<<" "<<endl

cout<<" 请选择:"

loop:cin>>choice

if (choice>=0&&choice<=5)

return choice

else

goto loop

}

int findstdentnumber(node *top,char namestr[5])

{

node *t=top

int count

fstream readcount("countsave.txt",ios::in)

readcount>>count

for (int i=0i<counti++)

{

t=top

top=top->next

if (strcmp(namestr,t->stu.studentnumber)==0)

{

return 1

}

}

return 0

}

node *getinfo()

{

char response='y'

char studentinfo[2][9] //二维数组保存学号和姓名

float studentcoursemark[3] //保存各科成绩

int count=0

static int count0

void savestudentinfo(node *)

node *readstudentinfo()

void freelist(node *)

fstream readcount("countsave.txt",ios::in)/*读出结点值*/

readcount>>count0

readcount.close()

do

{

loop0: system("cls")

cout<<endl

cout<<"请输入学号:"

cin>>studentinfo[0]

cout<<endl

if(findstdentnumber(top,studentinfo[0]))

{

cout<<"该学号已存在:"<<endl

system("pause")

strcpy(studentinfo[0]," ")

goto loop0

}

cout<<"姓名:"

cin>>studentinfo[1]

cout<<endl

cout<<"课程1:"

cin>>studentcoursemark[0]

cout<<endl

cout<<"课程2:"

cin>>studentcoursemark[1]

cout<<endl

cout<<"课程3:"

cin>>studentcoursemark[2]

top=createlist(studentinfo,studentcoursemark)

count++

cout<<endl

cout<<"要继续吗?(y or n)"

cin>>response

}while(count>=0&&count<30&&(response=='y'||response=='Y'))

orderWithAverageMark(top) /*对链表按学生平均分进行排序*/

fstream savecount("countsave.txt",ios::out)

savecount<<(count+count0)/*保存结点值并累加*/

savecount.close()

savestudentinfo(top) /*保存学生信息到文件*/

return top

}

void savestudentinfo(node *top) /*保存学生信息*/

{

int i,numberofstudent

node *head=top

fstream count("countsave.txt",ios::in)

count>>numberofstudent/*获得学生个数*/

count.close()

orderWithAverageMark(head)

fstream student("studentinfosave.txt",ios::out)

system("cls")

//遍历链表,保存信息

for (i=0i<numberofstudenti++)

{

head=top

top=top->next

student<<head->stu.studentnumber<<" "<<head->stu.studentname<<" "<<

head->stu.allcourse.course1<<" "<<head->stu.allcourse.course2<<" "<<

head->stu.allcourse.course3<<endl

}

student.close()

//*********************

cout<<" 保存成功!"<<endl

system("pause")

}

node *readstudentinfo() /*从文件读取学生信息*/

{

int numberofstudent

char studentinfo[2][9]

float studentcoursemark[3]

fstream readcount("countsave.txt",ios::in)

fstream readstuinfo("studentinfosave.txt",ios::in)

readcount>>numberofstudent

for (int i=0i<numberofstudenti++)

{

readstuinfo>>studentinfo[0]>>studentinfo[1]>>studentcoursemark[0]>>

studentcoursemark[1]>>studentcoursemark[2]

top=createlist(studentinfo,studentcoursemark)

}

readcount.close()

readstuinfo.close()

return top

}

void find(node *top)/*查找函数*/

{

int lowmark=0,i=0,count=0

struct node *t=top

system("cls")

cout<<"请输入一个平均分的下限:"

cin>>lowmark

system("cls")

fstream readcount("countsave.txt",ios::in)

readcount>>count

cout.flags(ios::left)

cout<<setw(14)<<"学号"<<setw(14)<<"姓名"<<setw(14)<<"课程1"<<setw(14)

<<"课程2"<<setw(14)<<"课程3"<<setw(14)<<"平均分"<<endl

if(lowmark>=0&&lowmark<=100)

{

for(int j=0j<countj++)

{

t=top

top=top->next

if(lowmark<=(t->stu.allcourse.averagemark))//****************************显示满足条件的信息

{

cout<<setw(14)<<t->stu.studentnumber<<setw(14)<<t->stu.studentname<<setw(14)<<t->stu.allcourse.course1<<setw(14)

<<t->stu.allcourse.course2<<setw(14)<<t->stu.allcourse.course3<<setw(14)<<t->stu.allcourse.averagemark<<endl

i++

}

}//****************************

if (i==0)

{

cout<<endl

system("cls")

cout<<"找不学生信息!"<<endl

}

cout<<"共找到"<<i<<"条信息!"<<endl

system("pause")

}

}

void main()

{

top=readstudentinfo() /*读取学生信息*/

for()

{

system("cls")

switch(menu())

{

case 1:

top=getinfo()

break

case 2:

studentaverage(top)

break

case 3:

courseaverage(top)

break

case 4:

find(top)

break

case 5:

savestudentinfo(top)

break

case 0:

exit(0)

break

}

}

}

main()

{ int k, a[10]={1,2,3,4,5}

for(k=1k<3k++) fun(a)

for(k=0k<5k++) printf("%d", a[k])

}

上面程序的输出结果是

A.34756

B.23445

C.35743

D.12345

(40) 请读程序:

#include

#define SUB(X,Y) (X)*Y

main()

{ int a=3, b=4

printf("%d", SUB(a++, b++))

}

上面程序的输出结果是

A.12

B.15

C.16

D.20(41) 请读程序:

#include

main()

{ int mun=0

while(num<=2)

{ num++printf("%d\n",num)}

}

上面程序的输出结果是

A.1

B.1

C.1

D.1

2 2 2

3 3

4

(42) 请读程序:

#include

main()

{ float x,y

scan("%f",&x)

if(x<0.0) y=0.0

else if((x<5.0)&&(x!=2.0))

y=1.0/(x+2.0)

else if (x<10.0) y=1.0/x

else y=10.0

printf("%f\n",y)

}

若运行时从键盘上输入2.0(表示回车),则上面程序的输出结果是

A.0.000000

B.0.250000

C.0.500000

D.1.000000

(43) 请读程序:

#include

main()

{ int x=1, y=0, a=0, b=0

switch(x)

{ case 1:

switch(y)

{ case 0: a++break

case 1: b++break

}

case 2:

a++b++break

}

printf("a=%d, b=%d\n",a,

B.

}

上面程序的输出结果是

A.a=2, b=1

B.a=1, b=1

C.a=1, b=0

D.a=2, b=2

(44) 若有下面的程序片段:

int a[12]={0}, *p[3], **pp, i

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

p[i]=&a[i*4]

pp=p

则对数组元素的错误引用是

A.pp[0][1]

B.a[10]

C.p[3][1]

D.*(*(p+2)+2)

(45) 请读程序:

#include

#include

main()

{ float x,y,z

scan("%f%f",&x,&y)

z=x/y

while(1)

{ if(fabs(z)>1.0)

{ x=yy=zz=x/y

}

else break

}

printf("%f\n",y)

}

若运行时从键盘上输入3.6 2.4(表示回车),则输出的结果是

A.1.500000

B.1.600000

C.2.000000

D.2.400000

(46) 请读程序:

#include

f(in b[], int n)

{ int i, r

r=1

for(i=0i<=ni++) r=r*b[i]

return r

}

main()

{ int x, a[]={ 2,3,4,5,6,7,8,9}

x=f(a, 3)

printf("%d\n",x)

}

上面程序的输出结果是

A.720

B.120

C.24

D.6

(47) 请读程序:

#include

#include

void fun( char *s)

{ char a[10]

strcpy( a, "STRING")

s=a

}

main()

{ char *p

fun(p)

x=f(a, 3)

printf("%s\n",p)

}

上面程序的输出结果是(└┘表示空格)

A.STRING└┘└┘└┘└┘

B.STRING

C.STRING└┘└┘└┘

D.不确定的值

(48) 若已建立下面的链表结构,指针p、s分别指向图中所示的结点,则不能将s所指的结点插入到链表末尾的语句组是

A.s->next=NULLp=p->nextp->next=s

B.p=p->nexts->next=p->nextp->next=s

C.p=p->nexts->next=pp->next=s

D.p=(*p).next(*s).next=(*p).next(*p).next=s

(49) 请读程序:

#include

void fun(float *pl, float *p2, float *s)

{ s=( float * )calloc( 1, sizeof(float))

*s=*p1+ *(p2++)

}

main()

{ float a[2]={1.1, 2.2}, b[2]={10.0, 20.0}, *s=a

fun(a, b, s)

printf("%f\n",*s)

}

上面程序的输出结果是

A.11.100000

B.12.100000

C.21.100000

D.1.100000

(50) 请读程序:

#include

#include

void fun( char *w, int m)

{ char s, *p1, *p2

p1=wp2=w+m-1

while (p1{ s=*p1++*p1=*p2--*p2=s}

}

main()

{ char a[]="ABCDEFG"

fun( a, strlen(a))

puts(a)

}

上面程序的输出结果是

A.GFEDCBA

B.AGADAGA

C.AGAAGAG

D.GAGGAGA

二、填空题(每空2分,共40分)

请将每空的正确答案写在答题卡上【1】-【20】序号后的横线上,答在试卷上不得分。

(1) DOS命令分为内部命令与外部命令,XCOPY命令是 【1】 命令

(2) 为了列出当前盘当前目录中所有第三个字符为C的文件名的有关信息,应该用命令 【2】 。

(3) 设当前盘为A盘,当前目录为\X\Y,A盘上的一个文件QR.C在当前目录下的子目录W中。现已将A盘的当前目录改为\D\XY,当前盘改为C盘,如需指定A盘上该文件应写成 【3】 。

(4) 用十六进制给存储器中的字节地址进行编号,其地址编号是从0000到FFFF,则该存储器的容量是【4】KB。

(5) 设有两个八位二进制数00010101与01000111相加,其结果的十进制表示为 【5】 。

(6) 数学式子 写成C语言表达式是 【6】 。

(7) 下面程序的输出结果是 【7】 。

#include

main()

{ static char b[]="Goodbye"

char *chp=&b[7]

while( --chp >=&b[0]) putchar( *chp)

putchar(’\n’)

}

(8) 下面程序的输出结果是 【8】 。

#include

void as( int x, int y, int *cp, int *dp)

{ *cp=x+y

*dp=x-y

}

main()

{ int a=4, b=3, c, d

as( a, b, &c, &d)

printf( ’%d %d\n", c, d)

}

(9) 请读程序:

#include

main( int argc, char *argv[])

{ int i

printf( ’%d\n", argc)

for( i=0i<=argc+1i++) printf( "%s ", argv[i])

printf("\n")

}

若上面的程序编译、连接后生成可执行文件ABC.EXE,则输入以下命令行

abc file1 file2(表示回车)

程序执行后的输出结果是 【9】 。

(10) 条件“20(11) 设二进制数a是00101101,若想通过异或运算a^b使a的高4位取反,低4位不变,则二进制数b应是 【11】 。

(12) 若有以下说明和定义语句,则变量w在内存中所占的字节数是 【12】 。

union aa

{ float x, y

char c[6]

}

struct st { union aa vfloat w[5]double ave} w

(13) 下面程序的输出结果是 【13】 。

#include

#define N 5

fun( char *s, char a, int n)

{ int j

*s=aj=n

while( ereturn j

}

main()

{ char s[N+1]

int k, p

for( k=1k<=Nk++)

s[k]=’A’+k+1

printf( "%d\n", fun(s, ’E’, N))

}(14) 下面程序的输出结果是 【14】 。

#include

void fun( char *a1, char *a2, int n)

{ int k

for( k=0ka2[k]=( a1[k]-’A’-3+26)&+’A’

a2[n]=’\0’

}

main()

{ char s1[5]="ABCD", s2[5]

fun( s1, s2, 4)

puts( s2)

}

(15) 下面的findmax函数返回数组s中最大元素的下标,数组中元素的个数由t传入,请填空。

findmax( int s[], int t)

{ int k, p

for( p=0, k=ppif( s[p]>s[k]) 【15】 。

return k

}

(16) 下面程序的输出结果是 【16】 。

#include

fun( int x)

{ int p

if( x==0||x==1) return(3)

p=x-fun( x-2)

return p

}

main()

{ printf( "%d\n", fun(9))}

(17) 下面程序的输出结果是 【17】 。

#include

funa( int a, int b)

{ return a+b}

funb( int a, int b)

{ return a-b}

sub( int (*t)(), int x, int y)

{ return((*t)( x,y))}

main()

{ int x, (*p)()

p=funa

x=sub(p,9,3)

x+=sub(funb, 8, 3)

printf("%d\n", x)

}

(18) 下面程序的输出结果是 【18】 。

#include

main()

{ char *p[]={ "BOOL", "OPK", "H", "SP"}

int i

for(i=3i>=0i--,i--) printf( "%c", *p[i])

printf("\n")

}

(19) 为了建立如图所示的存储结构(即每个结点含两个域,data是数据域,next是向结点的指针域),请填空。

data next

struct link { char data【19】 } node

(20) 下面的程序用来统计文件中字符的个数,请填空。

#include

main()

{ FILE *fp

long num=0

if(( fp=fopen("fname.dat","r"))==NULL)

{ printf( "Can’t open file! \n")exit(0)}

while 【20】

{ fgetc(fp)num++}

printf("num=%d\n", num)

fclose(fp)

}

参考答案

一`选择题((1)~(40)题每题1分,(41)~(50)题每题2分,共60分)

(1)C (2)A (3)B (4)D (5)D (6)A (7)C (8)B (9)D (10)C

(11)B (12)D (13)A (14)C (15)B (16)A (17)B (18)A (19)C (20)C

(21)D (22)B (23)B (24)A (25)C (26)D (27)D (28)B (29)C (30)A

(31)B (32)B (33)D (34)C (35)A (36)C (37)A (38)B (39)C (40)A

(41)C (42)C (43)A (44)C (45)B (46)B (47)D (48)C (49)D (50)C

二` 填空题(每空2分,共40分)

(第(2)`(3)空允许小写或大小写混合使用,其它空均须与答案一样)

(1) 外部

(2) DIR ?? C*.*

(3) A:\X\Y\W\QR.C

(4) 64

(5) 92

(6) pow(sin(x),2) * (a+b)/(a-b) 或 sit(x) * sit(x) * (a+b)/(a-b)

(7) eybdooG

(8) 7 1

(9) 3 abc.exe file1 file2 (注:两个答案,每个1分)

(10) ((20 (11) 11110000

(12) 34

(13) 3

(14) XYZA

(15) k=p

(16) 7

(17) 17

(18) SO

(19) struct link *next

(20) (!feof(fp))或(feof(fp)==0)

写个三重循环来寻找购买方案。

#include <stdio.h>

int main(void) {

int i,j,k,count=0

for (i=1i<=100/5i++) /*公鸡*/

for (j=1j<=100/3j++) /*母鸡*/

for (k=1k<=100*3k++) /*小鸡*/

if (i*5+j*3+k==100 && i+j+k*3==100) {

count ++

printf ("方案%d:%d只公鸡,%d只母鸡,%d只小鸡。\n",count,i,j,k*3)

printf ("100只鸡 = %d + %d + %d\n",i,j,k*3)

printf ("100元钱 = %d×5 + %d×3 + %d/3 = %d + %d + %d\n",i,j,k*3,i*5,j*3,k)

printf ("\n") 

}

}

运行结果


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11276542.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存