-
Notifications
You must be signed in to change notification settings - Fork 2
Add basic calculator functionality in test1.cpp #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include <string> | ||
| #include "big_data_int.h" | ||
|
|
||
| string getString(){ | ||
| auto* str = new string(); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🦊 AI代码审查 问题: 手动分配了string的内存但未正确使用,存在内存泄漏风险 |
||
| str->reserve(100); | ||
| std::cout << "请输入内容:"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mttests |
||
| std::cin.getline(str->c_str(), 100); // 直接读取到缓冲区 | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🦊 AI代码审查 问题: 通过c_str()修改string内容是未定义行为 |
||
|
|
||
| std::cout << "输入内容:" << str->c_str() << std::endl; | ||
|
|
||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🦊 AI代码审查 问题: 返回的是string指针而不是string对象,会导致编译错误或运行时错误 |
||
| return str; | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| cout<<"please input two numbers:"<<endl; | ||
| string s_a, s_b; | ||
|
|
||
| s_a = getString(); | ||
| s_b = getString(); | ||
|
|
||
| cout<<"Please input the operator(+,-,*,/,%):"<<endl; | ||
| string op; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🦊 AI代码审查 问题: 未对用户输入的操作符进行有效性验证 |
||
| cin>>op; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🦊 AI代码审查 问题: 直接访问op.c_str()[0]可能存在越界风险 |
||
| big_data_int a(s_a), b(s_b), c; | ||
| switch (op.c_str()[0]) { | ||
| case '+': | ||
| c = a + b; | ||
| break; | ||
| case '-': | ||
| c = a - b; | ||
| break; | ||
| case '*': | ||
| c = a * b; | ||
| break; | ||
| case '/': | ||
| c = a / b; | ||
| break; | ||
| case '%': | ||
| c = a % b; | ||
| break; | ||
| default: | ||
| cout<<"Wrong operator input"<<endl; | ||
| return -1; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这应该是xxx提的吧 |
||
| } | ||
| cout <<"Result is:"<<endl<<c.data()<<endl; | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🦊 AI代码审查
问题: 函数返回类型与实际返回值不匹配,应返回string对象而非指针
建议: 修改函数返回类型为string,并直接返回string对象,避免不必要的动态内存分配
严重程度: error