高手进来指点下

fred_zhu 2008-11-28 09:51:50
要求:定义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;
}
...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangqi1357 2008-12-07
  • 打赏
  • 举报
回复
新手报到,学习中,大家帮助大家,新手提高中。
BaihowFF 2008-11-29
  • 打赏
  • 举报
回复
额...为什么不用模板写呢?
把整分数的类的加号重载一下就ok了啊...
Qlaiaqu 2008-11-28
  • 打赏
  • 举报
回复
貌似我很无聊,给你参考一下
#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];
cout <<"Type a line,press enter to end:\n";
(cin>>char_buf).get();

p_data = new char[strlen(char_buf)+1];
if(p_data == NULL)
{
cout <<"Not enough memory from the heap.\n";
exit(0);
}

for(int i = 0; i < strlen(p_data); i++)
p_data[i] = char_buf[i];
}


void add(char* &a,char *b) //连接字符串
{
char *temp = new char[strlen(a)+strlen(b)+1];
strcpy(temp,a);
strcat(temp,b);
a = temp;
}


int main()
{

int a,b;
char *p_string1,*p_string2;
fraction *p_struct1 = new fraction ,*p_struct2 = new fraction;

cout <<"input your integer numbers!\n";
cin>>a>>b;
cout <<add(a,b) <<endl;

cout <<"input two fractions!\n";
cin>>p_struct1->son>>p_struct1->mum>>p_struct2->son>>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;

return 0;
}
就呆在云上 2008-11-28
  • 打赏
  • 举报
回复
程序很简单啦,就是分配空间没有把握好,我修改了,就是注释的地方
你看看:
#include <iostream> 
#include <string.h>
using namespace std;
//10不够大,用100比较好
#define BUF_LEN 100

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;
}

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

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

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);
/////strlen 是0,因为这个时候没有空间是0,只要你写就会越界,修改之
len += sizeof(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 main()
{
int a,b;
char *p_string1,*p_string2;

getLine(p_string1);
cout <<p_string1 <<endl;
getLine(p_string2);
cout <<p_string2 <<endl;
add(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;

cout <<p_string1 <<endl;

delete[]p_string1;
delete[]p_string2;
}
霍大脚 2008-11-28
  • 打赏
  • 举报
回复
不套格式,没有注释,直接飘
fred_zhu 2008-11-28
  • 打赏
  • 举报
回复
谁帮我看下啊

65,211

社区成员

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

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