如何用c解决这类c++方便解决的问题?

drizztguai 2009-03-16 01:30:24

描述


线性表是n个元素的有序集合(n³0),n是线性表中元素的个数,称为线性表的长度。可以用一组地址连续的存储单元依次存储线性

表中元素,采用这种存储方式的线性表称为顺序表。

请在顺序表上实现运算,实现顺序表的逆置,删除表中所有元素值等于x的元素。


输入


三组数据,顺序表元素类型分别为整型、字符型和实型。

每一组第一行给出元素数目n(0<n≤1000),第二行给出元素数值,第三行给出待删除的元素。


输出


三组数据,每一组第一行为逆置后的顺序表元素,第二行是在此基础上删除指定元素后的顺序表元素


样例输入

8




1 2 3 7 5 6 7 8
7
3
a c m
h
4
1.2 3.4 5.6 7.8
1.2

样例输出

8 7 6 5 7 3 2 1
8 6 5 3 2 1
m c a
m c a
7.8 5.6 3.4 1.2
7.8 5.6 3.4

我想用c来实现,大概的程序也写出来了,但是问题的关键怎么用c语言判断要处理的是 整型 还是字符 还是浮点?
格式化输入输出感觉好无奈。

请高手指点下,谢谢


#include <stdio.h>
#define MAX 100

typedef float t;

typedef struct list{
int len;
t num[MAX];
} List;

int CreateList(List &lst,int n){
if(n>=MAX){
printf("EE!");
return 0;
}
lst.len=n;
return 1;
}

int Nixu(List &lst){
int i;
float t=0;
i=lst.len/2;
if(lst.len<2){
printf("EE");
return 0;
}
while(i+1){
t=lst.num[i];
lst.num[i]=lst.num[lst.len-1-i];
lst.num[lst.len-1-i]=t;
i--;
}
return 1;
}



int DELlist(List &lst,t x){
int i;
i=0;
if(x>=lst.len){
printf("EE");
return 0;
}
for(;i<lst.len;i++){
if(x==lst.num[i]){
i++;
while(i!=lst.len){
lst.num[i-1]=lst.num[i];
i++;
}
--lst.len;
return 1;
}
}
return 1;
}

int PrintLst(List Lst){
int i;
i=0;
while(Lst.len--){
printf("%f\n",Lst.num[i++]);
}
printf("\n");
return 1;
}


int main()
{
List lst;
int i;
float j=1.2;
CreateList(lst,5);
for(i=0;i<5;i++){
j+=0.5;
lst.num[i]=j;
}
PrintLst(lst);
Nixu(lst);
PrintLst(lst);
DELlist(lst,3.2);
PrintLst(lst);
return 0;

}
...全文
50 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_rockboy 2009-03-16
  • 打赏
  • 举报
回复
没什么好办法,最多模拟一下多态


enum
{
TYPE_INT, TYPE_FLOAT
};

typedef struct list{
int type;
int len;
t num[MAX];
} List;

int PrintIntLst(List Lst){
int i;
i=0;
while(Lst.len--){
printf("%d\n",Lst.num[i++]);
}
printf("\n");
return 1;
}

int PrintFloatLst(List Lst){
int i;
i=0;
while(Lst.len--){
printf("%f\n",Lst.num[i++]);
}
printf("\n");
return 1;
}

int PrintLst(List Lst){
switch(Lst.type)
{
case TYPE_FLOAT:
PrintFloatLst(Lst);
break;
case TYPE_INT:
PrintIntLst(Lst);
break;
....
}

...

int main()
{
List lst;
lst.type = TYPE_FLOAT;
...
PrintLst(lst);
...
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧