本文目錄一覽:
- 1、求C語言練習題答案
- 2、求一道C語言編程答案
- 3、C語言編程,急求答案。。。。
- 4、20分求c語言編程題答案
- 5、c語言 請寫出詳細答案
求C語言練習題答案
二、填空題
1. ___變數__是指在程序運行過程中,值可以發生變化的量。
2.C語言是一種____區分_(區分/不區分)字母大小寫的語言,Password和password在C語言中是兩個___不同__(相同/不同)的標識符。
3.下面的轉義字元分別表示:
』\b』__退格___,』\n』__換行___,』\t』__水平製表___,』\」』 __雙引號字元「”」___
4.下面常量的類型分別為:
1.5E12__浮點型___,1288L _整型____,』a』__字元___,”a”__字元串___
08 __整形___,』\007』__整型___
5. C語言源程序一定要有而且只有一個__主函數___函數,程序執行時也從它開始。
6. 結構化程序設計主要有___順序__、__選擇___和__循環___三種基本結構。
三、判斷題
1. C語言不是一種結構化程序設計語言。(錯)
2. C語言是弱類型語言,為了類型轉換的方便,對類型的要求很不嚴格,在許多情況下不做類型檢查。( 對 )
3. C語言既具有高級語言的特點,同時也具有部分低級語言的特點。(對)
4. C源程序可以由多個文件組成,每個文件中都可有一個主函數main()。( 錯 )
5. C程序從main()函數開始執行,因此主函數main()必須放在程序的開頭。(錯)
6. 命名標識符時字元個數不得超過6個。(錯)
7. 對於同一個數據種類型,變數值越大,它所佔內存的位元組數越多。(錯)
8. 定義一個變數時必須指出變數類型,但是不一定必須給變數初始化。( 對 )
9. “”表示一個空字元串常量,』』表示一個空字元常量。( 對 )
10. 一個表達式中出現多種數值型數據類型,運算前先進行類型轉換。( 對 )
11. 浮點數不能作自增或自減運算,只有整型才可作自增或自減運算。( 錯 )
12. 已知a、b是整型變數,則表達式a=3,2+4,b=6,7+8是一個逗號表達式。( 對 )
四、問答題
1. 寫出下面表達式的值
(1)12/3*9=36
(2)int i=3,j;
i*=2+5;
i/=j=4;
i+=(j%2);
i、j的最終結果是多少?i=5,j=4
(3)int a=3,b,c;
b=(a++)+(a++)+(a++);
c=(++a)+(++a)+(++a);a、b、c的最終結果是多少?a=9,b=9,c=25
2. 下列表達式是否正確?若正確,表達式的值是什麼?
21/2=10 21/2.0= 10.5 21.0/2=10.5 21%2=1 21.0%2 21%2.0 18%15=3 15%18=15 3%15=3
五、編程題
編寫程序,在屏幕上輸出如下信息:
===================
= I am a student. =
===================
#include”stdio.h「
void main()
{
printf(“= I am a student. =”);
}
求一道C語言編程答案
樓主,樓上不太會說話,不要理,等等我貼出代碼。沒有其他語言基礎的C語言初學者起步是慢了一點,但是要渣油~
#include stdio.h
int main()
{
int i, n, t1 = 1, t2 = 1, nextTerm,ct=0;
//printf(“輸出幾項: “);
//scanf(“%d”, n);
n=20;
printf(“斐波那契數列: \n”);
for (i = 1; i = n; ++i)
{
printf(“%d “, t1);ct+=1;
if(ct%5==0)printf(“\n”);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
//推薦個學C的網站(我自己就是在這學的):m.runoob.com/cprogramming/
C語言編程,急求答案。。。。
第一個
#include stdio.h
#include math.h
double cal(double e)
{
int c=1;
double ret = 0;
double cur = 1;
double t = 1;
while(cur e)
{
ret += cur;
c ++;
t *= c;
cur = 1.0/c;
}
return ret;
}
int main()
{
int n, i;
double e;
printf(“input n:”);
scanf(“%d”, n);
for(i = 1; i = n; i ++)
{
e = pow(10, -i);
printf(“e=%lg result = %lg\n”, e, cal(e));
}
return 0;
}
第二個
#include stdio.h
int count(char *s, char c)
{
int ret = 0;
int i;
for(i = 0; s[i]; i ++)
if(s[i] == c) ret ++;
return ret;
}
void delete(char *s, char c)
{
int i,j;
for(i = j = 0; s[i]; i ++)
if(s[i] != c) s[j++] = s[i];
s[j] = 0;
}
int main()
{
char str[1024];
char c;
printf(“input str:”);
gets(str);
printf(“input char:”);
c = getchar();
printf(“count = %d\n”, count(str,c));
delete(str,c);
printf(“after delete:%s\n”, str);
return 0;
}
供參考 望採納
20分求c語言編程題答案
我這裡有一些程序(dev-c++編譯器),雖然並不是你所想要的完美答案,但是其中的一些代碼,希望對你有所幫助:
No.1 有5個學生,每個學生有3門課的成績,從鍵盤輸入數據(包括學生號,姓名,3門課的成績),計算出平均成績,將原有數據和計算出的平均分數存放在磁碟文件stud中。
程序:
#includestdio.h
#includestdlib.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;i5;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;j3;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;i5;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;i5;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中。
程序:
#includestdio.h
#includestdlib.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;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
printf(“\n”);
fclose(fp);
n=i;
for(i=0;in;i++)
for(j=i+1;jn;j++)
if(st[i].avest[j].ave)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
printf(“\nNow:”);
fp=fopen(“stu_sort”,”w”);
for(i=0;in;i++)
{
fwrite(st[i],sizeof(struct student),1,fp);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;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門課程成績,程序先計算新插入學生的平均成績,然後將它按平均成績高低順序插入,插入後建立一個新文件。
程序:
#includestdio.h
#includestdlib.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;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
n=i;
for(t=0;st[t].aves.avetn;t++);
printf(“\nNow:\n”);
fp1=fopen(“sort1.dat”,”w”);
for(i=0;it;i++)
{
fwrite(st[i],sizeof(struct student),1,fp1);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;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;in;i++)
{
fwrite(st[i],sizeof(struct student),1,fp1);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;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的結果仍存入原有的文件而不另建立新文件。
程序:
#includestdio.h
#includestdlib.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;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
n=i;
for(t=0;st[t].aves.avetn;t++);
printf(“\nNow:\n”);
fp=fopen(“stu_sort”,”w”);
for(i=0;it;i++)
{
fwrite(st[i],sizeof(struct student),1,fp);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;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;in;i++)
{
fwrite(st[i],sizeof(struct student),1,fp);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
printf(“\n”);
fclose(fp);
system(“pause”);
return 0;
}
對鏈表的綜合操作:
程序:
#includestdio.h
#includestdlib.h
#includemalloc.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-nump1-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-nump1-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語言 請寫出詳細答案
#include “stdio.h”
int main(int argc,char *argv[]){
int N,i;
double sum;
printf(“Please enter N(int N0)…\nN=”);
if(scanf(“%d”,N)!=1 || N1){
printf(“Input error, exit…\n”);
return 0;
}
for(sum=0,i=1;i=N;sum+=1.0/(i+i++-1));
printf(“sum = %f\n”,sum);
return 0;
}
運行樣例:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/246131.html