首页 > 其他 > 详细

第二次博客作业

时间:2018-12-08 20:59:26      阅读:214      评论:0      收藏:0      [点我收藏+]

目录:

  • 薄弱的知识点

  • 有挑战性的题目


 

薄弱的知识点:

 

  1. 对字符串的处理不熟悉,尤其是对字符串的处理函数(strcat,strcpy,strcmp)的运用。
  2. 二维数组作为形参来传递不熟悉,编译器经常报错 cannot convert from ‘int*‘ to ‘int* *‘
  3. 指针的运用不清晰,编译器经常报错段错误,后来我通过查询了解到段错误的原因及解决方法,下面是对应的连接:

 

有挑战性的题目:

 1. PTA 9 7-4 从小到大输出字符串 

  任意输入三个字符串,按从小到大顺序输出。编程要求:⑴在主函数中定义三个字符数组str1[]、str2[]、str3[],输入三个字符串分别存入这三个字符数组中。⑵将str1、str2、str3三个字符数组的指针传递给被调函数。⑶被调函数swap(char *, char *, char *)中做字符串变量值的交换。⑷返回主函数后,变量str1中是初始三个字符串中的最小者,变量str1中是初始三个字符串中的最大者,输出str1、str2、str3三个字符串的值。

输入格式:

输入共三行,每行是一个字符串。

输出格式:

从小到大输出三个字符串。

输入样例:

在这里给出一组输入。例如:

hello,world
hello,Judy
hello

 

输出样例:

在这里给出相应的输出。例如:

hello
hello,Judy
hello,world

  下面是我的代码:

#include <iostream>
#include <cstring>
using namespace std;

void swapa(char *a,char *b,char *c);
int main()

{
	char str1[20],str2[20],str3[20];
	cin.getline(str1,20);
	cin.getline(str2,20);
	cin.getline(str3,20);
	swapa(str1,str2,str3);
	//cout<<str1<<endl<<str2<<endl<<str3<<endl;
} 
void swapa(char *a,char *b,char *c)
{
	if(strlen(a)>strlen(b)&&strlen(b)>strlen(c))
	{
		swap(a,c);
	}
	if(strlen(a)>strlen(c)&&strlen(c)>strlen(b))
	{
		swap(c,b);
		swap(a,c);
	}
	if(strlen(b)>strlen(a)&&strlen(a)>strlen(c))
	{
		swap(a,c);
		swap(b,c);
	}
	if(strlen(b)>strlen(c)&&strlen(c)>strlen(a))
	{
		swap(b,c);
	}
	if(strlen(c)>strlen(a)&&strlen(a)>strlen(b))
	{
		swap(a,b);
	}
	cout<<a<<endl<<b<<endl<<c<<endl;
}

  我一开始想到运用strlen函数进行比较,把其当作一道排序,而且并没有考虑到输入输出方面的优化,而且这道题更适合运用strcmp来进行比较。例如:

if(strcmp(str1,str2)>0)

  最后由于在标准输入输出流中,存在swap已经被定义,存在

 

std::swap

 

 所以直接使用会报错,我就用swapa来代替副函数名,当然也有其他做法。比如不用命名空间

#include <iostream.h>
int main()

  2 .  PTA 9 7-1 拷贝奇数字符 

在主函数中输入字符串str1,调用函数将str1中下标为奇数的字符取出构成一个新的字符串放入字符串str2中(要求被调函数参数为str1和str2),在主函数中输出结果字符串str2。若输出的字符串为空串,输出 "empty string" 。要求函数用指针变量作形参。

输入格式:

一个字符串,以回车结束。

输出格式:

输出由奇数下标的字符组成的新字符串。

输入样例:

在这里给出一组输入。例如:

12345

输出样例:

 24

在这里给出相应的输出。例如:

  我一开始只是想通过在主函数中通过strlen函数来进行str2为空串的判断,却忽略了首格为空的错误情况,在是我开始时的代码(在sample4上报错不通过)

#include<iostream>
#include <string.h>
#define N 10 
using namespace std; 
void  chage(char* str2,int n);
int main() 
{   
    char str1[N] ;
    cin>>str1; 

	if (strlen(str1)>1)  
	{
		chage(str1,N);
		cout<<str1<<endl;
	}
	else  cout<<"empty string"<<endl;
    return 0; 
}   
void chage(char* str2,int n)
{ 
    int c=0;
    char str1[N] = {0}; 
    for(int j=0;j<n;j++)
    { 
        if(j%2!=0) 
        {
            str1[c++]=str2[j];  
        } 
    } 
    strcpy(str2, str1);
}

  其核心部分是在循环中通过下标数字是否为偶数来实现转录str2,并通过strcpy来完成与str1的交换(必须考虑是否有特殊情况导致发生变化,比如在本题中应是j%2!=0

void chage(char* str2,int n)
{ 
    int c=0;
    char str1[N] = {0}; 
    for(int j=0;j<n;j++)
    { 
        if(j%2!=0) 
        {
            str1[c++]=str2[j];  
        } 
    } 
    strcpy(str2, str1);
}

  当然也可以通过下标加二来实现

int strPartCopy(char *s1, char *s2) {
 int len1=strlen(s1);
 for(int i=1,j=0; i<len1; i+=2,j++)
  s2[j]=s1[i];
 return strlen(s2);
}

   这是我后来改正后的代码

#include<iostream>
#include<cstring>
using namespace std;
int strPartCopy(char *s1, char *s2) {
 int len1=strlen(s1);
 for(int i=1,j=0; i<len1; i+=2,j++)
  s2[j]=s1[i];
 return strlen(s2);
}
int main() {
 char str1[1000000],str2[1000000];
 int n;
 cin>>str1;
 n=strPartCopy(str1,str2);
 if(str2[0]==‘\0‘) cout<<"empty string";
 else for(int i=0; i<n; i++)
   cout<<str2[i];
 return 0;
}

  

 

 

  

 

第二次博客作业

原文:https://www.cnblogs.com/yingni/p/10088869.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!