用C语言将4个学生的姓名按拼音首字母从大到小排序并显示. c语言利用指针将10个学生的姓名排序
c\u8bed\u8a00\u8f93\u5165N\u4e2a\u59d3\u540d\u62fc\u97f3,\u627e\u51fa\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5728\u6700\u524d\u7684\u62fc\u97f3\u8c03\u8bd5\u4e86\u4e00\u4e0b\uff0c\u95ee\u9898\u5728\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002
\u6700\u7b80\u5355\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u7cfb\u7edf\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570\u3002
\u4fee\u6539\u4ee3\u7801\u548c\u6ce8\u91ca\u5982\u4e0b\uff1a
#include#include //\u589e\u52a0\u5b57\u7b26\u4e32\u64cd\u4f5c\u652f\u6301#define N 5int main(){ char cs[N][20],temp[20]="0";//\u5b9a\u4e49\u65f6\u521d\u59cb\u5316\u5b57\u7b26\u4e32\u6570\u7ec4 int i,p, maxLen; printf("input names:\n"); for(i=0;i<N;i++) gets(cs[i]); printf("\n"); // temp[20]="0"; //\u5b57\u7b26\u4e32\u64cd\u4f5c\u4e0d\u80fd\u76f4\u63a5\u8d4b\u503c\uff0c\u4f7f\u7528strcpy\u51fd\u6570 for(i=1;i<N;i++) { //\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u7684\u884c\u8fdb\u884c\u5faa\u73af //if (cs[i] < temp) if(strcmp(cs[i],temp)<0)//\u5b57\u7b26\u4e32\u6bd4\u8f83\u4f7f\u7528strcmp\u51fd\u6570 strcpy(temp,cs[i]); //\u5b57\u7b26\u4e32\u8d4b\u503c\uff0c\u4f7f\u7528strcpy\u51fd\u6570 } printf("The name is:%s\n", temp); return 0;}\u5728VC6\u7f16\u8bd1\u901a\u8fc7\uff0c\u5982\u4e0b\u56fe\uff1a
\u4f9b\u53c2\u8003\u3002
\u53c2\u8003\u4e0b\u9762
#include
#include
void input(char (*p)[10],int n)
{
int i; //int
for(i=0;i<n;i++)
{
printf("input\u7b2c%d\u4e2a\u59d3\u540d:",i+1);
scanf("%s",&p[i]);
printf("\n");
}
}
void output(char (*p)[10]\uff0cint\uff0cn)
{
int i;
printf("\u6392\u5e8f\u4e4b\u540e\u4e3a:");
for(i=0;i<n;i++)
{
printf("%s", p[i]);
printf("\n");
}
}
void sort(char (*p)[10],int n)
{
int i,j;
char t[10];//----------------
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(p[i],p[j])>0)
{
strcpy( t,p[i] );
strcpy( p[i],p[j] );
strcpy( p[j],t);
}
}
int main()
{
char s[4][10];
input(s,4);
sort(s,4);
output(s,4);
system("pause");
return 0;
}
一个学生的这样搞:
姓名分开,成两个字符串。
分别排序,形成新的字符串,然后输出。
其他学生一样的方法。
#include <stdio.h>
#include <string.h>
#define STU_NAME_MAX_LEN 20
char stu_name[][STU_NAME_MAX_LEN] =
{
"iker cassilas",
"luis suarez",
"cristiano ronaldo",
"lionl messi"
};
void main()
{
int stu_count = sizeof(stu_name)/sizeof(stu_name[0]);
int i, j, k;
for (i=0; i<stu_count; i++)
{
char firstname[STU_NAME_MAX_LEN];
char familyname[STU_NAME_MAX_LEN];
int blankpos = 0;
//将姓名分开
memset(firstname, 0, STU_NAME_MAX_LEN);
memset(familyname, 0, STU_NAME_MAX_LEN);
for (j=0; j<sizeof(stu_name[i]); j++)
{
if (stu_name[i][j] == ' ')
{
blankpos = j;
break;
}
}
memcpy(firstname, &stu_name[i][0], blankpos);
memcpy(familyname, &stu_name[i][blankpos+1], sizeof(stu_name[i])-blankpos-1);
printf("old: %s %s
", firstname, familyname);
//排序
for (j=0; firstname[j]!='\0'; j++)
{
for (k=j+1; firstname[k]!='\0'; k++)
{
if (firstname[j] < firstname[k])
{
char ctemp = firstname[j];
firstname[j] = firstname[k];
firstname[k] = ctemp;
}
}
}
for (j=0; familyname[j]!='\0'; j++)
{
for (k=j+1; familyname[k]!='\0'; k++)
{
if (familyname[j] < familyname[k])
{
char ctemp = familyname[j];
familyname[j] = familyname[k];
familyname[k] = ctemp;
}
}
}
printf("new:%s %s
", firstname, familyname);
}
}
绛旓細j, k; for (i=0; i<stu_count; i++) { char firstname[STU_NAME_MAX_LEN]; char familyname[STU_NAME_MAX_LEN]; int blankpos = 0; //灏濮撳悕鍒嗗紑 memset(firstname, 0, STU_NAME_
绛旓細void input(char *name[],int n){ char str[20]; /**鎷奸煶鐨勯暱搴. 20-1 ,鑷繁鏀 */ int i;printf("璇疯緭鍏 %d 涓鐢熺殑鍚嶅瓧(璇鐢ㄦ嫾闊) :\n",M);/** 杩欓噷涔熸瘮杈冨叧閿,濂藉ソ棰嗕細.**/ for(i=0; i < n; i++){ scanf("%s",str);name[i] = (char *)malloc(sizeof(char...
绛旓細void fsort(char cname[][6],int n){ int i,j,k;char temp[6]={0};for(k=1;k<n-1;k++)for(j=0;j<n-k-1;j++)if (cname[j][0]>cname[j+1][0]){ strcpy(temp,cname[j]);strcpy(cname[j],cname[j+1]);strcpy(cname[j+1],temp);} } void main(){ int i;c...
绛旓細void display_name(char (*pc)[MAX_NAME],int n)//鏄剧ず杩檔涓鍚峽 int i; for(i=0;i<n;i++) { printf("%s\n",pc[i]); }}int main(){ int i=1,n; char str[MAX_NUM][MAX_NAME]; printf("璇疯緭鍏ュ鐢熸绘暟:"); scanf("%d",&n); while(i<=n) { printf("璇疯緭鍏ョ%d涓鐢熷鍚:...
绛旓細for(int i=0;i<strlen(a);i++){ if(a[i]!=' ')b[k][j++]=a[i];//灏嗘瘡涓鍚嶅瓧瀛樺叆浜岀淮鏁扮粍鐨勬瘡涓琛屼腑 else { k++;j=0;} } for(int i=0;i<=k;i++){ for(int j=strlen(b[i])-1;j>=0;j--){ printf("%c",b[i][j]);} printf("\n");} return 0;} ...
绛旓細i<N;i++) { //瀵逛簩缁存暟缁勭殑琛岃繘琛屽惊鐜 //if (cs[i] < temp) if(strcmp(cs[i],temp)<0)//瀛楃涓叉瘮杈冧娇鐢╯trcmp鍑芥暟 strcpy(temp,cs[i]); //瀛楃涓茶祴鍊硷紝浣跨敤strcpy鍑芥暟 } printf("The name is:%s\n", temp); return 0;}鍦╒C6缂栬瘧閫氳繃锛屽涓嬪浘锛氫緵鍙傝冦
绛旓細struct stu { char name[20]; //*瀛樻斁濮撳悕锛鎷奸煶锛*// int Chinese;int Math;int English;int Physics; //*瀛樻斁鍥涢棬璇剧殑鎴愮哗锛屾垜闅忎究涓句簡4涓渚嬪瓙*// };struct stu student[5]; //*寤虹珛涓涓暟缁勫瓨鏀5涓悓瀛︾殑淇℃伅*// 鏌ユ壘鐨勮瘽鍙鐢╢or寰幆锛屾瘮杈冭緭鍏ョ殑瀛楃涓蹭笌姣忎竴涓悓瀛鐨勫鍚...
绛旓細ansi GB2312閲屼竴绾ф眽瀛楁槸鎸夋嫾闊鎺掑簭鐨勶紝鍏朵粬鐨勬眽瀛楁寜閮ㄩ/绗斿垝鎺掑簭銆傛墍浠ヨ兘鎸夋嫾闊虫帓搴忕殑姹夊瓧鍙湁3000澶氫釜锛岀洿鎺ョ敤strcmp()姣旇緝灏卞彲浠ヤ簡銆備絾鏄鏋滅敤鐨勬槸Unicode锛岀敱浜嶶nicode閲岀殑姹夊瓧鎸夌瑪鍒掗『搴忔帓搴忥紝鎵浠ユ病鏈夊姙娉曞疄鐜版寜鎷奸煶鎺掑簭銆備唬鐮佸涓嬶細include<stdio.h>#include<string.h> int main(){ char a[5]...
绛旓細char name[20]={0},tmp;int i,length;scanf("%s",name);length = strlen(name);for(i=0;i<length/2;i++){ tmp = name[i]; name[i] = name[length-i-1]; name[length-i-1] = name[i];}printf("name:%s",name);
绛旓細name(str,n);sort_bubble(str,n);printf("鎺掑簭鍚庣殑瀛︾敓鍚嶅崟濡備笅锛歕n");display_name(str,n);return 0;} 璇存槑锛1锛氶噰鐢ㄧ殑鏄啋娉℃帓搴忥紝鐢ㄥ揩閫熸帓搴忓綋鐒跺揩浜涳紝浣嗘垜蹇樹簡 2锛氬凡楠岃瘉閫氳繃锛岀粨鏋滄纭3锛氶殢鎰忚緭鍏ヤ汉鏁板拰濮撳悕锛屽彧瑕佹暟缁勪笉瓒婄晫灏辨病鏈夐棶棰橈紒4锛氫袱涓嚱鏁帮細鎺掑簭鍜屾樉绀 5锛氱粰鍒+濂借瘎 ...