A.static int a; while(a) int s=6; m:if(s<100) exit(0); else s-=2: goto m:这两段代码为什么不能构成循环
时间: 2024-03-07 21:51:20 浏览: 18
这两段代码不能构成循环是因为在第一段代码中,变量a没有被初始化,因此它的值是不确定的。在第二段代码中,当s的值小于100时,程序会执行exit(0)语句退出程序,不会执行goto语句跳回到标记m处,因此也不能构成循环。同时,第一段代码中的while循环条件永远为false,因为a的值没有被初始化,所以while循环中的语句永远不会被执行。
相关问题
帮我写一个c++程序,要求如下:esim_tool --model=<model.bin> --input=<ifmap.bin> --output=<ofmap.bin> --infer_order=<depthfirst|breadthfirst|random|parallel>,要保持120的行宽,并且将命令行参数解析使用getopt_long单独封装成一个函数
好的,下面是代码实现:
```c++
#include <getopt.h>
#include <iostream>
using namespace std;
void parse_args(int argc, char* argv[], string& model, string& input, string& output, string& infer_order) {
static struct option long_options[] = {
{"model", required_argument, nullptr, 0},
{"input", required_argument, nullptr, 0},
{"output", required_argument, nullptr, 0},
{"infer_order", required_argument, nullptr, 0},
{nullptr, 0, nullptr, 0}
};
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
switch (opt) {
case 0:
switch (option_index) {
case 0:
model = optarg;
break;
case 1:
input = optarg;
break;
case 2:
output = optarg;
break;
case 3:
infer_order = optarg;
break;
}
break;
default:
cout << "Usage: esim_tool --model=<model.bin> --input=<ifmap.bin> --output=<ofmap.bin> --infer_order=<depthfirst|breadthfirst|random|parallel>" << endl;
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char* argv[]) {
string model, input, output, infer_order;
parse_args(argc, argv, model, input, output, infer_order);
cout << "model: " << model << endl;
cout << "input: " << input << endl;
cout << "output: " << output << endl;
cout << "infer_order: " << infer_order << endl;
return 0;
}
```
在这个程序中,我们使用了 `getopt_long` 函数来解析命令行参数。程序会依次解析命令行参数并将解析结果保存在 `model`、`input`、`output`、`infer_order` 四个变量中。如果用户输入了无效的参数或缺少必须的参数,则程序会输出使用方法并退出。
你可以通过以下命令运行这个程序:
```bash
./esim_tool --model=model.bin --input=ifmap.bin --output=ofmap.bin --infer_order=depthfirst
```
程序会将命令行参数打印出来,输出如下:
```
model: model.bin
input: ifmap.bin
output: ofmap.bin
infer_order: depthfirst
```
完善:#include <iostream> using namespace std; enum ERROR{UnderFlow,OverFlow}; template<typename T> class StackTemplate { enum { ssize = 100 }; T stack[ssize]; int top; public: StackTemplate() : top(0) {} void push(const T& i) { if (top >= ssize) ; stack[top++] = i; } T pop() { if ( ) throw UnderFlow; return ; } int size() const{ return top; } }; int fibonacci(int n); int main() { { is; for(int i = 0; i < 10; i++) is.push(fibonacci(i)); for(int k = 0; k < 10; k++) cout << is.pop() << "\t"; } catch( ERROR e ) { switch( ) { case OverFlow: exit; case UnderFlow: exit; } } catch(...) { } return 0; } int fibonacci(int n) { int sz = 100; int i; static int f[sz]; if (n >= sz) ; f[0] = f[1] = 1; for(i = 0; i < sz; i++) if(f[i] == 0) break; while(i <= n) { = f[i-1] + f[i-2]; i++; } return ; }
完善后的代码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
enum ERROR{UnderFlow, OverFlow};
template<typename T>
class StackTemplate {
enum {ssize = 100};
T stack[ssize];
int top;
public:
StackTemplate() : top(0) {}
void push(const T& i) {
if (top >= ssize)
throw OverFlow;
stack[top++] = i;
}
T pop() {
if (top <= 0)
throw UnderFlow;
return stack[--top];
}
int size() const {
return top;
}
};
int fibonacci(int n);
int main() {
try {
StackTemplate<int> is;
for(int i = 0; i < 10; i++)
is.push(fibonacci(i));
for(int k = 0; k < 10; k++)
cout << is.pop() << "\t";
} catch(ERROR e) {
switch(e) {
case OverFlow:
cout << "Stack overflow." << endl;
break;
case UnderFlow:
cout << "Stack underflow." << endl;
break;
}
} catch(...) {
cout << "An exception has occurred." << endl;
}
return 0;
}
int fibonacci(int n) {
int sz = 100;
int i;
static int f[sz];
if (n >= sz)
exit(1);
f[0] = f[1] = 1;
for(i = 0; i < sz; i++)
if(f[i] == 0)
break;
while(i <= n) {
f[i] = f[i-1] + f[i-2];
i++;
}
return f[n];
}
注:完善的内容主要包括:1) 定义 StackTemplate 类时没有指定类型 T,需要在 main 函数中明确指定;2) 在 push 和 pop 函数中添加异常处理代码;3) 在 main 函数中添加异常处理代码,以处理 StackTemplate 类中的异常;4) 在 fibonacci 函数中添加一个异常处理代码,以处理 n 超出 f 数组大小的异常。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)