1. 请在控制台实现如下功能。
*
**
***
****
答:因为在控制台中,不存在半字符操作(最小显示单位就是字符,一个汉字由占据两个字符的位置),所以我并没有想出半字符缩进的实现方式。
程序:
#include "stdafx.h"
int main()
{
printf(" * \n"); //
printf(" * * \n");
printf(" * * * \n");
printf("* * * *\n");
return 0;
}
结果截图:

2.效果与第一题结果相同,但是打印的行数由程序运行时确定。
例如:
程序运行时输入2
*
**
程序运行时输入3
*
**
***
答:
程序源码:
#include "stdafx.h"
#include <stdio.h>
void show_stars(int num);
int main()
{
// 变量定义与初始化
int line_num = 0;
// 打印输入提示语句
printf("Please input the lines you wish to show and\n"\
"press Enter to confirm:");
// 接收行数变量
scanf("%d", &line_num);
// 调用打印星号函数
show_stars(line_num);
return 0;
}
void show_stars(int num)
{
// 判定输入的行数是否为正数,不是则给出错误提示
if (num <= 0){
printf("Please input a valid integer that is bigger\n"\
"than 0 and retry!");
}
else{
// 循环中逐行打印星号
for (int i = 0; i < num; i++) {
// 对于第i行,该行的第一个星号之前需要键入空格
for (int j = 0; j < num - 1 - i; j++) {
printf(" ");
}
// 之后输入星号和空格的组合
for (int j = 0; j < i; j++) {
printf("* ");
}
// 最后补上每行最后一个星号以及换行符
printf("*\n");
}
}
}
结果截图:


3.请实现2个函数完成BCD码到二进制格式的转换和二进制到BCD转换功能,并自己编写测试。
答:
程序源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Bin2BCD(char *str);
int BCD2Dec(char *str);
int main(void)
{
int mode = 0;
int num_length = 0;
printf("Please Input the length you wish to convert:");
scanf("%d", &num_length);
char *str_input = new char[num_length];
char *str_output = new char[num_length];
printf("BCD2Bin press 1 and Bin2BCD press 2 and press Enter to confirm:");
scanf("%d", &mode);
if (mode == 1) {
printf("Now Input the 8421 code you wish to convert to binary:");
scanf("%s", str_input);
printf("The result is:\n%s\n", _itoa(BCD2Dec(str_input), str_output, 2));
}
else if(mode == 2) {
printf("Now Input the binary you wish to convert to 8421 code:");
scanf("%s", str_input);
printf("The result is:\n%s\n", _itoa(Bin2BCD(str_input), str_output, 2));
}
else {
printf("Wrong Code!");
}
return 0;
}
int Bin2BCD(char *str)
{
int num_return = 0;
char *str_local = str;
while (*str_local != '\0') {
num_return = (num_return << 1) + (*str_local - 0x30);
str_local++;
}
int num_return2 = 0;
int index = 1;
while (num_return != 0) {
num_return2 += num_return % 10 * index;
num_return /= 10;
index *= 16;
}
return num_return2;
}
int BCD2Dec(char *str)
{
char *str_local = str;
char str_4[4] = { 0 };
int num_return = 0;
while (*str_local != '\0') {
strncpy(str_4, str_local,4);
num_return = 10 * num_return + 8 * (str_4[0] - 0x30) + 4 * (str_4[1] - 0x30) +
2 * (str_4[2] - 0x30) + 1 * (str_4[3] - 0x30);
str_local += 4 * sizeof(char);
}
return num_return;
}
结果图:


4.请利用宏来实现3题功能。
答:
程序源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ToDec(x) (x%10 + (x/10%10)*2 + x/100%10*4 + x/1000*8)
#define BCD2Bin(x) _itoa(ToDec(x%10000) + ToDec(x/10000)*10, str_output, 2)
#define Bin2BCD(x) _itoa((ToDec(x)>10)?ToDec(x)/10*16+ToDec(x)%10:ToDec(x),str_output,2)
int main(void)
{
int mode = 0;
int str_input;
char str_output[8];
int x = 10101;
int y = ToDec(x);
int z = ToDec(x % 1000) + ToDec(x / 1000) * 10;
printf("BCD2Bin press 1 and Bin2BCD press 2 and press Enter to confirm:");
scanf("%d", &mode);
if (mode == 1) {
printf("Now Input the 8421 number you wish to convert to binary:");
scanf("%d", &str_input);
printf("The result is:\n%04s\n", BCD2Bin(str_input));
}
else if(mode == 2) {
printf("Now Input the binary you wish to convert to 8421 code:");
scanf("%d", &str_input);
printf("The result is:\n%08s\n", Bin2BCD(str_input));
}
else {
printf("Wrong Code!");
}
return 0;
}
效果截图:


5.请利用来实现一个抽象数据类型的链表,他可以存放任意自定义数据类型,至少实现指定位置插入和制定位置得到数据,以及制定位置删除。
#include <stdio.h>
struct Node
{
struct Node* next;
void* data;
};
typedef struct bearList
{
bearList() {
head = new Node;
head->data = 0;
head->next = NULL;
}
~bearList() { delete head; }
void createbearList(int n);
void InsertNode(int position, int d);
void *GetNode(int position);
int GetLength();
void DeleteNode(int position);
void DeleteList();
struct Node *head;
} bear;
void bearList::createbearList(int n)
{
if (n < 0) {
printf("Input Wrong!");
}
else
{
Node *pnew, *ptemp;
ptemp = head;
int i = n;
while (n-- > 0) {
pnew = new Node;
printf("Input No.%d data for the list.", i - n);
scanf("%d", (int*)&((pnew->data)));
pnew->next = NULL;
ptemp->next = pnew;
ptemp = pnew;
}
}
}
void bearList::InsertNode(int position, int d)
{
if (position < 0 || position > GetLength() + 1) {
printf("Wrong Position!");
}
else {
Node *pnew, *ptemp;
ptemp = head;
pnew = new Node;
(pnew->data) = (void*)d;
pnew->next = NULL;
while (position-- > 1) {
ptemp = ptemp->next;
}
pnew->next = ptemp->next;
ptemp->next = pnew;
}
}
void * bearList::GetNode(int position)
{
Node *ptemp;
ptemp = head->next;
int i = 1;
while (position - i++) {
ptemp = ptemp->next;
}
return ptemp->data;
}
int bearList::GetLength()
{
Node *p = head->next;
int n = 0;
while (p != NULL) {
n++;
p = p->next;
}
return n;
}
void bearList::DeleteNode(int position)
{
if (position < 0 || position > GetLength()) {
printf("Wrong Position!");
}
else {
Node *ptemp = head, *pdelete;
int i = position;
while (i-- > 1) {
ptemp = ptemp->next;
}
pdelete = ptemp->next;
ptemp->next = pdelete->next;
delete pdelete;
pdelete = NULL;
}
}
void bearList::DeleteList()
{
int i = GetLength();
while (i-- > 0) {
DeleteNode(i);
}
}
int main(void)
{
bearList bear;
bear.createbearList(3);
bear.InsertNode(2,5);
int i = (int)bear.GetNode(2);
bear.DeleteNode(3);
bear.DeleteList();
return 0;
}
结果:
1. 初始化链表

2. 在位置2处插入数据5

3. 查询位置2处的数据

4. 删除位置3的数据
5. 清空空链表
