如何将此代码从 c 更改为 c++

How to change this code from c to c++

本文关键字:c++ 代码      更新时间:2023-10-16

我学了很多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++,但是,对于初学者来说......

  1. 包含将更改
  2. 你会使用coutcin<<>>
  3. 不再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,不要从程序开始,从设计开始。