Ⅰ C語言程序編寫
1
inti,n;
scanf("%d",&n);
for(i=1;i<=n/2;++i)
printf("*");
printf(" ")
for(i=1;i<=n/2;++i)
printf("A");
for(i=1;i<=n;++i)
printf("B");
2.
charc;
intn;
scanf("%c%d",&c,&n);
for(inti=1;i<=n;++i)
printf("%c",c);
3.
intn,s=0;
scanf("%d",&n);
for(inti=2;i<=n;++i)
s+=i;
Ⅱ C語言程序編寫!
#include<stdio.h> /*引用庫函數*/
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct /*定義結構體數組*/
{
char num[10]; /*學號*/
char name[20]; /*姓名*/
int score; /*成績*/
}Student;
Student stu[80]; /*結構體數組變數*/
int menu_select() /*菜單函數*/
{
char c;
do{
system("cls"); /*運行前清屏*/
printf("\t\t****Students' Grade Management System****\n"); /*菜單選擇*/
printf("\t\t | 1. Input Records |\n");
printf("\t\t | 2. Display All Records |\n");
printf("\t\t | 3. Sort |\n");
printf("\t\t | 4. Insert a Record |\n");
printf("\t\t | 5. Delete a Record |\n");
printf("\t\t | 6. Query |\n");
printf("\t\t | 7. Statistic |\n");
printf("\t\t | 8. Add Records from a Text File|\n");
printf("\t\t | 9. Write to a Text file |\n");
printf("\t\t | 0. Quit |\n");
printf("\t\t*****************************************\n");
printf("\t\t\tGive your Choice(0-9):");
c=getchar(); /*讀入選擇*/
}while(c<'0'||c>'9');
return(c-'0'); /*返回選擇*/
}
int Input(Student stud[],int n) /*輸入若干條記錄*/
{int i=0;
char sign,x[10]; /*x[10]為清除多餘的數據所用*/
while(sign!='n'&&sign!='N') /*判斷*/
{ printf("\t\t\tstudent's num:"); /*交互輸入*/
scanf("\t\t\t%s",stud[n+i].num);
printf("\t\t\tstudent's name:");
scanf("\t\t\t%s",stud[n+i].name);
printf("\t\t\tstudent's score:");
scanf("\t\t\t%d",&stud[n+i].score);
gets(x); /*清除多餘的輸入*/
printf("\t\t\tany more records?(Y/N)");
scanf("\t\t\t%c",&sign); /*輸入判斷*/
i++;
}
return(n+i);
}
void Display(Student stud[],int n) /*顯示所有記錄*/
{
int i;
printf("\t\t\t-----------------------------------\n"); /*格式頭*/
printf("\t\t\tnumber name score\n");
printf("\t\t\t-----------------------------------\n");
for(i=1;i<n+1;i++) /*循環輸入*/
{
printf("\t\t\t%-16s%-15s%d\n",stud[i-1].num,stud[i-1].name,stud[i-1].score);
if(i>1&&i%10==0) /*每十個暫停*/
{printf("\t\t\t-----------------------------------\n"); /*格式*/
printf("\t\t\t");
system("pause");
printf("\t\t\t-----------------------------------\n");
}
}
printf("\t\t\t");
system("pause");
}
void Sort_by_num(Student stud[],int n) /*按學號排序*/
{ int i,j,*p,*q,s;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
p=&stud[j+1].score;
q=&stud[j].score;
s=*p;
*p=*q;
*q=s;
}
}
int Insert_a_record(Student stud[],int n) /*插入一條記錄*/
{char x[10]; /*清除多餘輸入所用*/
printf("\t\t\tstudent's num:"); /*互動式輸入*/
scanf("\t\t\t%s",stud[n].num);
printf("\t\t\tstudent's name:");
scanf("\t\t\t%s",stud[n].name);
printf("\t\t\tstudent's score:");
scanf("\t\t\t%d",&stud[n].score);
gets(x);
n++;
Sort_by_num(stud,n); /*調用排序函數*/
printf("\t\t\tInsert Successed!\n"); /*返回成功信息*/
return(n);
}
int Delete_a_record(Student stud[],int n) /*按姓名查找,刪除一條記錄*/
{ char s[20];
int i=0,j;
printf("\t\t\ttell me his(her) name:"); /*互動式問尋*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{ printf("\t\t\tnot find!\n"); /*返回失敗信息*/
return(n);
}
for(j=i;j<n-1;j++) /*刪除操作*/
{
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].name,stud[j+1].name);
stud[j].score=stud[j+1].score;
}
printf("\t\t\tDelete Successed!\n"); /*返回成功信息*/
return(n-1);
}
void Query_a_record(Student stud[],int n) /*查找並顯示一個記錄*/
{ char s[20];
int i=0;
printf("\t\t\tinput his(her) name:"); /*互動式輸入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判斷*/
if(i==n)
{ printf("\t\t\tnot find!\n"); /*輸入失敗信息*/
return;
}
printf("\t\t\this(her) number:%s\n",stud[i].num); /*輸出該學生信息*/
printf("\t\t\this(her) score:%d\n",stud[i].score);
}
void Statistic(Student stud[],int n) /*新增功能,輸出統計信息*/
{ int i,j=0,k=0,sum=0;
float aver; /*成績平均值*/
for(i=0;i<n;i++) /*循環輸入判斷*/
{
sum+=stud[i].score;
if(stud[j].score>stud[i].score) j=i;
if(stud[k].score<stud[i].score) k=i;
}
aver=1.0*sum/n;
printf("\t\t\tthere are %d records.\n",n); /*總共記錄數*/
printf("\t\t\tthe hignest score:\n"); /*最高分*/
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[j].num,stud[j].name,stud[j].score);
printf("\t\t\tthe lowest score:\n"); /*最低分*/
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[k].num,stud[k].name,stud[k].score);
printf("\t\t\tthe average score is %5.2f\n",aver); /*平均分*/
}
int AddfromText(Student stud[],int n) /*從文件中讀入數據*/
{ int i=0,num;
FILE *fp; /*定義文件指針*/
char filename[20]; /*定義文件名*/
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename); /*輸入文件名*/
if((fp=fopen(filename,"rb"))==NULL) /*打開文件*/
{ printf("\t\t\tcann't open the file\n"); /*打開失敗信息*/
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*讀入總記錄量*/
while(i<num) /*循環讀入數據*/
{
fscanf(fp,"%s%s%d",stud[n+i].num,stud[n+i].name,&stud[n+i].score);
i++;
}
n+=num;
fclose(fp); /*關閉文件*/
printf("\t\t\tSuccessed!\n");
printf("\t\t\t");
system("pause");
return(n);
}
void WritetoText(Student stud[],int n) /*將所有記錄寫入文件*/
{
int i=0;
FILE *fp; /*定義文件指針*/
char filename[20]; /*定義文件名*/
printf("\t\t\tWrite Records to a Text File\n"); /*輸入文件名*/
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL) /*打開文件*/
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循環寫入數據*/
while(i<n)
{
fprintf(fp,"%-16s%-15s%d\n",stud[i].num,stud[i].name,stud[i].score);
i++;
}
fclose(fp); /*關閉文件*/
printf("Successed!\n"); /*返回成功信息*/
}
void main() /*主函數*/
{
int n=0;
for(;;)
{
switch(menu_select()) /*選擇判斷*/
{
case 1:
printf("\t\t\tInput Records\n"); /*輸入若干條記錄*/
n=Input(stu,n);
break;
case 2:
printf("\t\t\tDisplay All Records\n"); /*顯示所有記錄*/
Display(stu,n);
break;
case 3:
printf("\t\t\tSort\n");
Sort_by_num(stu,n); /*按學號排序*/
printf("\t\t\tSort Suceessed!\n");
printf("\t\t\t");
system("pause");
break;
case 4:
printf("\t\t\tInsert a Record\n");
n=Insert_a_record(stu,n); /*插入一條記錄*/
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\tDelete a Record\n");
n=Delete_a_record(stu,n); /*按姓名查找,刪除一條記錄*/
printf("\t\t\t");
system("pause");
break;
case 6:
printf("\t\t\tQuery\n");
Query_a_record(stu,n); /*查找並顯示一個記錄*/
printf("\t\t\t");
system("pause");
break;
case 7:
printf("\t\t\tStatistic\n");
Statistic(stu,n); /*新增功能,輸出統計信息*/
printf("\t\t\t");
system("pause");
break;
case 8:
printf("\t\t\tAdd Records from a Text File\n");
n=AddfromText(stu,n); /*新增功能,輸出統計信息*/
break;
case 9:
printf("\t\t\tWrite to a Text file\n");
WritetoText(stu,n); /*循環寫入數據*/
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\tHave a Good Luck,Bye-bye!\n"); /*結束程序*/
printf("\t\t\t");
system("pause");
exit(0);
}
}
}
Ⅲ 怎麼編寫C語言程序,如:Helloworld的
代碼如下:
#include<stdio.h>
intmain(void)
{
printf("Hello,world!");
return0;
}
一、首先,打開我們的Visual C++ 6.0軟體,我使用的為中文版,軟體主界面如下圖所示:
(3)c語言程序編程擴展閱讀:
C語言的基本構成:
1、數據類型。C的數據類型包括:整型、字元型、實型或浮點型(單精度和雙精度)、枚舉類型、數組類型、結構體類型、共用體類型、指針類型和空類型。
2、常量與變數。常量其值不可改變,符號常量名通常用大寫。變數是以某標識符為名字,其值可以改變的量。標識符是以字母或下劃線開頭的一串由字母、數字或下劃線構成的序列,請注意第一個字元必須為字母或下劃線,否則為不合法的變數名。變數在編譯時為其分配相應存儲單元。
3、數組。如果一個變數名後面跟著一個有數字的中括弧,這個聲明就是數組聲明。字元串也是一種數組。它們以ASCII的NULL作為數組的結束。要特別注意的是,方括內的索引值是從0算起的。
4、指針。
(1)、如果一個變數聲明時在前面使用 * 號,表明這是個指針型變數。換句話說,該變數存儲一個地址,而 *(此處特指單目運算符 * ,下同。C語言中另有 雙目運算符 *) 則是取內容操作符,意思是取這個內存地址里存儲的內容。指針是 C 語言區別於其他同時代高級語言的主要特徵之一
(2)、指針不僅可以是變數的地址,還可以是數組、數組元素、函數的地址。通過指針作為形式參數可以在函數的調用過程得到一個以上的返回值,不同於return(z)這樣的僅能得到一個返回值。
(3)指針是一把雙刃劍,許多操作可以通過指針自然的表達,但是不正確的或者過分的使用指針又會給程序帶來大量潛在的錯誤。
5、字元串。C語言的字元串其實就是以'