1、编写一个函数,删除一个字符串的指定字符,先判断是否存在,如果未出现则返回0,字符存在一次或多次,返回字符个数。
#include<cstdio>
int delStr(char *str, char ch){
char *p;
int n=0;
p = str;
while (*p != '\0')
{
if (*p == ch)
{
char *q;
q = p;
n++;//计算要删除字符个数
for (; *p != '\0'; p++)
{
*p = *(p + 1);
}
p = q;
}
else
{
p++;
}
}
return n;
}
int main(){
char str[20];
gets(str);
char ch;
scanf("%c", &ch);
int n=delStr(str, ch);
printf("%s删除了%d个字符\n", str, n);
return 0;
}
2、编写函数把参数字符串中的字符反序排列(使用指针)。
#include<cstdio>
#include<string>
void reverseter(char *str){
char *p, *q, ch;
p = str;
q = str;
while (*q != '\0')
{
q++;
}
q--;
while (p!=q)
{
ch = *p;
*p = *q;
*q = ch;
p++;
q--;
}
}
int main(){
char str[20];
gets(str);
reverseter(str);
printf("%s\n", str);
return 0;
}
3、从键盘输入10本书的名称和定价,存在结构体数组中,从中查找出定价最高和最低的书的名称和定价,并打印出来。
#include<cstdio>
struct Book
{
char name[20];
float price;
};
int main(){
Book book[10];
for (int i = 0; i < 10; i++)
{
scanf("%s %f", book[i].name, &book[i].price);
}
Book maxp, minp;
maxp = book[0];
minp = book[0];
for (int i = 0; i < 10; i++)
{
if (book[i].price >maxp.price)
{
maxp = book[i];
}
if (book[i].price <minp.price)
{
minp = book[i];
}
}
printf("%s is the max price :%f\n", maxp.name, maxp.price);
printf("%s is the min price :%f\n", minp.name, minp.price);
return 0;
}
4、编写两个函数,函数fun1把10名学生放在一个结构体数组中,从键盘输入学生的学号、姓名和四门成绩,然后计算出平均分放在结构体对应的域中,函数fun2输出10名学生的记录。
#include<cstdio>
struct Student
{
char sno[20];
char name[10];
float grade[4];
float ave;
};
void fun1(Student *student){
printf("please input sno and name:\n");
for (int i = 0; i < 5; i++)
{
int sum = 0;
scanf("%s %s", student[i].sno, student[i].name);
printf("please input the student 4 grades:\n");
for (int j = 0; j < 4; j++)
{
scanf("%f", &student[i].grade[j]);
sum = sum + student[i].grade[j];
}
student[i].ave = sum / 4.0;
}
}
void fun2(Student *student){
for (int i = 0; i < 5; i++)
{
printf("%s %s\n", student[i].sno, student[i].name);
printf("the student grade is:\n");
for (int j = 0; j < 4; j++)
{
printf("%f ", student[i].grade[j]);
}
printf("\n");
printf("the ave grade is:%f\n", student[i].ave);
}
}
int main(){
Student student[10];
fun1(student);
fun2(student);
return 0;
}
5、单链表逆置,不带头结点。
List Reverse( List L ){
struct Node *p,*q;
p=L;
q=L->Next;
L->Next =NULL;
while(q){
p=q;
q=q->Next;
p->Next = L;
L=p;
}
return L;
}
6、链式表操作集(不带头结点)
Position Find(List L, ElementType X){
struct LNode *p;
p = L;
while (p){
if (p->Data == X){
return p;
}
p = p->Next;
}
return ERROR;
}
List Insert(List L, ElementType X, Position P){
struct LNode *p, *q;
q = (List)malloc(sizeof(struct LNode));
q->Data = X;
q->Next = NULL;
p = L;
if (P == L){
q->Next = L;
return q;
}
while (p != NULL){
if (p->Next == P){
q->Next = p->Next;
p->Next = q;
return L;
}
p = p->Next;
}
printf("Wrong Position for Insertion\n");
return ERROR;
}
List Delete(List L, Position P){
struct LNode *p;
p = L;
if (L == P){
L = L->Next;
return L;
}
while (p != NULL){
if (p->Next == P){
p->Next = P->Next;
return L;
}
p = p->Next;
}
printf("Wrong Position for Deletion\n");
return ERROR;
}
7、二叉树的遍历。
void InorderTraversal( BinTree BT ){
if(BT){
InorderTraversal(BT->Left);
printf(" %c",BT->Data);
InorderTraversal(BT->Right);
}
}
void PreorderTraversal( BinTree BT ){
if(BT){
printf(" %c",BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
}
void PostorderTraversal( BinTree BT ){
if(BT){
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c",BT->Data);
}
}
void LevelorderTraversal( BinTree BT ){
if(!BT)
return;
BinTree queue[100];
int front,rear;
front=0;
rear=0;
queue[rear++]=BT;
while(front<rear){
if(queue[front]->Left)
queue[rear++]=queue[front]->Left;
if(queue[front]->Right)
queue[rear++]=queue[front]->Right;
printf(" %c",queue[front++]->Data);
}
}
8、二进制转化为十进制(多一个0乘2,如果多一个1则乘2再加1)
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(){
//int n;
char s[105];
scanf("%s", &s);
int ans = 0;
int len = strlen(s);
for (int i = 0; i <len; i++)
{
//if (s[i] == '0')
//ans = ans * 2;
//else
//ans = ans * 2 + 1;
ans = ans * 2 + (s[i] - '0');
}
cout << ans << endl;
return 0;
}
9、x进制转换为十进制
#include<cstdio>
#include<string>
using namespace std;
int main(){
int x, y = 0;
char str[100];
scanf("%s %d", str, &x);
int len = strlen(str);
for (int i = 0; i < len; i++)
{
if (str[i] >= '0' && str[i] <= '9'){
y = y*x + (str[i] - '0');
}
else
{
y = y*x + (str[i] - 'A')+10;
}
}
printf("y=%d\n", y);
return 0;
}
10、十进制转换成Y进制
#include<cstdio>
int main(){
char str[50];
int n, y,sum,m=0;
scanf("%d %y", &n,&y);
while (n>0)
{
sum = n%y;
if (sum < 10){
str[m++] = sum + '0';
}
else
{
str[m++] = 'A' + sum - 10;
}
n = n / y;
}
for (int i = m-1; i >=0; i--)
{
printf("%d", str[i]);
}
return 0;
}