當前位置:首頁 » 編程語言 » c語言輸入名字和幾門成績
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言輸入名字和幾門成績

發布時間: 2022-07-20 10:41:07

c語言中如何用多維數組實現輸入一個班30名學生的姓名,學號和5門成績

#include "stdio.h"
#define N 100
void main()
{
char stu_name[N][20]={""};
char stu_id[N][10]={""};
float stu_score[N][5]={0.0};
int stu_num=0;
int i,j;
printf("Please input the number of class:n=");
scanf("%d",&stu_num);
printf("Please input student's information:\n");
for(i=0;i<stu_num;i++)
{
printf("請輸入第%d位學生信息:\n",i+1);
printf("Please input Name:");
getchar();
gets(stu_name[i]);
printf("Please input ID:");
getchar();
gets(stu_id[i]);
for(j=0;j<5;j++)
{

printf("請輸入第%d門成績:",j+1);
scanf("%f",&stu_score[i][j]);
}
}
}

⑵ c語言輸入學生人數及課程的門數,輸入學生的姓名和相應課程的成績

#include
struct Student
{
int num;
char name[20];
float score[3], average;
};
int main(void)
{
int i, j;
struct Student std[10] = {0}, temp;
puts("Please enter information of student : ");
for (i = 0; i < 10; ++i)
{
scanf("%d%s", &std[i].num, std[i].name);
for (j = 0; j < 3; ++j)
{
scanf("%f", &std[i].score[j]);
std[i].average += std[i].score[j];
}
std[i].average /= 3;
}
for (i = 0; i < 9; ++i)
{
for (j = 0; j < 9 - i; ++j)
{
if (std[j].average < std[j + 1].average)
{
temp = std[j];
std[j] = std[j + 1];
std[j + 1] = temp;
}
}
}
for (i = 0; i < 10; ++i)
{
printf("Num=%d Name=%-6s ", std[i].num, std[i].name);
printf("Score1=%0.2f Score2=%0.2f Score3=%0.2f ", std[i].score[0], std[i].score[1], std[i].score[2]);
printf("Average=%0.2f\n", std[i].average);
}
return 0;
}

⑶ 用C語言做 輸入幾個同學的姓名、學號、三門功課成績,寫入到磁碟文件中(文件名自定)

#include <stdio.h>
main()
{
FILE *fp;
char ch,*fn;
stuct student{
char *nm;int id,a,b,c;
}std;
printf("Please input a file name:\n");
gets(fn);
if(fp=fopen(fn,"a+")){
do {
printf("\nPlease input a student's name,id,score a,score b,score c\n");
scanf("%s %d %d %d %d %d",std.nm,&std.id,&std.a,&std.b,&std.c);
fwrite(std,sizeof(struct student),1,fp);
printf("\nNext:[y/n]");
scanf("%c",ch);
}while(ch!='n'&&ch!='N');

}
else printf("The file cannot be openned!");

fclose(fp);
}

⑷ 使用C語言編程,輸入學生的學號,姓名,三門成績,並求學生、課程的平均成績。跪求高手速答,三十分鍾內

咨詢記錄 · 回答於2021-10-17

⑸ 用c語言編寫程序,要求輸入輸出三門課程名稱和成績 實在不會做,拜託大神了555

方法一:用數組實現。

#include <stdio.h>

int main( )

{

int i;

char cn[3][20];//3門課名稱

int cs[3];//3門課成績

printf("分別輸入3門課的名稱 成績: ");

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

scanf("%s%d",cn[i],&cs[i]);

printf("輸出: ");

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

printf("%s %d ",cn[i],cs[i]);

return 1;

}

方法二:把數組改成結構體

#include <stdio.h>

typedef struct cInfo

{

char cn[20];//3門課名稱

int cs;//3門課成績

}CIFO;

int main( )

{

int i;

CIFO cifos[3];

printf("分別輸入3門課的名稱 成績: ");

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

scanf("%s%d",cifos[i].cn,&cifos[i].cs);

printf("輸出: ");

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

printf("%s %d ",cifos[i].cn,cifos[i].cs);

return 1;

}

⑹ C語言。輸入n個學生的姓名,語文,數學,英語成績,

#include <stdio.h>

float average;

}stu[N],temp;

void main()

printf("請依次輸入%d個學生的姓名、語文成績、數學成績、英語成績: ",N);

scanf("%s%d%d%d",&stu[i].name,&stu[i].yuwen,&stu[i].shuxue,&stu[i].yingyu);

stu[i].average=(stu[i].yuwen+stu[i].shuxue+stu[i].yingyu)/3.0;

printf(" 姓名 語文 數學 英語 平均分 ");

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

簡介

C語言是一門面向過程的計算機編程語言,與C++、C#、Java等面向對象編程語言有所不同。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、僅產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。C語言描述問題比匯編語言迅速、工作量小、可讀性好、易於調試、修改和移植,而代碼質量與匯編語言相當。

⑺ C語言在鍵盤上輸入三個學生的信息(包含學號,姓名,三門課的成績)並在顯示器上輸

id, name, &m1

id, name, m1

voidprintScore(intscore[4][3])

{inti,j;for(i=0;i<4;i++)

{printf("學生%d的三門成績分別為:

",i+1);for(j=0;j<3;j++)

{printf("%d ",score<i>[j]);

if(j==2)printf(" ");}}printf(" ");}

printf()函數是格式化輸出函數,一般用於向標准輸出設備按規定格式輸出信息。格式輸出,它是c語言中產生格式化輸出的函數(在stdio.h中定義)。

函數介紹

printf()函數是格式化輸出函數,一般用於向標准輸出設備按規定格式輸出信息。在編寫程序時經常會用到此函數。函數的原型為:intprintf(constchar*format);

函數返回值為整型。若成功則返回輸出的字元數,輸出出錯則返回負值。

printf()函數的調用格式為:printf("<格式化字元串>",<參量表>);其中格式化字元串包括兩部分內容:一部分是正常字元,這些字元將按原樣輸出;另一部分是格式化規定字元,以"%"開始,後跟一個或幾個規定字元,用來確定輸出內容格式。

以上內容參考:網路-printf()

⑻ C語言 鍵盤輸入4個學生姓名以及每個學生3門課的成績。

有點小復雜哦

請看下面的代碼


#include<stdio.h>
#include<string.h>

/*常量字元串默認長度*/
constintSTRING_DEFAULT_LEN=50;
/*常量學生人數4人*/
constintSTUDENTS_COUNT=4;
/*常量課程數量3門*/
constintCOURSE_COUNT=3;

/*變數選課記錄*/
intsign_up_of_course_count;

/*課程數據結構*/
structcourse{
charname[STRING_DEFAULT_LEN];
};

/*學生數據結構*/
structstudent{
charname[STRING_DEFAULT_LEN];
};

/*學生的選課數據結構,包括學生/課程/得分*/
structsign_up_of_course{
structstudents;
structcoursec;
floatscore;
};


/*子函數輸出每個學生的平均分*/
voidprint_avg_of_each_student(conststructsign_up_of_course*,int);
/*子函數輸出每門課的平均分*/
voidprint_avg_of_each_course(conststructsign_up_of_course*,int);
/*子函數找出最高分對應的學生和課程*/
voidprint_max_score_info(conststructsign_up_of_course*,int);

intmain()
{
/*初始化4個學生*/
structstudents[STUDENTS_COUNT]={"StudentA","StudentB","StudentC","StudentD"};

/*初始化3門課程*/
structcoursec[COURSE_COUNT]={"Course1","Course2","Course3"};

/*初始化選課記錄*/
sign_up_of_course_count=STUDENTS_COUNT*COURSE_COUNT;
structsign_up_of_course*signup=newstructsign_up_of_course[sign_up_of_course_count];
memset(signup,0,sign_up_of_course_count*sizeof(structsign_up_of_course));

inti,j;
for(i=0;i<STUDENTS_COUNT;i++)
{
for(j=0;j<COURSE_COUNT;j++)
{
/*學生和課程數據到該選課記錄*/
memcpy(&((signup+i*(STUDENTS_COUNT-1)+j)->s),&s[i],sizeof(structstudent));
memcpy(&((signup+i*(STUDENTS_COUNT-1)+j)->c),&c[j],sizeof(structcourse));
}
}

/*請用戶輸入每個選課記錄的成績,即各個學生的成績*/
intk=0;
for(k=0;k<sign_up_of_course_count;k++)
{
/*用戶輸入成績,保存到選擇記錄的分數欄中*/
printf("請輸入學生%s的課程%s的成績:",((signup+k)->s).name,((signup+k)->c).name);
scanf("%d",&((signup+k)->score));
}

/*調用子函數輸出每個學生的平均分*/
print_avg_of_each_student(signup,sign_up_of_course_count);

/*調用子函數輸出每門課的平均分*/
print_avg_of_each_course(signup,sign_up_of_course_count);

/*調用子函數找出最高分對應的學生和課程*/
print_max_score_info(signup,sign_up_of_course_count);

/*銷毀分配的內存*/
delete[]signup;

getchar();
getchar();
return0;
}

/*實現子函數輸出每個學生的平均分*/
voidprint_avg_of_each_student(conststructsign_up_of_course*signup,intlen)
{
structstudent_score
{
structstudents; /*學生*/
floatavg; /*平均值*/
floattotal; /*總分*/
intcount; /*計數*/
};

structstudent_scorescores[STUDENTS_COUNT];
memset(scores,0,STUDENTS_COUNT*sizeof(structstudent_score));

inti,j;
chars1[STRING_DEFAULT_LEN];
chars2[STRING_DEFAULT_LEN];
intscores_filled_pos=-1;
boolscores_record_proessed=false;
/*處理每一條選課記錄,按學生合計*/
for(i=0;i<len;i++)
{
memset(s1,0,STRING_DEFAULT_LEN);
memset(s2,0,STRING_DEFAULT_LEN);

/*求學生的分數合計*/
scores_record_proessed=false;
for(j=0;j<STUDENTS_COUNT;j++)
{
strcpy(s1,signup[i].s.name);
strcpy(s2,scores[j].s.name);

if(strcmp(s1,s2)==0)
{
memcpy(&scores[j].s,&signup[i].s,sizeof(structstudent));
scores[j].total+=signup[i].score;
scores[j].count+=1;

scores_record_proessed=true;

continue;
}
}

if(scores_record_proessed==false)
{
scores_filled_pos+=1;
memcpy(&scores[scores_filled_pos].s,&signup[i].s,sizeof(structstudent));
scores[scores_filled_pos].total+=signup[i].score;
scores[scores_filled_pos].count+=1;
continue;
}
}

/*求平均值並輸出*/
for(j=0;j<STUDENTS_COUNT;j++)
{
scores[j].avg=scores[j].total/scores[j].count;
printf("學生%s的平均分數是%2.2f ",scores[j].s.name,scores[j].avg);
}

}

/*實現子函數輸出每門課的平均分*/
voidprint_avg_of_each_course(conststructsign_up_of_course*signup,intlen)
{
structcourse_score
{
structcoursec; /*課程*/
floatavg; /*平均值*/
floattotal; /*總分*/
intcount; /*計數*/
};

structcourse_scorescores[COURSE_COUNT];
memset(scores,0,COURSE_COUNT*sizeof(structcourse_score));

inti,j;
chars1[STRING_DEFAULT_LEN];
chars2[STRING_DEFAULT_LEN];
intscores_filled_pos=-1;
boolscores_record_proessed=false;
/*處理每一條選課記錄,按課程合計*/
for(i=0;i<len;i++)
{
memset(s1,0,STRING_DEFAULT_LEN);
memset(s2,0,STRING_DEFAULT_LEN);

/*求學生的分數合計*/
scores_record_proessed=false;
for(j=0;j<COURSE_COUNT;j++)
{
strcpy(s1,signup[i].c.name);
strcpy(s2,scores[j].c.name);

if(strcmp(s1,s2)==0)
{
memcpy(&scores[j].c,&signup[i].c,sizeof(structcourse));
scores[j].total+=signup[i].score;
scores[j].count+=1;

scores_record_proessed=true;

continue;
}
}

if(scores_record_proessed==false)
{
scores_filled_pos+=1;
memcpy(&scores[scores_filled_pos].c,&signup[i].c,sizeof(structstudent));
scores[scores_filled_pos].total+=signup[i].score;
scores[scores_filled_pos].count+=1;
continue;
}
}

/*求平均值並輸出*/
for(j=0;j<COURSE_COUNT;j++)
{
scores[j].avg=scores[j].total/scores[j].count;
printf("課程%s的平均分數是%2.2f ",scores[j].c.name,scores[j].avg);
}
}

/*實現子函數找出最高分對應的學生和課程*/
voidprint_max_score_info(conststructsign_up_of_course*signup,intlen)
{
/*最多的並列最高分的學生數,即學生總數*/
constintMAX_COUNT=STUDENTS_COUNT*COURSE_COUNT;

/*存儲最高分信息的數組,可存儲多個並列最高分*/
structsign_up_of_coursemax_course_signup[MAX_COUNT];
/*並列最高分個數*/
intcount=0;
inti;

for(i=0;i<len;i++)
{
/*處理第一件時,尚無比較對象,作為最高分的初始值*/
if(i==0)
{
count++;
memcpy(max_course_signup,signup,sizeof(structsign_up_of_course));

continue;
}

/*發現更高的分數時,並列最高分個數將降為1*/
if(max_course_signup[count-1].score<(signup+i)->score)
{
count=1;
memcpy(&max_course_signup[count-1],signup+i,sizeof(structsign_up_of_course));
}

/*發現並列最高分時,存儲數組個數自增1*/
if(max_course_signup[count-1].score==(signup+i)->score)
{
count++;
memcpy(&max_course_signup[count-1],signup+i,sizeof(structsign_up_of_course));
}
}

/*輸出最高分*/
for(i=0;i<count-1;i++)
{
printf("最高分的學生是%s,課程為:%s。 ",max_course_signup[i].s.name,max_course_signup[i].c.name);
}
}

⑼ C語言綜合問題:輸入5個學生的姓名、學號和5門課成績計算每個學生的總分和平均分並按從高到低排序

定義一個結構體
struct student
{
char *name;//姓名
int num[10];//學號
int scor[5];//5門課成績
int average;//平均成績
};
用student定義5個變數,再操作應該就很簡單了