本文目录一览:
C语言程序设计
1、C程序的执行特点是 (c)
A、从程序的main函数开始,到程序的最后一个函数结束
B、从程序的第一个函数开始,到程序的最后一个函数结束
C、从程序的main函数开始,到程序的main函数结束
D、从程序的第一个函数开始,到程序的main函数结束
2、字符型(char)数据在微机内存中的存储形式是 (b)
A、BCD码 B、ASCH码 C、补码 D、反码
3、设a=5,b=6,c=7,d=8,m=2,n=2,执行(m==ab)(n==cd)后n的值为(d)
A、2 B、3 C、1 D、0
4、设有定义;float a=2,b=4,h=3;
以下语言表达式中与代数式1/2(a+b)h计算结果相符的是 (d)
A、(a+b)*h/2 B、(1/2)*(a+b)*h
C、(a+b)*h*1/2 D、h/2*(a+b)
5、在以下一组运算符中,优先级最高的运算符是
(d)
A、 B、= C、= D、%
6、设有以下定义// 这题目 貌似错的
int a=0;
double b=1.25;
char c=’A’;
#define D 2
则下面语句中错误 的是 ()
A、a++ B、b++ C、c++ D、d++
7、设int m=1,n=2,m++==n表达式的结果是
(b)
A、0 B、1 C、2 D、3
8、设有如下定义:int x=10,y=3,z;则语句
printf(“%d/n”,z=(x%y,x/y));的输出结果是 (a)
A、3 B、1 C、0 D、4
9、假定所有变量均已正确说明,下列程序段运行后X的值是 (b)
int a,b,c,x;
a=b=c=0;
if(b)
x=3
else
x=4;
A、35 B、4 C、34 D、3
10、设变量c为char类型,能正确判断出c为小写字母的表达式是(c)
A、‘a’=c=’z’ B、(c=’a’)||(c=’z’)
C、(c=’a’)(c=’z’) D、以上都不是
11、为了避免在嵌套的条件语句(if-else)中产生二义性,C语言规定:else子句总是
与 (b) 配对。
A、缩排位置相同的if B、其之前最近的if
C、其之后最近的if D、同一行上的if
12、循环语句while和do-while的主要区别是(a)
A、do-while循环体至少要无条件执行一次,while循环可以一次都不执行
B、while的循环控制条件比do-while的循环控制条件更严格
C、do-while允许从外部跳转到循环体内
D、do-while的循环体不能是复合语句
13、当执行以下程序段时
x=-1;
do{x=x*x;} while(!x); (a)
A、循环体将执行一次 B、循环体将执行两次
C、循环体将执行无限次 D、系统将提示有语法错误
14、设有定义:int a{10};以下对a数组元素正确引用的是(d)
A、a[10] B、a[3,5] C、a (5) D、a[10-10]
15、若用数组名作为函数的实参,传递给形参的是
(a)
A、数组的首地址 B、数组第一个元素的值
C、数组中全部元素的值 D、数组元素的个数
16、C语言允许函数类型缺省定义,此时函数值隐含的类型是 (b) A、float B、int C、long D、double
C语言程序编写!
鄙人大一时的课程设计
/*TopBoy出品,品质保证*/
/*如果编译失败可将第一句#includemalloc.h改为#includealloc.h*/
#includemalloc.h
#includectype.h
#include graphics.h
#include stdio.h
#include conio.h
#define NULL 0
#define F10 17408
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define SPACE 0x3920
#define ESC 0x011b
#define ENTER 0x1c0d
#define LEN sizeof(struct student)
struct student *insert(struct student *head);
struct student *print(struct student *head);
struct student *change(struct student *head,int);
struct student *del(struct student *head,int);/*删除记录*/
struct student *search(struct student *head);
void stat(struct student *head);
void save(struct student *head);
void printrecord(struct student *head);
void menuinit(void);
struct student *order(struct student *head);
struct student
{
long num;
char name[20];
char sex[7];
int score;
struct student *next;
};
int n; /*记录总数*/
int x=1,y=1,max;/*x表示显示记录的页数 y表示该页第y个记录*/
char *menutext[]={"Order",
"Insert",
"Change",
"Delete",
"Search",
"Statis",
"Quit"};
void init(void) /* 图形初始化 */
{
int gd=DETECT,gm;
initgraph(gd,gm,"");
}
void screeninit()
{
int i;
window(1,2,80,25);
textbackground(BLUE);
textcolor(7);
clrscr();
gotoxy(1,2);
putch(0xc9);/*输出左上角边框┏*/
for(i=1;i79;i++)putch(0xcd); /*输出上边框水平线*/
putch(0xbb); /*输出右上角边框 ┓*/
for(i=3;i24;i++)
{
gotoxy(1,i);putch(0xba); /*输出左垂直线*/
gotoxy(80,i);putch(0xba); /*输出右垂直线*/
}
gotoxy(1,24);putch(0xc8); /*输出左下角边框┗*/
for(i=1;i79;i++)putch(0xcd); /*输出下边框水平线*/
putch(0xbc); /*输出右下角边框┛*/
textbackground(7);
textcolor(BLACK);
window(1,25,80,25);
clrscr();
cputs(" You can press F10 to use Menu or ESC to quit.");
}
int chooseab(int locx,int locy,char str1[],char str2[])
{
int i,text1=4,text2=14,back1=15,back2=1,key;
while(1)
{
window(locx,locy,locx+strlen(str1)-1,locy);
textbackground(back1);textcolor(text1);
clrscr();gotoxy(1,1);printf("%s",str1);
window(locx+strlen(str1)+3,locy,locx+strlen(str1)+strlen(str2)+2,locy);
textbackground(back2);textcolor(text2);clrscr();gotoxy(1,1);printf("%s",str2);
key=bioskey(0);
if(key==LEFT||key==RIGHT)
{
i=text1;text1=text2;text2=i;
i=back1;back1=back2;back2=i;
}
else if(key==ENTER)break;
}
window(59,5,74,20);
if(text1==4)return 1;
else return 2;
}
void menuinit()
{
int i;
window(1,1,80,1);
textbackground(7);
textcolor(BLACK);
clrscr();
for(i=0;i7;i++)
{
gotoxy(i*10+5,1);
cprintf("%s",menutext[i]);
}
}
struct student *menu(struct student *head,int choose)
{
int i=0,ii=0,key;
window(1,1,80,1);
gotoxy(i*10+5,1);
textbackground(BLACK);
textcolor(WHITE);
cprintf("%s",menutext[i]);
for(;;)
{
switch(key)
{
case LEFT:
{
ii=i-1;
if(ii0)ii=6;
break;
}
case RIGHT:
{
ii=i+1;
if(ii6)ii=0;
break;
}
}
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf("%s",menutext[i]);
gotoxy(ii*10+5,1);
textbackground(BLACK);
textcolor(WHITE);
cprintf("%s",menutext[ii]);
i=ii;
key=bioskey(0);
if(key==ESC)
{
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf("%s",menutext[i]);
break;
}
if(key==ENTER)
{
switch(i)
{
case 0:head=order(head);break;
case 1:head=insert(head);break;
case 2:change(head,choose);break;
case 3:head=del(head,choose);break;
case 4:search(head);break;
case 5:stat(head);break;
case 6:save(head);break;
}
menuinit();
printrecord(head);
break;
}
}
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
return(head);
}
int aboutpro()
{
int i;
window(58,4,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
putch(218);
for(i=3;i20;i++)putch(196); /*输出上边框水平线*/
putch(191);
for(i=2;i20;i++)
{
gotoxy(1,i);putch(179); /*输出左垂直线*/
gotoxy(19,i);putch(179); /*输出右垂直线*/
}
gotoxy(1,19);putch(192);
for(i=3;i20;i++)putch(196);
putch(217);
gotoxy(3,4);cputs("Version:V2.0");
gotoxy(3,6);cputs("Author:");
gotoxy(3,7);cputs("Zhang Shou Song");
gotoxy(5,9);cputs("NEU C.S. 053");
window(1,1,80,1);
}
struct student *order(struct student *head)
{
struct student *p,*q,*t,*h1;
h1=head-next;
head-next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1-next;
p=head;
q=head;
while(t-scorep-scorep!=NULL)
{
q=p;
p=p-next;
}
if(p==q)
{
t-next=p;
head=t;
}
else
{
t-next=p;
q-next=t;
}
}
return head;
}
struct student *insert(struct student *head)
{
struct student *p0,*p1,*p2;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
while(1)
{
clrscr();
p0=head;
p1=(struct student *)malloc(LEN);
gotoxy(5,1);printf("Insert");
gotoxy(1,3);printf("Quit by num=0");
gotoxy(1,5);printf("number:");
scanf("%ld",p1-num);
if(p1-num==0)break;
gotoxy(1,6);printf("name:");
scanf("%s",p1-name);
gotoxy(1,7);printf("sex:");
scanf("%s",p1-sex);
do{
gotoxy(1,8);printf("score:");
scanf("%d",p1-score);
}while(p1-score0||p1-score100);
if(head==NULL)
{
head=p1;
p1-next=NULL;
}
else
{
while((p1-scorep0-score)(p0-next!=NULL))
{
p2=p0;
p0=p0-next;
}
if(p1-score=p0-score)
{
if(head==p0)head=p1;
else p2-next=p1;
p1-next=p0;
}
else
{
p0-next=p1;
p1-next=NULL;
}
}
n++;
}
aboutpro();
clrscr();
return(head);
}
void printrecord(struct student *head)
{
struct student *p;
int i,j;
window(3,7,51,21);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
p=head;
for(i=1;ix;i++) /*处理分页*/
for(j=0;j15;j++)
p=p-next;
y=1;max=1;
if(head!=NULL)
do
{ gotoxy(3,y);
printf("%-8d%-9ld%-16s%-8s%-7d\n",y+(x-1)*15,p-num,p-name,p-sex,p-score);
p=p-next;
y++;
max=y-1;
if(y15)
{
max=15;
break;
}
}while(p!=NULL);
window(2,3,78,23);
}
void searchresult(struct student *p)
{
window(4,7,50,20);
textbackground(BLUE);
textcolor(YELLOW);
if(y==1)clrscr();
gotoxy(2,y);
printf("%-8d %-9ld%-16s%-8s%-7d\n",y,p-num,p-name,p-sex,p-score);
}
struct student *chooserecord(struct student *head)
{
int locate=1,key;
gotoxy(2,locate+4);putch(16);gotoxy(2,locate+4);
for(;;)
{
switch(key)
{
case UP:
{
locate--;
if(locate1n=15)locate=max;
if(locate1n15)
{
x--;
if(x1)x=n/15+1;
printrecord(head);
locate=max;
}
break;
}
case DOWN:
{
locate++;
if(locatemaxn=15)locate=1;
if(locatemaxn15)
{
x++;
if(x(n/15+1))x=1;/*判断是否到达页尾*/
locate=1;
printrecord(head);
}
break;
}
case F10:{head=menu(head,locate+(x-1)*15);break;}
}
putch(0);
gotoxy(2,locate+4);putch(16);gotoxy(2,locate+4);
key=bioskey(0);
if(key==ESC)save(head);
}
}
struct student *print(struct student *head)
{
window(2,3,78,23);
gotoxy(3,1);printf("******************** Student's Score Manage System **********************\n");
gotoxy(3,2);printf("| record | number | name | sex | score |\n");
gotoxy(3,3);printf("|--------|--------|---------------|-------|-------|\n");
printrecord(head);
chooserecord(head);
}
struct student *change(struct student *head,int choose)
{
struct student *p;
long stunum;
int i,key;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(5,1);printf("Change");
p=head;
for(i=1;ichoose;i++)p=p-next;
gotoxy(1,3);printf("input new name:");
gotoxy(1,4);scanf("%s",p-name);
gotoxy(1,5);printf("input new sex:");
gotoxy(1,6);scanf("%s",p-sex);
gotoxy(1,7);printf("input new score:");
gotoxy(1,8);scanf("%d",p-score);
gotoxy(2,10);printf("Change Success!\n");
getch();
aboutpro();
clrscr();
return(head);
}
struct student *del(struct student *head,int choose)/*删除记录*/
{
struct student *p,*q;
long stunum;
int i;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
q=p=head;
for(i=1;ichoose;i++)
{
q=p;
p=p-next;
}
gotoxy(5,1);printf("Delete");
gotoxy(1,3);printf("Are you sure");/*确认信息*/
gotoxy(2,4);printf(" to delete it?");
if(chooseab(62,11,"Yes","No")==1)
{
if(p==head)head=p-next;
else q-next=p-next;
free(p); /*释放内存*/
n--;
}
aboutpro();
clrscr();
return(head);
}
struct student *search(struct student *head)
{
struct student *p;
long stunum;
char *stuname;
int i,ii,score1,score2,key;
char *searchwith[]={"num","name","score"};
y=1;max=0;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
textbackground(BLUE);
textcolor(YELLOW);
for(i=1;i3;i++)
{
gotoxy(i*5+2,2);
cprintf("%s",searchwith[i]);
}
gotoxy(2,2);
textbackground(RED);
textcolor(WHITE);
cprintf("%s",searchwith[0]);
i=0;ii=0;
key=0;
while (1)
{
switch(key)
{
case LEFT:
{
ii=i-1;
if(ii0)ii=2;
break;
}
case RIGHT:
{
ii=i+1;
if(ii2)ii=0;
break;
}
}
gotoxy(i*5+2,2);
textbackground(BLUE);
textcolor(YELLOW);
cprintf("%s",searchwith[i]);
gotoxy(ii*5+2,2);
textbackground(RED);
textcolor(WHITE);
cprintf("%s",searchwith[ii]);
i=ii;
key=bioskey(0);
if(key==ESC)
{
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf("%s",searchwith[i]);
break;
}
if(key==ENTER)
{
switch(i)
{
case 0:
{
gotoxy(1,5);printf("number:");
scanf("%ld",stunum);
p=head;
while(1)
{
while(p-num!=stunump!=NULL)p=p-next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p-next;
}
}
break;
}
case 1:
{
gotoxy(1,5);printf("name:");
gotoxy(1,6);scanf("%s",stuname);
p=head;
while(1)
{
while(strstr(p-name,stuname)==NULLp!=NULL)p=p-next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p-next;
}
}
break;
}
case 2:
{
gotoxy(1,5);printf("from score1:");scanf("%d",score1);
gotoxy(1,6);printf("to score2:");scanf("%d",score2);
p=head;
while(1)
{
while((p-scorescore1||p-scorescore2)p!=NULL)p=p-next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p-next;
}
}
break;
}
}
break;
}
}
if(y==1){window(59,5,74,20);gotoxy(4,7);printf("Not Found");}
getch();
aboutpro();
clrscr();
}
void stat(struct student *head)
{
int i,j,begin=0,end=0,stutotal[5]={0,0,0,0,0};
int pointx1=50,pointy1=281,pointx2,pointy2;
char str[5];
struct student *p;
p=head;
for(i=0;in;i++)
{
if(p-score=0p-score60)stutotal[0]++;
else if(p-score=60p-score70)stutotal[1]++;
else if(p-score=70p-score80)stutotal[2]++;
else if(p-score=80p-score90)stutotal[3]++;
else if(p-score=90p-score=100)stutotal[4]++;
p=p-next;
}
init();
for(i=0;i4;i++) /*根据比例画扇形*/
{
setfillstyle(1,i+1);
begin=end; end=end+360*stutotal[i]/n;
pieslice(200,180,begin,end,100);
if(end==360)break;
}
if(end!=360)
{ setfillstyle(1,5);
pieslice(200,180,end,360,100);
}
rectangle(340,90,560,272);
settextstyle(0,0,1);
outtextxy(350,100,"color");/*显示图示的说明*/
outtextxy(410,100,"range");
outtextxy(500,100,"number");
outtextxy(400,132,"0 to 60:");
outtextxy(400,162,"60 to 70:");
outtextxy(400,192,"70 to 80:");
outtextxy(400,222,"80 to 90:");
outtextxy(400,252,"90 to 100:");
outtextxy(420,292,"total:");
sprintf(str,"%d",n);
outtextxy(520,292,str);
for(i=0;i5;i++)
{
sprintf(str,"%d",stutotal[i]);
outtextxy(520,132+i*30,str);
}
for(i=1;i=5;i++)
{
setfillstyle(1,i);
bar(350,100+i*30,380,110+i*30);
}
getch();
cleardevice();
setfillstyle(1,0); /*画折线图*/
bar(100,80,300,280);
line(50,281,280,281);
line(50,281,50,80);
outtextxy(60,285,"0-60");
outtextxy(105,285,"60-70");
outtextxy(150,285,"70-80");
outtextxy(195,285,"80-90");
outtextxy(250,285,"90-100");
for(i=0;i5;i++)
{ setfillstyle(1,i+1);
pointx2=pointx1+45; pointy2=280-200*stutotal[i]/n;
line(45,pointy2,50,pointy2);
sprintf(str,"%d",stutotal[i]);
outtextxy(30,pointy2,str);
line(pointx1,pointy1,pointx2,pointy2);
pointx1=pointx2;pointy1=pointy2;
}
line(45,80,50,80);
sprintf(str,"%d",n);
outtextxy(30,80,str);
outtextxy(30,280,"0"); getch();
closegraph();
screeninit();
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(3,1);printf("******************** Student's Score Manage System **********************\n");
gotoxy(3,2);printf("| record | number | name | sex | score |\n");
gotoxy(3,3);printf("|--------|--------|---------------|-------|-------|\n");
aboutpro();
clrscr();
}
void save(struct student *head)
{
FILE *fp;
int i;
struct student *p;
p=head;
fp=fopen("c:\\studin.txt","wb");
fprintf(fp,"zhangshousong"); /*将验证字符串写入文件*/
fprintf(fp,"\r\n"); /*将换行符号写入文件*/
fprintf(fp,"%d",n); /*将记录数写入文件*/
fprintf(fp,"\r\n"); /*将换行符号写入文件*/
for(i=0;in;i++)
{
fprintf(fp,"%-20ld%-30s%-10s%-10d",p-num,p-name,p-sex,p-score);/*格式写入记录*/
fprintf(fp,"\r\n"); /*将换行符号写入文件*/
p=p-next;
}
fclose(fp);
exit(0);
}
struct student *readdata()
{
FILE *fp;
int i;
char str[20]="zhangshousong",str2[20];
struct student *p,*q,*head;
p=(struct student *)malloc(LEN);
head=p;
if((fp=fopen("c:\\studin.txt","rb"))==NULL)/*打开文件*/
{
head=NULL;
n=0;
}
else
{
fscanf(fp,"%s",str2);
if(strcmp(str,str2)!=0)head=NULL;/*判断文件是不是合法的*/
else
{
fscanf(fp,"%d",n); /*读入记录数*/
if(n==0)head=NULL;
else
{
for(i=0;in;i++)
{
fscanf(fp,"%20ld%30s%10s%10d",p-num,p-name,p-sex,p-score); /*按格式读入记录*/
p-next=(struct student *)malloc(LEN);
q=p;
p=p-next;
}
q-next=NULL;
}
}
}
fclose(fp);
return head;
}
void main()
{
struct student *head;
int key=0,i;
init();
closegraph();
clrscr();
head=readdata();
menuinit();/*菜单初始化*/
screeninit();
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
aboutpro();
head=print(head);
}
C语言设计案例张传学P196编写结构体链表
#includestdio.h
#includestring.h
#includestdlib.h
typedef struct Node
{
char name[20];
char phone[20];
char cell[20];
char mail[50];
struct Node *next;
}Node;
void show(Node *h)
{
Node *p;
for(p=h;p!=NULL;p=p-next)
{
printf("%s,%s,%s,%s\n",p-name, p-phone, p-cell, p-mail);
}
}
Node *Insert(Node *h)
{
Node *p=(Node *)malloc(sizeof(Node));
scanf("%s%s%s%s", p-name, p-phone, p-cell, p-mail);
if(h==NULL)
{
p-next=NULL;
}
else
{
p-next=h;
}
return p;
}
Node *find(Node *h, char *name)
{
Node *r = NULL;
while(h)
{
if(strcmp(h-name, name) == 0)
{
r = h;
break;
}
h = h-next;
}
return r;
}
void destroy(Node *h)
{
Node *p;
while(h)
{
p = h;
h = h-next;
free(p);
}
}
int main()
{
Node *h=NULL;
int c = -1;
do
{
printf("choose a function:\n");
printf("1:insert a new node\n");
printf("2:print all nodes\n");
printf("3:find a node\n");
printf("4:exit\n");
scanf("%d",c);
switch(c)
{
case 1:
h = Insert(h);
break;
case 2:
show(h);
break;
case 3:
{
Node *r;
char name[20];
scanf("%s", name);
r = find(h, name);
if(r)
printf("%s,%s,%s,%s\n",r-name, r-phone, r-cell, r-mail);
else printf("not find\n");
break;
}
case 4:
break;
default:
printf("unknow command\n");
break;
}
}while(c != 4);
destroy(h);
return 0;
}