
C语言程序设计题,共两个。求助。
2,“china”,”japan”,”korea”,”pakistan”,”vietnam”,“thailand”等六个国家按升序排序并打印输出。 展开
1. 第一题,我估计你的问题没人回答的很大一部分原因就是因为第一题,难度不高,代码量不小,分数也不多,所以嘛,望而却步很正常,所以,这个苦差事还是我给你解决了吧。源码及注释如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LIMIT 32 /* 姓名长度限制 */
/* 学生信息结构体 */
typedef struct student_t
{
int serial; /* 学号 */
int age; /* 年龄 */
float score; /* 成绩 */
char name[NAME_LIMIT]; /* 姓名 */
} stu_t;
/* 链表节点结构体 */
typedef struct node_t
{
stu_t info;
node_t* next;
} node_t;
/* 链表头指针 */
node_t *head = NULL;
/* 头插法 */
void insert_head(node_t *node)
{
if (NULL == node)
{
perror("node is null.\n");
return;
}
if (NULL == head) head = node;
else
{
node->next = head;
head = node;
}
}
/* 输入单个学生信息 */
int input(node_t *node)
{
if (NULL == node)
{
perror("node is null.\n");
return 1;
}
memset(node, 0, sizeof(*node)); /* 初始化 */
int tmp = 0;
printf("student number: ");
scanf("%d", &tmp);
if (tmp < 0) return 1;
else node->info.serial = tmp;
printf("student name: ");
scanf("%s", node->info.name);
printf("student age: ");
scanf("%d", &(node->info.age));
printf("student score: ");
scanf("%f", &(node->info.score));
printf("\n");
return 0;
}
/* 显示单个学生信息 */
void display(stu_t *info)
{
if (NULL != info)
printf("%05d\t%10s\t%2d\t%3.1f\n", info->serial, info->name, info->age, info->score);
}
/* 显示所有学生信息 */
void display_all()
{
node_t *p = head;
if (NULL == p)
{
printf("Nothing to display.\n");
return;
}
while (NULL != p)
{
display(&p->info);
p = p->next;
}
}
/* 删除单个学生信息(比对学号) */
void remove(int number)
{
node_t *p = head;
node_t *tmp = NULL;
while (NULL != p)
{
if (p->info.serial == number)
{
if (NULL == tmp) head = p->next;
else tmp->next = p->next;
p->next = NULL;
free(p); /* 清理内存 */
printf("remove student(%d) success.\n", number);
return;
}
tmp = p;
p = p->next;
}
printf("no student with number %d\n", number);
}
/* 操作菜单 */
void menu()
{
char choice[255] = {0};
printf("====================================================\n"
"1. Add students(escape with negative serial number).\n"
"2. Delete\n"
"3. Display\n"
"0. Exit\n"
"input your choice: ");
scanf("%s", choice);
printf("====================================================\n");
switch (choice[0])
{
case '1':
{
node_t *stu = NULL;
while (1)
{
stu = (node_t *)malloc(sizeof(node_t));
if (0 == input(stu)) insert_head(stu);
else
{
free(stu); /* 结束输入,清理内存 */
break;
}
}
break;
}
case '2':
{
int no = 0;
printf("input the student number you want to remove: ");
scanf("%d", &no);
remove(no);
break;
}
case '3':
display_all();
break;
case '0':
exit(0);
break;
default:
break;
}
}
/* 主函数 */
int main()
{
while (1) menu();
return 0;
}
====================================
运行结果见附图
====================================
2. 这个题目就轻松多了,一个strcmp和一个strcpy两个库函数就可以解决问题了,源码如下:
int main()
{
char *oldc[20] = {"china","japan","korea","pakistan","vietnam","thailand"};
char newc[6][20] = {0}; /* 保存排序后的国家 */
int i = 0, j = 0;
char tmp[20];
while (i < 5)
{
j = i + 1;
while (j < 6)
{
if (0 < strcmp(oldc[i], oldc[j]))
{
strcpy(tmp, oldc[i]);
strcpy(newc[i], oldc[j]);
strcpy(newc[j], tmp);
}
else
{
strcpy(newc[i], oldc[i]);
strcpy(newc[j], oldc[j]);
}
j++;
}
i++;
}
i = 0;
while (i < 6)
{
printf("%s\n", newc[i++]);
}
return 0;
}
==============================
排序后,结果如下:
china
japan
korea
pakistan
thailand
vietnam
============================
希望我的回答能够给你帮助。