C++编程:(1) 用一个函数来实现将一行字符中最长的单词输出。

A_little_idiot 2016-10-29 06:43:50

各位帮我看看我的注释对不对啊
有没有可能修改该代码,实现更好的编程
...全文
1061 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
RoadStillFaraway 2016-10-30
  • 打赏
  • 举报
回复
引用 2 楼 A_little_idiot 的回复:
#include "stdafx.h" #include <string.h>//在用字符串处理函数时,应把“string.h"文件包含到本文件中 int main(void) { int i; char str[80],max[80];//尽量赋予数组足够长的长度 int maxlen,len;//定义最大长度和长度 printf("Input 6 words:"); scanf("%s",str); strcpy(max,str);//先将第一个str拷贝给max maxlen=strlen(max);//计算max的长度,作为maxlen的初值 for(i=1;i<6;i++) //从第2个单词开始循环 { scanf("%s",str);//将第i+1个单词输入给str len=strlen(str);//计算第i+1个str的长度 if(maxlen<len ) //比较maxlen和第i+1个str的长度的大小 { strcpy(max,str); /*如果第i+1个str的长度的值大于maxlen, 则将str拷贝给max*/ maxlen= len;//将maxlen的值被第i+1个str的长度的值代替 } } printf("The longest word is:%s\n",max); return 0; }
//#include "stdafx.h"
#include <stdio.h>
#include <string.h>//在用字符串处理函数时,应把“string.h"文件包含到本文件中
int main(void)
{
	int i;
	char str[80],max[80];//尽量赋予数组足够长的长度
	int maxlen=0,len=0;//定义最大长度和长度
	i=6;
	printf("Input 6 words:\n");
	do{
		printf("第%d个:",6-i);
		scanf("%s",str);//将第i+1个单词输入给str
		len=strlen(str);//计算第i+1个str的长度
		if(maxlen<len  ) //比较maxlen和第i+1个str的长度的大小          
		{
			strcpy(max,str); //如果第i+1个str的长度的值大于maxlen,则将str拷贝给max
			maxlen= len;//将maxlen的值被第i+1个str的长度的值代替
		}
		printf("目前最长的单词:%s\n",max);
	}while(--i);
	
	printf("The longest word is:%s\n",max);
	return 0;
}
A_little_idiot 2016-10-29
  • 打赏
  • 举报
回复
大神,那我再问问我这个代码哪里有问题吗? main() { int i,j; float s[10][5],a[10]; printf("请输入10个学生的成绩,按照“科目一 科目二 科目三 科目四 科目五”的顺序依次输入:\n"); for(i=0;i<10;++i) { printf("第%d个学生:",i+1); for(j=0;j<5;++j) { scanf("%f",&s[i][j]); } a[i]=(s[i][0]+s[i][1]+s[i][2]+s[i][3]+s[i][4])/5; a[j]=(s[0][j]+s[1][j]+s[2][j]+s[3][j]+s[4][j]+s[5][j]+s[6][j]+s[7][j]+s[8][j]+s[9][j])/10; } printf("\n"); for(i=0;i<10;++i) { printf("第%d个学生的平均分为:%.2f\n",i+1,a[i]); } for(j=0;j<5;++j) { printf("第%d门课的平均分为:%.2f\n",j+1,a[j]); } return 0; }
paschen 2016-10-29
  • 打赏
  • 举报
回复
改进的话防止字符串缓冲区溢出
paschen 2016-10-29
  • 打赏
  • 举报
回复
没问题,不过太哆嗦,注释不用去把代码的意思解释一遍
A_little_idiot 2016-10-29
  • 打赏
  • 举报
回复
帮我看看我的代码有没有错啊
A_little_idiot 2016-10-29
  • 打赏
  • 举报
回复
为什么没人帮我啊
A_little_idiot 2016-10-29
  • 打赏
  • 举报
回复
#include "stdafx.h" #include <string.h>//在用字符串处理函数时,应把“string.h"文件包含到本文件中 int main(void) { int i; char str[80],max[80];//尽量赋予数组足够长的长度 int maxlen,len;//定义最大长度和长度 printf("Input 6 words:"); scanf("%s",str); strcpy(max,str);//先将第一个str拷贝给max maxlen=strlen(max);//计算max的长度,作为maxlen的初值 for(i=1;i<6;i++) //从第2个单词开始循环 { scanf("%s",str);//将第i+1个单词输入给str len=strlen(str);//计算第i+1个str的长度 if(maxlen<len ) //比较maxlen和第i+1个str的长度的大小 { strcpy(max,str); /*如果第i+1个str的长度的值大于maxlen, 则将str拷贝给max*/ maxlen= len;//将maxlen的值被第i+1个str的长度的值代替 } } printf("The longest word is:%s\n",max); return 0; }
RoadStillFaraway 2016-10-29
  • 打赏
  • 举报
回复
为何不将代码贴出来,给什么图片

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧