C++小题测试

chrisjzhu 2008-04-15 12:46:18
要求:定义3个重载函数add(),分别实现两个整数,两个整分数的相加运算,以及两个字符串的合并运算!

以下是小弟写的程序,但是感觉有点繁琐,欢迎大家来交流指证!

#include<iostream>
#include<string.h>
using namespace std;
#define BUF_LEN 10




int add(int a, int b)
{
return a+b;
}


struct fraction
{
int son;
int mum;
};


void add(struct fraction *a,struct fraction *b)
{
a->son = (b->son * a->mum) + (a->son * b->mum);
a->mum *= b->mum;
}


void getLine(char* &p_data) //读入字符串函数
{
char char_buf[BUF_LEN],*p_temp=NULL;
int len=1;
cout<<"Type a line,press enter to end:\n";

do
{
cin.get(char_buf,BUF_LEN);
len += strlen(char_buf);
p_data = new char[len];

if(p_data == NULL)
{
cout<<"Not enough memory from the heap.\n";
return;
}

if(p_temp == NULL)
strcpy(p_data,char_buf);
else
{
strcpy(p_data,p_temp);
strcat(p_data,char_buf);
}
delete[]p_temp;
p_temp = p_data;

if(cin.peek() == '\n')
{
cin.get();
break;
}
}while(true);
}


void add(char *a,char *b) //连接字符串
{
strcat(a,b);
}


void main()

{

int a,b;
char *p_string1,*p_string2;
struct fraction struct1,struct2,*p_struct1,*p_struct2;


cout<<"input your integer numbers!\n";

cin>>a;

cin>>b;

cout<<add(a,b)<<endl;


p_struct1 = &struct1;
p_struct2 = &struct2;

cout<<"input two fractions!\n";

cin>>p_struct1->son;

cin>>p_struct1->mum;

cin>>p_struct2->son;

cin>>p_struct2->mum;

add(p_struct1,p_struct2);

cout<<"the sum of the fractions is"<<(float)p_struct1->son/p_struct1->mum<<endl;


getLine(p_string1);

cout<<p_string1<<endl;

getLine(p_string2);

cout<<p_string2<<endl;

add(p_string1,p_string2);

cout<<p_string1<<endl;

delete[]p_string1;
delete[]p_string2;
}


...全文
198 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chrisjzhu 2008-04-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 c_spark 的回复:]
读入字符串的时候直接使用cin>>char_buf
使用cin.get会将上次输入缓冲区的回车读入
还有就是使用指针的时候函数退出即销毁了
要么使用char **,要么还是使用char[]这样开辟空间才有效
帮你简化了一个,实现了你需要的操作

C/C++ code#include <iostream>
#include <string.h>
using namespace std;
#define BUF_LEN 100


int add(int a, int b)
{
return a+b;
}


struct fraction
{
int s…
[/Quote]




如果定义了大小,就有了输入的限制,而且不是动态的分配内存,会不会导致内存的浪费?

不知道可不可以用fflush (stdin);

jieao111 2008-04-16
  • 打赏
  • 举报
回复
char* add(char* a, char* b)
{
return strcat(a,b);
}
c_spark 2008-04-16
  • 打赏
  • 举报
回复
读入字符串的时候直接使用cin>>char_buf
使用cin.get会将上次输入缓冲区的回车读入
还有就是使用指针的时候函数退出即销毁了
要么使用char **,要么还是使用char[]这样开辟空间才有效
帮你简化了一个,实现了你需要的操作
#include <iostream> 
#include <string.h>
using namespace std;
#define BUF_LEN 100


int add(int a, int b)
{
return a+b;
}


struct fraction
{
int son;
int mum;
};


void add(struct fraction *a,struct fraction *b)
{
a->son = (b->son * a->mum) + (a->son * b->mum);
a->mum *= b->mum;
}


void getLine(char* p_data) //读入字符串函数
{
char char_buf[BUF_LEN];
int len=1;
cout <<"Type a line,press enter to end:\n";
cin>>char_buf;
cout<<char_buf<<endl;
len += strlen(char_buf);

if(p_data == NULL)
{
cout <<"Not enough memory from the heap.\n";
return;
}
strcpy(p_data,char_buf);
}


void add(char *a,char *b) //连接字符串
{
strcat(a,b);
}


void main()

{

int a,b;
char p_string1[100],p_string2[100];
struct fraction struct1,struct2,*p_struct1,*p_struct2;


cout <<"input your integer numbers!\n";

cin>>a;

cin>>b;

cout <<add(a,b) <<endl;


p_struct1 = &struct1;
p_struct2 = &struct2;

cout <<"input two fractions!\n";

cin>>p_struct1->son;

cin>>p_struct1->mum;

cin>>p_struct2->son;

cin>>p_struct2->mum;

add(p_struct1,p_struct2);

cout <<"the sum of the fractions is " <<((float)p_struct1->son/p_struct1->mum) <<endl;


getLine(p_string1);

cout <<"p_string1: "<<p_string1 <<endl;

getLine(p_string2);

cout <<"P_string2: "<<p_string2 <<endl;

add(p_string1,p_string2);

cout <<p_string1 <<endl;

}
chrisjzhu 2008-04-16
  • 打赏
  • 举报
回复
5楼和8楼所用的string作为字符串变量类型都不错,但是如果用指针类型,又该怎么设计呢?欢迎继续讨论!!!
chrisjzhu 2008-04-15
  • 打赏
  • 举报
回复
这里有个很奇怪的错误,不知道大家能否发现~~~
chrisjzhu 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liyuzhu_1984 的回复:]
请设计一个类 谢谢
[/Quote]

初学者,还没学到设计类这里~~
laolaoliu2002 2008-04-15
  • 打赏
  • 举报
回复
挺好的,自己跟踪一下然后看看流程和算法那里可以优化。
liyuzhu_1984 2008-04-15
  • 打赏
  • 举报
回复
请设计一个类 谢谢
liveforme 2008-04-15
  • 打赏
  • 举报
回复

int add(int a,int b)
{
return a+b;
}

double add(int sa, int ma, int sb, int mb)
{
return (double)sa/(double)ma + (double)sb/(double)mb;
}

string add(string a, string b)
{
return a+b;
}

int main()
{
int ia,ib;
int sa, ma, sb, mb;
string a,b;

cout<<"输两整数"<<endl;
cin>>ia>>ib;
cout<<"整数和 = "<<add(ia,ib)<<endl;
cout<<"输两分数"<<endl;
cin>>sa>>ma>>sb>>mb;
cout<<"分数和 = "<<add(sa, ma, sb, mb)<<endl;
cout<<"输两字符串"<<endl;
cin>>a>>b;
cout<<"字符串和 = "<<add(a,b)<<endl;
system("pause");
return 0;
}
chrisjzhu 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 clhposs 的回复:]
C/C++ code

#include<iostream>
#include<string>

using namespace std;

int add(int a,int b)
{
return a+b;
}

string add(string s1,string s2)
{
s1+=s2;
return s1;
}

double add(double a,double b)
{
return a+b;
}

int main()
{
cout<<add(1,2)<<endl;
cout<< add("Hello","word")<<endl;
cout<<add(1.1,2.2)<<endl;
return 0;
}
[/Quote]


所有数据需从键盘输入~~
jullyroggy 2008-04-15
  • 打赏
  • 举报
回复
看的有点花啊
clhposs 2008-04-15
  • 打赏
  • 举报
回复


#include<iostream>
#include<string>

using namespace std;

int add(int a,int b)
{
return a+b;
}

string add(string s1,string s2)
{
s1+=s2;
return s1;
}

double add(double a,double b)
{
return a+b;
}

int main()
{
cout<<add(1,2)<<endl;
cout<< add("Hello","word")<<endl;
cout<<add(1.1,2.2)<<endl;
return 0;
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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