
一道c++的题,急……
在主函数中输入一个字符串,由若干个英文单词组成,单词之间用空格分开。调用一个函数输出此字符串中最长的包含字母a的单词。(若没有保护字母a的单词则输出相应的提示。)...
在主函数中输入一个字符串,由若干个英文单词组成,单词之间用空格分开。调用一个函数输出此字符串中最长的包含字母a的单词。(若没有保护字母a的单词则输出相应的提示。)
展开
4个回答
展开全部
我也试着写了一个:
#include <iostream.h>
#include <cmath>
class yicihanshu
{public:
yicihanshu(double=0,double=0); //声明带默认参数的构造函数。
yicihanshu addition (yicihanshu &); //声明多项式相加的函数。
void set(); //声明设置一次多项式的系数的函数。
void display(); //声明用于输出的函数。
double addx(double x) //定义给定x计算多项式的值的函数。
void equation(); //声明解一次方程的函数。
~yicihanshu() //定义析构函数。
protected:
static int count;
double b;
double c;
};
int yicihanshu:: count=0; //对静态数据成员只能在类外进行初始化。
yicihanshu::yicihanshu(double ib,double ic)
yicihanshu yicihanshu:: addition (yicihanshu &y2) //定义多项式相加的函数。
void yicihanshu::set()
{
cout<<"输入系数b和c:"<<endl;
cin>>b>>c;
}
void yicihanshu::display()
{
if(b==0&&c==0) cout<<0<<endl;
else if(b==0&&c!=0) cout<<c<<endl;
else if(c==0&&b!=0) cout<<b<<"x"<<endl;
else if(c<0&&b!=0) cout<<b<<"x"<<c<<endl;
else cout<<b<<"x+"<<c<<endl;
}
void yicihanshu::equation()
{
if(b==0)
{
if(c==0) cout<<"方程的解为全体实数!"<<endl;
else cout<<"该方程无解!"<<endl;
}
else
cout<<"方程的解为:"<<-c/b<<endl;
}
class ercihanshu : public yicihanshu
{public:
ercihanshu(double=0,double=0,double=0); //声明带默认参数的构造函数。
ercihanshu addition (ercihanshu &); //声明多项式相加的函数。
void set(); //声明设置一次多项式的系数的函数。
void display(); //声明用于输出的函数。
double addx(double); //声明给定x计算多项式的值的函数。
friend int flag(ercihanshu&); //声明判断判别式符号的友元函数。
int equation(); //声明解一次方程的函数。
~ercihanshu() //定义析构函数。
private:
double a;
};
ercihanshu::ercihanshu(double ia,double ib,double ic) :yicihanshu(ib,ic),a(ia){}
ercihanshu ercihanshu:: addition (ercihanshu &y2) //定义多项式相加的函数。注意到ax^2+bx+c=(ax+b)x+c !
{
return ercihanshu(a+y2.a,b+y2.b,c+y2.c);
}
void ercihanshu::set()
{
cout<<"输入系数a、b和c:"<<endl;
cin>>a>>b>>c;
}
void ercihanshu::display()
{
if(a==0&&b==0&&c==0) cout<<0<<endl;
else if(a==0&&b==0&&c!=0) cout<<c<<endl;
else if(a==0&&b!=0&&c==0) cout<<b<<"x"<<endl;
else if(a!=0&&b==0&&c==0) cout<<a<<"x^2"<<endl;
else if(a==0&&b!=0&&c!=0)
{
if(c<0) cout<<b<<"x"<<c<<endl;
else cout<<b<<"x+"<<c<<endl;
}
else if(a!=0&&b==0&&c!=0)
{
if(c<0) cout<<a<<"x^2"<<c<<endl;
else cout<<a<<"x^2+"<<c<<endl;
}
else if(a!=0&&b!=0&&c==0)
{
if(b<0) cout<<a<<"x^2"<<b<<"x"<<endl;
else cout<<a<<"x^2+"<<b<<"x"<<endl;
}
else
{
if(b<0&&c<0) cout<<a<<"x^2"<<b<<"x"<<c<<endl;
else if(b>0&&c<0) cout<<a<<"x^2+"<<b<<"x"<<c<<endl;
else if(b<0&&c>0) cout<<a<<"x^2"<<b<<"x+"<<c<<endl;
else cout<<a<<"x^2+"<<b<<"x+"<<c<<endl;
}
}
double ercihanshu::addx(double x)
{
return a*x*x+b*x+c;
}
int flag(ercihanshu& y)
{
double a1,b1,c1,d;
a1=y.a;b1=y.b;c1=y.c;
d=b1*b1-4*a1*c1;
if(d>0) return 1;
else if(d==0) return 0;
else return -1;
}
int ercihanshu::equation()
{
if(a==0)
{
yicihanshu::equation();
return 0;//返回0以表示a为0
}
else
{
double d,e;
d=-b/(2*a);
e=sqrt(b*b-4*a*c)/(2*a);
if(flag(*this)==1)
cout<<"两个不同实数解分别为:"<<endl<<d+e<<","<<d-e<<endl;
if(flag(*this)==0)
cout<<"两个相同实数解为:"<<endl<<d<<endl;
if(flag(*this)==-1)
cout<<"该方程无实数解!"<<endl;
}
}
int main()
{ercihanshu e1;
cout<<"e1:";e1.display();
e1.equation();
e1.set();
cout<<"e1:";e1.display();
e1.equation();
return 0;
}
#include <iostream.h>
#include <cmath>
class yicihanshu
{public:
yicihanshu(double=0,double=0); //声明带默认参数的构造函数。
yicihanshu addition (yicihanshu &); //声明多项式相加的函数。
void set(); //声明设置一次多项式的系数的函数。
void display(); //声明用于输出的函数。
double addx(double x) //定义给定x计算多项式的值的函数。
void equation(); //声明解一次方程的函数。
~yicihanshu() //定义析构函数。
protected:
static int count;
double b;
double c;
};
int yicihanshu:: count=0; //对静态数据成员只能在类外进行初始化。
yicihanshu::yicihanshu(double ib,double ic)
yicihanshu yicihanshu:: addition (yicihanshu &y2) //定义多项式相加的函数。
void yicihanshu::set()
{
cout<<"输入系数b和c:"<<endl;
cin>>b>>c;
}
void yicihanshu::display()
{
if(b==0&&c==0) cout<<0<<endl;
else if(b==0&&c!=0) cout<<c<<endl;
else if(c==0&&b!=0) cout<<b<<"x"<<endl;
else if(c<0&&b!=0) cout<<b<<"x"<<c<<endl;
else cout<<b<<"x+"<<c<<endl;
}
void yicihanshu::equation()
{
if(b==0)
{
if(c==0) cout<<"方程的解为全体实数!"<<endl;
else cout<<"该方程无解!"<<endl;
}
else
cout<<"方程的解为:"<<-c/b<<endl;
}
class ercihanshu : public yicihanshu
{public:
ercihanshu(double=0,double=0,double=0); //声明带默认参数的构造函数。
ercihanshu addition (ercihanshu &); //声明多项式相加的函数。
void set(); //声明设置一次多项式的系数的函数。
void display(); //声明用于输出的函数。
double addx(double); //声明给定x计算多项式的值的函数。
friend int flag(ercihanshu&); //声明判断判别式符号的友元函数。
int equation(); //声明解一次方程的函数。
~ercihanshu() //定义析构函数。
private:
double a;
};
ercihanshu::ercihanshu(double ia,double ib,double ic) :yicihanshu(ib,ic),a(ia){}
ercihanshu ercihanshu:: addition (ercihanshu &y2) //定义多项式相加的函数。注意到ax^2+bx+c=(ax+b)x+c !
{
return ercihanshu(a+y2.a,b+y2.b,c+y2.c);
}
void ercihanshu::set()
{
cout<<"输入系数a、b和c:"<<endl;
cin>>a>>b>>c;
}
void ercihanshu::display()
{
if(a==0&&b==0&&c==0) cout<<0<<endl;
else if(a==0&&b==0&&c!=0) cout<<c<<endl;
else if(a==0&&b!=0&&c==0) cout<<b<<"x"<<endl;
else if(a!=0&&b==0&&c==0) cout<<a<<"x^2"<<endl;
else if(a==0&&b!=0&&c!=0)
{
if(c<0) cout<<b<<"x"<<c<<endl;
else cout<<b<<"x+"<<c<<endl;
}
else if(a!=0&&b==0&&c!=0)
{
if(c<0) cout<<a<<"x^2"<<c<<endl;
else cout<<a<<"x^2+"<<c<<endl;
}
else if(a!=0&&b!=0&&c==0)
{
if(b<0) cout<<a<<"x^2"<<b<<"x"<<endl;
else cout<<a<<"x^2+"<<b<<"x"<<endl;
}
else
{
if(b<0&&c<0) cout<<a<<"x^2"<<b<<"x"<<c<<endl;
else if(b>0&&c<0) cout<<a<<"x^2+"<<b<<"x"<<c<<endl;
else if(b<0&&c>0) cout<<a<<"x^2"<<b<<"x+"<<c<<endl;
else cout<<a<<"x^2+"<<b<<"x+"<<c<<endl;
}
}
double ercihanshu::addx(double x)
{
return a*x*x+b*x+c;
}
int flag(ercihanshu& y)
{
double a1,b1,c1,d;
a1=y.a;b1=y.b;c1=y.c;
d=b1*b1-4*a1*c1;
if(d>0) return 1;
else if(d==0) return 0;
else return -1;
}
int ercihanshu::equation()
{
if(a==0)
{
yicihanshu::equation();
return 0;//返回0以表示a为0
}
else
{
double d,e;
d=-b/(2*a);
e=sqrt(b*b-4*a*c)/(2*a);
if(flag(*this)==1)
cout<<"两个不同实数解分别为:"<<endl<<d+e<<","<<d-e<<endl;
if(flag(*this)==0)
cout<<"两个相同实数解为:"<<endl<<d<<endl;
if(flag(*this)==-1)
cout<<"该方程无实数解!"<<endl;
}
}
int main()
{ercihanshu e1;
cout<<"e1:";e1.display();
e1.equation();
e1.set();
cout<<"e1:";e1.display();
e1.equation();
return 0;
}
展开全部
你把要输入的字符串付给一个数组。然后利用下标值的变化遍历数组的元素。然后判断长度。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是半成品,没办法,我也在学……你先看看吧 或许有帮助
#include <iostream.h>
#include <stdio.h>
void f(char a[99])
{
char words[99];
int length=0,yes=0,j=0;
for (int k=0;a[k]!=0;k++)
{
for(int i=0;a[i-1+j]!=' ';i++)
//运行到一个单词结尾的空格处
{
int lengthi=i;
//得出这个单词的长度lengthi
for (int i=0;i<lengthi;i++)
//在这个单词中
{
if(a[i+j]=='a')
//如果含有字母a
yes=1;
}
if (lengthi>length&&yes)
//如果该单词比length长,并且含有字母a
{
for(int i=0;i<length;i++)
words[99]=a[j+i];
//把这个单词赋给words[99]
}
}
j=k-1;
}
if (yes)
puts(words);
else
cout<<
#include <iostream.h>
#include <stdio.h>
void f(char a[99])
{
char words[99];
int length=0,yes=0,j=0;
for (int k=0;a[k]!=0;k++)
{
for(int i=0;a[i-1+j]!=' ';i++)
//运行到一个单词结尾的空格处
{
int lengthi=i;
//得出这个单词的长度lengthi
for (int i=0;i<lengthi;i++)
//在这个单词中
{
if(a[i+j]=='a')
//如果含有字母a
yes=1;
}
if (lengthi>length&&yes)
//如果该单词比length长,并且含有字母a
{
for(int i=0;i<length;i++)
words[99]=a[j+i];
//把这个单词赋给words[99]
}
}
j=k-1;
}
if (yes)
puts(words);
else
cout<<
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#define max 200
void main()
{
int i,j=0,k=0;
char a[max];
char b[max][max];
printf("输入字符串:");
gets(a);
for(i=0;a[i]!='\0';i++) //这里是将所得的字符串的每一个单 词 { 放在一个二维数组里面
if(a[i]!=' ')
{
b[j][k]=a[i];
k++;
}
else if(a[i]==' ') j++;
}
for(j=0;b[j][0]!='\0';j++)
{
for(i=0;b[j][i]!='\0';i++)
{
if(b[j][i]=='a')
printf("%s",b[j][0]);
}
printf("不存在含有字母a的单词");
}
}
这个程序只是输出含有字母a的单词,时间关系写不完了
希望对你有帮助
#define max 200
void main()
{
int i,j=0,k=0;
char a[max];
char b[max][max];
printf("输入字符串:");
gets(a);
for(i=0;a[i]!='\0';i++) //这里是将所得的字符串的每一个单 词 { 放在一个二维数组里面
if(a[i]!=' ')
{
b[j][k]=a[i];
k++;
}
else if(a[i]==' ') j++;
}
for(j=0;b[j][0]!='\0';j++)
{
for(i=0;b[j][i]!='\0';i++)
{
if(b[j][i]=='a')
printf("%s",b[j][0]);
}
printf("不存在含有字母a的单词");
}
}
这个程序只是输出含有字母a的单词,时间关系写不完了
希望对你有帮助
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询