用C语言编写程序,完成如下功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言编写程序,完成如下功能相关的知识,希望对你有一定的参考价值。
要用C语言!!!
(1)用结构体存储学生信息(学号sno、姓名sname、年龄sage、班级sclass);
(2)用链表存储班级所有同学;
(3)实现新增、查找(根据学号)、修改、删除、打印名单的功能
#include<Windows.h>
struct STU
long sno;
char sname[8];
int sage;
int sclass;
struct STU *next;
;
int *pi;
void Print_slist_no(struct STU *h)
struct STU *p;
p=h->next;
if(p=='\\0')
printf("Linklist is null!\\n");
else
// printf("heead");
while(p!='\\0')
printf("%d\\t",p->sno);
p=p->next;
printf("end\\n");
void Print_slist_name(struct STU *h)
struct STU *p;
p=h->next;
if(p=='\\0')
printf("Linklist is null!\\n");
else
// printf("heead");
while(p!='\\0')
printf("%d\\t",p->sname);
p=p->next;
printf("end\\n");
void Print_slist_age(struct STU *h)
struct STU *p;
p=h->next;
if(p=='\\0')
printf("Linklist is null!\\n");
else
// printf("heead");
while(p!='\\0')
printf("%d\\t",p->sage);
p=p->next;
printf("end\\n");
void Print_slist_class(struct STU *h)
struct STU *p;
p=h->next;
if(p=='\\0')
printf("Linklist is null!\\n");
else
// printf("heead");
while(p!='\\0')
printf("%d\\t",p->sclass);
p=p->next;
printf("end\\n");
void Delete_STU_no(struct STU *h,int x)
#define PRO sno
struct STU *p,*q;
q=h;p=h->next;
if(p!='\\0')
while((p!='\\0')&&(p->PRO!=x))
q=p;p=p->next;
if(p->PRO==x)
q->next=p->next;free(p);
#undef PRO
void Delete_STU_name(struct STU *h,int x)
#define PRO sname
struct STU *p,*q;
q=h;p=h->next;
if(p!='\\0')
while((p!='\\0')&&(p->PRO!=x))
q=p;p=p->next;
if(p->PRO==x)
q->next=p->next;free(p);
#undef PRO
void Delete_STU_age(struct STU *h,int x)
#define PRO sage
struct STU *p,*q;
q=h;p=h->next;
if(p!='\\0')
while((p!='\\0')&&(p->PRO!=x))
q=p;p=p->next;
if(p->PRO==x)
q->next=p->next;free(p);
#undef PRO
void Delete_STU_class(struct STU *h,int x)
#define PRO sclass
struct STU *p,*q;
q=h;p=h->next;
if(p!='\\0')
while((p!='\\0')&&(p->PRO!=x))
q=p;p=p->next;
if(p->PRO==x)
q->next=p->next;free(p);
#undef PRO
void Insert_STU(struct STU *h,int x,int y)
struct STU *s,*p,*q;
s=(struct STU *)malloc(sizeof(struct STU));
s->sage=y;
q=h;
p=h->next;
while((p!='\\0') && (p->sage!=x))
q=p;p=p->next;
q->next=s;s->next=p;
void main()
int x;
struct STU *phead,*s,*r;
phead=(struct STU*)malloc(sizeof(struct STU));
r=phead;
scanf("%d",&x);
while(x>=0)
s=(struct STU*)malloc(sizeof(struct STU));
s->sage=x;
r->next=s;
scanf("%d",&x);
r->next='\\0';
Print_slist_no(s);
Print_slist_name(s);
Print_slist_age(s);
Print_slist_class(s);
追问
好厉害,大神能不能加一些注释?我有些地方看不太明白。。。
参考技术A #include <iostream>#include <list>
#include <string>
using namespace std;
class student
private:
char* snum;//学号
char* sname;//名字
char* sage;//年龄
char* sclass;//班级
public:
void set_num(char* num)
this->snum=num;
;
char* get_num()
return this->snum;
;
void set_name(char* name)
this->sname=name;
char* get_name()
return this->sname;
;
void set_age(char *age)
this->sage=age;
;
char *get_age()
return this->sage;
;
void set_class(char* classs)
this->sclass=classs;
;
char* get_class()
return this->sclass;
;
;
class all_student
protected:
list<student> all_student1;
public:
list<student> get_all_student()
return all_student1;
;
void add_student(student stu)
all_student1.push_back(stu);
void find_student_by_num(char* num)
for(list<student>::iterator it=all_student1.begin();it!=all_student1.end();it++)
if(!strcmp((*it).get_num(),num))
printf("学号:%s\n",(*it).get_num());
printf("姓名:%s\n",(*it).get_name());
printf("年龄:%s\n",(*it).get_age());
printf("班级:%s\n",(*it).get_class());
break;
;
;
int main()
int num=0;
int i=0;
char snum[20];//学号
char sname[20];//名字
char sage[20];//年龄
char sclass[20];//班级
int flag=1;
all_student *all_stu=new all_student();
while(true)
if(flag==1)
printf("输入录入学员总数\n");
scanf("%d",&num);
flag=0;
student *stu=(student *)malloc(num*sizeof(student));
// student *stu=new student[num];
while(i<num)
printf("学员[%d]学号:\n",i);
scanf("%s",&snum);
printf("学员[%d]=名字:\n",i);
scanf("%s",&sname);
printf("学员[%d]=年龄:\n",i);
scanf("%s",&sage);
printf("学员[%d]=班级:\n",i);
scanf("%s",&sclass);
stu[i].set_num(snum);
stu[i].set_name(sname);
stu[i].set_age(sage);
stu[i].set_class(sclass);
printf("%s,%s,%s,%s\n",stu[i].get_num(),stu[i].get_name(),stu[i].get_age(),stu[i].get_class());
all_stu->add_student(stu[i]);
i++;
printf("all_stu->get_all_student().size()=%d\n",all_stu->get_all_student().size());
printf("请输入要查找的学员学号!");
char find_num[20];
scanf("%s",&find_num);
all_stu->find_student_by_num(find_num);
return 0;
其中部分代码 参考技术B 看这里吧,基本全了:http://www.cnblogs.com/yezhenhan/archive/2012/06/16/2551622.html
用数组编写C语言程序
输入一个字符串,要求按相反的顺序输出各个字符。
参考技术A 程序如下:int main(void)
int n;
char str[100];
gets(str);//输入字符串
n=strlen(str);
for (int i=0;i<n;i++)
putc(str[n-1-i]);//倒序输出
参考技术B main()
int n,i;
char str[100];
printf("Please input the string:");
gets(str);
n=strlen(str);
for(i=n-1;i>=0;i--)
printf("%s",str[i]);
本回答被提问者采纳
以上是关于用C语言编写程序,完成如下功能的主要内容,如果未能解决你的问题,请参考以下文章