C语言简单编程题.docx

上传人:b****6 文档编号:12663977 上传时间:2023-06-07 格式:DOCX 页数:21 大小:32.48KB
下载 相关 举报
C语言简单编程题.docx_第1页
第1页 / 共21页
C语言简单编程题.docx_第2页
第2页 / 共21页
C语言简单编程题.docx_第3页
第3页 / 共21页
C语言简单编程题.docx_第4页
第4页 / 共21页
C语言简单编程题.docx_第5页
第5页 / 共21页
C语言简单编程题.docx_第6页
第6页 / 共21页
C语言简单编程题.docx_第7页
第7页 / 共21页
C语言简单编程题.docx_第8页
第8页 / 共21页
C语言简单编程题.docx_第9页
第9页 / 共21页
C语言简单编程题.docx_第10页
第10页 / 共21页
C语言简单编程题.docx_第11页
第11页 / 共21页
C语言简单编程题.docx_第12页
第12页 / 共21页
C语言简单编程题.docx_第13页
第13页 / 共21页
C语言简单编程题.docx_第14页
第14页 / 共21页
C语言简单编程题.docx_第15页
第15页 / 共21页
C语言简单编程题.docx_第16页
第16页 / 共21页
C语言简单编程题.docx_第17页
第17页 / 共21页
C语言简单编程题.docx_第18页
第18页 / 共21页
C语言简单编程题.docx_第19页
第19页 / 共21页
C语言简单编程题.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C语言简单编程题.docx

《C语言简单编程题.docx》由会员分享,可在线阅读,更多相关《C语言简单编程题.docx(21页珍藏版)》请在冰点文库上搜索。

C语言简单编程题.docx

C语言简单编程题

CompanyDocumentnumber:

WTUT-WT88Y-W8BBGB-BWYTT-19998

 

C语言简单编程题

题目:

1.编写一个程序,要求输入一个ASCII码值(比如66),

然后输出相应的字符(比如输入66,输出B)。

#include<>

#include<>

main()

{

inti,j;

charc;

printf("输入一个ASCII码值:

\n");

scanf("%d",&i);

while

(1)

{

if((i<=255)&&(i>=0))

{

printf("输出相对应的字符:

\n");

c=toascii(i);

printf("%c\n",c);

printf("继续输入一个数:

\n");

scanf("%d",&i);

}

else

{

printf("结束");

break;

}

}

return0;

}

2.功能描述:

编写函数,实现对10个整数按由小到达排序,在主函数中调用此函数。

*要求:

完成至少3个函数分别实现插入排序(InsertionSort)、选择排序、冒泡排序()

#include<>

#defineM10

voidinsert(inta[],intn);

voidchoice(inta[],intn);

voidBubbleSort(inta[],intn);

voidprint(inta[],intn);

voidmain()

{

inta[M],i;

printf("请输入%d个整数:

\n",M);

for(i=0;i

{

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

}

printf("请输出%d个整数:

\n",M);

for(i=0;i

{

printf("%d",a[i]);

}

printf("\n");

insert(a,M);知head指向一个带头结点的单向链表,链表中每个结点包含数据域(data)和指针域(next)。

请编写函数实现如图所示链表逆置。

要求:

不开辟任何额外的链表结点空间,实现链表的就地逆置

考察:

对链表结构的基本操作

#include<>

#include<>

#include<>

#include<>

#defineLENsizeof(structsth)

structsth

{

chara[20];

structsth*next;

};

structsth*creat(void);

structsth*exchange(structsth*creat);

voidprint(structsth*head);

voidfree1(structsth*head);

voidmain(void)

{

structsth*head;

printf("请输入链表:

\n");

head=creat();

printf("请输出链表:

\n");

print(head);

exchange(head);

printf("输出逆置的链表:

\n");

print(head);

free1(head);

}

structsth*creat(void)

{

structsth*head=NULL;

structsth*p1,*p2;

charinput[10]={'\0'};

两个链表a和b,设结点中包含学号和姓名。

从a链表中删去与b链表中相同学号的结点。

#include<>

#include<>

#include<>

#defineLENsizeof(structstudent)

structstudent

{

charnum[10];

charname[10];

structstudent*next;

};

structstudent*creat(void);

voidprint(structstudent*heada);

voidfree1(structstudent*heada);

structstudent*found(structstudent*heada,structstudent*headb);

intmain(void)

{

structstudent*head1,*head2;

printf("请输入链表a:

\n");

head1=creat();

print(head1);

printf("请输入链表b:

\n");

head2=creat();

print(head2);

printf("输出删除后的链表a:

\n");

found(head1,head2);

free1(head1);

free1(head2);

return(0);

}

structstudent*creat(void)

{

intn=0,i;

charnum[2]={'\0'};

structstudent*head;

structstudent*p1,*p2;

head=NULL;

p1=p2=(structstudent*)malloc(LEN);

printf("请分别输入学号和姓名:

例:

1204王岩\n");

scanf("%s%s",p1->num,p1->name);

while(p1->num!

=NULL&&strcmp("q",p1->num))

{

fflush(stdin);

n=n+1;

if(n==1)

{

head=p1;

}

else

{

p2->next=p1;

}

p2=p1;

p1=(structstudent*)malloc(LEN);

printf("请分别输入学号和姓名:

\n");

scanf("%s%s",p1->num,p1->name);

}

p2->next=NULL;

return(head);

}

structstudent*found(structstudent*heada,structstudent*headb)

{

structstudent*p1,*p2;

structstudent*p3,*p4;

intflag;

if(heada==NULL)

{

printf("end");

}

p1=p2=heada;

p3=p4=headb;

while(p1!

=NULL)

{

flag=0;

while(p3!

=NULL)

{

if(strcmp(p1->num,p3->num)==0)

{

flag=1;

if(p1==heada)

{

heada=p1->next;

free(p1);

p1=heada;

}

else

{

p2->next=p1->next;

free(p1);

p1=p2->next;

}

break;

}

else

{

p3=p3->next;

}

}

if(flag==0)

{

p1=p1->next;

}

p3=headb;

}

print(heada);

return(0);

}

voidprint(structstudent*heada)

{

structstudent*p1;

p1=heada;

while(p1!

=NULL)

{

printf("%s,%s\n",p1->num,p1->name);

p1=p1->next;

}

}

voidfree1(structstudent*heada)

{

structstudent*p1,*p2;

p2=p1=heada;

while(p1!

=NULL)

{

p2=p1->next;

free(p1);

p1=p2;

}

}

题目:

5.请编写这样一个程序,要求反复输入您的年龄,

然后显示该年龄已经过了多少秒

当数据溢出时或者输入非法时发出报警声,输入q则退出程序

(注意对常数的定义,年按每年365天计算)。

#include<>

#include<>

#defineNUM_YEAR365

intmain()

{

intsecond,s,i,a;

printf("输入年龄:

\n");

scanf("%d",&a);

while

(1)

{

if((a>=1)&&(a<=68))

{

s=NUM_YEAR*24*60*60;

second=s*a;

printf("该年龄过的秒数:

\n");

printf("%d\n",second);

printf("继续输入年龄:

\n");

scanf("%d",&a);

}

elseif(a>68)

{

printf("\a");

}

elseif(a='q')

{

printf("out");

break;

1)编写一段程序,实现统计一个或多个文件的行数、字数和字符数,程序中包括错误检查,

以确定参数数目是否正确和是否能打开文件。

如果不能打开文件,程序要报告这一情况然后继续处理下一个文件。

要求说明:

a)一个行由一个换行符限定,一个字由空格分隔(包括空白符、制表符和换行符),

b)字符是指文件中的所有字符。

要求程序设置3个任选的参数,让用户指定他所要的统计或操作。

它们是:

1统计文件行数

w统计文件字数

c统计文件字符数

若用户未指定任选的参数,则默认是lwc都有。

c)运行本程序时的参数按以下格式给出:

-l-w-c文件1文件2...文件n

其中,参数l、w、c的出现与否和出现顺序任意,或任意组合在一起出现,

如:

-lwc,-cwl,-lw,-wl,-lc,-cl,-cw等。

程序依次打开每个文件,

然后报告每个文件中相应的统计信息。

文件名和统计信息一起报告。

考察:

(1)程序的命令行参数提供

(2)文件的基本操作

(3)错误检测和程序运行控制。

(2)在

(1)的基础上可以添加每个文件中统计特定字符(串)和替换功能。

a)程序参数运行如下:

f查找文件中的某个字符(串)

r替换文件中的某个字符(串)为其他

b)运行本程序时的参数按以下格式给出:

-f字符(串)文件1文件2…文件n

程序依次打开每个文件,然后报告每个文件中该字符(串)出现的次数。

文件名和字符(串)本身也与计数值一起报告。

-r字符(串)1字符(串)2文件1文件2…文件n

程序依次打开每个文件,然后依次将每个文件中字符(串)1全部替换为字符(串)2

#include<>

#include<>

intfile1(char*p);

intfile2(char*p);

intfile3(char*p);

intmain(intargc,char*argv[])

{

intcount,i,lflag=0,wflag=0,cflag=0;

char*t;

printf("Thecommandlinehas%darguments:

\n",argc);

if(argc<2)

{

printf("请重新输入:

\n");

return0;

}

elseif(argc>=2)

{

if(*argv[1]=='-')

{

for(count=1;count

{

for(t=argv[count];*t!

='\0';t++)

{

if(*t=='l')

{

lflag=1;

}

if(*t=='w')

{

wflag=1;

}

if(*t=='c')

{

cflag=1;

}

}

}

for(count=2;count

{

if(*argv[count]!

='-')

{

if(lflag==1)

{

file1(argv[count]);

}

if(wflag==1)

{

file2(argv[count]);

}

if(cflag==1)

{

file3(argv[count]);

}

else

{

printf("错误\n");

}

}

}

}

elseif(*argv[1]!

='-')

{

for(count=2;count

{

if(*argv[count]=='-')

{

printf("参数输入有误\n");

return0;

}

}

for(count=1;count

{

file1(argv[count]);

file2(argv[count]);

file3(argv[count]);

}

}

}

return0;

}

intfile1(char*p)

{

FILE*fp;

charch;

intflag=0,count=0;

if((fp=fopen(p,"r"))==NULL)

{

printf("cannotopenfile\n");

return0;

}

ch=fgetc(fp);

while(ch!

=EOF)

{

if(ch=='\n')

{

flag=0;

}

elseif(flag==0)

{

count++;

flag=1;

}

ch=fgetc(fp);

}

printf("输出文件中的行数:

\n");

printf("%d\n",count);

}

intfile2(char*p)

{

FILE*fp;

charch;

intflag=0,count=0;

if((fp=fopen(p,"r"))==NULL)

{

printf("cannotopenfile\n");

return0;

}

ch=fgetc(fp);

while(ch!

=EOF)

{

if(ch==''||ch=='\n'||ch=='\t')

{

flag=0;

}

elseif(flag==0)

{

count++;

flag=1;

}

ch=fgetc(fp);

}

printf("输出文件中的字数:

\n");

printf("%d\n",count);

}

intfile3(char*p)

{

FILE*fp;

charch;

intflag=0,count=0;

if((fp=fopen(p,"r"))==NULL)

{

printf("cannotopenfile\n");

return0;

}

ch=fgetc(fp);

while(ch!

=EOF)

{

count++;

ch=fgetc(fp);

}

printf("输出文件中的字符数:

\n");

printf("%d\n",count);

}

编写一个函数。

该函数接受下列参数:

一个int数组的名称,数组大小和一个代表选取次数的值。

然后函数从数组中随机选择指定数量的元素并打印他们。

每个元素最多选择一次(模拟抽奖或挑选陪审成员)。

另外,请使用time()或其它类似的函数生成种子。

以便srand()函数初始化随机数生成器rand()。

编写一个简单的程序测试该函数。

#include<>

#include<>

#include<>

voidarray(intb[],intn,intm);

intmain(void)

{

inta[10],i,j,k;

printf("输入10个整数:

\n");

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

{

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

}

printf("输出10个整数:

\n");

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

{

printf("%d",a[i]);

}

printf("\n");

printf("输入10以内的整数:

\n");

scanf("%d",&j);

for(k=0;k<20;k++)

{

if(j>=10)

{

printf("请重新输入,请输入10以内的整数:

\n");

scanf("%d",&j);

}

}

array(a,10,j);

return0;

}

voidarray(intb[],intn,intm)

{

intj,i,l,flag;

intc[m];

srand(time(0));/*播种子返回的是一个值*/

for(j=0;j

{

flag=0;

i=(int)*rand()/(RAND_MAX+);/*产生10以内的随机整数*/

c[j]=i;

for(l=0;l

{

if(c[j]==c[l])

{

flag=1;

break;

}

}

if(flag==0)

{

printf("输出下标:

%d,输出元素:

%d\n",c[j],b[c[j]]);

j++;

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2