一
//命令行加法计算
//需要在命令行中运行
#include <iostream>
int main(int argc,char** argv)
{
int sum = 0;
int i = 1;
while (argv[i] != NULL) {
sum += atoi(argv[i]);
i++;
}
printf("Total=%d", sum);
}
二
#include <iostream>
struct flight {
int time;
int arrive;
};
void TimeChangePrint(int time);
int main()
{
struct flight timetable[8] = {
{480,616},
{583,712},
{679,811},
{767,900},
{840,968},
{945,1075},
{1140,1280},
{1305,1438}
};
int Nowtime = 0;
printf("Enter a 24-hour time:");
int hour, min;
scanf_s("%d:%d", &hour, &min);
Nowtime = (hour - 1) * 60 + min;
for (int i = 0; i < 8; i++) {
if ((timetable[i].time) >= Nowtime) {
printf("Clost departure time is ");
TimeChangePrint(timetable[i].time);
printf(", arriving at ");
TimeChangePrint(timetable[i].arrive);
break;
}
}
}
void TimeChangePrint(int time) {
if (time < 720) {
printf("%d:%02d a.m.", time / 60, time % 60);
}
else if (time < 780) {
printf("12:%02d p.m.", time % 60);
} else{
printf("%d:%02d p.m.", (time - 720) / 60, time % 60);
}
}
三
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char* word[100];
char in[20];
int i = 0;
while (1) {
int a = 0;
printf("Enter word:");
while ((in[a] = getchar() )!= '\n')a++;
if (a == 0)break;
in[a] = 0;
word[i++] = (char*)malloc(sizeof(char) * a);
for (int n = 0; n < a; n++)word[i - 1][n] = in[n];
}
for (int n = 0; n < i - 1; n++) {
for (int c = 0; c < i - 1 - n; c++) {
if (strcmp(word[c], word[c + 1]) > 0) {
char* temp = word[c];
word[c] = word[c + 1];
word[c + 1] = temp;
}
}
}
printf("In sorted order:");
for(int n=0;n<i;n++) {
printf("%s ", word[n]);
}
}