当前位置:首页 » 编程语言 » 求c语言编程题及答案
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

求c语言编程题及答案

发布时间: 2022-09-01 17:10:36

㈠ 7道c语言编程题,急求答案

#include <stdio.h>
void main()
{
int i,j;
int sum=0; //定义对角线的和
int a[100][100],n;
printf("输入是n*n矩阵的n值:");
scanf("%d",&n);
printf("输入一个%d*%d矩阵:",n,n);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]); //按矩阵格式输入
}
for(i=0;i<n;i++)
{
sum=sum+a[i][i]; //求出对角线数之和
}
printf("矩阵对角线之和为: %d\n",sum);

}

㈡ 简单的c语言编程题6(附答案,求不同的答案!!)

你老师编的很好了,我想了很久,本想用其它原理来做,后来发现,要么太麻烦,要么不太好用.我想还是这个好,就把FOR改成了WHILE循环,有点偷工取巧的意思.哈哈,你先将就用吧!
#include<stdio.h>
int fun(char s[], int c)
{ int i=0, j=0;
while( s[i]!='\0')
{
if(s[i]!=c)
s[j++]=s[i];
i++;
}
s[j]=0;
}
void main()
{
int i;
char s[50]="turbo c and borland c",c;
scanf("%c",&c);
fun(s,c);
for(i=0;s[i]!='\0';i++)
printf("%c",s[i]);
}

㈢ 20分求c语言编程题答案

我这里有一些程序(dev-c++编译器),虽然并不是你所想要的完美答案,但是其中的一些代码,希望对你有所帮助:

No.1 有5个学生,每个学生有3门课的成绩,从键盘输入数据(包括学生号,姓名,3门课的成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
程序:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char num[10];
char name[8];
int score[3];
float ave;
}stu[5];

int main()
{
int i,j,sum;
FILE *fp;
for(i=0;i<5;i++)
{
printf("\ninput score of student %d:\n",i+1);
printf("No.:");
scanf("%s",stu[i].num);
printf("name:");
scanf("%s",stu[i].name);
sum=0;
for(j=0;j<3;j++)
{
printf("score %d:",j+1);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].ave=sum/3.0;
}

fp=fopen("stud","w");
for(i=0;i<5;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);

fp=fopen("stud","r");
for(i=0;i<5;i++)
{
fread(&stu[i],sizeof(struct student),1,fp);
printf("\n%s,%s,%d,%d,%d,%6.2f\n",stu[i].num,stu[i].name,
stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
}
system("pause");
return 0;
}

No.2 将运行No.1程序所得到的stud文件中的学生数据按平均分进行排序处理,将已排序的学生数据存入一个新文件stu_sort中。
程序:
#include<stdio.h>
#include<stdlib.h>
#define N 10
struct student
{
char num[10];
char name[8];
int score[3];
float ave;
}st[N],temp;

int main()
{
FILE *fp;
int i,j,n;
if((fp=fopen("stud","r"))==NULL)
{
printf("can not open.");
exit(0);
}
printf("File 'stud':");
for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
n=i;

for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(st[i].ave<st[j].ave)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
printf("\nNow:");
fp=fopen("stu_sort","w");
for(i=0;i<n;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
system("pause");
return 0;
}

No.3 将题No.2已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。
程序:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char num[10];
char name[8];
int score[3];
float ave;
}st[10],s;

int main()
{
FILE *fp,*fp1;
int i,j,t,n;
printf("\nNo.:");
scanf("%s",s.num);
printf("name:");
scanf("%s",s.name);
printf("score1,score2,score3:");
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;

if((fp=fopen("stu_sort","r"))==NULL)
{
printf("can not open file.");
exit(0);
}
printf("Original data:\n");
for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}

n=i;
for(t=0;st[t].ave>s.ave&&t<n;t++);
printf("\nNow:\n");
fp1=fopen("sort1.dat","w");
for(i=0;i<t;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp1);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[t].ave);
}
fwrite(&s,sizeof(struct student),1,fp1);
printf("\n%8s%8s%8d%8d%8d%10.2f",s.num,s.name,s.score[0],
s.score[1],s.score[2],s.ave);

for(i=t;i<n;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp1);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
fclose(fp1);
system("pause");
return 0;
}

No.4 将No.3的结果仍存入原有的文件而不另建立新文件。
程序:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char num[10];
char name[8];
int score[3];
float ave;
}st[10],s;

int main()
{
FILE *fp;
int i,j,t,n;
printf("\nNo.:");
scanf("%s",s.num);
printf("name:");
scanf("%s",s.name);
printf("score1,score2,score3:");
scanf("%d,%d,%d",&s.score[0],&s.score[1],&s.score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;

if((fp=fopen("stu_sort","r"))==NULL)
{
printf("can not open file.");
exit(0);
}
printf("Original data:\n");
for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}

n=i;
for(t=0;st[t].ave>s.ave&&t<n;t++);
printf("\nNow:\n");
fp=fopen("stu_sort","w");
for(i=0;i<t;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[t].ave);
}
fwrite(&s,sizeof(struct student),1,fp);
printf("\n%8s%8s%8d%8d%8d%10.2f",s.num,s.name,s.score[0],
s.score[1],s.score[2],s.ave);

for(i=t;i<n;i++)
{
fwrite(&st[i],sizeof(struct student),1,fp);
printf("\n%8s%8s",st[i].num,st[i].name);
for(j=0;j<3;j++)
printf("%8d",st[i].score[j]);
printf("%10.2f",st[i].ave);
}
printf("\n");
fclose(fp);
system("pause");
return 0;
}

对链表的综合操作:
程序:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};

int n;

struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}

struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");
return(head);
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;p1=p1->next;
}
if(num==p1->num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
}
else printf("%ld not been found!\n",num);
return(head);
}

struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1) head=p0;
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}

int main()
{
struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
system("pause");
return 0;
}

㈣ 求几道简单C语言编程题答案

1.
#include
<stdio.h>
int
main()
{
int
y0,
m0,
d0,
y1,
m1,
d1,
age;
while
(
scanf("%d%d%d%d%d%d",
&y0,
&m0,
&d0,
&y1,
&m1,
&d1
)
){
age
=
y1
-
y0
-
1;
if
(
m1
>
m0
||
m1
==
m0
&&
d1
>=
d0
)
++age;
printf("年龄为:%d周岁!\n",
age);
}
return
0;
}
4.
#include
<stdio.h>
#include
<memory.h>
int
main()
{
char
p[500];
int
i,
count;
while
(
scanf("%s",
&p)
){
count
=
0;
for
(
i
=
0;
i
!=
strlen(p);
++i
)
if
(
p[i]
>=
'a'
&&
p[i]
<=
'z'
)
++count;
printf("%d\n",
count);
}
return
0;
}
2.
#include
<stdio.h>
int
main()
{
int
n;
while
(
scanf("%d",
&n)
){
if
(
(
n
&
1
)
==
0
)
printf("%d是偶数!\n",
n);
else
printf("%d,是奇数!\n",
n);
}
return
0;
}
第三题(用EFO结束)?EOF吧?EOF已经是文件尾,怎样输出结果?

㈤ C语言编程题,求答案+解释,谢谢大佬

(1)这个位置,因为在主函数之外,只有一行。一般来说是#include了其他的库,或者是#define了一些什么,或者是前向声明(但鉴于题目里扫视了一眼只有一个非主函数,所以不会是这个)。那我最大的倾向就是#include,接着我会直接看主函数
先学会看变量,可以看到题目里有三个变量X,Y1,Y2。和我们设置的变量不谋而合,接下来输入X的值,直接就可以算Y1,Y2。因为刚刚记得只看到了一个外函数,而且下面两个空是在形参上让我们填空,我就回头看了一眼题目,原来是函数的形式完全相同,只是改变了传进去的参数,第一个直接传x+8,第二个要传sinx,那两个答案就出来了,同时(1)的答案也出来了,因为sin()这个函数是在#include<math.h>里面的

接着剩下最后一个空,我们看到被调函数fun里面,这是一个返回类型float的函数,但是没有返回值,所以我们需要return 一些什么,很明显这里只有一个能return的,那就是y

综上
(1)#include<math.h>
(2)return y;
(3)x+8
(4)sin(x)

(在我写完之后看到3分钟前下面还有个答案,include用了""。需要说明的是,一般只有使用非库头文件,也就是自定义的头文件时,会选择"",这并不是一个好习惯,会不会引发报错可以自己尝试一下)

㈥ c语言编程题及答案

【4.1】已知银行整存整取存款不同期限的月息利率分别为:
0.315% 期限一年
0.330% 期限二年
月息利率 = 0.345% 期限三年
0.375% 期限五年
0.420% 期限八年
要求输入存钱的本金和期限,求到期时能从银行得到的利息与本金的合计。

【4.2】输入年份year和月month,求该月有多少天。判断是否为闰年,可用如下C语言表达式:year%4==0 && year0!=0 || year@0==0。若表达式成立(即表达式值为1),则year为闰年;否则,表达式不成立(即值为0),year为平年。

【4.3】编写一个简单计算器程序,输入格式为:data1 op data2。其中data1和data2是参加运算的两个数,op为运算符,它的取值只能是+、-、*、/。

【4.4】输入n值,输出如图所示矩形。

【4.5】输入n值,输出如图所示平行四边形。

【4.6】输入n值,输出如图所示高为n的等腰三角形。

【4.7】输入n值,输出如图所示高为n的等腰三角形。

【4.8】输入n值,输出如图所示高和上底均为n的等腰梯形。

【4.9】输入n值,输出如图所示高和上底均为n的等腰空心梯形。

【4.10】输入n值,输出如图所示边长为n的空心正六边型。

㈦ 求c语言编程题的答案

#include<stdio.h>

typedefstructdemo

{

intdata[10];

intlength;

}Demo;

voidInit(Demo*d)

{

d->length=0;

}


voidputValue(Demo*d)

{

intn=0;

intj=0;

while(1)

{

printf("请输入任意整数:");

scanf("%d",&n);

if(n==-1)

break;

d->data[j]=n;

d->length++;

j++;

}

}

voidvisitDemo(Demod){

printf("值为:");

for(inti=0;i<d.length;i++){

printf("%d,",d.data[i]);

}

printf(" ");

printf("长度为:%d",d.length);

}

intmain()

{

Demod;

Init(&d);

putValue(&d);

visitDemo(d);

return0;

}

测试结果:

望采纳,谢谢

㈧ 四道C语言编程题,急求答案

//第3题
#include <stdio.h>
#include <string.h>
int main(void)
{
char f_name[24];
char str[100];
printf("please enther the file name:");
scanf("%s",str);
sprintf(f_name,"%s.txt",str);
FILE *fp=fopen(f_name,"w+");
printf("please enther the string:");
scanf("%s",str);
fprintf(fp,"%s%d",str,strlen(str));
return 0;
}
//第4题
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int num;
char name[20];
int math;
int chin;
int eng;
int avg;
}student;
void cal_avg(student *stu)
{
int i=0;
for (i;i<5;i++)
{
stu[i].avg=(stu[i].chin+stu[i].math+stu[i].eng)/3;
}
}
int output(student *stu)
{
FILE *fp=fopen("stud.txt","w+");
if (fp==NULL)
{
printf("can't open file!");
return -1;
}
fwrite(stu,sizeof(student),5,fp);
return 1;
}
void input(student *stu)
{
int i=0;
for (i;i<5;i++)
{
stu[i].num=i+1;
printf("please enther the %dth student's name:",i+1);
scanf("%s",stu[i].name);
printf("please enther the %dth student's math grade:",i+1);
scanf("%d",&stu[i].math);
printf("please enther the %dth student's chin grade:",i+1);
scanf("%d",&stu[i].chin);
printf("please enther the %dth student's eng grade:",i+1);
scanf("%d",&stu[i].eng);
printf("/********************************************/\n");
}
}
int main(void)
{
student stu[5];
input(stu);
cal_avg(stu);
output(stu);
return 0;
}

㈨ 求这道c语言编程题的答案

部分测试样例