如何将此代码从 c 更改为 c++
How to change this code from c to c++
我学了很多C,但不是C ++,只是想知道这段代码在C ++中会是什么样子。 谢谢。 这不是出于好奇,因为我下学期将学习C ++。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void rotateCW(FILE * ifp, FILE * ofp);
void rotateCounterCW(FILE * ifp, FILE * ofp);
int main(int argc, char *argv)
{
FILE *ifp, *ofp;
int ifpFlag = 0;
//loops until valid input file is put in
while(ifpFlag == 0)
{
char fnamer[100]="";
printf("nWhats the input file name?n");
scanf("%s", &fnamer);
ifp = fopen(fnamer, "r");
if (ifp == NULL)
{
fprintf(stderr, "Can't open input file in.list!n");
}
else
{
ifpFlag = 1;
}
}
char outname[100]="";
printf("nWhats the output file name?n");
scanf("%s", &outname);
ofp = fopen(outname, "w");
if (ofp == NULL)
{
fprintf(stderr, "Can't open output file %s!n",
outname);
exit(1);
}
int flag = 0;
//loops until valid input or quit
while(flag == 0)
{
//print menu
int input;
printf("nChoose an option:n1)Rotate Clockwise 90 degreesn2)Rotate Counter-Clockwise 90 degreesn3)Quitn");
scanf("%d", &input);
switch (input)
{
case 1:
rotateCW(ifp,ofp);
flag = 1;
break;
case 2:
rotateCounterCW(ifp,ofp);
flag = 1;
break;
case 3: //quit
printf("nProgram Terminatedn");
flag = 1;
break;
default: //valid input was not put in
printf("Invalid input.n");
break;
}
}
fclose(ifp);
fclose(ofp);
return 0;
}
void rotateCounterCW(FILE * ifp, FILE * ofp)
{
int length, width;
fscanf(ifp, "%d", &width);
fscanf(ifp, "%d", &length);
int array[length][width];
int CCWarray[length * width];
int i, j, k;
for (i = 0; i < length; i++)
{
for (j = 0; j < width; j++)
{
fscanf(ifp, "%d", &array[i][j]);
}
}
k = 0;
for (j = width - 1; j >= 0; --j)
{
for (i = 0; i < length; i++)
{
CCWarray[k++] = array[i][j];
}
}
fprintf(ofp, "P2n");
fprintf(ofp, "# Comments here.n");
fprintf(ofp, "%d %dn", length, width);
fprintf(ofp, "255n");
//writing the output file
for (i = 0; i < (length * width); i++)
{
fprintf(ofp, "%d ", CCWarray[i]);
}
fprintf(ofp, "n");
printf("nThe pgm was rotated CCW by 90 degreesn");
}
void rotateCW(FILE * ifp, FILE * ofp)
{
int length, width;
fscanf(ifp, "%d", &width);
fscanf(ifp, "%d", &length);
int array[length][width];
int CWarray[length * width];
int i, j, k;
for (i = 0; i < length; i++)
{
for (j = 0; j < width; j++)
{
fscanf(ifp, "%d", &array[i][j]);
}
}
k = 0;
for (j = 0; j < width; j++)
{
for (i = length - 1; i >= 0; --i)
{
CWarray[k++] = array[i][j];
}
}
fprintf(ofp, "P2n");
fprintf(ofp, "# Comments here.n");
fprintf(ofp, "%d %dn", length, width);
fprintf(ofp, "255n");
//writing the output file
for (i = 0; i < (length * width); i++)
{
fprintf(ofp, "%d ", CWarray[i]);
}
fprintf(ofp, "n");
printf("nThe pgm was rotated CW by 90 degreesn");
}
你有一些可变长度的数组,这在C++中是不允许的。 将它们替换为 std::vector
。
例如
int CCWarray[length * width];
成为
std::vector<int> CCWarray;
CCWarray.resize(length * width);
其余代码(除了 WhozCraig 在注释中指出的错误类型argv
)已经有效C++。
我不知道
C++,但是,对于初学者来说......
- 包含将更改
- 你会使用
cout
和cin
,<<
和>>
- 不再
char[]
,替换为std::string
大多数格式良好的 C 代码也是C++代码。如果你用C++编译器编译它,它已经C++了(即使它也是C)。 C99 中添加的一些 C 语言功能不包含在C++中 - 例如可变长度数组(正如 Ben Voigt 已经指出的那样),但许多C++编译器将通过扩展支持它们。
也就是说,C 的某些功能在 C++ 中被弃用,或者在 C++ 中具有更好的替代方案。 你可以通过使用C++样式库标头、std:: 命名空间、iostreams、std::string 类和 STL 容器来轻松C++此代码,但这本身传达了一个有用但很小的优势,并且可能会让您想知道为什么C++更好。
C++擅长的是支持面向对象编程,要从中受益,需要重新设计代码,而不是直接的"转换"。 换句话说 - 如果你想达到OOP,不要从程序开始,从设计开始。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值