谁能给些C\C++的练习题
谁能给些C\C++的练习题!要求不能太深奥的,我刚学不久,就像那些一个人去卖苹果,一个苹果多少钱,然后他带了多少钱,什么什么的之内的,只要编程题!不要填空题!
追问:blood_ants@126.com 谢谢了!
谁能给些C\C++的练习题!要求不能太深奥的,我刚学不久,就像那些一个人去卖苹果,一个苹果多少钱,然后他带了多少钱,什么什么的之内的,只要编程题!不要填空题!
追问:blood_ants@126.com 谢谢了!
答案:请用面向过程的方式进行编程1.求某数的平方 int square(int x)
2.交换两个数 void swap(int *x, int *y)
3.复制字符串 void copy_str(char *p1, char *p2)
4.判断是否包含子字符串 int find_substr(char* s1, char* s2)
5.反序 void inv(int *x, int n)
6.排序
7.矩阵的转置 void convert(int element[N][N])
8.计算的二叉树的叶子结点个数 int leaf_count(btree t)
9.统计若干个学生的数学课考试平均分
10.用两个栈模拟队列
===============================================================
//求某数的平方
# include <stdio.h>int square(int x);
void main()
{
int m = 12;
printf("%d %d\n", square(m), m);
}int square(int x)
{
x = x*x;
return x;
}
===============================================================
//交换两个数
# include <stdio.h>void swap(int *x, int *y);
void main()
{
int i, j;i = 12;
j = 36;printf("i and j before swapping: %d %d\n", i, j);
swap(&i, &j);
printf("i and j after swapping: %d %d\n", i, j);
}void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
==========================================================
/* 将字符串a复制到字符串b */
# include <stdio.h>void copy_str(char *a, char *b);
void main()
{
char a[] = "I am a student.";
char b[20];copy_str(a,b);
printf("string a is: %s\n", a);int i;
printf("string b is: ");
for(i=0; b[i]!='\0'; i++)
printf("%c", b[i]);
printf("\n");
}void copy_str(char *p1, char *p2)
{
for(; *p1!='\0'; p1++, p2++)
*p2 = *p1;
*p2 = '\0';
}
===============================================================
/* 包含子字符串 */
# include <stdio.h>int find_substr(char* s1, char* s2);
void main()
{
if(find_substr("C is fun", "is") != -1)
printf("Substring is found.\n");
else
printf("Substring is not found.\n");
}int find_substr(char* s1, char* s2)
{
int t;
char *p, *p2;
for(t=0; s1[t]; t++)
{
p = &s1[t];
p2 = s2;
//对比字符串
while(*p2 && *p2==*p)
{
p++;
p2++;
}
if(! *p2)
return t;
}
return -1;
}
==========================================================
//反序
# include <stdio.h>void inv(int *x, int n);
void main()
{
int i;
int array[10] = {1, 3, 9, 11, 0, 8, 5, 6, 14, 98};
printf("原始数组是:\n");
for(i=0; i<10; i++)
printf("%d", array[i]);
printf("\n");inv(array, 10);
printf("按相反次序存放后的数组为:\n");
for(i=0; i<10; i++)
printf("%d", array[i]);
printf("\n");
}void inv(int *x, int n)
{
int *p, *i, *j;
int t;
int m = (n-1)/2;
i = x; //头部
j = x + n - 1; //尾部
p = x + m; //中间
for(; i<=p; i++, j--)
{
t = *i;
*i = *j;
*j = t;
}
}
===============================================================
/* 使用选择法排序 */
# include <stdio.h>void main()
{
int i, j, min, temp;
int array[10];
/* 输入 */
printf("Please input ten integer: \n");
for(i=0; i<10; i++)
{
printf("array[%d] = ", i);
scanf("%d", &array[i]);
}
printf("The array is: ");
for(i=0; i<10; i++)
printf("%d ", array[i]);
printf("\n");/* 排序 */
for(i=0; i<9; i++)
{
min = i;
for(j=i; j<10; j++)
if(array[min]>array[j]) min = j;
temp = array[i];
array[i] = array[min];
array[min] = temp;
}
/* 输出 */
printf("\nThe result: \n");
for(i=0; i<10; i++)
printf("%d ", array[i]);
printf("\n");
}
===============================================================
/* 矩阵的转置 */
# include <stdio.h>
# define N 3void convert(int element[N][N]);
void main()
{
int array[N][N];int i, j;
printf("输入数组元素:\n");
for(i=0; i<N; i++)
for(j=0; j<N; j++)
scanf("%d", &array[i][j]);
printf("\n数组是:\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%5d", array[i][j]);//不足的位用空格代替,美观
printf("\n");
}convert(array);
printf("转置数组是:\n");
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%5d", array[i][j]);
printf("\n");
}
}/* 转置函数定义 */
void convert(int element[N][N])
{
int i, j, t;
for(i=0; i<N; i++)
for(j=i+1; j<N; j++)
{
t = element[i][j];
element[i][j] = element[j][i];
element[j][i] = t;
}
}
===============================================================
//计算的二叉树的叶子结点个数int bt_leaf_count(btree tree)
{
int num1, num2;
if (tree == NULL)
return 0;
else if (tree->lchild == NULL && tree->rchild == NULL)
return 1;
else
{
num1 = bt_leaf_count(tree->lchild);
num2 = bt_leaf_count(tree->rchild);
return (num1 + num2);
}
}
===============================================================
//计算的二叉树的叶子结点个数/ s1是容量为n的栈,栈中元素类型是elemtp。本函数将x入栈,若入栈成功返回1,否则返回0。
int enqueue( stack s1, elemtp x )
{
if( top1==n && !Sempty(s2) ) // top1是栈s1的栈顶指针,是全局变量
{
// s1满、s2非空,这时s1不能再入栈
printf(“栈满”);
return(0);
}
if( top1==n && Sempty(s2) ) // 若s2为空,先将s1退栈,元素再压栈到s2
{
while( !Sempty(s1) )
POP( s1, x );
PUSH( s2, x );
}
PUSH( s1, x ); // x入栈,实现了队列元素的入队
return(1);
}// s2是输出栈,本函数将s2栈顶元素退栈,实现队列元素的出队
void dequeue( stack s2, stack s1 )
{
&