- 求一维数组中的第二大值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
#if 0
int num[6];
int i = 0;
int j = 0;printf("数组中元素为:");
for(i=0;i < 6;i++){
printf("%d ",num[i]);
}
putchar(10);int tmp = 0;
for(i=1;i<sizeof(num)/sizeof(int);i++){
for(j=0;j<sizeof(num)/sizeof(int)-i;j++){
if(num[j]>num[j+1]){
tmp = num[j];
num[j] = num[j+1];
num[j+1]=tmp;
}
}
}printf("数组中第二大元素为:%d\n",num[4]);
#endif#if 1
int num[6];
int i = 0;
int index = 0;
int min = num[0];printf("数组中元素为:");
for(i=0;i < 6;i++){
printf("%d ",num[i]);
}
putchar(10);
for(i=0;i<sizeof(num)/sizeof(int);i++){
if(num[i]>num[index]){
index = i;
}
if(num[i]<min){
min = num[i];
}
}num[index] = min;
for(i=0;i<sizeof(num)/sizeof(int);i++){
if(num[i]>num[index]){
index = i;
}
}printf("数组中第二大元素为:%d\n",num[index]);
#endif
return 0;
}2、终端输入带空格的字符串,删除字符中的空格
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
#if 1
char str[20] = {0};gets(str);
int i = 0;
int j = 0;
int tmp = 0;for(i=1;i<strlen(str);i++){
for(j=0; j <=strlen(str)-i;j++){
if(str[j]==' '){
tmp = str[j];
str[j] = str[j+1];
str[j+1] = tmp;
}
}
}puts(str);
#endif
#if 0
char str[20] = {0};
int i = 0;gets(str);
int index = 0;
while(str[index]!='\0'){
while(str[index]!=' '&&str[index]!='\0'){
index++;
}
for(i=index;i<=strlen(str);i++){
str[i]=str[i+1];
}
}printf("%s\n",str);
#endif
return 0;
}3、自己实现剩余的string函数族函数,strcpy、strcat、strcmp
strcpy:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str[20] = {0};
char str1[20] = "hello world";
char str2[20] = "here is hqyj";int i = 0;
printf("复制前str:");
puts(str);
putchar(10);
printf("复制前str2:");
puts(str2);
putchar(10);
while(str1[i]!='\0'){
str[i] = str1[i];
str2[i] = str1[i];
i++;
}
str[i] = str1[i];
str2[i] = str1[i];
printf("复制后str:");
puts(str);
putchar(10);
printf("复制后str2:");
puts(str2);
putchar(10);return 0;
}strcat:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str1[32] = "hello world";
char str2[20] = "here is hqyj";int i = 0;
printf("复制前str1:");
puts(str1);
putchar(10);
while(str1[i]!='\0'){
i++;
}int j = 0;
while(str2[j]!='\0'){
str1[i+j]=str2[j];
j++;
}
printf("复制后str1:");
puts(str1);
putchar(10);return 0;
}strcmp:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str1[20] = "hello";
char str2[20] = {0};gets(str2);
int i = 0;
while(str1[i]!=0){
if(str1[i]!=str2[i]){
printf("%d\n",(int)(str1[i]-str2[i]));
break;
}
i++;
}
if(str1[i]==str2[i]){
printf("%d\n",(int)(str1[i]-str2[i]));
}else{
printf("%d\n",(int)(str1[i]-str2[i]));
}
return 0;
} -
4、思维导图
5、终端输入10个学生成绩,使用冒泡排序对学生成绩从低到高排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int score[10] = {0};
int i = 0;printf("依次输入学生成绩:");
for(i=0; i<10 ;i++){
scanf("%d",&score[i]);
}int j = 0;
int len = sizeof(score)/sizeof(int);
int tmp = 0;for(i=1;i < len ;i++){
for(j=0; j < len - i;j++){
if(score[j]>score[j+1]){
tmp = score[j];
score[j] = score[j+1];
score[j+1] = tmp;
}
}
}printf("从低到高排序后:");
for(i=0; i<10 ;i++){
printf("%d ",score[i]);
}
putchar(10);
return 0;
}
day60717作业
于 2023-07-17 20:57:02 首次发布