第一题:已知某班N(〈=50)名学生进行了高数、英语和C语言等3门课考试,将3门课的成绩以及计算3门课的总分存放于一个二维数组中,将学生姓名存放在另一个二维字符数组中,按总分(由高到低)将学生姓名排序并输出排序结果(序号和姓名)。
第二题:编写函数fun(char s[ ], int num[ ]),其功能是统计字符串 s 中数字字符、大写字母、小写字母和空格字符的出现次数,统计结果存于num数组中。再设计main函数,调用fun函数,实现预期功能。
第三题:编程输出右边图形,其行数n (<= 26)由输入而定。 A
A B A
A B C B A
A B C D C B A
不好意思,第三题图形有误,我解释下。图形就像三角型那样,第一层是A,第二层是A B A,以此类推
/*
第一题:已知某班N(〈=50)名学生进行了高数、英语和C语言等3门课考试,将3门课
的成绩以及计算3门课的总分存放于一个二维数组中,将学生姓名存放在另一个二维字
符数组中,按总分(由高到低)将学生姓名排序并输出排序结果(序号和姓名)。
*/
#include "stdafx.h"
#include "stdio.h"
struct scoreInfo{
int scoreMath;
int scoreProC;
int scoreEng;
int scorSum;
};
struct studentInfo{
char name[20];
struct scoreInfo _scoreInfo;
};
void inputInfo(struct studentInfo *stu,int n)
{
int i = 0;
for(i = 0; i < n; i++)
{
printf("第%d个学生的姓名:",i+1);
gets(stu[i].name);
printf("第%d个学生的数学成绩:",i+1);
scanf("%d",&stu[i]._scoreInfo.scoreMath);
getchar();
printf("第%d学生的C语言成绩:",i+1);
scanf("%d",&stu[i]._scoreInfo.scoreProC);
getchar();
printf("第%d个学生的英语成绩:",i+1);
scanf("%d",&stu[i]._scoreInfo.scoreEng);
getchar();
}
printf("\n");
}
void outputInfo(struct studentInfo *stu,int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
stu[i]._scoreInfo.scorSum = stu[i]._scoreInfo.scoreEng + stu[i]._scoreInfo.scoreMath
+ stu[i]._scoreInfo.scoreProC;
}
printf("姓名\t数学成绩\tC语言成绩\t英语成绩\t总分\n");
for (i = 0; i < n; i++)
{
printf("%s\t%d\t\t%d\t\t%d\t\t%d\n",stu[i].name,stu[i]._scoreInfo.scoreMath, stu
[i]._scoreInfo.scoreProC
,stu[i]._scoreInfo.scoreEng,stu[i]._scoreInfo.scorSum);
}
}
void arry_max_to_min(struct studentInfo *stu,int n)
{
int indexi;
int indexj;
struct studentInfo _temp;
// struct stdentInfo *ptemp;
// ptemp = stu;
for (indexi = 0; indexi < n;indexi++)
{
for (indexj = indexi+1; indexj < n; indexj++)
{
if (stu[indexi]._scoreInfo.scorSum < stu[indexj]._scoreInfo.scorSum)
{
_temp = stu[indexj];
stu[indexj] = stu[indexi];
stu[indexi] = _temp;
}
}
}
printf("总分从高到低排列:\n");
for (indexi = 0 ; indexi < n; indexi++)
{
printf("%s\t%d\t\t%d\t\t%d\t\t%d\n",stu[indexi].name,stu[indexi]._scoreInfo.scoreMath, stu
[indexi]._scoreInfo.scoreProC
,stu[indexi]._scoreInfo.scoreEng,stu[indexi]._scoreInfo.scorSum);
}
}
int main()
{
int studentNum;
struct studentInfo stu[50];
printf("输入学生数:");
scanf("%d",&studentNum);
getchar();
inputInfo(stu,studentNum);
outputInfo(stu,studentNum);
arry_max_to_min(stu,studentNum);
return 0;
}
运行结果:
输入学生数:3
第1个学生的姓名:Jack
第1个学生的数学成绩:89
第1学生的C语言成绩:86
第1个学生的英语成绩:96
第2个学生的姓名:Tom
第2个学生的数学成绩:95
第2学生的C语言成绩:94
第2个学生的英语成绩:92
第3个学生的姓名:Star
第3个学生的数学成绩:98
第3学生的C语言成绩:78
第3个学生的英语成绩:89
姓名 数学成绩 C语言成绩 英语成绩 总分
Jack 89 86 96 271
Tom 95 94 92 281
Star 98 78 89 265
总分从高到低排列:
Tom 95 94 92 281
Jack 89 86 96 271
Star 98 78 89 265
Press any key to continue
/*
第二题:编写函数fun(char s[ ], int num[ ]),其功能是统计字符串 s 中数字字符、大写字母、
小写字母和空格字符的出现次数,统计结果存于num数组中。再设计main函数,调用fun函数,
实现预期功能。
*/
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
void fun(char *s, int *num)
{
int length;
int index;
int sum_number = 0;
int sum_char = 0;
int sum_spac = 0;
int sum_CHAR = 0;
length = strlen(s);
for (index = 0; index < length; index++)
{
if ((*s >= '0') && (*s <= '9') )
{
sum_number++;
}
if ((*s >= 'a') && (*s < 'z') )
{
sum_char++;
}
if ((*s >= 'A') && (*s <= 'Z') )
{
sum_CHAR++;
}
if (*s == 0x20 )
{
sum_spac++;
}
s++;
}
num[0] = sum_number;
num[1] = sum_CHAR;
num[2] = sum_char;
num[3] = sum_spac;
}
int main()
{
char testarry[50];
int num[4];//用来放数字num[0],num[1]大写字母,num[2]小写字母,num[3]空格数字的个数
//num = (int *)malloc(sizeof(int) * 4);
printf("请输入字符串:");
gets(testarry);
fun(testarry,num);
// printf("%s",testarry);
printf("数字的个数:%d\n大写字母的个数:%d\n小写字母的个数:%d\n空格数字的个数:%d\n",
num[0],num[1],num[2],num[3]);
return 0;
}
运行结果:
请输入字符串:12345 AFASDFE 74897 asfdasf
数字的个数:10
大写字母的个数:7
小写字母的个数:7
空格数字的个数:3
Press any key to continue
第三题:
#include "stdafx.h"
#include "stdio.h"
int main()
{
int i;
int j;
int k;
int half = 0;
char prin;
prin = 'A';
for (i = 0; i < 26; i++)
{
prin = 'A';
for (j = 26-i-1;j > 0; j--)
{
printf(" ");
}
for (k = 1; k<= 2*i+1 ;k++)
{
half = k;
if (--half <= i)
{
printf("%c",prin++);
}
else
{
printf("%c",--prin - 1);
}
}
printf("\n");
}
return 0;
}
运行结果:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
ABCDEFGHIJKJIHGFEDCBA
ABCDEFGHIJKLKJIHGFEDCBA
ABCDEFGHIJKLMLKJIHGFEDCBA
ABCDEFGHIJKLMNMLKJIHGFEDCBA
ABCDEFGHIJKLMNONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
Press any key to continue
[附加说明:]由于百度在栏目里屏蔽了空格和TAB,所以看不出效果,把
需要的话你可以把Email给我 我发给你。打印效果这个上面看不出来。呵呵
Press any key to continue