A. c語言程序設計
#include<stdio.h>
struct student /*定義一個學生類型結構體*/
{
long num;
char name[20];
float score1;
float score2;
float score3;
float total;
float aver;
};
main()
{
void input(struct student t[3]);/*定義input函數,輸入學生信息*/
void average(struct student v[3]);/*定義average函數,求每個學生總成績、平均成績和所有學生的總平均成績*/
void maximum(struct student r[3]);/*定義maximum函數,找出最高分的學生的數據*/
struct student s[3];
int i;
input(s);
printf("the information of student is:\n");
printf("num name score1 score2 score3 total aver\n");
for(i=0;i<3;i++)
printf("%ld %s %f %f %f %f %f\n",s[i].num,s[i].name,s[i].score1,s[i].score2,s[i].score3,s[i].total,s[i].aver);
average(s);
maximum(s);
printf("the information of student with the highest total score is:\n");
printf("%s %f\n",s[0].name,s[0].total);
}
void input(struct student t[3])
{
int i;
printf("please input student's information:\n");
for(i=0;i<3;i++)
{t[i].total=0;
t[i].aver=0;
scanf("%ld%s%f%f%f",&t[i].num,&t[i].name,&t[i].score1,&t[i].score2,&t[i].score3);
t[i].total=t[i].score1+t[i].score2+t[i].score3;
t[i].aver=t[i].total/3;
}
}
void average(struct student v[3])
{
int i;
float sum=0,average=0;
for(i=0;i<3;i++)
{v[i].total=v[i].score1+v[i].score2+v[i].score3;
v[i].aver=v[i].total/3;
sum+=v[i].aver;}
average=sum/3;
printf("the average score of all the students is :average=%f\n",average);
}
void maximum(struct student r[3])
{
int i;
float t;
for(i=0;i<3;i++)
if(r[i].total>r[0].total)
{t=r[0].total;
r[0].total=r[i].total;
r[i].total=t;
strcpy(r[0].name,r[i].name);
r[0].num=r[i].num;r[0].score1=r[i].score1;r[0].score2=r[i].score2;r[0].score3=r[i].score3;
r[0].aver=r[i].aver;
}/*將所有學生信息進行排序,總分高的放在第一組,輸出時只需輸出第一組學生信息*/
}
程序中限定了學生個數為3,要改成10,則需將3替換為10。運行結果如下:
please input student's information:
1061 lili 80 78 98
1062 liujun 85 80 92
1063 guowei 82 83 93
the information of student is:
num name score1 score2 score3 total aver
1061 lili 80.000000 78.000000 98.000000 256.000000 85.333336
1062 liujun 85.000000 80.000000 92.000000 257.000000 85.666664
1063 guowei 82.000000 83.000000 93.000000 258.000000 86.000000
the average score of all the students is :average=85.666664
the information of student with the highest total score is:
guowei 258.000000
第二題不會。
B. 學習C語言應遵循哪些步驟
第1步:定義程序的目標
在動手寫程序之前,要在腦中有清晰的思路。想要程序去做什麼首先自己要明確自己想做什麼,思考程序需要哪些信息,要進行哪些計算和控制,以及程序應該要報告什麼信息。在這一步驟中,不涉及具體的計算機語言,應該用一般術語來描述問題。
第2步:設計程序
對程序應該完成什麼任務有概念性的認識後,就應該考慮如何用程序來完成它。除此之外,還要決定在程序(還可能是輔助文件)中如何表示數據,以及用什麼方法處理數據。
學習C語言之初,遇到的問題都很簡單,沒什麼可選的。
第3步:編寫代碼
設計好程序後,就可以編寫代碼來實現。也就是說,把設計的程序翻譯成C語言。這里是真正需要使用C語言的地方。可以把思路寫在紙上,但是最終還是要把代碼輸入計算機。
程序清單1.1C源代碼示例
#include <stdio.h>
int main(void)
{
int dogs;
printf("How many dogs do you have? ");
scanf("%d", &dogs);
printf("So you have %d dog(s)! ", dogs);
return 0;
}
在這一步驟中,應該給自己編寫的程序添加文字注釋。最簡單的方式是使用C的注釋工具在源代碼中加入對代碼的解釋。
第4步:編譯
接下來的這一步是編譯源代碼。再次提醒讀者注意,編譯的細節取決於編程的環境,稍後馬上介紹一些常見的編程環境。現在,先從概念的角度講解編譯發生了什麼事情。
編譯器是把源代碼轉換成可執行代碼的程序。可執行代碼是用計算機的機器語言表示的代碼。這種語言由數字碼表示的指令組成。如前所述,不同的計算機使用不同的機器語言方案。C編譯器負責把C代碼翻譯成特定的機器語言。
此外,C編譯器還將源代碼與C庫(庫中包含大量的標准函數供用戶使用,如printf()和scanf())的代碼合並成最終的程序(更精確地說,
編譯器還會檢查C語言程序是否有效。如果C編譯器發現錯誤,就不生成可執行文件並報錯。理解特定編譯器報告的錯誤或警告信息是程序員要掌握的另一項技能。
第5步:運行程序
傳統上,可執行文件是可運行的程序。在常見環境(包括Windows命令提示符模式、UNIX終端模式和Linux終端模式)中運行程序要輸入可執行文件的文件名,而其他環境可能要運行命令(如,在VAX中的VMS[2])或一些其他機制。
例如,在Windows和Macintosh提供的集成開發環境(IDE)中,用戶可以在IDE中通過選擇菜單中的選項或按下特殊鍵來編輯和執行C程序。最終生成的程序可通過單擊或雙擊文件名或圖標直接在操作系統中運行。
第6步:測試和調試程序
程序能運行是個好跡象,但有時也可能會出現運行錯誤。查找並修復程序錯誤的過程叫調試。學習的過程中不可避免會犯錯,學習編程也是如此。因此,把所學的知識應用於編程時,最好為自己會犯錯做好心理准備。
第7步:維護和修改代碼
創建完程序後,發現程序有錯,或者想擴展程序的用途,這時就要修改程序。例如,用戶輸入以Zz開頭的姓名時程序出現錯誤、想到了一個更好的解決方案、想添加一個更好的新特性,或者要修改程序使其能在不同的計算機系統中運行,等等。
C. C語言 小型文件管理系統的編寫 要具體源程序
看樣子你也是四川理工的吧、、、、我也是為咯這道題而來的、、、、做不來、、、
D. c語言中源程序清單什麼意思
就是用C語言寫的可以運行的程序全文。
E. C語言程序設計模塊化程序設計
printf("%c",a);
or printf("%c",*name);
F. 求C語言中數制轉換代碼
四、源程序清單
/* 用 途: 2~36之間的各種數制的數任意轉換 */
/********************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
/* 顯示幫助信息 */
void help()
{
printf("\nUsage : Translate the number between two number systems\n");
printf("Syntex: TNS <number> <radix1> <radix2>\n");
exit(0);
}
/* 顯示錯誤信息 */
void printerror(errno,num,base1)
char errno,*num,*base1;
{
switch (errno) {
case 1 : printf("\nError : Origin number %s(%s) is valid !!!\n",num,base1);
break;
case 2 : printf("\nError : radix (%s) is invalid !!!\n%s\n",base1,
"Correct : radix>=2 and radix <=36");
break;
}
help();
}
/* 數制轉換函數 */
void transnum(num,base1,base2)
char *num,*base1,*base2;
{
int i,k,l,m,j,ibase1,ibase2;
long inum=0;
char temp[20];
double r=0;
i = strlen(num); /* 數值的長度 */
ibase1 = atoi(base1); /* 數基1 */
if ((ibase1<2) || (ibase1>36)) printerror(2,"",base1); /* 有效嗎? */
ibase2 = atoi(base2); /* 數基2 */
if ((ibase2<2) || (ibase2>36)) printerror(2,"",base2); /* 有效嗎? */
for (j=0;j<i;j++) {
r = pow(ibase1,i-j-1); /* 計算數基的冪指數 */
if (ibase1<=10) l = '9' - (10 - ibase1); /* 計算有效的數范圍 */
else {
m = 'a' + (ibase1 - 11);
l = '9';
}
if ((num[j]>=48) && (num[j]<=l)) /* 求每位數字的十進制值 */
k = num[j]-48;
else if (ibase1>10) {
/* 求每個字母所代表的十進制值 */
if ((num[j]>='A') && (num[j]<=m - 32))
k = num[j] - 'A'+10;
else if ((num[j]>='a') && (num[j]<=m))
k = num[j] - 'a'+10;
else printerror(1,num,base1);
}
else printerror(1,num,base1);
inum += k * (int) r; /* 累加計算結果 */
}
/* 輸出轉換結果 */
printf("%s(%d) = %s(%d)\n",num,ibase1,ltoa(inum,temp,ibase2),ibase2);
}
/* 主程序 */
main(argc,argv)
int argc;
char *argv[];
{
static char num[10],base1[10],base2[10];
printf("(TNS)Translator of Number System 1.0 Copyright (c) 1995 Dong Zhanshan\n");
switch (argc) {
case 1:
case 2:
case 3: help();
break;
case 4: strcpy(num,argv[1]);
strcpy(base1,argv[2]);
strcpy(base2,argv[3]);
transnum(num,base1,base2);
}
}
G. 急急急!!!會C語言編程的朋友 幫個忙啊
案例一 C++
#include <iostream>
using namespace std;
char num[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
int ToInt(char &n){
if (n<='9') return (int )(n-48);
else return (int )(n-'A'+10);
}
int main(){
int P = 10, Q; \\Q是要輸入的2 8 16進制
string N = "";
while (cin >> Q >> N){
if (N == "0"){
cout << 0 << endl;
return 0;
}
string OUT = "";
long long sum = 0, p = 1;
for (int i = 0; i<N.size(); i++){
sum += ToInt(N[N.size()-1-i])*p;
p*=P;
}
while (sum>0) OUT.insert(0, 1, num[sum%Q]), sum/=Q;
cout << OUT << endl;
}
return 0;
}
H. c語言程序填空求助大神
if(argc< 3 ) //補上 3,命令行參數個數
f2=fopen(argv[2], "w"); // 把h 改成 g
while(!feof(f1)) fputc( fgetc(f1),f2 ); //用 fputc,讀一個字元寫一個字元,加上文件結束EOF判斷 .
循環讀寫結束後關閉文件:
fclose(f1);
fclose(f2);
I. 程序是什麼
1.什麼是程序?什麼是程序設計語言?
答:程序是可以連續執行,並能夠完成一定任務的一條條指令的集合。
它是人與機器之間進行交流的語言。
2.( A )是構成C語言的基本單位。
(A)函數(B)過程(C)子程序(D)子函數
3.C語言的程序一行寫不下時,可以( B )。
(A)用逗號換行
(B)在任意一空格處換行
(C)用回車符換行
(D)用分號換行
4.以下說法正確的是( C )。
(A)C語言程序中是從第一個定義的函數開始執行
(B)在C程序中,要調用的函數必須在main()函數中定義
(C)C程序是從main()函數開始執行
(D)C程序中的main()函數必須放在程序的開始部分
5.結構化程序的三種基本控制結構是(順序)、(選擇)、(循環)。
6.C語言源程序的擴展名是( .c )。
7.請參照本章例題,編寫一個C程序,輸出以下字元串。
######################
You are Welcome!
######################
源程序清單:
main()
{
printf("######################\n");
printf("You are Welcome!\n");
printf("######################\n");
}
或者:
main()
{
printf("####################\n You are Welcome!\n ##################\n");
}
8.開發一個C程序,一般要經過哪些步驟?
答:編輯、編譯、連接、調試、運行。
9.歸納C程序的構成。
答:
(1)一個C程序由一個或多個函數構成,其中必須有main()主函數。
(2)每個函數由頭部和函數體兩部分組成,並以「{」、「}」作為函數的開始和結束標志。
函數頭部說明返回值的類型、函數名和形式參數等;
函數體由若干條C語句組成;
(3)每條C語句以分號結尾,分號是組成C語句的重要部分。
(4)可以在程序的任何位置用「/* …… */。」對程序中的任何部分注釋。
J. c語言編程
程序中定義了幾個特殊鍵:
"V」:畫筆提起
"W」:開始畫圖
"R」:開始擦圖
"S」:當前圖形存入文件
"E」:調出已有文件
"C」:畫圓
程序一運行,屏幕上出現一個黃色的邊框來設定畫圖的區域,區域中間出現提起的畫筆符號 ,當按下」W「鍵時,畫筆符號變為,此時可移動方向鍵(上、下、左、右、左上、左下、右上、右下)來畫圖;當按下」R「鍵時,畫筆符號變為,此時可移動方向鍵來擦圖;在畫圖過程中,按下「C」鍵,可畫出一個半徑為20個象素點的圓;當結束畫圖時,按下「S」鍵,將畫好的圖形存檔;按下「E」 鍵可調出已有的圖形進行編輯。
3.源程序清單
# include "graphics.h"
# include "stdio.h"
# include "fcntl.h"
# include "stdlib.h"
main()
void save(),load();
void *wg,*rg,*vg,*fy;
int driver,mode;
int c=RED;
int x=320,y=225;
int x1,y1,x2,y2;
int k,k1,k2;
/* initialize grapher */
detectgraph(&driver,&mode);
initgraph(&driver,&mode,"c:\tc");
/* write the pen */
bar(200,10,206,16);
line(203,7,200,10);
line(203,7,206,10);
line(243,7,240,16);
line(243,7,246,16);
line(283,7,280,10);
line(283,7,286,10);
line(283,7,283,16);
/* save the pen */
wg=malloc(imagesize(200,7,206,16));
rg=malloc(imagesize(240,7,246,16));
vg=malloc(imagesize(280,7,286,16));
fy=malloc(imagesize(200,7,206,16));
getimage(200,7,206,16,wg);
getimage(240,7,246,16,rg);
getimage(280,7,286,16,vg);
cleardevice();
/* write the box */
setcolor(YELLOW);
rectangle(4,19,637,447);
x1=x-3;
y1=y+1;
x2=x+3;
y2=y+10;
getimage(x1,y1,x2,y2,fy);
putimage(x1,y1,vg,XOR_PUT);
/* receive the command */
for (;;)
while (bioskey(1)==0);
k=bioskey(0);
putimage(x1,y1,fy,AND_PUT);
if (((k&0x00ff)|0x00)==0)
k1=k&0xff?0:k>>8; /* k1 is the specialkey value */
else
k2=k&0x00ff; /* k2 is the non-specialkey value */
if (((k&0x00ff)|0x00)==0) /* Special key */
switch(k1)
case 45:
restorecrtmode();
exit(0);
case 72:
if (y>20)
y=y-1;
break;
case 75:
if (x>5)
x=x-1;
break;
case 77:
if (x<636)
x=x+1;
break;
case 80:
if (y<446)
y=y+1;
break;
case 71:
if ((x>5)&&(y>20))
x=x-1;
y=y-1;
break;
case 79:
if ((x>5)&&(y<446))
x=x-1;
y=y+1;
break;
case 73:
if ((x<636)&&(y>20))
x=x+1;
y=y-1;
break;
case 81:
if ((x<636)&&(y<446))
x=x+1;
y=y+1;
break;
x1=x-3;
y1=y+1;
x2=x+3;
y2=y+10;
getimage(x1,y1,x2,y2,fy);
/* non-special key */
switch(k2)
case 118: /* 'v' */
case 86: /* 'V' */
putimage(x1,y1,vg,OR_PUT);
break;
case 119: /* 'w' */
case 87: /* 'W' */
putimage(x1,y1,wg,OR_PUT);
putpixel(x,y,c);
break;
case 114: /* 'r' */
case 82: /* 'R' */
putimage(x1,y1,rg,OR_PUT);
putpixel(x,y,BLACK);
break;
case 115: /* 's' */
case 83: /* 'S' */
save("pic.dat");
break;
case 101: /* 'e' */
case 69: /* 'E' */
load("pic.dat");
break;
case 99: /*'c'*/
case 67: /*'C'*/
setcolor(RED);
circle(x,y,20);
break;
default:continue;
/* function for screen picture save
*/
void save(char *fname)
FILE *fp;
int i;
register long j;
char far *ptr;
fp=fopen(fname,"wb");
for(i=0;i<4;i++)
outportb(0x3CE,4);
outportb(0x3CF,i);
ptr=(char far *) 0xA0000000L;
for (j=0;j<38400L;j++)
putc(*ptr,fp);
ptr++;
fclose(fp);
outportb(0x3CF,0);
/* function for screen picture display
*/
void load(char *fname)
FILE *fp;
register int i;
int k4=1;
register long j;
char far *ptr;
fp=fopen(fname,"rb");
for (i=0;i<4;i++)
outportb(0x3C4,2);
outportb(0x3C5,k4);
ptr=(char far *)0xA0000000L;
for (j=0;j<38400L;j++)
*ptr=getc(fp);
ptr++;
k4*=2;
fclose(fp);
outportb(0x3C5,0xF);
4.結束語
該程序在Turbo C 2.0環境下運行通過,使用效果良好。可以根據具體需要,對該程序進行擴充,以增加繪圖功能。