散点方程 2022-03-10 11:38 采纳率: 50%
浏览 139
已结题

C语言typedef int ElemType替换问题

将实验中:
typedef struct node{ //定义数据元素类型
int num; //学号
char name[10]; //姓名
float s; //成绩
}ElemType;

替换为:
typedef int ElemType; //定义数据元素类型

然后实现以下功能:
1:创建定长学生信息表;
2:取学号为n的学生信息;
3:修改学号为n的学生的成绩;
4:判断学生表是否按学号递减有序;
5:判断学生表是否按成绩递增排序;
6:将学生信息表按姓名递增排序;
0:结束。

源码:
#include "stdio.h"
#include "stdlib.h" //包含动态分配头函数
#include "string.h"
#include "time.h"
#define MAX 5

typedef struct node{ //定义数据元素类型
int num; //学号
char name[10]; //姓名
float s; //成绩
}ElemType;
typedef ElemType *Triplet; //采用动态分配的顺序存储结构表示三元组
typedef int Status;

#define OK 1
#define ERROR 0
#define OVERFLOW -2

static int NUM=0;

//Status InitTriplet (Triplet);
//Status DestroyTriplet(Triplet);
//Status Get(Triplet,int,ElemType*);
//Status Put(Triplet,int,ElemType*);
//Status IsAscending(Triplet);
//Status IsDescending(Triplet);
//void sort_name(Triplet,ElemType);
void Print(Triplet);

Status InitTriplet (Triplet T){ //构造三元组T
int n=1;
(T)=(ElemType)malloc(MAXsizeof(ElemType));
srand((unsigned)time(NULL));
if ((*T)==NULL)
return OVERFLOW; //存储分配失败
while(n<=MAX-1){
//if(n==0) {*T=NULL;n++;continue;}
printf("\n请输入学号( 学号小于等于 0 则停止录入 ): ");
scanf("%d",&((*T)[n].num));
if((*T)[n].num<=0)
break;
printf("请输入姓名: ");
scanf("%9s",&((*T)[n].name[0]));
printf("请输入成绩( 随机生成 ): ") ;
(*T)[n].s=(float)(rand()%100);
printf("%.2f\n",(*T)[n].s);
n++;
NUM++;
}
return OK;
}

Status DestroyTriplet(Triplet T){ //销毁三元组T
free(T);
T=NULL;
return OK;
}

Status Get(Triplet T,int i,ElemType *e){ //用e返回T的第i个元的值
int _num=NUM;
T[0].num=i;
while(T[_num].num!=i)
_num--;
if ( _num==0)
return ERROR;
*e=T[i];
return OK;
}

Status Put(Triplet T,int i,ElemType *e){ //修改三元组第i个元的值为e
int _num=NUM;
T[0].num=i;
while(T[_num].num!=i)
_num--;
if ( _num==0)
return ERROR;
T[_num].s=(*e).s;
return OK;
}

Status IsAscending(Triplet T){ //判断学生表是否按学号递减
int f=1,_num=NUM;
for(;_num>0;_num--){
if(T[_num].num>T[_num-1].num){
f=0;
return f;
}
}
return f;
}

Status IsDescending(Triplet T){ //判断学生表是否按成绩递增
int f=1,_num=NUM;
for(;_num>0;_num--){
if(T[_num].s<T[_num-1].s){
f=0;
return f;
}
}
return f;
}

void sort_name(Triplet T,ElemType e){ //按姓名递增排序
int i,j,f;
for(i=1;i<NUM+1;i++){
f=0;
for(j=NUM;j>i;j--){
if(strcmp(T[j-1].name,T[j].name)<0){
e=T[j];
T[j]=T[j-1];
T[j-1]=e;
f=1;
}
}
if(f==0)
break;
}
}

void Print(Triplet T){
for(int i=1;i<=NUM;i++){
printf("\n学号: %d\t",T[i].num);
printf("姓名: %s\t",T[i].name);
printf("成绩: %.2f",T[i].s);
}
printf("\n\n");
}

void Print_Elem(ElemType *e){
printf("\n学号: %d\t",e->num);
printf("姓名: %s\t",e->name);
printf("成绩: %.2f",e->s);
printf("\n\n");
}

void main(){
Triplet T=NULL;
ElemType e;
int select, i;
printf("输入三个数,建立一个三元组: \n");
if(InitTriplet(&T)==OVERFLOW)
printf("分配失败,退出程序! ");
else //否则显示操作菜单,输入操作选择,直到结束
do {
printf("\n*\n");
printf("\t1:打印输出学生信息\t\n");
printf("\t2:取学号为i的学生信息\t\n");
printf("\t3:修改学号为i的学生的成绩\t\n" );
printf("\t4:判断学生表是否按学号递减有序\t\n");
printf("\t5:判断学生表是否按成绩递增排序\t\n");
printf("\t6:将学生表按姓名递增排序\t\n");
printf("\t0:结束\t\n");
printf("*\n");
scanf("%d",&select);
switch (select){
case 1:
printf("学生信息表为: \n");
Print(T);
break;
case 2:
do{
printf("i= ");
scanf("%d",&i);
if(Get(T,i,&e)==ERROR)
printf("i值不合法!\n");
}while(i<=0||i>NUM);
Print_Elem(&e);
break;
case 3:
printf("请输入待修改学生的学号信息和修改后成绩信息( 学号和成绩逗号间隔 ): ");
scanf("%d,%f",&i,&(e.s));
if(Put(T,i,&e)==ERROR)
printf("i值不合法\n");
else{
printf("修改第%d位置后,该学生表内容为: \n",i);
Get(T,i,&e);
}
break;
case 4:
if(IsAscending(T)==1)
printf("学生表按学号递减有序!\n");
else
printf("学生表不按学号递减有序!\n");
break;
case 5:
if(IsDescending(T)==1)
printf("学生表按成绩递增排序!\n");
else
printf("学生表不按成绩递增排序!\n");
break;
case 6:
sort_name(T,e);
printf("排序完毕,排后结果为: \n");
Print(T);
break;
case 0:
printf("操作结束!\n");
break;
default:
printf("输入选择出错!\n");
}//switch
}while(select!=0);
DestroyTriplet(T); //销毁三元组
}//main

  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-03-10 16:52
    关注

    typedef
    typedef type-declaration synonym;

    The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition.

    A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.

    Example

    // Example of the typedef keyword
    typedef unsigned long ulong;

    ulong ul; // Equivalent to "unsigned long ul;"

    typedef struct mystructtag
    {
    int i;
    float f;
    char c;
    } mystruct;

    mystruct ms; // Equivalent to "struct mystructtag ms;"

    typedef int (*funcptr)(); // funcptr is synonym for "pointer
    // to function returning int"

    funcptr table[10]; // Equivalent to "int (*table[10])();"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月2日
  • 已采纳回答 5月25日
  • 提问应符合社区要求 3月10日
  • 创建了问题 3月10日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来