将C更改为C ++代码,任何人都可以提供帮助 [英] Change C to C++ code, can anyone help
本文介绍了将C更改为C ++代码,任何人都可以提供帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
毫无疑问
毫无疑问
毫无疑问
毫无疑问
无疑问
毫无疑问
毫无疑问
我尝试过:
没有
no question
no question
no question
no question
no question
no question
no question
What I have tried:
nothing
推荐答案
圣诞节礼物:
A Christmas present:
#include <stdio.h>
#include <string.h>
// Prints the minimum number that can be formed from
// input sequence of I's and D's
void PrintMinNumberForPattern(const char * arr)
{
// Initialize current_max (to make sure that
// we don't use repeated character
int curr_max = 0;
// Initialize last_entry (Keeps track for
// last printed digit)
int last_entry = 0;
int j;
size_t length = strlen(arr);
// Iterate over input array
int i;
for (i=0; i<length; i++)
{
// Initialize 'noOfNextD' to get count of
// next D's available
int noOfNextD = 0;
switch(arr[i])
{
case 'I':
// If letter is 'I'
// Calculate number of next consecutive D's
// available
j = i+1;
while (arr[j] == 'D' && j < length)
{
noOfNextD++;
j++;
}
if (i==0)
{
curr_max = noOfNextD + 2;
// If 'I' is first letter, print incremented
// sequence from 1
printf(" %d", ++last_entry);
printf(" %d", curr_max);
// Set max digit reached
last_entry = curr_max;
}
else
{
// If not first letter
// Get next digit to print
curr_max = curr_max + noOfNextD + 1;
// Print digit for I
last_entry = curr_max;
printf(" %d", last_entry);
}
// For all next consecutive 'D' print
// decremented sequence
int k;
for (k=0; k<noOfNextD; k++)
{
printf(" %d", --last_entry);
i++;
}
break;
// If letter is 'D'
case 'D':
if (i == 0)
{
// If 'D' is first letter in sequence
// Find number of Next D's available
j = i+1;
while (arr[j] == 'D' && j < length)
{
noOfNextD++;
j++;
}
// Calculate first digit to print based on
// number of consecutive D's
curr_max = noOfNextD + 2;
// Print twice for the first time
printf(" %d %d", curr_max, (curr_max -1));
// Store last entry
last_entry = curr_max - 1;
}
else
{
// If current 'D' is not first letter
// Decrement last_entry
printf(" %d", last_entry -1);
last_entry--;
}
break;
}
}
printf("\n");
}
int main()
{
PrintMinNumberForPattern("IIDDIIDDDDD");
return 0;
}
代码只包含两个C ++类: string - C ++ Reference [ ^ ]和 cout - C ++参考a> [ ^ ]。
对于它只使用的字符串length
成员函数和运算符[]
访问单个字符。因此,您可以将函数参数从string
更改为const char *
。然后运算符[]
仍然有效。要获得长度,请使用 strlen - C ++参考a> [ ^ ]。
使用printf - C ++参考a> [ ^ ]替换cout
来电:
The code contains only two C++ classes: string - C++ Reference[^] and cout - C++ Reference[^] .
For the string it uses only thelength
member function and theoperator[]
to access single characters. So you can change the function argument fromstring
toconst char*
. Theoperator[]
will then still work. To get the length use strlen - C++ Reference[^].
Use printf - C++ Reference[^] to replace thecout
calls:
// C++
cout << " " << ++last_entry;
cout << " " << curr_max;
cout << " " << curr_max << " " << curr_max - 1;
cout << endl;
// C
printf(" %d", ++last_entry);
printf(" %d", curr_max);
printf(" %d %d", curr_max, curr_max - 1);
printf("\n");
从我的解决方案中读取链接,了解C和C ++函数的工作原理。
Read the links from my solution to understand how the C and C++ functions work.
这篇关于将C更改为C ++代码,任何人都可以提供帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文