C#示例:
XNamespace aw = "http://www.adventure-works.com";
Run Code Online (Sandbox Code Playgroud)
F#我是怎么写的?
我试着这样:
let ns : XNamespace = "URI ADDRESS";;
Run Code Online (Sandbox Code Playgroud)
F#说错误
我有以下python代码:
a, b = 1, 1
for i in range(0, 100):
print a
a, b = b, a + b
Run Code Online (Sandbox Code Playgroud)
它产生了这个:1 1 2 3 5 8等
我在c中写了同样的文章:
#include <stdio.h>
long long unsigned int a = 1, b = 1;
void main(){
for(int i = 0; i < 100; i++){
printf("%llu \n", a);
a = b, b = a + b;
}
}
Run Code Online (Sandbox Code Playgroud)
它产生了这个:1 1 2 4 8 16 32等
为什么c程序在使用完全相同的操作时会产生2的幂?
我想从输入中读取两个长句子,使用scanf()
它们在两行中。
码:
int main() {
char a[100], b[100];
scanf("%[^\n]s", a);
scanf("%[^\n]s", b);
printf("%s\n%s", a, b);
}
Run Code Online (Sandbox Code Playgroud)
输入:
she is beautiful
That is a flower
Run Code Online (Sandbox Code Playgroud)
输出:
she is beautiful
Run Code Online (Sandbox Code Playgroud)
该scanf()
语句未读取第二个输入。
如何解决?
这是C中的基本字符计数程序:
#include <stdio.h>
#include <stdlib.h>
int main(){
long nc = 0;
while(getchar() != EOF)
nc++;
printf("%ld\n" , nc);
}
Run Code Online (Sandbox Code Playgroud)
当我输入"abcde"作为输入时,它显示值6(触发EOF测试后),换行符字符+1.但我怀疑的是getchar()
,正如其名称本身所暗示的那样,只考虑了1个字符.但是当我一口气进入"abcde"时,它仍然有效.为什么会这样?我在这做什么问题?
输入文件
Miller Andrew 65789.87 5
Green Sheila 75892.56 9
Sethi Amit 74900.50 6.1
Run Code Online (Sandbox Code Playgroud)
ifstream inFile;
ofstream outFile;
string laastName;
string firstName;
double salary;
double percent;
double new Salary;
double increase;
inFile.open("Ch3_Ex6Data.txt");
outFile.open("Ch3_Ex6Output.dat");
while(!inFile.eof()) {
inFile >> lastName;
inFile >> firstName;
inFile >> salary;
inFile >> percent;
percent /= 100;
increase = salary * percent;
newSalary = increase + salary;
outFile << firstName << " " << lastName << " ";
outFile << setprecision(2) << fixed << newSalary << endl;
} …
Run Code Online (Sandbox Code Playgroud) 大小char[]
是number of char times sizeof(char)
,大小char*
是sizeof(pointer)
- 指向第一个元素的指针.
sizeof(char[])
打印number of char times sizeof(char)
中main()
,在它的声明,但如果我通过这个数组的功能,其功能转换char[]
到char*
和它的imposibble获得使用数组的大小sizeof()
,
"void pr(char chr[])" is changed to "void pr(char chr*)"
Run Code Online (Sandbox Code Playgroud)
代码示例:
using namespace std;
void pr(char chr[])
{
std::cout << "in pr(): " << sizeof(chr)<<"\n";
}
int main()
{
char* c;
char z[] = { 1,2,3,4,5,6,7,8,9};
c = z;
std::cout << "sizeof char* c in main(): " << sizeof(c) << "\n"; …
Run Code Online (Sandbox Code Playgroud) 首先,我知道sizeof取决于机器和编译器的实现.我使用的是Windows 8.1.x64,gcc 5.3.0.,没有标志传递给编译器.
我的大学讲座中有以下代码:
#include <iostream>
class A
{
public:
int a;
int b;
A(){ a = 1; b = 2; }
};
class S1 : public A {
public:
int x1;
S1(){ x1 = 5;}
};
class S2 : public A {
public:
int x2;
S2(){ x2 = 6;}
};
class S12 : public S1, public S2 {
public:
int x12;
S12(){ x12 = 7;}
};
int main()
{
std::cout << "S1: " << sizeof(A) << std::endl;
std::cout << …
Run Code Online (Sandbox Code Playgroud) 我试图从传递给getPhoneNumber(char [] str)的字符串中获取电话号码,但由于某种原因,每次运行代码时我都会附加一些随机字符,请我需要帮助.
源代码
#include <stdio.h>
#include <string.h>
char* getPhoneNumber(char str[]);
int main(){
getPhoneNumber("AT+CMGR=5 \n+CMGR: \"REC READ\",\"+9349036332058\",\"samuel\",\"17/03/31,20:44:52+04\"\nHOW THINS fa OK");
return 0;
}
char* getPhoneNumber(char str[]){
char *temp = strchr(str, ',')+2;
const unsigned short len1 = strlen(temp);
printf("value in temp : %s\n\n",temp);
char *strPtr = strchr(temp, '\"');
const unsigned short len2 = strlen(strPtr);
printf("value in strPtr : %s\n\n",strPtr);
int phone_num_len = len1-len2;
char phone_num[phone_num_len];
strncpy(phone_num, temp,phone_num_len);
printf("Phone number : %s",phone_num);
}
Run Code Online (Sandbox Code Playgroud)
我还打印出了temp和strPtr的各个值用于调试目的,但返回的值似乎没问题.程序的输出如下图所示.
我正在从命令行运行C++ 11程序,如此
marco @ host $ ./program.out a bb ccc
用于生成可执行文件的.cpp文件如下所示:
#include<iostream>
#include<vector>
#include<string>
int main(int argc, char* argv[]){
std::vector<std::string> strs = {argv + 1, argv + argc};
std::cout << strs[2] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它输出ccc.我没想到,因为列表初始化程序只包含char*类型的两个元素.我之前看过这样的列表内容:
vector<string> strs = {"str1", "str2", ..., "strN"};
Run Code Online (Sandbox Code Playgroud)
但指针init对我来说是全新的.似乎指针被视为开始和结束迭代器.我的问题是,编译器如何知道从argv + 1迭代到argv + argc来初始化strs中的各个字符串?只是为了把事情放在上下文中,有没有办法在C中以类似的方式使用列表初始化来初始化char*数组?